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