Refactor StatusReachFinder to be used in more places
This commit is contained in:
parent
c99d28369a
commit
ce463ccb50
13 changed files with 321 additions and 99 deletions
|
@ -1,12 +1,13 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class AccountReachFinder
|
||||
def initialize(account)
|
||||
def initialize(account, options = {})
|
||||
@account = account
|
||||
@options = options
|
||||
end
|
||||
|
||||
def inboxes
|
||||
(followers_inboxes + reporters_inboxes + relay_inboxes).uniq
|
||||
(followers_inboxes + reporters_inboxes + blocked_by_inboxes + relay_inboxes).uniq
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -19,6 +20,14 @@ class AccountReachFinder
|
|||
Account.where(id: @account.targeted_reports.select(:account_id)).inboxes
|
||||
end
|
||||
|
||||
def blocked_by_inboxes
|
||||
if @options[:with_blocking_accounts]
|
||||
@account.blocked_by.inboxes
|
||||
else
|
||||
[]
|
||||
end
|
||||
end
|
||||
|
||||
def relay_inboxes
|
||||
Relay.enabled.pluck(:inbox_url)
|
||||
end
|
||||
|
|
|
@ -490,7 +490,16 @@ class ActivityPub::Activity::Create < ActivityPub::Activity
|
|||
def forward_for_reply
|
||||
return unless @status.distributable? && @json['signature'].present? && reply_to_local?
|
||||
|
||||
ActivityPub::RawDistributionWorker.perform_async(Oj.dump(@json), replied_to_status.account_id, [@account.preferred_inbox_url])
|
||||
# If the status is replying to a local status, we should forward it
|
||||
# everywhere where that local status was distributed
|
||||
|
||||
status_reach_finder = StatusReachFinder.new(@status)
|
||||
sender_id = replied_to_status.account_id
|
||||
exclude_inboxes = [@options[:relayed_through_account], @account].compact.map(&:preferred_inbox_url)
|
||||
|
||||
ActivityPub::LowPriorityDeliveryWorker.push_bulk(status_reach_finder.inboxes - exclude_inboxes) do |inbox_url|
|
||||
[payload, sender_id, inbox_url]
|
||||
end
|
||||
end
|
||||
|
||||
def increment_voters_count!
|
||||
|
|
|
@ -37,22 +37,14 @@ class ActivityPub::Activity::Delete < ActivityPub::Activity
|
|||
|
||||
return if @status.nil?
|
||||
|
||||
forward! if @json['signature'].present? && @status.distributable?
|
||||
forward!
|
||||
delete_now!
|
||||
end
|
||||
end
|
||||
|
||||
def rebloggers_ids
|
||||
return @rebloggers_ids if defined?(@rebloggers_ids)
|
||||
@rebloggers_ids = @status.reblogs.includes(:account).references(:account).merge(Account.local).pluck(:account_id)
|
||||
end
|
||||
|
||||
def inboxes_for_reblogs
|
||||
Account.where(id: ::Follow.where(target_account_id: rebloggers_ids).select(:account_id)).inboxes
|
||||
end
|
||||
|
||||
def replied_to_status
|
||||
return @replied_to_status if defined?(@replied_to_status)
|
||||
|
||||
@replied_to_status = @status.thread
|
||||
end
|
||||
|
||||
|
@ -60,18 +52,19 @@ class ActivityPub::Activity::Delete < ActivityPub::Activity
|
|||
!replied_to_status.nil? && replied_to_status.account.local?
|
||||
end
|
||||
|
||||
def inboxes_for_reply
|
||||
replied_to_status.account.followers.inboxes
|
||||
end
|
||||
|
||||
def forward!
|
||||
inboxes = inboxes_for_reblogs
|
||||
inboxes += inboxes_for_reply if reply_to_local?
|
||||
inboxes -= [@account.preferred_inbox_url]
|
||||
return unless @json['signature'].present? && @status.distributable?
|
||||
|
||||
sender_id = reply_to_local? ? replied_to_status.account_id : rebloggers_ids.first
|
||||
# A remote status is being deleted. We could have local users that
|
||||
# have reblogged it, who have remote followers. And if it's a reply
|
||||
# to a local status, the parent would have forwarded it to
|
||||
# lots of places
|
||||
|
||||
ActivityPub::LowPriorityDeliveryWorker.push_bulk(inboxes.uniq) do |inbox_url|
|
||||
status_reach_finder = StatusReachFinder.new(@status)
|
||||
sender_id = Account.representative.id
|
||||
exclude_inboxes = [@options[:relayed_through_account], @account].compact.map(&:preferred_inbox_url)
|
||||
|
||||
ActivityPub::LowPriorityDeliveryWorker.push_bulk(status_reach_finder.inboxes - exclude_inboxes) do |inbox_url|
|
||||
[payload, sender_id, inbox_url]
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,10 +1,19 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class StatusReachFinder
|
||||
def initialize(status)
|
||||
@status = status
|
||||
DEFAULT_OPTIONS = {
|
||||
with_parent: true,
|
||||
}.freeze
|
||||
|
||||
# @param [Status] status
|
||||
# @param [Hash] options
|
||||
# @option [Boolean] with_parent
|
||||
def initialize(status, options = {})
|
||||
@status = status
|
||||
@options = DEFAULT_OPTIONS.merge(options)
|
||||
end
|
||||
|
||||
# @return [Array<String>]
|
||||
def inboxes
|
||||
(reached_account_inboxes + followers_inboxes + relay_inboxes).uniq
|
||||
end
|
||||
|
@ -15,11 +24,15 @@ class StatusReachFinder
|
|||
# When the status is a reblog, there are no interactions with it
|
||||
# directly, we assume all interactions are with the original one
|
||||
|
||||
if @status.reblog?
|
||||
[]
|
||||
else
|
||||
Account.where(id: reached_account_ids).inboxes
|
||||
end
|
||||
return [] if @status.reblog?
|
||||
|
||||
# If a status is a reply to a local status, we also want to send
|
||||
# it everywhere the parent status was sent
|
||||
|
||||
arr = []
|
||||
arr.concat(self.class.new(@status.thread, with_parent: false).inboxes) if @status.in_reply_to_local_account? && @options[:with_parent]
|
||||
arr.concat(Account.where(id: reached_account_ids).inboxes)
|
||||
arr
|
||||
end
|
||||
|
||||
def reached_account_ids
|
||||
|
@ -28,6 +41,7 @@ class StatusReachFinder
|
|||
reblog_of_account_id,
|
||||
mentioned_account_ids,
|
||||
reblogs_account_ids,
|
||||
reblogger_follower_account_ids,
|
||||
favourites_account_ids,
|
||||
replies_account_ids,
|
||||
].tap do |arr|
|
||||
|
@ -38,7 +52,7 @@ class StatusReachFinder
|
|||
end
|
||||
|
||||
def replied_to_account_id
|
||||
@status.in_reply_to_account_id
|
||||
@status.in_reply_to_account_id if @status.local?
|
||||
end
|
||||
|
||||
def reblog_of_account_id
|
||||
|
@ -46,27 +60,27 @@ class StatusReachFinder
|
|||
end
|
||||
|
||||
def mentioned_account_ids
|
||||
@status.mentions.pluck(:account_id)
|
||||
@status.mentions.pluck(:account_id) if @status.local?
|
||||
end
|
||||
|
||||
def reblogs_account_ids
|
||||
@status.reblogs.pluck(:account_id)
|
||||
@reblogs_account_ids ||= @status.reblogs.pluck(:account_id)
|
||||
end
|
||||
|
||||
def reblogger_follower_account_ids
|
||||
Follow.where(target_account_id: reblogs_account_ids).pluck(:account_id)
|
||||
end
|
||||
|
||||
def favourites_account_ids
|
||||
@status.favourites.pluck(:account_id)
|
||||
@status.favourites.pluck(:account_id) if @status.local?
|
||||
end
|
||||
|
||||
def replies_account_ids
|
||||
@status.replies.pluck(:account_id)
|
||||
@status.replies.pluck(:account_id) if @status.local?
|
||||
end
|
||||
|
||||
def followers_inboxes
|
||||
if @status.in_reply_to_local_account? && @status.distributable?
|
||||
@status.account.followers.or(@status.thread.account.followers).inboxes
|
||||
else
|
||||
@status.account.followers.inboxes
|
||||
end
|
||||
@status.account.followers.inboxes
|
||||
end
|
||||
|
||||
def relay_inboxes
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue