fetch_remote_status_service.rb 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # frozen_string_literal: true
  2. class ActivityPub::FetchRemoteStatusService < BaseService
  3. include JsonLdHelper
  4. include DomainControlHelper
  5. include Redisable
  6. DISCOVERIES_PER_REQUEST = 1000
  7. # Should be called when uri has already been checked for locality
  8. def call(uri, prefetched_body: nil, on_behalf_of: nil, expected_actor_uri: nil, request_id: nil)
  9. return if domain_not_allowed?(uri)
  10. @request_id = request_id || "#{Time.now.utc.to_i}-status-#{uri}"
  11. @json = if prefetched_body.nil?
  12. fetch_resource(uri, true, on_behalf_of)
  13. else
  14. body_to_json(prefetched_body, compare_id: uri)
  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