process_collection_service.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # frozen_string_literal: true
  2. class ActivityPub::ProcessCollectionService < BaseService
  3. include JsonLdHelper
  4. def call(body, account, **options)
  5. @account = account
  6. @json = Oj.load(body, mode: :strict)
  7. @options = options
  8. return unless supported_context?
  9. return if different_actor? && verify_account!.nil?
  10. return if @account.suspended? || @account.local?
  11. case @json['type']
  12. when 'Collection', 'CollectionPage'
  13. process_items @json['items']
  14. when 'OrderedCollection', 'OrderedCollectionPage'
  15. process_items @json['orderedItems']
  16. else
  17. process_items [@json]
  18. end
  19. rescue Oj::ParseError
  20. nil
  21. end
  22. private
  23. def different_actor?
  24. @json['actor'].present? && value_or_id(@json['actor']) != @account.uri && @json['signature'].present?
  25. end
  26. def process_items(items)
  27. items.reverse_each.map { |item| process_item(item) }.compact
  28. end
  29. def supported_context?
  30. super(@json)
  31. end
  32. def process_item(item)
  33. activity = ActivityPub::Activity.factory(item, @account, @options)
  34. activity&.perform
  35. end
  36. def verify_account!
  37. @account = ActivityPub::LinkedDataSignature.new(@json).verify_account!
  38. end
  39. end