2017-08-08 20:52:15 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ActivityPub::Activity
|
|
|
|
include JsonLdHelper
|
2019-02-02 18:11:38 +00:00
|
|
|
include Redisable
|
2017-08-08 20:52:15 +01:00
|
|
|
|
2019-03-03 21:18:23 +00:00
|
|
|
SUPPORTED_TYPES = %w(Note Question).freeze
|
2019-12-16 22:55:28 +00:00
|
|
|
CONVERTED_TYPES = %w(Image Audio Video Article Page Event).freeze
|
2019-02-13 17:36:23 +00:00
|
|
|
|
2017-12-06 10:41:57 +00:00
|
|
|
def initialize(json, account, **options)
|
2017-08-08 20:52:15 +01:00
|
|
|
@json = json
|
|
|
|
@account = account
|
|
|
|
@object = @json['object']
|
2017-10-08 16:34:34 +01:00
|
|
|
@options = options
|
2017-08-08 20:52:15 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def perform
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
class << self
|
2017-12-06 10:41:57 +00:00
|
|
|
def factory(json, account, **options)
|
2017-08-08 20:52:15 +01:00
|
|
|
@json = json
|
2020-01-10 20:57:05 +00:00
|
|
|
klass&.new(json, account, **options)
|
2017-08-08 20:52:15 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def klass
|
|
|
|
case @json['type']
|
|
|
|
when 'Create'
|
|
|
|
ActivityPub::Activity::Create
|
|
|
|
when 'Announce'
|
|
|
|
ActivityPub::Activity::Announce
|
|
|
|
when 'Delete'
|
|
|
|
ActivityPub::Activity::Delete
|
|
|
|
when 'Follow'
|
|
|
|
ActivityPub::Activity::Follow
|
|
|
|
when 'Like'
|
|
|
|
ActivityPub::Activity::Like
|
|
|
|
when 'Block'
|
|
|
|
ActivityPub::Activity::Block
|
|
|
|
when 'Update'
|
|
|
|
ActivityPub::Activity::Update
|
|
|
|
when 'Undo'
|
|
|
|
ActivityPub::Activity::Undo
|
2017-08-10 21:33:12 +01:00
|
|
|
when 'Accept'
|
|
|
|
ActivityPub::Activity::Accept
|
|
|
|
when 'Reject'
|
|
|
|
ActivityPub::Activity::Reject
|
2018-02-28 05:54:55 +00:00
|
|
|
when 'Flag'
|
|
|
|
ActivityPub::Activity::Flag
|
2018-03-04 08:19:11 +00:00
|
|
|
when 'Add'
|
|
|
|
ActivityPub::Activity::Add
|
|
|
|
when 'Remove'
|
|
|
|
ActivityPub::Activity::Remove
|
2018-12-29 01:24:36 +00:00
|
|
|
when 'Move'
|
|
|
|
ActivityPub::Activity::Move
|
2017-08-08 20:52:15 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def status_from_uri(uri)
|
|
|
|
ActivityPub::TagManager.instance.uri_to_resource(uri, Status)
|
|
|
|
end
|
|
|
|
|
|
|
|
def account_from_uri(uri)
|
|
|
|
ActivityPub::TagManager.instance.uri_to_resource(uri, Account)
|
|
|
|
end
|
|
|
|
|
|
|
|
def object_uri
|
2020-08-30 11:34:20 +01:00
|
|
|
@object_uri ||= begin
|
|
|
|
str = value_or_id(@object)
|
|
|
|
|
2020-10-13 21:41:00 +01:00
|
|
|
if str&.start_with?('bear:')
|
2020-08-30 11:34:20 +01:00
|
|
|
Addressable::URI.parse(str).query_values['u']
|
|
|
|
else
|
|
|
|
str
|
|
|
|
end
|
|
|
|
end
|
2017-08-08 20:52:15 +01:00
|
|
|
end
|
|
|
|
|
2019-02-13 17:36:23 +00:00
|
|
|
def unsupported_object_type?
|
|
|
|
@object.is_a?(String) || !(supported_object_type? || converted_object_type?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def supported_object_type?
|
|
|
|
equals_or_includes_any?(@object['type'], SUPPORTED_TYPES)
|
|
|
|
end
|
|
|
|
|
|
|
|
def converted_object_type?
|
|
|
|
equals_or_includes_any?(@object['type'], CONVERTED_TYPES)
|
|
|
|
end
|
|
|
|
|
2017-08-08 20:52:15 +01:00
|
|
|
def distribute(status)
|
2017-10-17 19:05:21 +01:00
|
|
|
crawl_links(status)
|
|
|
|
|
2019-12-04 19:36:33 +00:00
|
|
|
notify_about_reblog(status) if reblog_of_local_account?(status) && !reblog_by_following_group_account?(status)
|
2018-01-19 18:11:35 +00:00
|
|
|
notify_about_mentions(status)
|
|
|
|
|
2018-05-03 22:02:46 +01:00
|
|
|
# Only continue if the status is supposed to have arrived in real-time.
|
|
|
|
# Note that if @options[:override_timestamps] isn't set, the status
|
|
|
|
# may have a lower snowflake id than other existing statuses, potentially
|
|
|
|
# "hiding" it from paginated API calls
|
|
|
|
return unless @options[:override_timestamps] || status.within_realtime_window?
|
2017-10-17 19:05:21 +01:00
|
|
|
|
2017-08-08 20:52:15 +01:00
|
|
|
distribute_to_followers(status)
|
|
|
|
end
|
|
|
|
|
|
|
|
def reblog_of_local_account?(status)
|
|
|
|
status.reblog? && status.reblog.account.local?
|
|
|
|
end
|
|
|
|
|
2019-12-04 19:36:33 +00:00
|
|
|
def reblog_by_following_group_account?(status)
|
|
|
|
status.reblog? && status.account.group? && status.reblog.account.following?(status.account)
|
|
|
|
end
|
|
|
|
|
2017-08-08 20:52:15 +01:00
|
|
|
def notify_about_reblog(status)
|
2020-09-18 16:26:45 +01:00
|
|
|
NotifyService.new.call(status.reblog.account, :reblog, status)
|
2017-08-08 20:52:15 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def notify_about_mentions(status)
|
2018-10-17 16:13:04 +01:00
|
|
|
status.active_mentions.includes(:account).each do |mention|
|
2017-08-08 20:52:15 +01:00
|
|
|
next unless mention.account.local? && audience_includes?(mention.account)
|
2020-09-18 16:26:45 +01:00
|
|
|
NotifyService.new.call(mention.account, :mention, mention)
|
2017-08-08 20:52:15 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def crawl_links(status)
|
|
|
|
return if status.spoiler_text?
|
2018-08-25 23:33:57 +01:00
|
|
|
|
|
|
|
# Spread out crawling randomly to avoid DDoSing the link
|
|
|
|
LinkCrawlWorker.perform_in(rand(1..59).seconds, status.id)
|
2017-08-08 20:52:15 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def distribute_to_followers(status)
|
2017-08-12 23:44:41 +01:00
|
|
|
::DistributionWorker.perform_async(status.id)
|
2017-08-08 20:52:15 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete_arrived_first?(uri)
|
2020-07-01 18:05:21 +01:00
|
|
|
redis.exists?("delete_upon_arrival:#{@account.id}:#{uri}")
|
2017-08-08 20:52:15 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete_later!(uri)
|
2021-04-21 03:46:09 +01:00
|
|
|
redis.setex("delete_upon_arrival:#{@account.id}:#{uri}", 6.hours.seconds, true)
|
2017-08-08 20:52:15 +01:00
|
|
|
end
|
2018-05-18 10:33:56 +01:00
|
|
|
|
2019-02-13 17:36:23 +00:00
|
|
|
def status_from_object
|
|
|
|
# If the status is already known, return it
|
|
|
|
status = status_from_uri(object_uri)
|
2019-02-15 17:19:45 +00:00
|
|
|
|
2019-02-13 17:36:23 +00:00
|
|
|
return status unless status.nil?
|
|
|
|
|
|
|
|
# If the boosted toot is embedded and it is a self-boost, handle it like a Create
|
|
|
|
unless unsupported_object_type?
|
2019-06-04 22:24:31 +01:00
|
|
|
actor_id = value_or_id(first_of_value(@object['attributedTo']))
|
2019-02-15 17:19:45 +00:00
|
|
|
|
2019-02-13 17:36:23 +00:00
|
|
|
if actor_id == @account.uri
|
|
|
|
return ActivityPub::Activity.factory({ 'type' => 'Create', 'actor' => actor_id, 'object' => @object }, @account).perform
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-17 14:16:36 +00:00
|
|
|
fetch_remote_original_status
|
2019-02-13 17:36:23 +00:00
|
|
|
end
|
|
|
|
|
2020-07-22 10:43:17 +01:00
|
|
|
def dereference_object!
|
|
|
|
return unless @object.is_a?(String)
|
|
|
|
|
2020-08-30 11:34:20 +01:00
|
|
|
dereferencer = ActivityPub::Dereferencer.new(@object, permitted_origin: @account.uri, signature_account: signed_fetch_account)
|
2020-07-22 10:43:17 +01:00
|
|
|
|
2020-08-30 11:34:20 +01:00
|
|
|
@object = dereferencer.object unless dereferencer.object.nil?
|
2020-07-22 10:43:17 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def signed_fetch_account
|
2020-08-24 15:56:21 +01:00
|
|
|
return Account.find(@options[:delivered_to_account_id]) if @options[:delivered_to_account_id].present?
|
|
|
|
|
2020-07-22 10:43:17 +01:00
|
|
|
first_mentioned_local_account || first_local_follower
|
|
|
|
end
|
|
|
|
|
|
|
|
def first_mentioned_local_account
|
2020-08-24 13:11:47 +01:00
|
|
|
audience = (as_array(@json['to']) + as_array(@json['cc'])).map { |x| value_or_id(x) }.uniq
|
2020-07-22 10:43:17 +01:00
|
|
|
local_usernames = audience.select { |uri| ActivityPub::TagManager.instance.local_uri?(uri) }
|
|
|
|
.map { |uri| ActivityPub::TagManager.instance.uri_to_local_id(uri, :username) }
|
|
|
|
|
|
|
|
return if local_usernames.empty?
|
|
|
|
|
|
|
|
Account.local.where(username: local_usernames).first
|
|
|
|
end
|
|
|
|
|
|
|
|
def first_local_follower
|
|
|
|
@account.followers.local.first
|
|
|
|
end
|
|
|
|
|
2019-10-24 21:45:43 +01:00
|
|
|
def follow_request_from_object
|
|
|
|
@follow_request ||= FollowRequest.find_by(target_account: @account, uri: object_uri) unless object_uri.nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
def follow_from_object
|
2019-11-04 12:02:27 +00:00
|
|
|
@follow ||= ::Follow.find_by(target_account: @account, uri: object_uri) unless object_uri.nil?
|
2019-10-24 21:45:43 +01:00
|
|
|
end
|
|
|
|
|
2018-05-18 10:33:56 +01:00
|
|
|
def fetch_remote_original_status
|
|
|
|
if object_uri.start_with?('http')
|
|
|
|
return if ActivityPub::TagManager.instance.local_uri?(object_uri)
|
|
|
|
ActivityPub::FetchRemoteStatusService.new.call(object_uri, id: true, on_behalf_of: @account.followers.local.first)
|
|
|
|
elsif @object['url'].present?
|
|
|
|
::FetchRemoteStatusService.new.call(@object['url'])
|
|
|
|
end
|
|
|
|
end
|
2018-11-16 18:46:23 +00:00
|
|
|
|
2021-04-21 03:46:09 +01:00
|
|
|
def lock_or_return(key, expire_after = 2.hours.seconds)
|
2018-11-16 18:46:23 +00:00
|
|
|
yield if redis.set(key, true, nx: true, ex: expire_after)
|
|
|
|
ensure
|
|
|
|
redis.del(key)
|
|
|
|
end
|
2019-02-15 17:19:45 +00:00
|
|
|
|
2021-05-19 22:52:08 +01:00
|
|
|
def lock_or_fail(key, expire_after = 15.minutes.seconds)
|
|
|
|
RedisLock.acquire({ redis: Redis.current, key: key, autorelease: expire_after }) do |lock|
|
2021-04-21 03:46:09 +01:00
|
|
|
if lock.acquired?
|
|
|
|
yield
|
|
|
|
else
|
|
|
|
raise Mastodon::RaceConditionError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-15 17:19:45 +00:00
|
|
|
def fetch?
|
|
|
|
!@options[:delivery]
|
|
|
|
end
|
|
|
|
|
|
|
|
def followed_by_local_accounts?
|
2020-07-14 18:05:34 +01:00
|
|
|
@account.passive_relationships.exists? || @options[:relayed_through_account]&.passive_relationships&.exists?
|
2019-02-15 17:19:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def requested_through_relay?
|
|
|
|
@options[:relayed_through_account] && Relay.find_by(inbox_url: @options[:relayed_through_account].inbox_url)&.enabled?
|
|
|
|
end
|
2019-02-17 02:38:25 +00:00
|
|
|
|
|
|
|
def reject_payload!
|
|
|
|
Rails.logger.info("Rejected #{@json['type']} activity #{@json['id']} from #{@account.uri}#{@options[:relayed_through_account] && "via #{@options[:relayed_through_account].uri}"}")
|
|
|
|
nil
|
|
|
|
end
|
2017-08-08 20:52:15 +01:00
|
|
|
end
|