闭社主体 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.

48 lines
1.1 KiB

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