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