2017-08-08 19:52:15 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ActivityPub::Activity::Announce < ActivityPub::Activity
|
|
|
|
def perform
|
2019-02-17 02:38:25 +00:00
|
|
|
return reject_payload! if delete_arrived_first?(@json['id']) || !related_to_local_activity?
|
|
|
|
|
2021-04-21 02:46:09 +00:00
|
|
|
lock_or_fail("announce:#{@object['id']}") do
|
2021-02-12 11:32:55 +00:00
|
|
|
@original_status = status_from_object
|
2019-02-15 17:19:45 +00:00
|
|
|
|
2021-02-12 11:32:55 +00:00
|
|
|
return reject_payload! if @original_status.nil? || !announceable?(@original_status)
|
2017-08-08 19:52:15 +00:00
|
|
|
|
2021-02-12 11:32:55 +00:00
|
|
|
@status = Status.find_by(account: @account, reblog: @original_status)
|
2017-08-20 14:53:47 +00:00
|
|
|
|
2021-02-12 11:32:55 +00:00
|
|
|
if @status.nil?
|
|
|
|
process_status
|
|
|
|
elsif @options[:delivered_to_account_id].present?
|
|
|
|
postprocess_audience_and_deliver
|
2021-05-07 12:33:43 +00:00
|
|
|
end
|
2020-07-20 09:25:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@status
|
2017-08-08 19:52:15 +00:00
|
|
|
end
|
2017-08-19 16:44:48 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-02-12 11:32:55 +00:00
|
|
|
def process_status
|
|
|
|
@mentions = []
|
|
|
|
@params = {}
|
|
|
|
|
|
|
|
process_status_params
|
2021-07-16 06:38:50 +00:00
|
|
|
process_expiry_params
|
2021-02-12 11:32:55 +00:00
|
|
|
process_audience
|
|
|
|
|
|
|
|
ApplicationRecord.transaction do
|
|
|
|
@status = Status.create!(@params)
|
|
|
|
attach_mentions(@status)
|
|
|
|
end
|
|
|
|
|
|
|
|
@original_status.tags.each do |tag|
|
|
|
|
tag.use!(@account)
|
|
|
|
end
|
|
|
|
|
|
|
|
distribute(@status)
|
2021-07-16 06:38:50 +00:00
|
|
|
expire_queue_action
|
2020-08-24 12:11:47 +00:00
|
|
|
end
|
|
|
|
|
2021-02-12 11:32:55 +00:00
|
|
|
def process_status_params
|
|
|
|
@params = begin
|
|
|
|
{
|
|
|
|
account: @account,
|
|
|
|
reblog: @original_status,
|
|
|
|
uri: @json['id'],
|
|
|
|
created_at: @json['published'],
|
|
|
|
override_timestamps: @options[:override_timestamps],
|
2021-07-16 06:38:50 +00:00
|
|
|
visibility: visibility_from_audience,
|
|
|
|
expires_at: @json['expiry'],
|
|
|
|
expires_action: :mark,
|
2021-02-12 11:32:55 +00:00
|
|
|
}
|
|
|
|
end
|
2020-08-24 12:11:47 +00:00
|
|
|
end
|
|
|
|
|
2021-07-16 06:38:50 +00:00
|
|
|
def process_expiry_params
|
2021-07-27 12:05:40 +00:00
|
|
|
expiry = @object['expiry']&.to_time
|
2021-07-16 06:38:50 +00:00
|
|
|
|
|
|
|
if expiry.nil?
|
|
|
|
@params
|
|
|
|
elsif expiry <= Time.now.utc + PostStatusService::MIN_EXPIRE_OFFSET
|
|
|
|
@params.merge!({
|
|
|
|
expired_at: @object['expiry']
|
|
|
|
})
|
|
|
|
else
|
|
|
|
@params.merge!({
|
|
|
|
expires_at: @object['expiry'],
|
|
|
|
expires_action: :mark,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-12 11:32:55 +00:00
|
|
|
def attach_mentions(status)
|
|
|
|
@mentions.each do |mention|
|
|
|
|
mention.status = status
|
|
|
|
mention.save
|
2019-01-18 14:56:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-16 06:38:50 +00:00
|
|
|
def expire_queue_action
|
|
|
|
@status.status_expire.queue_action if expires_soon?
|
|
|
|
end
|
|
|
|
|
|
|
|
def expires_soon?
|
|
|
|
expires_at = @status&.status_expire&.expires_at
|
|
|
|
expires_at.present? && expires_at <= Time.now.utc + PostStatusService::MIN_SCHEDULE_OFFSET
|
|
|
|
end
|
|
|
|
|
2018-01-09 18:35:10 +00:00
|
|
|
def announceable?(status)
|
2021-02-12 11:32:55 +00:00
|
|
|
status.account_id == @account.id || (@account.group? && dereferenced?) || status.distributable? || status.account.mutual?(@account)
|
2018-01-09 18:35:10 +00:00
|
|
|
end
|
2019-02-15 17:19:45 +00:00
|
|
|
|
|
|
|
def related_to_local_activity?
|
|
|
|
followed_by_local_accounts? || requested_through_relay? || reblog_of_local_status?
|
|
|
|
end
|
|
|
|
|
2019-03-27 18:58:24 +00:00
|
|
|
def requested_through_relay?
|
|
|
|
super || Relay.find_by(inbox_url: @account.inbox_url)&.enabled?
|
|
|
|
end
|
|
|
|
|
2019-02-15 17:19:45 +00:00
|
|
|
def reblog_of_local_status?
|
2021-02-12 11:32:55 +00:00
|
|
|
ActivityPub::TagManager.instance.local_uri?(object_uri) && status_from_uri(object_uri)&.account&.local?
|
2019-02-15 17:19:45 +00:00
|
|
|
end
|
2017-08-08 19:52:15 +00:00
|
|
|
end
|