Hydrate reactions on streaming API

This commit is contained in:
Essem 2024-02-09 17:42:19 -06:00
parent b51d0750c6
commit 170b07d56b
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
3 changed files with 19 additions and 6 deletions

View file

@ -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

View file

@ -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 (

View file

@ -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