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