2017-08-08 21:52:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ActivityPub::FetchRemoteStatusService < BaseService
|
|
|
|
include JsonLdHelper
|
2023-05-02 15:22:19 +02:00
|
|
|
include DomainControlHelper
|
2023-02-10 22:16:37 +01:00
|
|
|
include Redisable
|
|
|
|
|
|
|
|
DISCOVERIES_PER_REQUEST = 1000
|
2017-08-08 21:52:15 +02:00
|
|
|
|
|
|
|
# Should be called when uri has already been checked for locality
|
2024-02-01 15:56:46 +01:00
|
|
|
def call(uri, prefetched_body: nil, on_behalf_of: nil, expected_actor_uri: nil, request_id: nil)
|
2023-05-02 15:22:19 +02:00
|
|
|
return if domain_not_allowed?(uri)
|
|
|
|
|
2023-02-10 22:16:37 +01:00
|
|
|
@request_id = request_id || "#{Time.now.utc.to_i}-status-#{uri}"
|
2023-02-18 23:09:40 +01:00
|
|
|
@json = if prefetched_body.nil?
|
2024-02-01 15:56:46 +01:00
|
|
|
fetch_resource(uri, true, on_behalf_of)
|
2023-02-18 23:09:40 +01:00
|
|
|
else
|
2024-02-01 15:56:46 +01:00
|
|
|
body_to_json(prefetched_body, compare_id: uri)
|
2023-02-18 23:09:40 +01:00
|
|
|
end
|
2017-08-08 21:52:15 +02:00
|
|
|
|
2021-10-21 01:14:04 +02:00
|
|
|
return unless supported_context?
|
|
|
|
|
2022-03-12 09:11:36 +01:00
|
|
|
actor_uri = nil
|
2021-10-21 01:14:04 +02:00
|
|
|
activity_json = nil
|
2022-03-12 09:11:36 +01:00
|
|
|
object_uri = nil
|
2021-10-21 01:14:04 +02:00
|
|
|
|
|
|
|
if expected_object_type?
|
2022-03-12 09:11:36 +01:00
|
|
|
actor_uri = value_or_id(first_of_value(@json['attributedTo']))
|
|
|
|
activity_json = { 'type' => 'Create', 'actor' => actor_uri, 'object' => @json }
|
|
|
|
object_uri = uri_from_bearcap(@json['id'])
|
2021-10-21 01:14:04 +02:00
|
|
|
elsif expected_activity_type?
|
2022-03-12 09:11:36 +01:00
|
|
|
actor_uri = value_or_id(first_of_value(@json['actor']))
|
2021-10-21 01:14:04 +02:00
|
|
|
activity_json = @json
|
2022-03-12 09:11:36 +01:00
|
|
|
object_uri = uri_from_bearcap(value_or_id(@json['object']))
|
2021-10-21 01:14:04 +02:00
|
|
|
end
|
|
|
|
|
2022-03-12 09:11:36 +01:00
|
|
|
return if activity_json.nil? || object_uri.nil? || !trustworthy_attribution?(@json['id'], actor_uri)
|
2022-12-07 00:15:24 +01:00
|
|
|
return if expected_actor_uri.present? && actor_uri != expected_actor_uri
|
2022-05-17 14:52:26 +02:00
|
|
|
return ActivityPub::TagManager.instance.uri_to_resource(object_uri, Status) if ActivityPub::TagManager.instance.local_uri?(object_uri)
|
2017-08-08 21:52:15 +02:00
|
|
|
|
2022-03-12 09:11:36 +01:00
|
|
|
actor = account_from_uri(actor_uri)
|
2017-08-08 21:52:15 +02:00
|
|
|
|
2017-11-19 15:33:15 +01:00
|
|
|
return if actor.nil? || actor.suspended?
|
2017-09-13 11:05:02 +02:00
|
|
|
|
2022-03-12 09:11:36 +01:00
|
|
|
# If we fetched a status that already exists, then we need to treat the
|
|
|
|
# activity as an update rather than create
|
2024-01-23 12:41:34 +01:00
|
|
|
activity_json['type'] = 'Update' if equals_or_includes_any?(activity_json['type'], %w(Create)) && Status.exists?(uri: object_uri, account_id: actor.id)
|
2022-03-12 09:11:36 +01:00
|
|
|
|
2023-02-10 22:16:37 +01:00
|
|
|
with_redis do |redis|
|
|
|
|
discoveries = redis.incr("status_discovery_per_request:#{@request_id}")
|
|
|
|
redis.expire("status_discovery_per_request:#{@request_id}", 5.minutes.seconds)
|
|
|
|
return nil if discoveries > DISCOVERIES_PER_REQUEST
|
|
|
|
end
|
|
|
|
|
|
|
|
ActivityPub::Activity.factory(activity_json, actor, request_id: @request_id).perform
|
2017-08-08 21:52:15 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def trustworthy_attribution?(uri, attributed_to)
|
2018-05-05 18:22:34 +02:00
|
|
|
return false if uri.nil? || attributed_to.nil?
|
2023-02-20 06:58:28 +01:00
|
|
|
|
2017-08-08 21:52:15 +02:00
|
|
|
Addressable::URI.parse(uri).normalized_host.casecmp(Addressable::URI.parse(attributed_to).normalized_host).zero?
|
|
|
|
end
|
|
|
|
|
2022-03-12 09:11:36 +01:00
|
|
|
def account_from_uri(uri)
|
|
|
|
actor = ActivityPub::TagManager.instance.uri_to_resource(uri, Account)
|
2024-02-01 15:56:46 +01:00
|
|
|
actor = ActivityPub::FetchRemoteAccountService.new.call(uri, request_id: @request_id) if actor.nil? || actor.possibly_stale?
|
2022-03-12 09:11:36 +01:00
|
|
|
actor
|
|
|
|
end
|
|
|
|
|
2017-08-08 21:52:15 +02:00
|
|
|
def supported_context?
|
|
|
|
super(@json)
|
|
|
|
end
|
|
|
|
|
2021-10-21 01:14:04 +02:00
|
|
|
def expected_activity_type?
|
|
|
|
equals_or_includes_any?(@json['type'], %w(Create Announce))
|
|
|
|
end
|
|
|
|
|
|
|
|
def expected_object_type?
|
2018-05-02 12:40:24 +02:00
|
|
|
equals_or_includes_any?(@json['type'], ActivityPub::Activity::Create::SUPPORTED_TYPES + ActivityPub::Activity::Create::CONVERTED_TYPES)
|
2017-08-08 21:52:15 +02:00
|
|
|
end
|
|
|
|
end
|