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

feat[Op#40437]: define emoji reactions aggregation in index component

Kabiru Mwenja29f0155d98a7
diff --git a/app/components/work_packages/activities_tab/journals/index_component.html.erb b/app/components/work_packages/activities_tab/journals/index_component.html.erb
index adc7832bac0..f9e0a1cad2e 100644
--- a/app/components/work_packages/activities_tab/journals/index_component.html.erb
+++ b/app/components/work_packages/activities_tab/journals/index_component.html.erb
@@ -17,7 +17,7 @@
journals.each do |journal|
journals_index_container.with_row do
- render(WorkPackages::ActivitiesTab::Journals::ItemComponent.new(journal:, filter:))
+ render(WorkPackages::ActivitiesTab::Journals::ItemComponent.new(journal:, filter:, index_component: self))
end
end
end
diff --git a/app/components/work_packages/activities_tab/journals/index_component.rb b/app/components/work_packages/activities_tab/journals/index_component.rb
index ecacf00f175..dc6982d78f8 100644
--- a/app/components/work_packages/activities_tab/journals/index_component.rb
+++ b/app/components/work_packages/activities_tab/journals/index_component.rb
@@ -43,6 +43,10 @@ module WorkPackages
@filter = filter
end
+ def grouped_emoji_reactions
+ @grouped_emoji_reactions ||= Journal.grouped_work_package_journals_emoji_reactions(work_package)
+ end
+
private
attr_reader :work_package, :filter
diff --git a/app/components/work_packages/activities_tab/journals/item_component.html.erb b/app/components/work_packages/activities_tab/journals/item_component.html.erb
index 9419133d802..0e87fad9783 100644
--- a/app/components/work_packages/activities_tab/journals/item_component.html.erb
+++ b/app/components/work_packages/activities_tab/journals/item_component.html.erb
@@ -1,28 +1,31 @@
<%=
component_wrapper(data: wrapper_data_attributes, class: "work-packages-activities-tab-journals-item-component") do
- flex_layout(data: { "test_selector": "op-wp-journal-entry-#{journal.id}" }) do |journal_container|
+ flex_layout(data: { test_selector: "op-wp-journal-entry-#{journal.id}" }) do |journal_container|
if show_comment_container?
journal_container.with_row do
render(border_box_container(
- id: "activity-anchor-#{journal.version}",
- padding: :condensed,
- "aria-label": I18n.t("activities.work_packages.activity_tab.commented")
- )) do |border_box_component|
+ id: "activity-anchor-#{journal.version}",
+ padding: :condensed,
+ "aria-label": I18n.t("activities.work_packages.activity_tab.commented")
+ )) do |border_box_component|
border_box_component.with_header(px: 2, py: 1, data: { test_selector: "op-journal-notes-header" }) do
flex_layout(align_items: :center, justify_content: :space_between) do |header_container|
- header_container.with_column(flex_layout: true, classes: "work-packages-activities-tab-journals-item-component--header-start-container ellipsis") do |header_start_container|
+ header_container.with_column(flex_layout: true,
+ classes: "work-packages-activities-tab-journals-item-component--header-start-container ellipsis") do |header_start_container|
header_start_container.with_column(mr: 2) do
render Users::AvatarComponent.new(user: journal.user, show_name: false, size: :mini)
end
- header_start_container.with_column(mr: 1, flex_layout: true, classes: "work-packages-activities-tab-journals-item-component--user-name-container hidden-for-desktop") do |user_name_container|
+ header_start_container.with_column(mr: 1, flex_layout: true,
+ classes: "work-packages-activities-tab-journals-item-component--user-name-container hidden-for-desktop") do |user_name_container|
user_name_container.with_row(classes: "work-packages-activities-tab-journals-item-component--user-name ellipsis") do
truncated_user_name(journal.user)
end
user_name_container.with_row do
render(Primer::Beta::Text.new(font_size: :small, color: :subtle, mt: 1)) { format_time(journal.created_at) }
- end
+ end
end
- header_start_container.with_column(mr: 1, classes: "work-packages-activities-tab-journals-item-component--user-name ellipsis hidden-for-mobile") do
+ header_start_container.with_column(mr: 1,
+ classes: "work-packages-activities-tab-journals-item-component--user-name ellipsis hidden-for-mobile") do
truncated_user_name(journal.user)
end
header_start_container.with_column(mr: 1, classes: "hidden-for-mobile") do
@@ -33,32 +36,33 @@
if has_unread_notifications?
header_end_container.with_column(mr: 2, pt: 1) do
render(Primer::Beta::Octicon.new(
- :"dot-fill", # color is set via CSS as requested by UI/UX Team
- classes: "work-packages-activities-tab-journals-item-component--notification-dot-icon",
- size: :medium,
- data: { test_selector: "op-journal-unread-notification", "op-ian-center-update-immediate": true }
- ))
+ :"dot-fill", # color is set via CSS as requested by UI/UX Team
+ classes: "work-packages-activities-tab-journals-item-component--notification-dot-icon",
+ size: :medium,
+ data: { test_selector: "op-journal-unread-notification", "op-ian-center-update-immediate": true }
+ ))
end
end
header_end_container.with_column do
render(Primer::Beta::Link.new(
- href: "#",
- scheme: :secondary,
- underline: false,
- font_size: :small,
- data: {
- turbo: false,
- action: "click->work-packages--activities-tab--index#setAnchor:prevent",
- "work-packages--activities-tab--index-id-param": journal.version
- }
- )) do
+ href: "#",
+ scheme: :secondary,
+ underline: false,
+ font_size: :small,
+ data: {
+ turbo: false,
+ action: "click->work-packages--activities-tab--index#setAnchor:prevent",
+ "work-packages--activities-tab--index-id-param": journal.version
+ }
+ )) do
"##{journal.version}"
end
end
- header_end_container.with_column(ml: 1, classes: "work-packages-activities-tab-journals-item-component--action-menu") do
- render(Primer::Alpha::ActionMenu.new(data: { "test_selector": "op-wp-journal-#{journal.id}-action-menu" })) do |menu|
+ header_end_container.with_column(ml: 1,
+ classes: "work-packages-activities-tab-journals-item-component--action-menu") do
+ render(Primer::Alpha::ActionMenu.new(data: { test_selector: "op-wp-journal-#{journal.id}-action-menu" })) do |menu|
menu.with_show_button(icon: "kebab-horizontal",
- 'aria-label': I18n.t(:button_actions),
+ "aria-label": I18n.t(:button_actions),
scheme: :invisible)
copy_url_action_item(menu)
edit_action_item(menu) if allowed_to_edit?
@@ -70,24 +74,28 @@
end
border_box_component.with_body(
classes: "work-packages-activities-tab-journals-item-component--journal-notes-body",
- data: { "test_selector": "op-journal-notes-body" }
+ data: { test_selector: "op-journal-notes-body" }
) do
- unless noop?
+ if noop?
+ render(Primer::Beta::Text.new(font_style: :italic, color: :subtle, mt: 1)) do
+ I18n.t(:"journals.changes_retracted")
+ end
+ else
case state
when :show
- render(WorkPackages::ActivitiesTab::Journals::ItemComponent::Show.new(journal:, filter:))
+ render(WorkPackages::ActivitiesTab::Journals::ItemComponent::Show.new(journal:, filter:,
+ grouped_emoji_reactions:))
when :edit
render(WorkPackages::ActivitiesTab::Journals::ItemComponent::Edit.new(journal:, filter:))
end
- else
- render(Primer::Beta::Text.new(font_style: :italic, color: :subtle, mt: 1)) { I18n.t(:"journals.changes_retracted") }
end
end
end
end
end
journal_container.with_row do
- render(WorkPackages::ActivitiesTab::Journals::ItemComponent::Details.new(journal:, has_unread_notifications: notification_on_details?, filter:))
+ render(WorkPackages::ActivitiesTab::Journals::ItemComponent::Details.new(journal:,
+ has_unread_notifications: notification_on_details?, filter:))
end
end
end
diff --git a/app/components/work_packages/activities_tab/journals/item_component.rb b/app/components/work_packages/activities_tab/journals/item_component.rb
index e79ecc2c766..9bce8e57e32 100644
--- a/app/components/work_packages/activities_tab/journals/item_component.rb
+++ b/app/components/work_packages/activities_tab/journals/item_component.rb
@@ -35,16 +35,19 @@ module WorkPackages
include OpPrimer::ComponentHelpers
include OpTurbo::Streamable
- def initialize(journal:, filter:, state: :show)
+ def initialize(journal:, filter:, index_component:, state: :show)
super
@journal = journal
- @state = state
@filter = filter
+ @index_component = index_component
+ @state = state
end
private
+ delegate :grouped_emoji_reactions, to: :@index_component
+
attr_reader :journal, :state, :filter
def wrapper_uniq_by
diff --git a/app/components/work_packages/activities_tab/journals/item_component/reactions.html.erb b/app/components/work_packages/activities_tab/journals/item_component/reactions.html.erb
index 76966c3a6da..6e330720447 100644
--- a/app/components/work_packages/activities_tab/journals/item_component/reactions.html.erb
+++ b/app/components/work_packages/activities_tab/journals/item_component/reactions.html.erb
@@ -1,26 +1,26 @@
<%=
component_wrapper do
flex_layout(test_selector: "emoji-reactions") do |reactions_container|
- journal.detailed_grouped_emoji_reactions.each do |emoji, data|
+ emoji_reactions.each do |reaction, data|
reactions_container.with_column(mr: 2) do
render(Primer::Beta::Button.new(
scheme: button_scheme(data[:users]),
color: :default,
bg: counter_color(data[:users]),
- id: "#{journal.id}-#{data[:reaction]}",
- test_selector: "reaction-#{data[:reaction]}",
+ id: "#{journal.id}-#{reaction}",
+ test_selector: "reaction-#{reaction}",
tag: :a,
- href: href(reaction: data[:reaction]),
+ href: href(reaction:),
data: { turbo_stream: true, turbo_method: :put },
- aria: { label: aria_label_text(data[:reaction], data[:users]) },
+ aria: { label: aria_label_text(reaction, data[:users]) },
disabled: current_user_cannot_react?,
classes: "op-reactions-button"
)) do |button|
button.with_tooltip(text: number_of_user_reactions_text(data[:users]),
- test_selector: "reaction-tooltip-#{data[:reaction]}") do
- button.with_icon(emoji, size: :small)
+ test_selector: "reaction-tooltip-#{reaction}") do
+ button.with_icon(EmojiReactions.emoji(reaction), size: :small)
end
- "#{emoji} #{data[:count]}"
+ "#{EmojiReaction.emoji(reaction)} #{data[:count]}"
end
end
end
diff --git a/app/components/work_packages/activities_tab/journals/item_component/reactions.rb b/app/components/work_packages/activities_tab/journals/item_component/reactions.rb
index c8e74f9c7b2..d4e561555eb 100644
--- a/app/components/work_packages/activities_tab/journals/item_component/reactions.rb
+++ b/app/components/work_packages/activities_tab/journals/item_component/reactions.rb
@@ -34,19 +34,20 @@ module WorkPackages
include OpPrimer::ComponentHelpers
include OpTurbo::Streamable
- def initialize(journal:)
+ def initialize(journal:, emoji_reactions:)
super
@journal = journal
+ @emoji_reactions = emoji_reactions
end
def render?
- journal.detailed_grouped_emoji_reactions.any?
+ emoji_reactions.any?
end
private
- attr_reader :journal
+ attr_reader :journal, :emoji_reactions
def wrapper_uniq_by = journal.id
def work_package = journal.journable
@@ -65,7 +66,7 @@ module WorkPackages
# ARIA-label, show names and emoji type: "{Name of reaction} by {user A}, {user B} and {user C}".
def aria_label_text(reaction, users)
- "#{I18n.t('reactions.reaction_by', reaction: reaction.tr('_', ' '))} #{number_of_user_reactions_text(users)}"
+ "#{I18n.t('reactions.reaction_by', reaction: reaction.to_s.tr('_', ' '))} #{number_of_user_reactions_text(users)}"
end
# Visually, show just names: "{user A}, {user B} and {user C}"
diff --git a/app/components/work_packages/activities_tab/journals/item_component/show.html.erb b/app/components/work_packages/activities_tab/journals/item_component/show.html.erb
index ca5bae75d1d..65051e6b6c6 100644
--- a/app/components/work_packages/activities_tab/journals/item_component/show.html.erb
+++ b/app/components/work_packages/activities_tab/journals/item_component/show.html.erb
@@ -12,7 +12,7 @@
render(WorkPackages::ActivitiesTab::Journals::ItemComponent::AddReactions.new(journal:))
end
reactions_container.with_column do
- render(WorkPackages::ActivitiesTab::Journals::ItemComponent::Reactions.new(journal:))
+ render(WorkPackages::ActivitiesTab::Journals::ItemComponent::Reactions.new(journal:, emoji_reactions:))
end
end
end
diff --git a/app/components/work_packages/activities_tab/journals/item_component/show.rb b/app/components/work_packages/activities_tab/journals/item_component/show.rb
index e3abc222206..91edf0e77fd 100644
--- a/app/components/work_packages/activities_tab/journals/item_component/show.rb
+++ b/app/components/work_packages/activities_tab/journals/item_component/show.rb
@@ -36,20 +36,25 @@ module WorkPackages
include OpPrimer::ComponentHelpers
include OpTurbo::Streamable
- def initialize(journal:, filter:)
+ def initialize(journal:, filter:, grouped_emoji_reactions:)
super
@journal = journal
@filter = filter
+ @grouped_emoji_reactions = grouped_emoji_reactions
end
private
- attr_reader :journal, :filter
+ attr_reader :journal, :filter, :grouped_emoji_reactions
def wrapper_uniq_by
journal.id
end
+
+ def emoji_reactions
+ grouped_emoji_reactions[journal.id]
+ end
end
end
end
diff --git a/app/controllers/work_packages/activities_tab_controller.rb b/app/controllers/work_packages/activities_tab_controller.rb
index e99cb1efeff..ca1718b87d7 100644
--- a/app/controllers/work_packages/activities_tab_controller.rb
+++ b/app/controllers/work_packages/activities_tab_controller.rb
@@ -160,7 +160,8 @@ class WorkPackages::ActivitiesTabController < ApplicationController
update_via_turbo_stream(
component: WorkPackages::ActivitiesTab::Journals::ItemComponent::Show.new(
journal: @journal,
- filter: params[:filter]&.to_sym || :all
+ filter: params[:filter]&.to_sym || :all,
+ grouped_emoji_reactions: Journal.grouped_journal_emoji_reactions(@journal)
)
)
end
diff --git a/app/models/concerns/reactable.rb b/app/models/concerns/reactable.rb
index 10dde80ccf4..bb379a5d0d5 100644
--- a/app/models/concerns/reactable.rb
+++ b/app/models/concerns/reactable.rb
@@ -34,8 +34,22 @@ module Reactable
end
class_methods do
+ def grouped_journal_emoji_reactions(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: work_package.journal_ids, reactable_type: "Journal")
+ grouped_emoji_reactions_by_reactable(reactable_id: work_package.journal_ids, 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.count,
+ users: row.user_details.map { |(id, name)| { id:, name: } }
+ }
+ end
end
def grouped_emoji_reactions(reactable_id:, reactable_type:)
@@ -74,27 +88,4 @@ module Reactable
EmojiReaction.emoji(reaction)
end.sort
end
-
- def detailed_grouped_emoji_reactions
- # TODO: Refactor this to be database agnostic
- # fetch all emoji reactions and group them by emoji with their count and user ids
- reaction_groups = emoji_reactions
- .select("reaction, COUNT(*) as count, ARRAY_AGG(user_id) as user_ids")
- .group(:reaction)
- .order("reaction ASC")
-
- # avoid N+1 queries by preloading all reacting users
- user_ids = reaction_groups.flat_map(&:user_ids).uniq
- users = User.where(id: user_ids).select(:id, :firstname, :lastname).index_by(&:id)
-
- # convert the result to a hash indexed by reaction suitable for rendering
- reaction_groups.map do |result|
- {
- reaction: result.reaction,
- emoji: EmojiReaction.emoji(result.reaction),
- count: result.count,
- users: result.user_ids.map { |id| { id:, name: users[id].name } }
- }
- end.index_by { |r| r[:emoji] }
- end
end
diff --git a/spec/components/work_packages/activities_tab/journals/item_component/reactions_spec.rb b/spec/components/work_packages/activities_tab/journals/item_component/reactions_spec.rb
index 20ce3ea5712..e47c6743428 100644
--- a/spec/components/work_packages/activities_tab/journals/item_component/reactions_spec.rb
+++ b/spec/components/work_packages/activities_tab/journals/item_component/reactions_spec.rb
@@ -32,12 +32,8 @@ RSpec.describe WorkPackages::ActivitiesTab::Journals::ItemComponent::Reactions,
let(:journal) { build_stubbed(:work_package_journal) }
context "with reactions" do
- before do
- allow(journal).to receive(:detailed_grouped_emoji_reactions).and_return(mock_detailed_grouped_emoji_reactions)
- end
-
it "renders the reactions" do
- render_inline(described_class.new(journal:))
+ render_inline(described_class.new(journal:, emoji_reactions: mock_detailed_grouped_emoji_reactions))
{
thumbs_up: {
@@ -65,12 +61,8 @@ RSpec.describe WorkPackages::ActivitiesTab::Journals::ItemComponent::Reactions,
end
context "with no reactions" do
- before do
- allow(journal).to receive(:detailed_grouped_emoji_reactions).and_return([])
- end
-
it "does not render" do
- render_inline(described_class.new(journal:))
+ render_inline(described_class.new(journal:, emoji_reactions: {}))
expect(page.text).to be_empty
end
@@ -86,11 +78,11 @@ RSpec.describe WorkPackages::ActivitiesTab::Journals::ItemComponent::Reactions,
users = build_stubbed_list(:user, 20).map { |user| { id: user.id, name: user.name } }
{
- EmojiReaction.emoji(:thumbs_up) => { reaction: "thumbs_up", users: users.sample(3), count: 3 },
- EmojiReaction.emoji(:thumbs_down) => { reaction: "thumbs_down", users: users.sample(1), count: 1 },
- EmojiReaction.emoji(:eyes) => { reaction: "eyes", users: users, count: 20 },
- EmojiReaction.emoji(:rocket) => { reaction: "rocket", users: users.sample(5), count: 5 },
- EmojiReaction.emoji(:confused_face) => { reaction: "confused_face", users: users.sample(6), count: 6 }
+ thumbs_up: { users: users.sample(3), count: 3 },
+ thumbs_down: { users: users.sample(1), count: 1 },
+ eyes: { users: users, count: 20 },
+ rocket: { users: users.sample(5), count: 5 },
+ confused_face: { users: users.sample(6), count: 6 }
}
end
end
diff --git a/spec/models/concerns/reactable_spec.rb b/spec/models/concerns/reactable_spec.rb
index 910bfdfb932..a3b155b0d1a 100644
--- a/spec/models/concerns/reactable_spec.rb
+++ b/spec/models/concerns/reactable_spec.rb
@@ -45,18 +45,55 @@ RSpec.describe Reactable do
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 "Associations" do
it { expect(wp_journal1).to have_many(:emoji_reactions) }
end
describe ".grouped_work_package_journals_emoji_reactions" do
- before do
- thumbs_up_reactions
- thumbs_down_reactions
+ it "returns grouped emoji reactions for work package journals" do
+ result = Journal.grouped_work_package_journals_emoji_reactions(work_package)
+
+ expect(result.size).to eq(2)
+
+ expect(result[wp_journal1.id].size).to eq(1)
+ expect(result[wp_journal1.id][:thumbs_up]).to eq(
+ count: 2,
+ users: [{ id: user1.id, name: user1.name }, { id: user2.id, name: user2.name }]
+ )
+
+ expect(result[wp_journal2.id].size).to eq(1)
+ expect(result[wp_journal2.id][:thumbs_down]).to eq(
+ count: 1,
+ users: [{ id: user2.id, name: user2.name }]
+ )
end
+ end
+ describe ".grouped_journal_emoji_reactions" do
+ context "with a single reactable" do
+ it "returns grouped emoji reactions for that journal" do
+ result = Journal.grouped_journal_emoji_reactions(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
+ end
+
+ describe ".grouped_emoji_reactions" do
it "returns grouped emoji reactions" do
- result = Journal.grouped_work_package_journals_emoji_reactions(work_package)
+ 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 })
@@ -71,7 +108,7 @@ RSpec.describe Reactable do
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_work_package_journals_emoji_reactions(work_package)
+ result = Journal.grouped_emoji_reactions(reactable_id: work_package.journal_ids, reactable_type: "Journal")
expect(result[0].user_details).to eq([[user1.id, user1.login], [user2.id, user2.login]])
end
@@ -79,20 +116,15 @@ RSpec.describe Reactable do
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_work_package_journals_emoji_reactions(work_package)
+ result = Journal.grouped_emoji_reactions(reactable_id: wp_journal2.id, reactable_type: "Journal")
- expect(result[0].user_details).to eq(
- [
- [user1.id, user1.firstname],
- [user2.id, user2.firstname]
- ]
- )
+ expect(result[0].user_details).to eq([[user2.id, user2.firstname]])
end
end
context "when user format is set to :lastname_coma_firstname", with_settings: { user_format: :lastname_coma_firstname } do
it "returns grouped emoji reactions with last coma firstname" do
- result = Journal.grouped_work_package_journals_emoji_reactions(work_package)
+ result = Journal.grouped_emoji_reactions(reactable_id: wp_journal1.id, reactable_type: "Journal")
expect(result[0].user_details).to eq(
[
@@ -105,7 +137,7 @@ RSpec.describe Reactable do
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_work_package_journals_emoji_reactions(work_package)
+ result = Journal.grouped_emoji_reactions(reactable_id: wp_journal1.id, reactable_type: "Journal")
expect(result[0].user_details).to eq(
[