You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.3 KiB

  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?) || @account.suspended? || @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 process_items(items)
  25. items.reverse_each.map { |item| process_item(item) }.compact
  26. end
  27. def supported_context?
  28. super(@json)
  29. end
  30. def process_item(item)
  31. activity = ActivityPub::Activity.factory(item, @account, **@options)
  32. activity&.perform
  33. end
  34. def verify_account!
  35. @options[:relayed_through_account] = @account
  36. @account = ActivityPub::LinkedDataSignature.new(@json).verify_account!
  37. rescue JSON::LD::JsonLdError => e
  38. Rails.logger.debug "Could not verify LD-Signature for #{value_or_id(@json['actor'])}: #{e.message}"
  39. nil
  40. end
  41. end