fetch_remote_status_service.rb 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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, id: true, prefetched_body: nil)
  6. @json = if prefetched_body.nil?
  7. fetch_resource(uri, id)
  8. else
  9. body_to_json(prefetched_body)
  10. end
  11. return unless expected_type? && supported_context?
  12. return if actor_id.nil? || !trustworthy_attribution?(@json['id'], actor_id)
  13. actor = ActivityPub::TagManager.instance.uri_to_resource(actor_id, Account)
  14. actor = ActivityPub::FetchRemoteAccountService.new.call(actor_id, id: true) if actor.nil?
  15. return if actor.suspended?
  16. ActivityPub::Activity.factory(activity_json, actor).perform
  17. end
  18. private
  19. def activity_json
  20. { 'type' => 'Create', 'actor' => actor_id, 'object' => @json }
  21. end
  22. def actor_id
  23. first_of_value(@json['attributedTo'])
  24. end
  25. def trustworthy_attribution?(uri, attributed_to)
  26. Addressable::URI.parse(uri).normalized_host.casecmp(Addressable::URI.parse(attributed_to).normalized_host).zero?
  27. end
  28. def supported_context?
  29. super(@json)
  30. end
  31. def expected_type?
  32. %w(Note Article).include? @json['type']
  33. end
  34. end