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
feat[Op#58250]: Store reactions as indexed enums later mapped to unicode
Kabiru Mwenjab29c2770e436
diff --git a/app/components/work_packages/activities_tab/journals/item_component/add_reactions.html.erb b/app/components/work_packages/activities_tab/journals/item_component/add_reactions.html.erbindex 0161d7998a1..e451fae6786 100644--- a/app/components/work_packages/activities_tab/journals/item_component/add_reactions.html.erb+++ b/app/components/work_packages/activities_tab/journals/item_component/add_reactions.html.erb@@ -1,6 +1,6 @@<%=component_wrapper do- if journal.available_untaken_emojis.any?+ if journal.available_emoji_reactions.any?render(Primer::Alpha::Overlay.new(title: I18n.t("reactions.action_title"),padding: :condensed,@@ -17,17 +17,17 @@d.with_body(pt: 2) doflex_layout do |add_reactions_container|- journal.available_untaken_emojis.each do |emoji|+ journal.available_emoji_reactions.each do |emoji, reaction|add_reactions_container.with_column(mr: 2) dorender(Primer::Beta::Button.new(scheme: :invisible,- id: "#{journal.id}-#{emoji}",+ id: "#{journal.id}-#{reaction}",tag: :a,- href: toggle_reaction_work_package_activity_path(journal.journable.id, id: journal.id, emoji: emoji),- data: { "turbo-stream": true, "turbo-method": :put},- "aria-label": I18n.t("reactions.react_with", emoji_shortcode: EmojiReaction.shortcode(emoji)),+ href: toggle_reaction_work_package_activity_path(journal.journable.id, id: journal.id, reaction:),+ data: { "turbo-stream": true, "turbo-method": :put },+ "aria-label": I18n.t("reactions.react_with", reaction: reaction.tr("_", "")),)) do- emoji.html_safe+ emojiendendenddiff --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.erbindex 187240412d0..f2109ce4528 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@@ -8,16 +8,16 @@scheme: button_scheme(data[:users]),color: :default,bg: counter_color(data[:users]),- id: "#{journal.id}-#{emoji_alias(emoji)}",- test_selector: "reaction-#{emoji_alias(emoji)}",+ id: "#{journal.id}-#{data[:reaction]}",+ test_selector: "reaction-#{data[:reaction]}",tag: :a,- href: href(emoji:),+ href: href(reaction: data[:reaction]),data: { turbo_stream: true, turbo_method: :put },disabled: current_user_cannot_react?,classes: 'op-reactions-button')) do |button|- button.with_tooltip(text: tooltip_text(emoji, data[:users]))- "#{emoji} #{data[:count]}".html_safe+ button.with_tooltip(text: tooltip_text(data[:reaction], data[:users]))+ "#{emoji} #{data[:count]}"endendenddiff --git a/app/components/work_packages/activities_tab/journals/item_component/reactions.rb b/app/components/work_packages/activities_tab/journals/item_component/reactions.rbindex d92d41617a2..e208f36ded6 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@@ -62,7 +62,7 @@ module WorkPackagesreacted_by_current_user?(users) ? :default : :invisibleend- def tooltip_text(emoji, users)+ def tooltip_text(reaction, users)max_displayed_users_count = 5user_count = users.length@@ -79,18 +79,14 @@ module WorkPackagesresult += " "- result += I18n.t("reactions.reacted_with", emoji_shortcode: EmojiReaction.shortcode(emoji))+ result += I18n.t("reactions.reacted_with", reaction: reaction.tr("_", " "))resultend- def href(emoji:)+ def href(reaction:)return if current_user_cannot_react?- toggle_reaction_work_package_activity_path(journal.journable.id, id: journal.id, emoji:)- end-- def emoji_alias(emoji)- EmojiReaction::EMOJI_MAP.invert[emoji].to_s+ toggle_reaction_work_package_activity_path(journal.journable.id, id: journal.id, reaction:)enddef current_user_can_react?diff --git a/app/contracts/emoji_reactions/base_contract.rb b/app/contracts/emoji_reactions/base_contract.rbindex cfb8943c86a..6474099506a 100644--- a/app/contracts/emoji_reactions/base_contract.rb+++ b/app/contracts/emoji_reactions/base_contract.rb@@ -28,7 +28,7 @@module EmojiReactionsclass BaseContract < ::ModelContract- attribute :emoji+ attribute :reactionattribute :user_idattribute :reactable_idattribute :reactable_typediff --git a/app/controllers/work_packages/activities_tab_controller.rb b/app/controllers/work_packages/activities_tab_controller.rbindex 06f01a8bd13..e99cb1efeff 100644--- a/app/controllers/work_packages/activities_tab_controller.rb+++ b/app/controllers/work_packages/activities_tab_controller.rb@@ -145,15 +145,15 @@ class WorkPackages::ActivitiesTabController < ApplicationControllerdef toggle_reaction # rubocop:disable Metrics/AbcSizeemoji_reaction_service =- if @journal.emoji_reactions.exists?(user: User.current, emoji: params[:emoji])+ if @journal.emoji_reactions.exists?(user: User.current, reaction: params[:reaction])EmojiReactions::DeleteService.new(user: User.current,- model: @journal.emoji_reactions.find_by(user: User.current, emoji: params[:emoji]))+ model: @journal.emoji_reactions.find_by(user: User.current, reaction: params[:reaction])).callelseEmojiReactions::CreateService.new(user: User.current)- .call(user: User.current, reactable: @journal, emoji: params[:emoji])+ .call(user: User.current, reactable: @journal, reaction: params[:reaction])endemoji_reaction_service.on_success dodiff --git a/app/models/concerns/reactable.rb b/app/models/concerns/reactable.rbindex ef5eb26e433..9ed36b07bcd 100644--- a/app/models/concerns/reactable.rb+++ b/app/models/concerns/reactable.rb@@ -33,45 +33,32 @@ module Reactablehas_many :emoji_reactions, as: :reactable, dependent: :destroyend- def add_reaction(user, emoji)- emoji_reactions.create(user:, emoji:)- end-- def remove_reaction(user, emoji)- emoji_reactions.find_by(user:, emoji:)&.destroy- end-- def grouped_emoji_reactions- emoji_reactions.group(:emoji).count+ def available_emoji_reactions+ (EmojiReaction.reactions.values - emoji_reactions.pluck(:reaction).uniq).index_by do |reaction|+ EmojiReaction.emoji(reaction)+ end.sortenddef 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- emoji_groups = emoji_reactions- .select("emoji, COUNT(*) as count, ARRAY_AGG(user_id) as user_ids")- .group(:emoji)- .order("emoji ASC")+ 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 = emoji_groups.flat_map(&:user_ids).uniq+ user_ids = reaction_groups.flat_map(&:user_ids).uniqusers = User.where(id: user_ids).select(:id, :firstname, :lastname).index_by(&:id)- # convert the result to a hash indexed by emoji suitable for rendering- emoji_groups.map do |result|+ # convert the result to a hash indexed by reaction suitable for rendering+ reaction_groups.map do |result|{- emoji: result.emoji,+ 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-- def available_emojis- EmojiReaction.available_emojis.sort- end-- def available_untaken_emojis- (EmojiReaction.available_emojis - detailed_grouped_emoji_reactions.keys).sort- endenddiff --git a/app/models/emoji_reaction.rb b/app/models/emoji_reaction.rbindex da8b3344326..1c7e8651592 100644--- a/app/models/emoji_reaction.rb+++ b/app/models/emoji_reaction.rb@@ -44,16 +44,16 @@ class EmojiReaction < ApplicationRecordbelongs_to :userbelongs_to :reactable, polymorphic: true- validates :emoji, presence: true- validates :user_id, uniqueness: { scope: %i[reactable_type reactable_id emoji] }+ validates :reaction, presence: true+ validates :user_id, uniqueness: { scope: %i[reactable_type reactable_id reaction] }- enum :emoji, EMOJI_MAP+ enum :reaction, EMOJI_MAP.each_with_object({}) { |(k, _v), h| h[k] = k.to_s }def self.available_emojisAVAILABLE_EMOJISend- def self.shortcode(html_code)- EMOJI_MAP.key(html_code)&.to_s&.prepend(":")&.concat(":") || html_code+ def self.emoji(reaction)+ EMOJI_MAP[reaction.to_sym]endenddiff --git a/config/locales/en.yml b/config/locales/en.ymlindex 0cfa99f7de2..228a2e15417 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -549,10 +549,10 @@ en:reactions:action_title: "React"add_reaction: "Add reaction"- react_with: "React with %{emoji_shortcode}"+ react_with: "React with %{reaction}"and_n_others_singular: "and %{n} other"and_n_others_plural: "and %{n} others"- reacted_with: "reacted with %{emoji_shortcode}"+ reacted_with: "reacted with %{reaction}"reportings:index:diff --git a/db/migrate/20240624103354_create_emoji_reactions.rb b/db/migrate/20240624103354_create_emoji_reactions.rbindex d0f6ac11456..0e3ab38496c 100644--- a/db/migrate/20240624103354_create_emoji_reactions.rb+++ b/db/migrate/20240624103354_create_emoji_reactions.rb@@ -3,11 +3,13 @@ class CreateEmojiReactions < ActiveRecord::Migration[7.1]create_table :emoji_reactions do |t|t.references :user, null: false, foreign_key: truet.references :reactable, polymorphic: true, null: false- t.string :emoji, null: false+ t.string :reaction, null: falset.timestampsend- add_index :emoji_reactions, %i[user_id reactable_type reactable_id emoji], unique: true,- name: "index_emoji_reactions_uniqueness"+ add_index :emoji_reactions, :reaction+ add_index :emoji_reactions, %i[user_id reactable_type reactable_id reaction],+ unique: true,+ name: "index_emoji_reactions_uniqueness"endenddiff --git a/spec/contracts/emoji_reactions/base_contract_spec.rb b/spec/contracts/emoji_reactions/base_contract_spec.rbindex ad5a95bc49a..435fd8919f6 100644--- a/spec/contracts/emoji_reactions/base_contract_spec.rb+++ b/spec/contracts/emoji_reactions/base_contract_spec.rb@@ -120,13 +120,5 @@ RSpec.describe EmojiReactions::BaseContract doendend- describe "validate emoji type" do- context "when emoji is not included in the available emojis" do- before { emoji_reaction.emoji = "not_an_emoji" }-- it_behaves_like "contract is invalid", emoji: :inclusion- end- end-include_examples "contract reuses the model errors"enddiff --git a/spec/factories/emoji_reactions_factory.rb b/spec/factories/emoji_reactions_factory.rbindex 56800b9a8e4..6979a7bb36f 100644--- a/spec/factories/emoji_reactions_factory.rb+++ b/spec/factories/emoji_reactions_factory.rb@@ -28,12 +28,8 @@FactoryBot.define dofactory :emoji_reaction do- transient do- emoji_alias { EmojiReaction::EMOJI_MAP.keys.sample }- end-user- emoji { EmojiReaction::EMOJI_MAP[emoji_alias] }+ reaction { EmojiReaction.reactions.keys.sample }reactable factory: :work_package_journalendenddiff --git a/spec/features/activities/work_package/emoji_reactions_spec.rb b/spec/features/activities/work_package/emoji_reactions_spec.rbindex bc53a2329b2..7a97e346273 100644--- a/spec/features/activities/work_package/emoji_reactions_spec.rb+++ b/spec/features/activities/work_package/emoji_reactions_spec.rb@@ -55,7 +55,7 @@ RSpec.describe "Emoji reactions on work package activity", :js, :with_cuprite,before dofirst_comment- create(:emoji_reaction, user: admin, reactable: first_comment, emoji_alias: :thumbs_down)+ create(:emoji_reaction, user: admin, reactable: first_comment, reaction: :thumbs_down)wp_page.visit!wp_page.wait_for_activity_tab@@ -77,7 +77,7 @@ RSpec.describe "Emoji reactions on work package activity", :js, :with_cuprite,before dofirst_comment- create(:emoji_reaction, user: admin, reactable: first_comment, emoji_alias: :thumbs_down)+ create(:emoji_reaction, user: admin, reactable: first_comment, reaction: :thumbs_down)wp_page.visit!wp_page.wait_for_activity_tab@@ -96,7 +96,7 @@ RSpec.describe "Emoji reactions on work package activity", :js, :with_cuprite,before dofirst_comment- create(:emoji_reaction, user: admin, reactable: first_comment, emoji_alias: :rocket)+ create(:emoji_reaction, user: admin, reactable: first_comment, reaction: :rocket)wp_page.visit!wp_page.wait_for_activity_tab@@ -121,7 +121,7 @@ RSpec.describe "Emoji reactions on work package activity", :js, :with_cuprite,context "when visited by an anonymous visitor" dobefore dofirst_comment- create(:emoji_reaction, user: admin, reactable: first_comment, emoji_alias: :party_popper)+ create(:emoji_reaction, user: admin, reactable: first_comment, reaction: :party_popper)login_as User.anonymousdiff --git a/spec/models/emoji_reaction_spec.rb b/spec/models/emoji_reaction_spec.rbindex 035de1ba51b..bb4d697bd1b 100644--- a/spec/models/emoji_reaction_spec.rb+++ b/spec/models/emoji_reaction_spec.rb@@ -36,27 +36,32 @@ RSpec.describe EmojiReaction dodescribe "Enums" doit do- expect(subject).to define_enum_for(:emoji)+ expect(subject).to define_enum_for(:reaction).with_values(- thumbs_up: "\u{1F44D}",- thumbs_down: "\u{1F44E}",- grinning_face_with_smiling_eyes: "\u{1F604}",- confused_face: "\u{1F615}",- heart: "\u{2764}",- party_popper: "\u{1F389}",- rocket: "\u{1F680}",- eyes: "\u{1F440}"+ thumbs_up: "thumbs_up",+ thumbs_down: "thumbs_down",+ grinning_face_with_smiling_eyes: "grinning_face_with_smiling_eyes",+ confused_face: "confused_face",+ heart: "heart",+ party_popper: "party_popper",+ rocket: "rocket",+ eyes: "eyes").backed_by_column_of_type(:string)end++ it "stores the reaction identifier" do+ emoji_reaction = create(:emoji_reaction, reaction: :thumbs_up)+ expect(emoji_reaction.reaction).to eq("thumbs_up")+ endenddescribe "Validations" do- it { is_expected.to validate_presence_of(:emoji) }+ it { is_expected.to validate_presence_of(:reaction) }it doemoji_reaction = create(:emoji_reaction)- expect(emoji_reaction).to validate_uniqueness_of(:user_id).scoped_to(%i[reactable_type reactable_id emoji])+ expect(emoji_reaction).to validate_uniqueness_of(:user_id).scoped_to(%i[reactable_type reactable_id reaction])endend@@ -66,15 +71,13 @@ RSpec.describe EmojiReaction doendend- describe ".emoji_name" do- it "returns the emoji name for a given unicode", :aggregate_failures do- expect(described_class.emoji_name("\u{1F44D}")).to eq("thumbs up")- expect(described_class.emoji_name("😄")).to eq("grinning face with smiling eyes")+ describe ".emoji" do+ it "returns the emoji for a given reaction" do+ expect(described_class.emoji("thumbs_up")).to eq("👍")end- it "returns nil if no emoji exists with given unicode" do- expect(described_class.emoji_name("\u{1F607}")).to be_nil- expect(described_class.emoji_name("🤗")).to be_nil+ it "returns nil if no reaction exists with given name" do+ expect(described_class.emoji("rock_on")).to be_nilendendend