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
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
# @yield [Account]
# @return [void]
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
# Redis key of a feed

View file

@ -40,7 +40,7 @@ class EmojiReaction < ApplicationRecord
private
def queue_publish
PublishEmojiReactionWorker.perform_async(status_id, name) unless status.destroyed?
PublishEmojiReactionWorker.perform_async(status_id, account_id, name) unless status.destroyed?
end
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 Redisable
include RoutingHelper
include PublishScope
def perform(status_id, name)
status = Status.find(status_id)
payload = status.grouped_emoji_reactions.find { |emoji_reaction| emoji_reaction['name'] == name }
payload ||= { name: name, count: 0, account_ids: [] }
def perform(status_id, account_id, name)
@status = Status.find(status_id)
@account_id = account_id
@name = name
payload['status_id'] = status_id.to_s
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}")
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}")
end
rescue ActiveRecord::RecordNotFound
true
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