fetch_remote_status_service.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # frozen_string_literal: true
  2. class ActivityPub::FetchRemoteStatusService < BaseService
  3. include JsonLdHelper
  4. include Redisable
  5. DISCOVERIES_PER_REQUEST = 1000
  6. # Should be called when uri has already been checked for locality
  7. def call(uri, prefetched_body: nil, on_behalf_of: nil, expected_actor_uri: nil, request_id: nil)
  8. @request_id = request_id || "#{Time.now.utc.to_i}-status-#{uri}"
  9. @json = begin
  10. if prefetched_body.nil?
  11. fetch_resource(uri, true, on_behalf_of)
  12. else
  13. body_to_json(prefetched_body, compare_id: uri)
  14. end
  15. end
  16. return unless supported_context?
  17. actor_uri = nil
  18. activity_json = nil
  19. object_uri = nil
  20. if expected_object_type?
  21. actor_uri = value_or_id(first_of_value(@json['attributedTo']))
  22. activity_json = { 'type' => 'Create', 'actor' => actor_uri, 'object' => @json }
  23. object_uri = uri_from_bearcap(@json['id'])
  24. elsif expected_activity_type?
  25. actor_uri = value_or_id(first_of_value(@json['actor']))
  26. activity_json = @json
  27. object_uri = uri_from_bearcap(value_or_id(@json['object']))
  28. end
  29. return if activity_json.nil? || object_uri.nil? || !trustworthy_attribution?(@json['id'], actor_uri)
  30. return if expected_actor_uri.present? && actor_uri != expected_actor_uri
  31. return ActivityPub::TagManager.instance.uri_to_resource(object_uri, Status) if ActivityPub::TagManager.instance.local_uri?(object_uri)
  32. actor = account_from_uri(actor_uri)
  33. return if actor.nil? || actor.suspended?
  34. # If we fetched a status that already exists, then we need to treat the
  35. # activity as an update rather than create
  36. activity_json['type'] = 'Update' if equals_or_includes_any?(activity_json['type'], %w(Create)) && Status.where(uri: object_uri, account_id: actor.id).exists?
  37. with_redis do |redis|
  38. discoveries = redis.incr("status_discovery_per_request:#{@request_id}")
  39. redis.expire("status_discovery_per_request:#{@request_id}", 5.minutes.seconds)
  40. return nil if discoveries > DISCOVERIES_PER_REQUEST
  41. end
  42. ActivityPub::Activity.factory(activity_json, actor, request_id: @request_id).perform
  43. end
  44. private
  45. def trustworthy_attribution?(uri, attributed_to)
  46. return false if uri.nil? || attributed_to.nil?
  47. Addressable::URI.parse(uri).normalized_host.casecmp(Addressable::URI.parse(attributed_to).normalized_host).zero?
  48. end
  49. def account_from_uri(uri)
  50. actor = ActivityPub::TagManager.instance.uri_to_resource(uri, Account)
  51. actor = ActivityPub::FetchRemoteAccountService.new.call(uri, request_id: @request_id) if actor.nil? || actor.possibly_stale?
  52. actor
  53. end
  54. def supported_context?
  55. super(@json)
  56. end
  57. def expected_activity_type?
  58. equals_or_includes_any?(@json['type'], %w(Create Announce))
  59. end
  60. def expected_object_type?
  61. equals_or_includes_any?(@json['type'], ActivityPub::Activity::Create::SUPPORTED_TYPES + ActivityPub::Activity::Create::CONVERTED_TYPES)
  62. end
  63. end