From 4cbaf4695000b86c46dab3181ad91823f527230f Mon Sep 17 00:00:00 2001 From: Essem Date: Fri, 9 Feb 2024 17:42:19 -0600 Subject: [PATCH] Hydrate reactions on streaming API --- app/lib/status_cache_hydrator.rb | 13 +++++++++++++ app/models/status.rb | 10 +++++----- app/serializers/rest/status_serializer.rb | 2 +- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/app/lib/status_cache_hydrator.rb b/app/lib/status_cache_hydrator.rb index 34f6199ec0..e3c9e49847 100644 --- a/app/lib/status_cache_hydrator.rb +++ b/app/lib/status_cache_hydrator.rb @@ -32,6 +32,7 @@ class StatusCacheHydrator payload[:bookmarked] = Bookmark.exists?(account_id: account_id, status_id: @status.id) payload[:pinned] = StatusPin.exists?(account_id: account_id, status_id: @status.id) if @status.account_id == account_id payload[:filtered] = mapped_applied_custom_filter(account_id, @status) + payload[:reactions] = serialized_reactions(account_id) if payload[:poll] payload[:poll][:voted] = @status.account_id == account_id @@ -57,6 +58,7 @@ class StatusCacheHydrator payload[:reblog][:bookmarked] = Bookmark.exists?(account_id: account_id, status_id: @status.reblog_of_id) payload[:reblog][:pinned] = StatusPin.exists?(account_id: account_id, status_id: @status.reblog_of_id) if @status.reblog.account_id == account_id payload[:reblog][:filtered] = payload[:filtered] + payload[:reblog][:reactions] = serialized_reactions(account_id) if payload[:reblog][:poll] if @status.reblog.account_id == account_id @@ -71,6 +73,7 @@ class StatusCacheHydrator payload[:favourited] = payload[:reblog][:favourited] payload[:reblogged] = payload[:reblog][:reblogged] + payload[:reactions] = payload[:reblog][:reactions] end end @@ -87,6 +90,16 @@ class StatusCacheHydrator ).as_json end + def serialized_reactions(account_id) + reactions = @status.reactions(account_id) + ActiveModelSerializers::SerializableResource.new( + reactions, + each_serializer: REST::ReactionSerializer, + scope: account_id, # terrible + scope_name: :current_user + ).as_json + end + def payload_application @status.application.present? ? serialized_status_application_json : nil end diff --git a/app/models/status.rb b/app/models/status.rb index 33998a2b87..bff401e4e2 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -283,10 +283,10 @@ class Status < ApplicationRecord @emojis = CustomEmoji.from_text(fields.join(' '), account.domain) end - def reactions(account = nil) + def reactions(account_id = nil) grouped_ordered_status_reactions.select( [:name, :custom_emoji_id, 'COUNT(*) as count'].tap do |values| - values << value_for_reaction_me_column(account) + values << value_for_reaction_me_column(account_id) end ).to_a.tap do |records| ActiveRecord::Associations::Preloader.new(records: records, associations: :custom_emoji).call @@ -488,15 +488,15 @@ class Status < ApplicationRecord ) end - def value_for_reaction_me_column(account) - if account.nil? + def value_for_reaction_me_column(account_id) + if account_id.nil? 'FALSE AS me' else <<~SQL.squish EXISTS( SELECT 1 FROM status_reactions inner_reactions - WHERE inner_reactions.account_id = #{account.id} + WHERE inner_reactions.account_id = #{account_id} AND inner_reactions.status_id = status_reactions.status_id AND inner_reactions.name = status_reactions.name AND ( diff --git a/app/serializers/rest/status_serializer.rb b/app/serializers/rest/status_serializer.rb index 40e6e7a2e8..ae10dd8268 100644 --- a/app/serializers/rest/status_serializer.rb +++ b/app/serializers/rest/status_serializer.rb @@ -158,7 +158,7 @@ class REST::StatusSerializer < ActiveModel::Serializer end def reactions - object.reactions(current_user&.account) + object.reactions(current_user&.account&.id) end private