process_collection_service.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 if !supported_context? || (different_actor? && verify_account!.nil?) || suspended_actor? || @account.local?
  9. case @json['type']
  10. when 'Collection', 'CollectionPage'
  11. process_items @json['items']
  12. when 'OrderedCollection', 'OrderedCollectionPage'
  13. process_items @json['orderedItems']
  14. else
  15. process_items [@json]
  16. end
  17. rescue Oj::ParseError
  18. nil
  19. end
  20. private
  21. def different_actor?
  22. @json['actor'].present? && value_or_id(@json['actor']) != @account.uri
  23. end
  24. def suspended_actor?
  25. @account.suspended? && !activity_allowed_while_suspended?
  26. end
  27. def activity_allowed_while_suspended?
  28. %w(Delete Reject Undo Update).include?(@json['type'])
  29. end
  30. def process_items(items)
  31. items.reverse_each.map { |item| process_item(item) }.compact
  32. end
  33. def supported_context?
  34. super(@json)
  35. end
  36. def process_item(item)
  37. activity = ActivityPub::Activity.factory(item, @account, **@options)
  38. activity&.perform
  39. end
  40. def verify_account!
  41. @options[:relayed_through_account] = @account
  42. @account = ActivityPub::LinkedDataSignature.new(@json).verify_account!
  43. rescue JSON::LD::JsonLdError => e
  44. Rails.logger.debug "Could not verify LD-Signature for #{value_or_id(@json['actor'])}: #{e.message}"
  45. nil
  46. end
  47. end