2016-11-15 10:56:29 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-03-08 14:16:11 -05:00
|
|
|
class FanOutOnWriteService < BaseService
|
2022-04-28 11:47:34 -04:00
|
|
|
include Redisable
|
|
|
|
|
2016-03-08 14:16:11 -05:00
|
|
|
# Push a status into home and mentions feeds
|
|
|
|
# @param [Status] status
|
2022-01-19 16:37:27 -05:00
|
|
|
# @param [Hash] options
|
|
|
|
# @option options [Boolean] update
|
|
|
|
# @option options [Array<Integer>] silenced_account_ids
|
2023-11-02 10:58:37 -04:00
|
|
|
# @option options [Boolean] skip_notifications
|
2022-01-19 16:37:27 -05:00
|
|
|
def call(status, options = {})
|
|
|
|
@status = status
|
|
|
|
@account = status.account
|
|
|
|
@options = options
|
|
|
|
|
|
|
|
check_race_condition!
|
2022-11-04 08:21:06 -04:00
|
|
|
warm_payload_cache!
|
2022-01-19 16:37:27 -05:00
|
|
|
|
|
|
|
fan_out_to_local_recipients!
|
2022-07-17 07:49:29 -04:00
|
|
|
fan_out_to_public_recipients! if broadcastable?
|
2022-01-19 16:37:27 -05:00
|
|
|
fan_out_to_public_streams! if broadcastable?
|
|
|
|
end
|
2016-11-05 10:20:05 -04:00
|
|
|
|
2022-01-19 16:37:27 -05:00
|
|
|
private
|
2016-11-05 10:20:05 -04:00
|
|
|
|
2022-01-19 16:37:27 -05: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 06:33:59 -04:00
|
|
|
|
2022-01-19 16:37:27 -05:00
|
|
|
raise Mastodon::RaceConditionError if @status.visibility.nil?
|
|
|
|
end
|
2017-01-31 16:34:33 -05:00
|
|
|
|
2022-01-19 16:37:27 -05:00
|
|
|
def fan_out_to_local_recipients!
|
|
|
|
deliver_to_self!
|
2023-11-02 10:58:37 -04:00
|
|
|
|
|
|
|
unless @options[:skip_notifications]
|
|
|
|
notify_mentioned_accounts!
|
|
|
|
notify_about_update! if update?
|
|
|
|
end
|
2017-01-31 16:34:33 -05:00
|
|
|
|
2022-01-19 16:37:27 -05: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!
|
2022-01-19 17:19:00 -05:00
|
|
|
deliver_to_direct_timelines!
|
2022-01-19 16:37:27 -05:00
|
|
|
end
|
2016-03-21 12:02:16 -04:00
|
|
|
end
|
|
|
|
|
2022-07-17 07:49:29 -04:00
|
|
|
def fan_out_to_public_recipients!
|
|
|
|
deliver_to_hashtag_followers!
|
|
|
|
end
|
|
|
|
|
2022-01-19 16:37:27 -05:00
|
|
|
def fan_out_to_public_streams!
|
|
|
|
broadcast_to_hashtag_streams!
|
|
|
|
broadcast_to_public_streams!
|
|
|
|
end
|
2016-03-08 14:16:11 -05:00
|
|
|
|
2022-01-19 16:37:27 -05:00
|
|
|
def deliver_to_self!
|
|
|
|
FeedManager.instance.push_to_home(@account, @status, update: update?) if @account.local?
|
2022-01-19 17:19:00 -05:00
|
|
|
FeedManager.instance.push_to_direct(@account, @status, update: update?) if @account.local? && @status.direct_visibility?
|
2016-03-21 12:02:16 -04:00
|
|
|
end
|
2016-03-08 14:16:11 -05:00
|
|
|
|
2022-01-19 16:37:27 -05: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-27 18:43:56 -05:00
|
|
|
[mention.account_id, mention.id, 'Mention', 'mention']
|
2022-01-19 16:37:27 -05:00
|
|
|
end
|
2023-12-12 05:39:21 -05:00
|
|
|
|
|
|
|
next unless update?
|
|
|
|
|
|
|
|
# This may result in duplicate update payloads, but this ensures clients
|
|
|
|
# are aware of edits to posts only appearing in mention notifications
|
|
|
|
# (e.g. private mentions or mentions by people they do not follow)
|
|
|
|
PushUpdateWorker.push_bulk(mentions.filter { |mention| subscribed_to_streaming_api?(mention.account_id) }) do |mention|
|
|
|
|
[mention.account_id, @status.id, "timeline:#{mention.account_id}:notifications", { 'update' => true }]
|
|
|
|
end
|
2022-01-19 16:37:27 -05:00
|
|
|
end
|
|
|
|
end
|
2016-11-06 09:56:34 -05:00
|
|
|
|
2022-02-11 16:20:19 -05: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-19 16:37:27 -05:00
|
|
|
def deliver_to_all_followers!
|
|
|
|
@account.followers_for_local_distribution.select(:id).reorder(nil).find_in_batches do |followers|
|
2017-06-03 18:11:15 -04:00
|
|
|
FeedInsertWorker.push_bulk(followers) do |follower|
|
2022-01-27 18:43:56 -05:00
|
|
|
[@status.id, follower.id, 'home', { 'update' => update? }]
|
2017-11-17 18:16:48 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-17 07:49:29 -04:00
|
|
|
def deliver_to_hashtag_followers!
|
|
|
|
TagFollow.where(tag_id: @status.tags.map(&:id)).select(:id, :account_id).reorder(nil).find_in_batches do |follows|
|
|
|
|
FeedInsertWorker.push_bulk(follows) do |follow|
|
|
|
|
[@status.id, follow.account_id, 'tags', { 'update' => update? }]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-19 16:37:27 -05:00
|
|
|
def deliver_to_lists!
|
|
|
|
@account.lists_for_local_distribution.select(:id).reorder(nil).find_in_batches do |lists|
|
2017-11-17 18:16:48 -05:00
|
|
|
FeedInsertWorker.push_bulk(lists) do |list|
|
2022-01-27 18:43:56 -05:00
|
|
|
[@status.id, list.id, 'list', { 'update' => update? }]
|
2017-06-03 18:11:15 -04:00
|
|
|
end
|
2016-03-08 14:16:11 -05:00
|
|
|
end
|
2016-03-21 12:02:16 -04:00
|
|
|
end
|
2016-03-08 14:16:11 -05:00
|
|
|
|
2022-01-19 16:37:27 -05: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-08-31 12:11:27 -04:00
|
|
|
FeedInsertWorker.push_bulk(mentions) do |mention|
|
2022-01-27 18:43:56 -05:00
|
|
|
[@status.id, mention.account_id, 'home', { 'update' => update? }]
|
2020-08-30 06:33:59 -04:00
|
|
|
end
|
2018-10-17 11:13:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-19 17:19:00 -05:00
|
|
|
def deliver_to_direct_timelines!
|
2023-04-09 05:25:30 -04:00
|
|
|
FeedInsertWorker.push_bulk(@status.mentions.includes(:account).map(&:account).select(&:local?)) do |account|
|
2022-01-28 03:07:56 -05:00
|
|
|
[@status.id, account.id, 'direct', { 'update' => update? }]
|
2022-01-19 17:19:00 -05:00
|
|
|
end
|
2017-04-05 08:26:17 -04:00
|
|
|
end
|
|
|
|
|
2022-01-19 16:37:27 -05:00
|
|
|
def broadcast_to_hashtag_streams!
|
2022-07-17 07:49:29 -04:00
|
|
|
@status.tags.map(&:name).each do |hashtag|
|
2022-04-28 11:47:34 -04:00
|
|
|
redis.publish("timeline:hashtag:#{hashtag.mb_chars.downcase}", anonymous_payload)
|
|
|
|
redis.publish("timeline:hashtag:#{hashtag.mb_chars.downcase}:local", anonymous_payload) if @status.local?
|
2016-11-05 10:20:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-19 16:37:27 -05:00
|
|
|
def broadcast_to_public_streams!
|
2022-01-19 17:19:00 -05:00
|
|
|
return if @status.reply? && @status.in_reply_to_account_id != @account.id && !Setting.show_replies_in_public_timelines
|
2017-02-06 17:46:14 -05:00
|
|
|
|
2022-04-28 11:47:34 -04:00
|
|
|
redis.publish('timeline:public', anonymous_payload)
|
|
|
|
redis.publish(@status.local? ? 'timeline:public:local' : 'timeline:public:remote', anonymous_payload)
|
2017-02-06 17:46:14 -05:00
|
|
|
|
2022-03-09 03:06:17 -05:00
|
|
|
if @status.with_media?
|
2022-04-28 11:47:34 -04:00
|
|
|
redis.publish('timeline:public:media', anonymous_payload)
|
|
|
|
redis.publish(@status.local? ? 'timeline:public:local:media' : 'timeline:public:remote:media', anonymous_payload)
|
2020-05-12 09:24:35 -04:00
|
|
|
end
|
2016-10-07 10:00:11 -04:00
|
|
|
end
|
2018-04-18 07:09:06 -04:00
|
|
|
|
2022-01-19 16:37:27 -05:00
|
|
|
def deliver_to_conversation!
|
|
|
|
AccountConversation.add_status(@account, @status) unless update?
|
2018-05-21 06:43:38 -04:00
|
|
|
end
|
|
|
|
|
2022-11-04 08:21:06 -04:00
|
|
|
def warm_payload_cache!
|
|
|
|
Rails.cache.write("fan-out/#{@status.id}", rendered_status)
|
|
|
|
end
|
|
|
|
|
2022-01-19 16:37:27 -05:00
|
|
|
def anonymous_payload
|
|
|
|
@anonymous_payload ||= Oj.dump(
|
|
|
|
event: update? ? :'status.update' : :update,
|
2022-11-04 08:21:06 -04:00
|
|
|
payload: rendered_status
|
2022-01-19 16:37:27 -05:00
|
|
|
)
|
|
|
|
end
|
2018-10-22 12:15:51 -04:00
|
|
|
|
2022-11-04 08:21:06 -04:00
|
|
|
def rendered_status
|
|
|
|
@rendered_status ||= InlineRenderer.render(@status, nil, :status)
|
|
|
|
end
|
|
|
|
|
2022-01-19 16:37:27 -05:00
|
|
|
def update?
|
2022-01-26 14:53:50 -05:00
|
|
|
@options[:update]
|
2018-10-22 12:15:51 -04:00
|
|
|
end
|
|
|
|
|
2022-01-19 16:37:27 -05:00
|
|
|
def broadcastable?
|
2022-01-19 17:19:00 -05:00
|
|
|
@status.public_visibility? && !@account.silenced? && (!@status.reblog? || Setting.show_reblogs_in_public_timelines)
|
2018-10-07 17:44:58 -04:00
|
|
|
end
|
2023-12-12 05:39:21 -05:00
|
|
|
|
|
|
|
def subscribed_to_streaming_api?(account_id)
|
|
|
|
redis.exists?("subscribed:timeline:#{account_id}") || redis.exists?("subscribed:timeline:#{account_id}:notifications")
|
|
|
|
end
|
2016-03-08 14:16:11 -05:00
|
|
|
end
|