Skip to content

Commit

Permalink
Hydrate reactions on streaming API
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEssem authored and jordemort committed Sep 3, 2024
1 parent 8412231 commit a48082c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
13 changes: 13 additions & 0 deletions app/lib/status_cache_hydrator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def hydrate_non_reblog_payload(empty_payload, account_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[: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
Expand All @@ -57,6 +58,7 @@ def hydrate_reblog_payload(empty_payload, account_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][:filtered] = payload[:filtered]
payload[:reblog][:reactions] = serialized_reactions(account_id)

if payload[:reblog][:poll]
if @status.reblog.account_id == account_id
Expand All @@ -71,6 +73,7 @@ def hydrate_reblog_payload(empty_payload, account_id)

payload[:favourited] = payload[:reblog][:favourited]
payload[:reblogged] = payload[:reblog][:reblogged]
payload[:reactions] = payload[:reblog][:reactions]
end
end

Expand All @@ -87,6 +90,16 @@ def serialized_filter(filter)
).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
Expand Down
10 changes: 5 additions & 5 deletions app/models/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,10 @@ def emojis
@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
Expand Down Expand Up @@ -461,15 +461,15 @@ def grouped_ordered_status_reactions
)
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 (
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/rest/status_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def ordered_mentions
end

def reactions
object.reactions(current_user&.account)
object.reactions(current_user&.account&.id)
end

private
Expand Down

0 comments on commit a48082c

Please sign in to comment.