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.2 KiB

  1. # frozen_string_literal: true
  2. class ActivityPub::FetchRemoteStatusService < BaseService
  3. include JsonLdHelper
  4. # Should be called when uri has already been checked for locality
  5. def call(uri, prefetched_json = nil)
  6. @json = body_to_json(prefetched_json) || fetch_resource(uri)
  7. return unless supported_context?
  8. activity = activity_json
  9. actor_id = value_or_id(activity['actor'])
  10. return unless expected_type?(activity) && trustworthy_attribution?(uri, actor_id)
  11. actor = ActivityPub::TagManager.instance.uri_to_resource(actor_id, Account)
  12. actor = ActivityPub::FetchRemoteAccountService.new.call(actor_id) if actor.nil?
  13. ActivityPub::Activity.factory(activity, actor).perform
  14. end
  15. private
  16. def activity_json
  17. if %w(Note Article).include? @json['type']
  18. {
  19. 'type' => 'Create',
  20. 'actor' => first_of_value(@json['attributedTo']),
  21. 'object' => @json,
  22. }
  23. else
  24. @json
  25. end
  26. end
  27. def trustworthy_attribution?(uri, attributed_to)
  28. Addressable::URI.parse(uri).normalized_host.casecmp(Addressable::URI.parse(attributed_to).normalized_host).zero?
  29. end
  30. def supported_context?
  31. super(@json)
  32. end
  33. def expected_type?(json)
  34. %w(Create Announce).include? json['type']
  35. end
  36. end