闭社主体 forked from https://github.com/tootsuite/mastodon
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.

52 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 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. rescue JSON::LD::JsonLdError => e
  39. Rails.logger.debug "Could not verify LD-Signature for #{value_or_id(@json['actor'])}: #{e.message}"
  40. nil
  41. end
  42. end