Skip to content

Commit

Permalink
fix[Op#40437]: ensure emojis and user reactions are ordered in asc order
Browse files Browse the repository at this point in the history
  • Loading branch information
akabiru committed Oct 25, 2024
1 parent 30b831f commit e6aed59
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
18 changes: 15 additions & 3 deletions app/models/concerns/reactable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,30 @@ def grouped_emoji_reactions_by_reactable(reactable_id:, reactable_type:, last_up

def grouped_emoji_reactions(reactable_id:, reactable_type:, last_updated_at: nil)
query = EmojiReaction
.select("emoji_reactions.reactable_id, emoji_reactions.reaction, COUNT(emoji_reactions.id) as count, " \
"json_agg(json_build_array(users.id, #{user_name_concat_format_sql}) ORDER BY emoji_reactions.created_at) as user_details") # rubocop:disable Layout/LineLength
.select(emoji_reactions_group_selection_sql)
.joins(:user)
.where(reactable_id:, reactable_type:)

query = query.where("emoji_reactions.updated_at > ?", last_updated_at) if last_updated_at

query.group("emoji_reactions.reactable_id, emoji_reactions.reaction")
query
.group("emoji_reactions.reactable_id, emoji_reactions.reaction")
.order("first_created_at ASC")
end

private

def emoji_reactions_group_selection_sql
<<~SQL.squish
emoji_reactions.reactable_id, emoji_reactions.reaction, COUNT(emoji_reactions.id) as count,
json_agg(
json_build_array(users.id, #{user_name_concat_format_sql})
ORDER BY emoji_reactions.created_at
) as user_details,
MIN(emoji_reactions.created_at) as first_created_at
SQL
end

def user_name_concat_format_sql
case Setting.user_format
when :firstname_lastname
Expand Down
32 changes: 30 additions & 2 deletions spec/models/concerns/reactable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,42 @@
)
end
end

context "with multiple reactions from different users at different times" do
let(:user3) { create(:user) }

before do
create(:emoji_reaction, reactable: wp_journal1, user: user3, reaction: :thumbs_up, created_at: 1.day.ago)
create(:emoji_reaction, reactable: wp_journal1, user: user3, reaction: :thumbs_down, created_at: 2.days.ago)
end

it "groups emoji reactions and users in ascending order" do
result = Journal.grouped_journal_emoji_reactions(wp_journal1)

expect(result).to eq(
wp_journal1.id => {
thumbs_down: {
count: 1,
users: [{ id: user3.id, name: user3.name }]
},
thumbs_up: {
count: 3,
users: [
{ id: user3.id, name: user3.name },
{ id: user1.id, name: user1.name },
{ id: user2.id, name: user2.name }
]
}
}
)
end
end
end

describe ".grouped_emoji_reactions" do
it "returns grouped emoji reactions" do
result = Journal.grouped_emoji_reactions(reactable_id: work_package.journal_ids, reactable_type: "Journal")

expect(result.size).to eq({ "thumbs_up" => 2, "thumbs_down" => 1 })

expect(result[0].reaction).to eq("thumbs_up")
expect(result[0].count).to eq(2)
expect(result[0].user_details).to eq([[user1.id, user1.name], [user2.id, user2.name]])
Expand Down

0 comments on commit e6aed59

Please sign in to comment.