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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Restructure emoji reactions queries to be independent of the Reactable assoc mixin
Kabiru Mwenja98ce241beb07
diff --git a/app/components/work_packages/activities_tab/journals/index_component.rb b/app/components/work_packages/activities_tab/journals/index_component.rbindex 3f93b0dab9d..04fc8edb5d8 100644--- a/app/components/work_packages/activities_tab/journals/index_component.rb+++ b/app/components/work_packages/activities_tab/journals/index_component.rb@@ -125,7 +125,7 @@ module WorkPackagesdef wp_journals_grouped_emoji_reactions@wp_journals_grouped_emoji_reactions ||=- Journal.grouped_work_package_journals_emoji_reactions_by_reactable(work_package)+ EmojiReactions::GroupedQueries.grouped_work_package_journals_emoji_reactions_by_reactable(work_package)enddef empty_state?diff --git a/app/controllers/work_packages/activities_tab_controller.rb b/app/controllers/work_packages/activities_tab_controller.rbindex 061397ee554..5d0fba52794 100644--- a/app/controllers/work_packages/activities_tab_controller.rb+++ b/app/controllers/work_packages/activities_tab_controller.rb@@ -337,7 +337,7 @@ class WorkPackages::ActivitiesTabController < ApplicationControllerjournals = journals.where.not(notes: "")end- grouped_emoji_reactions = Journal.grouped_emoji_reactions_by_reactable(+ grouped_emoji_reactions = EmojiReactions::GroupedQueries.grouped_emoji_reactions_by_reactable(reactable_id: journals.pluck(:id), reactable_type: "Journal")@@ -352,7 +352,8 @@ class WorkPackages::ActivitiesTabController < ApplicationControllerenddef generate_work_package_journals_emoji_reactions_update_streams- wp_journal_emoji_reactions = Journal.grouped_work_package_journals_emoji_reactions_by_reactable(@work_package)+ wp_journal_emoji_reactions =+ EmojiReactions::GroupedQueries.grouped_work_package_journals_emoji_reactions_by_reactable(@work_package)@work_package.journals.each do |journal|update_via_turbo_stream(component: WorkPackages::ActivitiesTab::Journals::ItemComponent::Reactions.new(@@ -467,7 +468,9 @@ class WorkPackages::ActivitiesTabController < ApplicationControllerenddef grouped_emoji_reactions_for_journal- Journal.grouped_journal_emoji_reactions_by_reactable(@journal).fetch(@journal.id, {})+ EmojiReactions::GroupedQueries+ .grouped_emoji_reactions_by_reactable(reactable: @journal)+ .fetch(@journal.id, {})enddef allowed_to_edit?(journal)diff --git a/app/models/concerns/emoji_reactions/grouped_queries.rb b/app/models/concerns/emoji_reactions/grouped_queries.rbnew file mode 100644index 00000000000..c7fde0e83c9--- /dev/null+++ b/app/models/concerns/emoji_reactions/grouped_queries.rb@@ -0,0 +1,113 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++module EmojiReactions+ module GroupedQueries+ class << self+ def grouped_work_package_journals_emoji_reactions(work_package)+ grouped_emoji_reactions(reactable_id: journal_ids_for(work_package), reactable_type: "Journal")+ end++ def grouped_work_package_journals_emoji_reactions_by_reactable(work_package)+ grouped_emoji_reactions_by_reactable(reactable_id: journal_ids_for(work_package), reactable_type: "Journal")+ end++ def grouped_emoji_reactions_by_reactable(**)+ grouped_emoji_reactions(**)+ .each_with_object(Hash.new { |h, k| h[k] = {} }) do |row, hash|+ hash[row.reactable_id][row.reaction.to_sym] = {+ count: row.reactions_count,+ users: row.reacting_users.map { |(id, name)| { id:, name: } }+ }+ end+ end++ def grouped_emoji_reactions(**)+ reactable_id, reactable_type = extract_reactable_id_and_type(**)++ EmojiReaction+ .select(emoji_reactions_group_selection_sql)+ .joins(:user)+ .includes(:reactable)+ .where(reactable_id:, reactable_type:)+ .group("emoji_reactions.reactable_type, 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.reactable_type, emoji_reactions.reaction,+ COUNT(emoji_reactions.id) as reactions_count,+ json_agg(+ json_build_array(users.id, #{user_name_concat_format_sql})+ ORDER BY emoji_reactions.created_at+ ) as reacting_users,+ MIN(emoji_reactions.created_at) as first_created_at+ SQL+ end++ def user_name_concat_format_sql+ case Setting.user_format+ when :firstname_lastname+ "concat_ws(' ', users.firstname, users.lastname)"+ when :firstname+ "users.firstname"+ when :lastname_firstname+ "concat_ws(' ', users.lastname, users.firstname)"+ when :lastname_comma_firstname+ "concat_ws(', ', users.lastname, users.firstname)"+ when :lastname_n_firstname+ "concat_ws('', users.lastname, users.firstname)"+ when :username+ "users.login"+ else+ raise ArgumentError, "Unsupported user format: #{Setting.user_format}"+ end+ end++ def extract_reactable_id_and_type(**kwargs)+ case kwargs+ in { reactable: reactable }+ [reactable.id, reactable.class.name]+ in { reactable_id: id, reactable_type: type }+ [id, type]+ else+ raise ArgumentError, "Must specify either reactable or both reactable_id and reactable_type"+ end+ end++ def journal_ids_for(reactable)+ reactable.journals.internal_visible.select(:id).pluck(:id)+ end+ end+ end+enddiff --git a/app/models/concerns/reactable.rb b/app/models/concerns/reactable.rbindex 5c292fbec2d..687480dcb8e 100644--- a/app/models/concerns/reactable.rb+++ b/app/models/concerns/reactable.rb@@ -34,75 +34,4 @@ module Reactableincluded dohas_many :emoji_reactions, as: :reactable, dependent: :destroyend-- class_methods do- def grouped_journal_emoji_reactions_by_reactable(journal)- grouped_emoji_reactions_by_reactable(reactable_id: journal.id, reactable_type: "Journal")- end-- def grouped_work_package_journals_emoji_reactions(work_package)- grouped_emoji_reactions(reactable_id: journal_ids_for(work_package), reactable_type: "Journal")- end-- def grouped_work_package_journals_emoji_reactions_by_reactable(work_package)- grouped_emoji_reactions_by_reactable(reactable_id: journal_ids_for(work_package), reactable_type: "Journal")- end-- def grouped_emoji_reactions_by_reactable(reactable_id:, reactable_type:)- grouped_emoji_reactions(reactable_id:, reactable_type:).each_with_object({}) do |row, hash|- hash[row.reactable_id] ||= {}- hash[row.reactable_id][row.reaction.to_sym] = {- count: row.reactions_count,- users: row.reacting_users.map { |(id, name)| { id:, name: } }- }- end- end-- def grouped_emoji_reactions(reactable_id:, reactable_type:)- EmojiReaction- .select(emoji_reactions_group_selection_sql)- .joins(:user)- .includes(:reactable)- .where(reactable_id:, reactable_type:)- .group("emoji_reactions.reactable_type, 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.reactable_type, emoji_reactions.reaction,- COUNT(emoji_reactions.id) as reactions_count,- json_agg(- json_build_array(users.id, #{user_name_concat_format_sql})- ORDER BY emoji_reactions.created_at- ) as reacting_users,- MIN(emoji_reactions.created_at) as first_created_at- SQL- end-- def user_name_concat_format_sql- case Setting.user_format- when :firstname_lastname- "concat_ws(' ', users.firstname, users.lastname)"- when :firstname- "users.firstname"- when :lastname_firstname- "concat_ws(' ', users.lastname, users.firstname)"- when :lastname_comma_firstname- "concat_ws(', ', users.lastname, users.firstname)"- when :lastname_n_firstname- "concat_ws('', users.lastname, users.firstname)"- when :username- "users.login"- else- raise ArgumentError, "Unsupported user format: #{Setting.user_format}"- end- end-- def journal_ids_for(reactable)- reactable.journals.internal_visible.select(:id).pluck(:id)- end- endenddiff --git a/lib/api/v3/activities/activity_representer.rb b/lib/api/v3/activities/activity_representer.rbindex d4553b2e271..09ce7b941da 100644--- a/lib/api/v3/activities/activity_representer.rb+++ b/lib/api/v3/activities/activity_representer.rb@@ -119,7 +119,7 @@ module APIdef emoji_reactionsreturn unless represented.journable.is_a?(WorkPackage)- emoji_reactions = Journal.grouped_emoji_reactions(reactable_id: represented.id, reactable_type: "Journal")+ emoji_reactions = ::EmojiReactions::GroupedQueries.grouped_emoji_reactions(reactable: represented)API::V3::EmojiReactions::EmojiReactionCollectionRepresenter.new(emoji_reactions,self_link: api_v3_paths.emoji_reactions_by_activity_comment(represented.id),diff --git a/lib/api/v3/emoji_reactions/emoji_reactions_by_activity_comment_api.rb b/lib/api/v3/emoji_reactions/emoji_reactions_by_activity_comment_api.rbindex d66f8c39a4d..0e66ee9f732 100644--- a/lib/api/v3/emoji_reactions/emoji_reactions_by_activity_comment_api.rb+++ b/lib/api/v3/emoji_reactions/emoji_reactions_by_activity_comment_api.rb@@ -42,14 +42,17 @@ module APIapi_v3_paths.emoji_reactions_by_activity_comment(reactable.id)end+ def grouped_emoji_reactions+ ::EmojiReactions::GroupedQueries.grouped_emoji_reactions(reactable:)+ end+def activity_comment?reactable.notes.present?endendget do- emoji_reactions = Journal.grouped_emoji_reactions(reactable_id: reactable.id, reactable_type: "Journal")- EmojiReactionCollectionRepresenter.new(emoji_reactions,+ EmojiReactionCollectionRepresenter.new(grouped_emoji_reactions,self_link: get_emoji_reactions_self_path,current_user: User.current)end@@ -73,8 +76,7 @@ module API)if toggle_service.success?- emoji_reactions = Journal.grouped_emoji_reactions(reactable_id: reactable.id, reactable_type: "Journal")- EmojiReactionCollectionRepresenter.new(emoji_reactions,+ EmojiReactionCollectionRepresenter.new(grouped_emoji_reactions,self_link: get_emoji_reactions_self_path,current_user:)elsediff --git a/lib/api/v3/emoji_reactions/emoji_reactions_by_work_package_comments_api.rb b/lib/api/v3/emoji_reactions/emoji_reactions_by_work_package_comments_api.rbindex eddf5045ca5..34f186ed556 100644--- a/lib/api/v3/emoji_reactions/emoji_reactions_by_work_package_comments_api.rb+++ b/lib/api/v3/emoji_reactions/emoji_reactions_by_work_package_comments_api.rb@@ -34,17 +34,13 @@ module APIclass EmojiReactionsByWorkPackageCommentsAPI < ::API::OpenProjectAPIresource :activities_emoji_reactions dohelpers do- def reactable- @work_package- end-def get_emoji_reactions_self_path- api_v3_paths.emoji_reactions_by_work_package_comments(reactable.id)+ api_v3_paths.emoji_reactions_by_work_package_comments(@work_package.id)endendget do- emoji_reactions = Journal.grouped_work_package_journals_emoji_reactions(reactable)+ emoji_reactions = ::EmojiReactions::GroupedQueries.grouped_work_package_journals_emoji_reactions(@work_package)EmojiReactionCollectionRepresenter.new(emoji_reactions,self_link: get_emoji_reactions_self_path,current_user: User.current)diff --git a/spec/models/concerns/emoji_reactions/grouped_queries_spec.rb b/spec/models/concerns/emoji_reactions/grouped_queries_spec.rbnew file mode 100644index 00000000000..5464d5aa1ec--- /dev/null+++ b/spec/models/concerns/emoji_reactions/grouped_queries_spec.rb@@ -0,0 +1,284 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++require "spec_helper"++RSpec.describe EmojiReactions::GroupedQueries do+ shared_let(:project) { create(:project, enabled_internal_comments: true) }+ shared_let(:work_package) { create(:work_package, project:) }++ shared_let(:wp_journal1) { add_journal }+ shared_let(:wp_journal2) { add_journal }+ shared_let(:wp_internal_journal) { add_journal(internal: true) }++ let(:user1) { create(:user) }+ let(:user2) { create(:user) }++ let(:thumbs_up_reactions) do+ [user1, user2].each do |user|+ create(:emoji_reaction, reactable: wp_journal1, user: user, reaction: :thumbs_up)+ create(:emoji_reaction, reactable: wp_internal_journal, user: user, reaction: :thumbs_up)+ end+ end++ let(:thumbs_down_reactions) { create(:emoji_reaction, reactable: wp_journal2, user: user2, reaction: :thumbs_down) }++ before do+ thumbs_up_reactions+ thumbs_down_reactions+ end++ describe ".grouped_work_package_journals_emoji_reactions" do+ it "returns grouped emoji reactions for work package journals" do+ result = described_class.grouped_work_package_journals_emoji_reactions(work_package)++ expect(result[0].reaction).to eq("thumbs_up")+ expect(result[0].reactions_count).to eq(2)+ expect(result[0].reacting_users).to eq([[user1.id, user1.name], [user2.id, user2.name]])++ expect(result[1].reaction).to eq("thumbs_down")+ expect(result[1].reactions_count).to eq(1)+ expect(result[1].reacting_users).to eq([[user2.id, user2.name]])+ end++ context "when the current user is allowed to view internal comments" do+ let(:current_user) do+ create(:user, member_with_permissions: { work_package.project => %i[view_work_packages view_internal_comments] })+ end++ before do+ allow(User).to receive(:current).and_return(current_user)+ end++ it "returns grouped emoji reactions for work package journals" do+ result = described_class.grouped_work_package_journals_emoji_reactions(work_package)++ result[0..1].each do |r|+ expect(r.reaction).to eq("thumbs_up")+ expect(r.reactions_count).to eq(2)+ expect(r.reacting_users).to eq([[user1.id, user1.name], [user2.id, user2.name]])+ end++ expect(result[2].reaction).to eq("thumbs_down")+ expect(result[2].reactions_count).to eq(1)+ expect(result[2].reacting_users).to eq([[user2.id, user2.name]])+ end+ end++ context "when no reactions exist" do+ it "returns an empty hash" do+ work_package = build_stubbed(:work_package)+ result = described_class.grouped_work_package_journals_emoji_reactions(work_package)++ expect(result).to eq([])+ end+ end+ end++ describe ".grouped_work_package_journals_emoji_reactions_by_reactable" do+ it "returns grouped emoji reactions for work package journals" do+ result = described_class.grouped_work_package_journals_emoji_reactions_by_reactable(work_package)++ expect(result.size).to eq(2)++ expect(result).to eq(+ wp_journal1.id => {+ thumbs_up: {+ count: 2,+ users: [{ id: user1.id, name: user1.name }, { id: user2.id, name: user2.name }]+ }+ },+ wp_journal2.id => {+ thumbs_down: {+ count: 1,+ users: [{ id: user2.id, name: user2.name }]+ }+ }+ )+ end++ context "when the current user is allowed to view internal comments" do+ let(:current_user) do+ create(:user, member_with_permissions: { work_package.project => %i[view_work_packages view_internal_comments] })+ end++ before do+ allow(User).to receive(:current).and_return(current_user)+ end++ it "returns grouped emoji reactions for work package journals" do+ result = described_class.grouped_work_package_journals_emoji_reactions_by_reactable(work_package)++ expect(result.size).to eq(3)++ expect(result).to eq(+ wp_journal1.id => {+ thumbs_up: {+ count: 2,+ users: [{ id: user1.id, name: user1.name }, { id: user2.id, name: user2.name }]+ }+ },+ wp_journal2.id => {+ thumbs_down: {+ count: 1,+ users: [{ id: user2.id, name: user2.name }]+ }+ },+ wp_internal_journal.id => {+ thumbs_up: {+ count: 2,+ users: [{ id: user1.id, name: user1.name }, { id: user2.id, name: user2.name }]+ }+ }+ )+ end+ end++ context "when no reactions exist" do+ it "returns an empty hash" do+ work_package = build_stubbed(:work_package)+ result = described_class.grouped_work_package_journals_emoji_reactions_by_reactable(work_package)++ expect(result).to eq({})+ end+ end+ end++ describe ".grouped_journal_emoji_reactions_by_reactable" do+ context "with a single reactable" do+ it "returns grouped emoji reactions for that journal" do+ result = described_class.grouped_emoji_reactions_by_reactable(reactable: wp_journal1)++ expect(result).to eq(+ wp_journal1.id => {+ thumbs_up: {+ count: 2,+ users: [{ id: user1.id, name: user1.name }, { id: user2.id, name: user2.name }]+ }+ }+ )+ 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 = described_class.grouped_emoji_reactions_by_reactable(reactable: 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 = described_class.grouped_emoji_reactions(reactable_id: work_package.journal_ids, reactable_type: "Journal")++ result[0..1].each do |r|+ expect(r.reaction).to eq("thumbs_up")+ expect(r.reactions_count).to eq(2)+ expect(r.reacting_users).to eq([[user1.id, user1.name], [user2.id, user2.name]])+ end++ expect(result[2].reaction).to eq("thumbs_down")+ expect(result[2].reactions_count).to eq(1)+ expect(result[2].reacting_users).to eq([[user2.id, user2.name]])+ end++ context "when user format is set to :username", with_settings: { user_format: :username } do+ it "returns grouped emoji reactions with usernames" do+ result = described_class.grouped_emoji_reactions(reactable_id: work_package.journal_ids, reactable_type: "Journal")++ expect(result[0].reacting_users).to eq([[user1.id, user1.login], [user2.id, user2.login]])+ end+ end++ context "when user format is set to :firstname", with_settings: { user_format: :firstname } do+ it "returns grouped emoji reactions with first and last names" do+ result = described_class.grouped_emoji_reactions(reactable: wp_journal2)++ expect(result[0].reacting_users).to eq([[user2.id, user2.firstname]])+ end+ end++ context "when user format is set to :lastname_comma_firstname", with_settings: { user_format: :lastname_comma_firstname } do+ it "returns grouped emoji reactions with last coma firstname" do+ result = described_class.grouped_emoji_reactions(reactable: wp_journal1)++ expect(result[0].reacting_users).to eq(+ [+ [user1.id, "#{user1.lastname}, #{user1.firstname}"],+ [user2.id, "#{user2.lastname}, #{user2.firstname}"]+ ]+ )+ end+ end++ context "when user format is set to :lastname_n_firstname", with_settings: { user_format: :lastname_n_firstname } do+ it "returns grouped emoji reactions with last firstname" do+ result = described_class.grouped_emoji_reactions(reactable: wp_journal1)++ expect(result[0].reacting_users).to eq(+ [+ [user1.id, "#{user1.lastname}#{user1.firstname}"],+ [user2.id, "#{user2.lastname}#{user2.firstname}"]+ ]+ )+ end+ end+ end++ def add_journal(notes: "This is a test note", internal: false)+ work_package.add_journal(user: work_package.author, notes:, internal:)+ work_package.save(validate: false)+ work_package.journals.last+ end+enddiff --git a/spec/models/concerns/reactable_spec.rb b/spec/models/concerns/reactable_spec.rbindex 2286dc93f31..90844e7f78c 100644--- a/spec/models/concerns/reactable_spec.rb+++ b/spec/models/concerns/reactable_spec.rb@@ -31,258 +31,9 @@require "spec_helper"RSpec.describe Reactable do- shared_let(:project) { create(:project, enabled_internal_comments: true) }- shared_let(:work_package) { create(:work_package, project:) }-- shared_let(:wp_journal1) { add_journal }- shared_let(:wp_journal2) { add_journal }- shared_let(:wp_internal_journal) { add_journal(internal: true) }-- let(:user1) { create(:user) }- let(:user2) { create(:user) }-- let(:thumbs_up_reactions) do- [user1, user2].each do |user|- create(:emoji_reaction, reactable: wp_journal1, user: user, reaction: :thumbs_up)- create(:emoji_reaction, reactable: wp_internal_journal, user: user, reaction: :thumbs_up)- end- end-- let(:thumbs_down_reactions) { create(:emoji_reaction, reactable: wp_journal2, user: user2, reaction: :thumbs_down) }-- before do- thumbs_up_reactions- thumbs_down_reactions- end+ shared_let(:wp_journal) { create(:work_package_journal) }describe "Associations" do- it { expect(wp_journal1).to have_many(:emoji_reactions) }- end-- describe ".grouped_work_package_journals_emoji_reactions" do- it "returns grouped emoji reactions for work package journals" do- result = Journal.grouped_work_package_journals_emoji_reactions(work_package)-- expect(result[0].reaction).to eq("thumbs_up")- expect(result[0].reactions_count).to eq(2)- expect(result[0].reacting_users).to eq([[user1.id, user1.name], [user2.id, user2.name]])-- expect(result[1].reaction).to eq("thumbs_down")- expect(result[1].reactions_count).to eq(1)- expect(result[1].reacting_users).to eq([[user2.id, user2.name]])- end-- context "when the current user is allowed to view internal comments" do- let(:current_user) do- create(:user, member_with_permissions: { work_package.project => %i[view_work_packages view_internal_comments] })- end-- before do- allow(User).to receive(:current).and_return(current_user)- end-- it "returns grouped emoji reactions for work package journals" do- result = Journal.grouped_work_package_journals_emoji_reactions(work_package)-- result[0..1].each do |r|- expect(r.reaction).to eq("thumbs_up")- expect(r.reactions_count).to eq(2)- expect(r.reacting_users).to eq([[user1.id, user1.name], [user2.id, user2.name]])- end-- expect(result[2].reaction).to eq("thumbs_down")- expect(result[2].reactions_count).to eq(1)- expect(result[2].reacting_users).to eq([[user2.id, user2.name]])- end- end-- context "when no reactions exist" do- it "returns an empty hash" do- work_package = build_stubbed(:work_package)- result = Journal.grouped_work_package_journals_emoji_reactions(work_package)-- expect(result).to eq([])- end- end- end-- describe ".grouped_work_package_journals_emoji_reactions_by_reactable" do- it "returns grouped emoji reactions for work package journals" do- result = Journal.grouped_work_package_journals_emoji_reactions_by_reactable(work_package)-- expect(result.size).to eq(2)-- expect(result).to eq(- wp_journal1.id => {- thumbs_up: {- count: 2,- users: [{ id: user1.id, name: user1.name }, { id: user2.id, name: user2.name }]- }- },- wp_journal2.id => {- thumbs_down: {- count: 1,- users: [{ id: user2.id, name: user2.name }]- }- }- )- end-- context "when the current user is allowed to view internal comments" do- let(:current_user) do- create(:user, member_with_permissions: { work_package.project => %i[view_work_packages view_internal_comments] })- end-- before do- allow(User).to receive(:current).and_return(current_user)- end-- it "returns grouped emoji reactions for work package journals" do- result = Journal.grouped_work_package_journals_emoji_reactions_by_reactable(work_package)-- expect(result.size).to eq(3)-- expect(result).to eq(- wp_journal1.id => {- thumbs_up: {- count: 2,- users: [{ id: user1.id, name: user1.name }, { id: user2.id, name: user2.name }]- }- },- wp_journal2.id => {- thumbs_down: {- count: 1,- users: [{ id: user2.id, name: user2.name }]- }- },- wp_internal_journal.id => {- thumbs_up: {- count: 2,- users: [{ id: user1.id, name: user1.name }, { id: user2.id, name: user2.name }]- }- }- )- end- end-- context "when no reactions exist" do- it "returns an empty hash" do- work_package = build_stubbed(:work_package)- result = Journal.grouped_work_package_journals_emoji_reactions_by_reactable(work_package)-- expect(result).to eq({})- end- end- end-- describe ".grouped_journal_emoji_reactions_by_reactable" do- context "with a single reactable" do- it "returns grouped emoji reactions for that journal" do- result = Journal.grouped_journal_emoji_reactions_by_reactable(wp_journal1)-- expect(result).to eq(- wp_journal1.id => {- thumbs_up: {- count: 2,- users: [{ id: user1.id, name: user1.name }, { id: user2.id, name: user2.name }]- }- }- )- 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_by_reactable(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")-- result[0..1].each do |r|- expect(r.reaction).to eq("thumbs_up")- expect(r.reactions_count).to eq(2)- expect(r.reacting_users).to eq([[user1.id, user1.name], [user2.id, user2.name]])- end-- expect(result[2].reaction).to eq("thumbs_down")- expect(result[2].reactions_count).to eq(1)- expect(result[2].reacting_users).to eq([[user2.id, user2.name]])- end-- context "when user format is set to :username", with_settings: { user_format: :username } do- it "returns grouped emoji reactions with usernames" do- result = Journal.grouped_emoji_reactions(reactable_id: work_package.journal_ids, reactable_type: "Journal")-- expect(result[0].reacting_users).to eq([[user1.id, user1.login], [user2.id, user2.login]])- end- end-- context "when user format is set to :firstname", with_settings: { user_format: :firstname } do- it "returns grouped emoji reactions with first and last names" do- result = Journal.grouped_emoji_reactions(reactable_id: wp_journal2.id, reactable_type: "Journal")-- expect(result[0].reacting_users).to eq([[user2.id, user2.firstname]])- end- end-- context "when user format is set to :lastname_comma_firstname", with_settings: { user_format: :lastname_comma_firstname } do- it "returns grouped emoji reactions with last coma firstname" do- result = Journal.grouped_emoji_reactions(reactable_id: wp_journal1.id, reactable_type: "Journal")-- expect(result[0].reacting_users).to eq(- [- [user1.id, "#{user1.lastname}, #{user1.firstname}"],- [user2.id, "#{user2.lastname}, #{user2.firstname}"]- ]- )- end- end-- context "when user format is set to :lastname_n_firstname", with_settings: { user_format: :lastname_n_firstname } do- it "returns grouped emoji reactions with last firstname" do- result = Journal.grouped_emoji_reactions(reactable_id: wp_journal1.id, reactable_type: "Journal")-- expect(result[0].reacting_users).to eq(- [- [user1.id, "#{user1.lastname}#{user1.firstname}"],- [user2.id, "#{user2.lastname}#{user2.firstname}"]- ]- )- end- end- end-- def add_journal(notes: "This is a test note", internal: false)- work_package.add_journal(user: work_package.author, notes:, internal:)- work_package.save(validate: false)- work_package.journals.last+ it { expect(wp_journal).to have_many(:emoji_reactions) }endenddiff --git a/spec/requests/api/v3/emoji_reactions/emoji_reaction_representer_spec.rb b/spec/requests/api/v3/emoji_reactions/emoji_reaction_representer_spec.rbindex 951c3a4198f..c9290c6ffe0 100644--- a/spec/requests/api/v3/emoji_reactions/emoji_reaction_representer_spec.rb+++ b/spec/requests/api/v3/emoji_reactions/emoji_reaction_representer_spec.rb@@ -38,7 +38,7 @@ RSpec.describe API::V3::EmojiReactions::EmojiReactionRepresenter doreactable: reactable,user: user,reaction: "thumbs_up")- Journal.grouped_emoji_reactions(reactable_id: reactable.id, reactable_type: "Journal").first+ EmojiReactions::GroupedQueries.grouped_emoji_reactions(reactable:).firstendlet(:current_user) { user }let(:representer) { described_class.new(emoji_reaction, current_user: current_user) }