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 = begin
  7. if prefetched_body.nil?
  8. fetch_resource(uri, id, on_behalf_of)
  9. else
  10. body_to_json(prefetched_body, compare_id: id ? uri : nil)
  11. end
  12. end
  13. return if !(supported_context? && expected_type?) || actor_id.nil? || !trustworthy_attribution?(@json['id'], actor_id)
  14. actor = ActivityPub::TagManager.instance.uri_to_resource(actor_id, Account)
  15. actor = ActivityPub::FetchRemoteAccountService.new.call(actor_id, id: true) if actor.nil? || needs_update?(actor)
  16. return if actor.nil? || actor.suspended?
  17. ActivityPub::Activity.factory(activity_json, actor).perform
  18. end
  19. private
  20. def activity_json
  21. { 'type' => 'Create', 'actor' => actor_id, 'object' => @json }
  22. end
  23. def actor_id
  24. value_or_id(first_of_value(@json['attributedTo']))
  25. end
  26. def trustworthy_attribution?(uri, attributed_to)
  27. return false if uri.nil? || attributed_to.nil?
  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?
  34. equals_or_includes_any?(@json['type'], ActivityPub::Activity::Create::SUPPORTED_TYPES + ActivityPub::Activity::Create::CONVERTED_TYPES)
  35. end
  36. def needs_update?(actor)
  37. actor.possibly_stale?
  38. end
  39. end