2017-08-09 04:52:15 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ActivityPub::Activity
|
|
|
|
include JsonLdHelper
|
2019-02-03 03:11:38 +09:00
|
|
|
include Redisable
|
2022-05-13 07:02:35 +09:00
|
|
|
include Lockable
|
2017-08-09 04:52:15 +09:00
|
|
|
|
2019-03-04 06:18:23 +09:00
|
|
|
SUPPORTED_TYPES = %w(Note Question).freeze
|
2019-12-17 07:55:28 +09:00
|
|
|
CONVERTED_TYPES = %w(Image Audio Video Article Page Event).freeze
|
2019-02-14 02:36:23 +09:00
|
|
|
|
2017-12-06 19:41:57 +09:00
|
|
|
def initialize(json, account, **options)
|
2017-08-09 04:52:15 +09:00
|
|
|
@json = json
|
|
|
|
@account = account
|
|
|
|
@object = @json['object']
|
2017-10-09 00:34:34 +09:00
|
|
|
@options = options
|
2017-08-09 04:52:15 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def perform
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
class << self
|
2017-12-06 19:41:57 +09:00
|
|
|
def factory(json, account, **options)
|
2017-08-09 04:52:15 +09:00
|
|
|
@json = json
|
2020-01-11 05:57:05 +09:00
|
|
|
klass&.new(json, account, **options)
|
2017-08-09 04:52:15 +09: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
|
2022-11-29 07:23:13 +09:00
|
|
|
when 'EmojiReact'
|
|
|
|
ActivityPub::Activity::EmojiReact
|
2017-08-09 04:52:15 +09:00
|
|
|
when 'Block'
|
|
|
|
ActivityPub::Activity::Block
|
|
|
|
when 'Update'
|
|
|
|
ActivityPub::Activity::Update
|
|
|
|
when 'Undo'
|
|
|
|
ActivityPub::Activity::Undo
|
2017-08-11 05:33:12 +09:00
|
|
|
when 'Accept'
|
|
|
|
ActivityPub::Activity::Accept
|
|
|
|
when 'Reject'
|
|
|
|
ActivityPub::Activity::Reject
|
2018-02-28 14:54:55 +09:00
|
|
|
when 'Flag'
|
|
|
|
ActivityPub::Activity::Flag
|
2018-03-04 17:19:11 +09:00
|
|
|
when 'Add'
|
|
|
|
ActivityPub::Activity::Add
|
|
|
|
when 'Remove'
|
|
|
|
ActivityPub::Activity::Remove
|
2018-12-29 10:24:36 +09:00
|
|
|
when 'Move'
|
|
|
|
ActivityPub::Activity::Move
|
2017-08-09 04:52:15 +09: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
|
2022-03-12 17:11:36 +09:00
|
|
|
@object_uri ||= uri_from_bearcap(value_or_id(@object))
|
2017-08-09 04:52:15 +09:00
|
|
|
end
|
|
|
|
|
2019-02-14 02:36:23 +09: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-09 04:52:15 +09:00
|
|
|
def delete_arrived_first?(uri)
|
2020-07-02 02:05:21 +09:00
|
|
|
redis.exists?("delete_upon_arrival:#{@account.id}:#{uri}")
|
2017-08-09 04:52:15 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete_later!(uri)
|
2021-04-21 11:46:09 +09:00
|
|
|
redis.setex("delete_upon_arrival:#{@account.id}:#{uri}", 6.hours.seconds, true)
|
2017-08-09 04:52:15 +09:00
|
|
|
end
|
2018-05-18 18:33:56 +09:00
|
|
|
|
2019-02-14 02:36:23 +09:00
|
|
|
def status_from_object
|
|
|
|
# If the status is already known, return it
|
|
|
|
status = status_from_uri(object_uri)
|
2019-02-16 02:19:45 +09:00
|
|
|
|
2019-02-14 02:36:23 +09: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-05 06:24:31 +09:00
|
|
|
actor_id = value_or_id(first_of_value(@object['attributedTo']))
|
2019-02-16 02:19:45 +09:00
|
|
|
|
2019-02-14 02:36:23 +09:00
|
|
|
if actor_id == @account.uri
|
2023-02-11 06:16:37 +09:00
|
|
|
virtual_object = { 'type' => 'Create', 'actor' => actor_id, 'object' => @object }
|
|
|
|
return ActivityPub::Activity.factory(virtual_object, @account, request_id: @options[:request_id]).perform
|
2019-02-14 02:36:23 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-17 23:16:36 +09:00
|
|
|
fetch_remote_original_status
|
2019-02-14 02:36:23 +09:00
|
|
|
end
|
|
|
|
|
2020-07-22 18:43:17 +09:00
|
|
|
def dereference_object!
|
|
|
|
return unless @object.is_a?(String)
|
|
|
|
|
2022-09-22 05:45:57 +09:00
|
|
|
dereferencer = ActivityPub::Dereferencer.new(@object, permitted_origin: @account.uri, signature_actor: signed_fetch_actor)
|
2020-07-22 18:43:17 +09:00
|
|
|
|
2020-08-30 19:34:20 +09:00
|
|
|
@object = dereferencer.object unless dereferencer.object.nil?
|
2020-07-22 18:43:17 +09:00
|
|
|
end
|
|
|
|
|
2022-09-22 05:45:57 +09:00
|
|
|
def signed_fetch_actor
|
2020-08-24 23:56:21 +09:00
|
|
|
return Account.find(@options[:delivered_to_account_id]) if @options[:delivered_to_account_id].present?
|
|
|
|
|
2020-07-22 18:43:17 +09:00
|
|
|
first_mentioned_local_account || first_local_follower
|
|
|
|
end
|
|
|
|
|
|
|
|
def first_mentioned_local_account
|
2020-08-24 21:11:47 +09:00
|
|
|
audience = (as_array(@json['to']) + as_array(@json['cc'])).map { |x| value_or_id(x) }.uniq
|
2020-07-22 18:43:17 +09: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-25 05:45:43 +09: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 21:02:27 +09:00
|
|
|
@follow ||= ::Follow.find_by(target_account: @account, uri: object_uri) unless object_uri.nil?
|
2019-10-25 05:45:43 +09:00
|
|
|
end
|
|
|
|
|
2018-05-18 18:33:56 +09:00
|
|
|
def fetch_remote_original_status
|
|
|
|
if object_uri.start_with?('http')
|
|
|
|
return if ActivityPub::TagManager.instance.local_uri?(object_uri)
|
2023-02-20 14:58:28 +09:00
|
|
|
|
2023-02-11 06:16:37 +09:00
|
|
|
ActivityPub::FetchRemoteStatusService.new.call(object_uri, id: true, on_behalf_of: @account.followers.local.first, request_id: @options[:request_id])
|
2018-05-18 18:33:56 +09:00
|
|
|
elsif @object['url'].present?
|
2023-02-11 06:16:37 +09:00
|
|
|
::FetchRemoteStatusService.new.call(@object['url'], request_id: @options[:request_id])
|
2018-05-18 18:33:56 +09:00
|
|
|
end
|
|
|
|
end
|
2018-11-17 03:46:23 +09:00
|
|
|
|
2019-02-16 02:19:45 +09:00
|
|
|
def fetch?
|
|
|
|
!@options[:delivery]
|
|
|
|
end
|
|
|
|
|
|
|
|
def followed_by_local_accounts?
|
2022-09-22 05:45:57 +09:00
|
|
|
@account.passive_relationships.exists? || (@options[:relayed_through_actor].is_a?(Account) && @options[:relayed_through_actor].passive_relationships&.exists?)
|
2019-02-16 02:19:45 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def requested_through_relay?
|
2022-09-22 05:45:57 +09:00
|
|
|
@options[:relayed_through_actor] && Relay.find_by(inbox_url: @options[:relayed_through_actor].inbox_url)&.enabled?
|
2019-02-16 02:19:45 +09:00
|
|
|
end
|
2019-02-17 11:38:25 +09:00
|
|
|
|
|
|
|
def reject_payload!
|
2022-09-22 05:45:57 +09:00
|
|
|
Rails.logger.info("Rejected #{@json['type']} activity #{@json['id']} from #{@account.uri}#{@options[:relayed_through_actor] && "via #{@options[:relayed_through_actor].uri}"}")
|
2019-02-17 11:38:25 +09:00
|
|
|
nil
|
|
|
|
end
|
2022-12-03 02:02:06 +09:00
|
|
|
|
2023-05-15 06:56:10 +09:00
|
|
|
# Ensure emoji declared in the activity's tags are
|
2022-12-03 02:02:06 +09:00
|
|
|
# present in the database and downloaded to the local cache.
|
2022-12-03 19:22:15 +09:00
|
|
|
# Required by EmojiReact and Like for emoji reactions.
|
2023-05-17 21:53:11 +09:00
|
|
|
def process_emoji_tags(name, tags)
|
|
|
|
tag = as_array(tags).find { |item| item['type'] == 'Emoji' }
|
|
|
|
return if tag.nil?
|
2023-05-15 06:56:10 +09:00
|
|
|
|
2022-12-03 19:22:15 +09:00
|
|
|
custom_emoji_parser = ActivityPub::Parser::CustomEmojiParser.new(tag)
|
2023-05-17 21:53:11 +09:00
|
|
|
return if custom_emoji_parser.shortcode.blank? || custom_emoji_parser.image_remote_url.blank? || !name.eql?(custom_emoji_parser.shortcode)
|
2022-12-03 02:02:06 +09:00
|
|
|
|
2022-12-03 19:22:15 +09:00
|
|
|
emoji = CustomEmoji.find_by(shortcode: custom_emoji_parser.shortcode, domain: @account.domain)
|
2023-05-17 21:53:11 +09:00
|
|
|
return emoji unless emoji.nil? ||
|
|
|
|
custom_emoji_parser.image_remote_url != emoji.image_remote_url ||
|
|
|
|
(custom_emoji_parser.updated_at && custom_emoji_parser.updated_at >= emoji.updated_at)
|
2022-12-03 02:02:06 +09:00
|
|
|
|
|
|
|
begin
|
2022-12-03 19:22:15 +09:00
|
|
|
emoji ||= CustomEmoji.new(domain: @account.domain,
|
|
|
|
shortcode: custom_emoji_parser.shortcode,
|
|
|
|
uri: custom_emoji_parser.uri)
|
|
|
|
emoji.image_remote_url = custom_emoji_parser.image_remote_url
|
2022-12-03 02:02:06 +09:00
|
|
|
emoji.save
|
|
|
|
rescue Seahorse::Client::NetworkingError => e
|
|
|
|
Rails.logger.warn "Error fetching emoji: #{e}"
|
2023-05-17 21:53:11 +09:00
|
|
|
return
|
2022-12-03 02:02:06 +09:00
|
|
|
end
|
2023-05-17 21:53:11 +09:00
|
|
|
emoji
|
2022-12-03 02:02:06 +09:00
|
|
|
end
|
2017-08-09 04:52:15 +09:00
|
|
|
end
|