Fix emoji reaction events to be notified according to visibility

This commit is contained in:
noellabo 2021-09-22 13:56:05 +09:00
parent 6b68245fa2
commit 394cebce57
4 changed files with 64 additions and 12 deletions

View file

@ -14,11 +14,16 @@ class FeedManager
# or the tracking sets will grow forever # or the tracking sets will grow forever
REBLOG_FALLOFF = 40 REBLOG_FALLOFF = 40
# Get active account
def active_accounts
Account.joins(:user).where('users.current_sign_in_at > ?', User::ACTIVE_DURATION.ago)
end
# Execute block for every active account # Execute block for every active account
# @yield [Account] # @yield [Account]
# @return [void] # @return [void]
def with_active_accounts(&block) def with_active_accounts(&block)
Account.joins(:user).where('users.current_sign_in_at > ?', User::ACTIVE_DURATION.ago).find_each(&block) active_accounts.find_each(&block)
end end
# Redis key of a feed # Redis key of a feed

View file

@ -40,7 +40,7 @@ class EmojiReaction < ApplicationRecord
private private
def queue_publish def queue_publish
PublishEmojiReactionWorker.perform_async(status_id, name) unless status.destroyed? PublishEmojiReactionWorker.perform_async(status_id, account_id, name) unless status.destroyed?
end end
def refresh_status def refresh_status

View file

@ -0,0 +1,41 @@
# frozen_string_literal: true
module PublishScope
extend ActiveSupport::Concern
included do
def visibility_scope
case @status.visibility
when 'public', 'unlisted'
local_scope
when 'private'
Account.where(id: Account
.union(local_followers_scope)
.union(local_mentions_scope)
.union(local_sender_scope)
)
when 'limited', 'direct'
Account.where(id: Account
.union(local_mentions_scope)
.union(local_sender_scope)
)
end.select(:id)
end
def local_scope
Account.local
end
def local_followers_scope
@status.account.delivery_followers.local.select(:id)
end
def local_mentions_scope
Account.local.where(id: @status.mentions.select(:account_id)).select(:id)
end
def local_sender_scope
Account.local.where(id: @account_id).select(:id)
end
end
end

View file

@ -4,20 +4,26 @@ class PublishEmojiReactionWorker
include Sidekiq::Worker include Sidekiq::Worker
include Redisable include Redisable
include RoutingHelper include RoutingHelper
include PublishScope
def perform(status_id, name) def perform(status_id, account_id, name)
status = Status.find(status_id) @status = Status.find(status_id)
payload = status.grouped_emoji_reactions.find { |emoji_reaction| emoji_reaction['name'] == name } @account_id = account_id
payload ||= { name: name, count: 0, account_ids: [] } @name = name
payload['status_id'] = status_id.to_s FeedManager.instance.active_accounts.merge(visibility_scope).find_each do |account|
redis.publish("timeline:#{account.id}", payload_json) if redis.exists?("subscribed:timeline:#{account.id}")
json = Oj.dump(event: :'emoji_reaction', payload: payload)
FeedManager.instance.with_active_accounts do |account|
redis.publish("timeline:#{account.id}", json) if redis.exists?("subscribed:timeline:#{account.id}")
end end
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound
true true
end end
def payload_json
payload = @status.grouped_emoji_reactions.find { |emoji_reaction| emoji_reaction['name'] == @name }
payload ||= { name: @name, count: 0, account_ids: [] }
payload['status_id'] = @status.id.to_s
Oj.dump(event: :'emoji_reaction', payload: payload)
end
end end