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.

52 lines
1.5 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, on_behalf_of: nil)
  6. @json = if prefetched_body.nil?
  7. fetch_resource(uri, id, on_behalf_of)
  8. else
  9. body_to_json(prefetched_body, compare_id: id ? uri : nil)
  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. value_or_id(first_of_value(@json['attributedTo']))
  24. end
  25. def trustworthy_attribution?(uri, attributed_to)
  26. return false if uri.nil? || attributed_to.nil?
  27. Addressable::URI.parse(uri).normalized_host.casecmp(Addressable::URI.parse(attributed_to).normalized_host).zero?
  28. end
  29. def supported_context?
  30. super(@json)
  31. end
  32. def expected_type?
  33. equals_or_includes_any?(@json['type'], ActivityPub::Activity::Create::SUPPORTED_TYPES + ActivityPub::Activity::Create::CONVERTED_TYPES)
  34. end
  35. def needs_update(actor)
  36. actor.possibly_stale?
  37. end
  38. end