fetch_remote_status_service.rb 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. return if actor.suspended?
  14. ActivityPub::Activity.factory(activity, actor).perform
  15. end
  16. private
  17. def activity_json
  18. if %w(Note Article).include? @json['type']
  19. {
  20. 'type' => 'Create',
  21. 'actor' => first_of_value(@json['attributedTo']),
  22. 'object' => @json,
  23. }
  24. else
  25. @json
  26. end
  27. end
  28. def trustworthy_attribution?(uri, attributed_to)
  29. Addressable::URI.parse(uri).normalized_host.casecmp(Addressable::URI.parse(attributed_to).normalized_host).zero?
  30. end
  31. def supported_context?
  32. super(@json)
  33. end
  34. def expected_type?(json)
  35. %w(Create Announce).include? json['type']
  36. end
  37. end