Skip to content

Commit

Permalink
Hide blocking domains/suspended/silenced emoji reactions and streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
kmycode committed Sep 12, 2023
1 parent cd252a6 commit 477d427
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/lib/activitypub/activity/like.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def write_stream(emoji_reaction)
emoji_group = @original_status.emoji_reactions_grouped_by_name
.find { |reaction_group| reaction_group['name'] == emoji_reaction.name && (!reaction_group.key?(:domain) || reaction_group['domain'] == emoji_reaction.custom_emoji&.domain) }
emoji_group['status_id'] = @original_status.id.to_s
DeliveryEmojiReactionWorker.perform_async(render_emoji_reaction(emoji_group), @original_status.id, emoji_reaction.account_id)
DeliveryEmojiReactionWorker.perform_async(render_emoji_reaction(emoji_group), emoji_reaction.id, emoji_reaction.account_id)
end

def render_emoji_reaction(emoji_group)
Expand Down
2 changes: 1 addition & 1 deletion app/lib/activitypub/activity/undo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def write_stream(emoji_reaction)
emoji_group = { 'name' => emoji_reaction.name, 'count' => 0, 'account_ids' => [], 'status_id' => @original_status.id.to_s }
emoji_group['domain'] = emoji_reaction.custom_emoji.domain if emoji_reaction.custom_emoji
end
DeliveryEmojiReactionWorker.perform_async(render_emoji_reaction(emoji_group), @original_status.id, emoji_reaction.account_id)
DeliveryEmojiReactionWorker.perform_async(render_emoji_reaction(emoji_group), emoji_reaction.id, emoji_reaction.account_id)
end

def render_emoji_reaction(emoji_group)
Expand Down
4 changes: 4 additions & 0 deletions app/lib/status_cache_hydrator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ def initialize(status)
end

def hydrate(account_id)
account = Account.find(account_id)

# The cache of the serialized hash is generated by the fan-out-on-write service
payload = Rails.cache.fetch("fan-out/#{@status.id}") { InlineRenderer.render(@status, nil, :status) }

Expand Down Expand Up @@ -33,6 +35,7 @@ def hydrate(account_id)
payload[:reblog][:bookmarked] = Bookmark.where(account_id: account_id, status_id: @status.reblog_of_id).exists?
payload[:reblog][:pinned] = StatusPin.where(account_id: account_id, status_id: @status.reblog_of_id).exists? if @status.reblog.account_id == account_id
payload[:reblog][:filtered] = payload[:filtered]
payload[:reblog][:emoji_reactions] = @status.emoji_reactions_grouped_by_name(account)

if payload[:reblog][:poll]
if @status.reblog.account_id == account_id
Expand All @@ -56,6 +59,7 @@ def hydrate(account_id)
payload[:filtered] = CustomFilter
.apply_cached_filters(CustomFilter.cached_filters_for(account_id), @status, following?(account_id))
.map { |filter| serialized_filter(filter) }
payload[:emoji_reactions] = @status.emoji_reactions_grouped_by_name(account)

if payload[:poll]
payload[:poll][:voted] = @status.account_id == account_id
Expand Down
8 changes: 8 additions & 0 deletions app/models/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,19 @@ def add_status_referred_by_count!(diff)
def emoji_reactions_grouped_by_name(account = nil)
(Oj.load(status_stat&.emoji_reactions || '', mode: :strict) || []).tap do |emoji_reactions|
if account.present?
remove_emoji_reactions = []
emoji_reactions.each do |emoji_reaction|
emoji_reaction['me'] = emoji_reaction['account_ids'].include?(account.id.to_s)
emoji_reaction['account_ids'] -= account.excluded_from_timeline_account_ids.map(&:to_s)

accounts = Account.where(id: emoji_reaction['account_ids'], silenced_at: nil, suspended_at: nil)
accounts = accounts.where.not(domain: account.excluded_from_timeline_domains) if account.excluded_from_timeline_domains.size.positive?
emoji_reaction['account_ids'] = accounts.pluck(:id).map(&:to_s)

emoji_reaction['count'] = emoji_reaction['account_ids'].size
remove_emoji_reactions << emoji_reaction if emoji_reaction['count'] <= 0
end
emoji_reactions - remove_emoji_reactions
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/services/emoji_react_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def write_stream(emoji_reaction)
emoji_group = emoji_reaction.status.emoji_reactions_grouped_by_name
.find { |reaction_group| reaction_group['name'] == emoji_reaction.name && (!reaction_group.key?(:domain) || reaction_group['domain'] == emoji_reaction.custom_emoji&.domain) }
emoji_group['status_id'] = emoji_reaction.status_id.to_s
DeliveryEmojiReactionWorker.perform_async(render_emoji_reaction(emoji_group), emoji_reaction.status_id, emoji_reaction.account_id)
DeliveryEmojiReactionWorker.perform_async(render_emoji_reaction(emoji_group), emoji_reaction.id, emoji_reaction.account_id)
end

def bump_potential_friendship(account, status)
Expand Down
2 changes: 1 addition & 1 deletion app/services/un_emoji_react_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def write_stream(emoji_reaction)
emoji_group = { 'name' => emoji_reaction.name, 'count' => 0, 'account_ids' => [], 'status_id' => @status.id.to_s }
emoji_group['domain'] = emoji_reaction.custom_emoji.domain if emoji_reaction.custom_emoji
end
DeliveryEmojiReactionWorker.perform_async(render_emoji_reaction(emoji_group), @status.id, emoji_reaction.account_id)
DeliveryEmojiReactionWorker.perform_async(render_emoji_reaction(emoji_group), emoji_reaction.id, emoji_reaction.account_id)
end

def build_json(emoji_reaction)
Expand Down
6 changes: 3 additions & 3 deletions app/validators/emoji_reaction_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ def deny_emoji_reactions?(emoji_reaction)
end

def deny_from_all?(emoji_reaction)
emoji_reaction.status.account.user.settings['emoji_reactions.deny_from_all']
emoji_reaction.status.account.emoji_reactions_deny_from_all?
end

def non_following?(emoji_reaction)
emoji_reaction.status.account.user.settings['emoji_reactions.must_be_following'] && !emoji_reaction.status.account.following?(emoji_reaction.account)
emoji_reaction.status.account.emoji_reactions_must_following? && !emoji_reaction.status.account.following?(emoji_reaction.account)
end

def non_follower?(emoji_reaction)
emoji_reaction.status.account.user.settings['emoji_reactions.must_be_follower'] && !emoji_reaction.account.following?(emoji_reaction.status.account)
emoji_reaction.status.account.emoji_reactions_must_follower? && !emoji_reaction.account.following?(emoji_reaction.status.account)
end
end
12 changes: 9 additions & 3 deletions app/workers/delivery_emoji_reaction_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ class DeliveryEmojiReactionWorker
include Lockable
include AccountScope

def perform(payload_json, status_id, _my_account_id = nil)
status = Status.find(status_id.to_i)
def perform(payload_json, emoji_reaction_id, _status_id, _my_account_id = nil)
emoji_reaction = EmojiReaction.find(emoji_reaction_id)
status = emoji_reaction&.status

if status.present?
return if status.account.excluded_from_timeline_domains.include?(emoji_reaction.account.domain)

scope_status(status).includes(:user).find_each do |account|
redis.publish("timeline:#{account.id}", payload_json) if (account.user.nil? || (!account.user&.setting_stop_emoji_reaction_streaming && !account.user&.setting_enable_emoji_reaction)) && redis.exists?("subscribed:timeline:#{account.id}")
next unless (account.user.nil? || (!account.user&.setting_stop_emoji_reaction_streaming && !account.user&.setting_enable_emoji_reaction)) && redis.exists?("subscribed:timeline:#{account.id}")
next if account.excluded_from_timeline_domains.include?(emoji_reaction.account.domain)

redis.publish("timeline:#{account.id}", payload_json)
end
end

Expand Down

0 comments on commit 477d427

Please sign in to comment.