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[: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[: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[:filtered] = mapped_applied_custom_filter(account_id, @status)
payload[:reactions] = serialized_reactions(account_id)
if payload[:poll] if payload[:poll]
payload[:poll][:voted] = @status.account_id == account_id 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][: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][: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][:filtered] = payload[:filtered]
payload[:reblog][:reactions] = serialized_reactions(account_id)
if payload[:reblog][:poll] if payload[:reblog][:poll]
if @status.reblog.account_id == account_id if @status.reblog.account_id == account_id
@ -71,6 +73,7 @@ class StatusCacheHydrator
payload[:favourited] = payload[:reblog][:favourited] payload[:favourited] = payload[:reblog][:favourited]
payload[:reblogged] = payload[:reblog][:reblogged] payload[:reblogged] = payload[:reblog][:reblogged]
payload[:reactions] = payload[:reblog][:reactions]
end end
end end
@ -87,6 +90,16 @@ class StatusCacheHydrator
).as_json ).as_json
end 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 def payload_application
@status.application.present? ? serialized_status_application_json : nil @status.application.present? ? serialized_status_application_json : nil
end end

View file

@ -283,10 +283,10 @@ class Status < ApplicationRecord
@emojis = CustomEmoji.from_text(fields.join(' '), account.domain) @emojis = CustomEmoji.from_text(fields.join(' '), account.domain)
end end
def reactions(account = nil) def reactions(account_id = nil)
grouped_ordered_status_reactions.select( grouped_ordered_status_reactions.select(
[:name, :custom_emoji_id, 'COUNT(*) as count'].tap do |values| [: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 end
).to_a.tap do |records| ).to_a.tap do |records|
ActiveRecord::Associations::Preloader.new(records: records, associations: :custom_emoji).call ActiveRecord::Associations::Preloader.new(records: records, associations: :custom_emoji).call
@ -488,15 +488,15 @@ class Status < ApplicationRecord
) )
end end
def value_for_reaction_me_column(account) def value_for_reaction_me_column(account_id)
if account.nil? if account_id.nil?
'FALSE AS me' 'FALSE AS me'
else else
<<~SQL.squish <<~SQL.squish
EXISTS( EXISTS(
SELECT 1 SELECT 1
FROM status_reactions inner_reactions 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.status_id = status_reactions.status_id
AND inner_reactions.name = status_reactions.name AND inner_reactions.name = status_reactions.name
AND ( AND (

View file

@ -158,7 +158,7 @@ class REST::StatusSerializer < ActiveModel::Serializer
end end
def reactions def reactions
object.reactions(current_user&.account) object.reactions(current_user&.account&.id)
end end
private private