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.

51 lines
1.4 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, 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 supported_context? && expected_type?
  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? || needs_update(actor)
  15. return if actor.nil? || 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. (ActivityPub::Activity::Create::SUPPORTED_TYPES + ActivityPub::Activity::Create::CONVERTED_TYPES).include? @json['type']
  33. end
  34. def needs_update(actor)
  35. actor.possibly_stale?
  36. end
  37. end