2016-11-16 02:56:29 +11:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-03-09 06:16:11 +11:00
|
|
|
class FanOutOnWriteService < BaseService
|
|
|
|
# Push a status into home and mentions feeds
|
|
|
|
# @param [Status] status
|
2022-01-20 08:37:27 +11:00
|
|
|
# @param [Hash] options
|
|
|
|
# @option options [Boolean] update
|
|
|
|
# @option options [Array<Integer>] silenced_account_ids
|
|
|
|
def call(status, options = {})
|
|
|
|
@status = status
|
|
|
|
@account = status.account
|
|
|
|
@options = options
|
|
|
|
|
|
|
|
check_race_condition!
|
|
|
|
|
|
|
|
fan_out_to_local_recipients!
|
|
|
|
fan_out_to_public_streams! if broadcastable?
|
|
|
|
end
|
2016-11-06 01:20:05 +11:00
|
|
|
|
2022-01-20 08:37:27 +11:00
|
|
|
private
|
2016-11-06 01:20:05 +11:00
|
|
|
|
2022-01-20 08:37:27 +11:00
|
|
|
def check_race_condition!
|
|
|
|
# I don't know why but at some point we had an issue where
|
|
|
|
# this service was being executed with status objects
|
|
|
|
# that had a null visibility - which should not be possible
|
|
|
|
# since the column in the database is not nullable.
|
|
|
|
#
|
|
|
|
# This check re-queues the service to be run at a later time
|
|
|
|
# with the full object, if something like it occurs
|
2020-08-30 20:33:59 +10:00
|
|
|
|
2022-01-20 08:37:27 +11:00
|
|
|
raise Mastodon::RaceConditionError if @status.visibility.nil?
|
|
|
|
end
|
2017-02-01 08:34:33 +11:00
|
|
|
|
2022-01-20 08:37:27 +11:00
|
|
|
def fan_out_to_local_recipients!
|
|
|
|
deliver_to_self!
|
|
|
|
notify_mentioned_accounts!
|
2022-02-12 08:20:19 +11:00
|
|
|
notify_about_update! if update?
|
2017-02-01 08:34:33 +11:00
|
|
|
|
2022-01-20 08:37:27 +11:00
|
|
|
case @status.visibility.to_sym
|
|
|
|
when :public, :unlisted, :private
|
|
|
|
deliver_to_all_followers!
|
|
|
|
deliver_to_lists!
|
|
|
|
when :limited
|
|
|
|
deliver_to_mentioned_followers!
|
|
|
|
else
|
|
|
|
deliver_to_mentioned_followers!
|
|
|
|
deliver_to_conversation!
|
|
|
|
end
|
2016-03-22 03:02:16 +11:00
|
|
|
end
|
|
|
|
|
2022-01-20 08:37:27 +11:00
|
|
|
def fan_out_to_public_streams!
|
|
|
|
broadcast_to_hashtag_streams!
|
|
|
|
broadcast_to_public_streams!
|
|
|
|
end
|
2016-03-09 06:16:11 +11:00
|
|
|
|
2022-01-20 08:37:27 +11:00
|
|
|
def deliver_to_self!
|
|
|
|
FeedManager.instance.push_to_home(@account, @status, update: update?) if @account.local?
|
2016-03-22 03:02:16 +11:00
|
|
|
end
|
2016-03-09 06:16:11 +11:00
|
|
|
|
2022-01-20 08:37:27 +11:00
|
|
|
def notify_mentioned_accounts!
|
|
|
|
@status.active_mentions.where.not(id: @options[:silenced_account_ids] || []).joins(:account).merge(Account.local).select(:id, :account_id).reorder(nil).find_in_batches do |mentions|
|
|
|
|
LocalNotificationWorker.push_bulk(mentions) do |mention|
|
2022-01-28 10:43:56 +11:00
|
|
|
[mention.account_id, mention.id, 'Mention', 'mention']
|
2022-01-20 08:37:27 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-11-07 01:56:34 +11:00
|
|
|
|
2022-02-12 08:20:19 +11:00
|
|
|
def notify_about_update!
|
|
|
|
@status.reblogged_by_accounts.merge(Account.local).select(:id).reorder(nil).find_in_batches do |accounts|
|
|
|
|
LocalNotificationWorker.push_bulk(accounts) do |account|
|
|
|
|
[account.id, @status.id, 'Status', 'update']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-20 08:37:27 +11:00
|
|
|
def deliver_to_all_followers!
|
|
|
|
@account.followers_for_local_distribution.select(:id).reorder(nil).find_in_batches do |followers|
|
2017-06-04 08:11:15 +10:00
|
|
|
FeedInsertWorker.push_bulk(followers) do |follower|
|
2022-01-28 10:43:56 +11:00
|
|
|
[@status.id, follower.id, 'home', { 'update' => update? }]
|
2017-11-18 10:16:48 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-20 08:37:27 +11:00
|
|
|
def deliver_to_lists!
|
|
|
|
@account.lists_for_local_distribution.select(:id).reorder(nil).find_in_batches do |lists|
|
2017-11-18 10:16:48 +11:00
|
|
|
FeedInsertWorker.push_bulk(lists) do |list|
|
2022-01-28 10:43:56 +11:00
|
|
|
[@status.id, list.id, 'list', { 'update' => update? }]
|
2017-06-04 08:11:15 +10:00
|
|
|
end
|
2016-03-09 06:16:11 +11:00
|
|
|
end
|
2016-03-22 03:02:16 +11:00
|
|
|
end
|
2016-03-09 06:16:11 +11:00
|
|
|
|
2022-01-20 08:37:27 +11:00
|
|
|
def deliver_to_mentioned_followers!
|
|
|
|
@status.mentions.joins(:account).merge(@account.followers_for_local_distribution).select(:id, :account_id).reorder(nil).find_in_batches do |mentions|
|
2020-09-01 02:11:27 +10:00
|
|
|
FeedInsertWorker.push_bulk(mentions) do |mention|
|
2022-01-28 10:43:56 +11:00
|
|
|
[@status.id, mention.account_id, 'home', { 'update' => update? }]
|
2020-08-30 20:33:59 +10:00
|
|
|
end
|
2018-10-18 02:13:04 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-20 08:37:27 +11:00
|
|
|
def broadcast_to_hashtag_streams!
|
|
|
|
@status.tags.pluck(:name).each do |hashtag|
|
|
|
|
Redis.current.publish("timeline:hashtag:#{hashtag.mb_chars.downcase}", anonymous_payload)
|
|
|
|
Redis.current.publish("timeline:hashtag:#{hashtag.mb_chars.downcase}:local", anonymous_payload) if @status.local?
|
|
|
|
end
|
2017-04-05 22:26:17 +10:00
|
|
|
end
|
|
|
|
|
2022-01-20 08:37:27 +11:00
|
|
|
def broadcast_to_public_streams!
|
|
|
|
return if @status.reply? && @status.in_reply_to_account_id != @account.id
|
|
|
|
|
|
|
|
Redis.current.publish('timeline:public', anonymous_payload)
|
|
|
|
Redis.current.publish(@status.local? ? 'timeline:public:local' : 'timeline:public:remote', anonymous_payload)
|
2017-02-07 09:46:14 +11:00
|
|
|
|
2022-01-20 08:37:27 +11:00
|
|
|
if @status.media_attachments.any?
|
|
|
|
Redis.current.publish('timeline:public:media', anonymous_payload)
|
|
|
|
Redis.current.publish(@status.local? ? 'timeline:public:local:media' : 'timeline:public:remote:media', anonymous_payload)
|
2016-11-06 01:20:05 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-20 08:37:27 +11:00
|
|
|
def deliver_to_conversation!
|
|
|
|
AccountConversation.add_status(@account, @status) unless update?
|
2016-10-08 01:00:11 +11:00
|
|
|
end
|
2018-04-18 21:09:06 +10:00
|
|
|
|
2022-01-20 08:37:27 +11:00
|
|
|
def anonymous_payload
|
|
|
|
@anonymous_payload ||= Oj.dump(
|
|
|
|
event: update? ? :'status.update' : :update,
|
|
|
|
payload: InlineRenderer.render(@status, nil, :status)
|
|
|
|
)
|
|
|
|
end
|
2018-05-21 20:43:38 +10:00
|
|
|
|
2022-01-20 08:37:27 +11:00
|
|
|
def update?
|
2022-01-27 06:53:50 +11:00
|
|
|
@options[:update]
|
2018-05-21 20:43:38 +10:00
|
|
|
end
|
|
|
|
|
2022-01-20 08:37:27 +11:00
|
|
|
def broadcastable?
|
|
|
|
@status.public_visibility? && !@status.reblog? && !@account.silenced?
|
2018-10-08 08:44:58 +11:00
|
|
|
end
|
2016-03-09 06:16:11 +11:00
|
|
|
end
|