Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

fix[Op#40437]: ensure emojis and user reactions are ordered in asc order

Kabiru Mwenjae6aed59bbe39
diff --git a/app/models/concerns/reactable.rb b/app/models/concerns/reactable.rb
index 855634d9cfe..73de0b65b92 100644
--- a/app/models/concerns/reactable.rb
+++ b/app/models/concerns/reactable.rb
@@ -54,18 +54,30 @@ module Reactable
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
diff --git a/spec/models/concerns/reactable_spec.rb b/spec/models/concerns/reactable_spec.rb
index 7f47ff64eaa..b8f11724c8b 100644
--- a/spec/models/concerns/reactable_spec.rb
+++ b/spec/models/concerns/reactable_spec.rb
@@ -114,14 +114,42 @@ RSpec.describe Reactable do
)
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]])