A platform for community discussion. Free, open, simple.
FIX: ensures removing a reaction doesn’t remove others (#20869)
Joffrey JAFFEUX69de5b161f53
diff --git a/plugins/chat/assets/javascripts/discourse/models/chat-message.js b/plugins/chat/assets/javascripts/discourse/models/chat-message.jsindex 1187c37f37b..7475c17e1ec 100644--- a/plugins/chat/assets/javascripts/discourse/models/chat-message.js+++ b/plugins/chat/assets/javascripts/discourse/models/chat-message.js@@ -151,18 +151,23 @@ export default class ChatMessage {}existingReaction.users.pushObject(actor);} else {- existingReaction.count = existingReaction.count - 1;+ const existingUserReaction = existingReaction.users.find(+ (user) => user.id === actor.id+ );++ if (!existingUserReaction) {+ return;+ }if (selfReaction) {existingReaction.reacted = false;}- if (existingReaction.count === 0) {+ if (existingReaction.count === 1) {this.reactions.removeObject(existingReaction);} else {- existingReaction.users.removeObject(- existingReaction.users.find((user) => user.id === actor.id)- );+ existingReaction.count = existingReaction.count - 1;+ existingReaction.users.removeObject(existingUserReaction);}}} else {diff --git a/plugins/chat/spec/system/page_objects/chat/chat_channel.rb b/plugins/chat/spec/system/page_objects/chat/chat_channel.rbindex 1d093778b8a..1d5ce7a615f 100644--- a/plugins/chat/spec/system/page_objects/chat/chat_channel.rb+++ b/plugins/chat/spec/system/page_objects/chat/chat_channel.rb@@ -112,15 +112,13 @@ module PageObjectswithin(message_by_id(message.id)) { find(".chat-message-bookmarked") }end- def find_reaction(message, reaction)- within(message_reactions_list(message)) do- return find("[data-emoji-name=\"#{reaction.emoji}\"]")- end+ def find_reaction(message, emoji)+ within(message_reactions_list(message)) { return find("[data-emoji-name=\"#{emoji}\"]") }end- def has_reaction?(message, reaction, text = nil)+ def has_reaction?(message, emoji, text = nil)within(message_reactions_list(message)) do- has_css?("[data-emoji-name=\"#{reaction.emoji}\"]", text: text)+ has_css?("[data-emoji-name=\"#{emoji}\"]", text: text)endend@@ -136,8 +134,8 @@ module PageObjectswithin(message_by_id(message.id)) { has_no_css?(".chat-message-reaction-list") }end- def click_reaction(message, reaction)- find_reaction(message, reaction).click+ def click_reaction(message, emoji)+ find_reaction(message, emoji).clickenddef open_action_menudiff --git a/plugins/chat/spec/system/react_to_message_spec.rb b/plugins/chat/spec/system/react_to_message_spec.rbindex 416f0a5028f..30bf15c9a56 100644--- a/plugins/chat/spec/system/react_to_message_spec.rb+++ b/plugins/chat/spec/system/react_to_message_spec.rb@@ -26,14 +26,14 @@ RSpec.describe "React to message", type: :system, js: true doit "shows existing reactions under the message" dosign_in(current_user)chat.visit_channel(category_channel_1)- expect(channel).to have_reaction(message_1, reaction_1)+ expect(channel).to have_reaction(message_1, reaction_1.emoji)endit "increments when clicking it" dosign_in(current_user)chat.visit_channel(category_channel_1)- channel.click_reaction(message_1, reaction_1)- expect(channel).to have_reaction(message_1, reaction_1, 2)+ channel.click_reaction(message_1, reaction_1.emoji)+ expect(channel).to have_reaction(message_1, reaction_1.emoji, 2)endend@@ -64,7 +64,7 @@ RSpec.describe "React to message", type: :system, js: true dofind(".chat-message-react-btn").clickfind(".chat-emoji-picker [data-emoji=\"nerd_face\"]").click- expect(channel).to have_reaction(message_1, reaction_1)+ expect(channel).to have_reaction(message_1, reaction_1.emoji)endcontext "when current user has multiple sessions" do@@ -86,12 +86,12 @@ RSpec.describe "React to message", type: :system, js: true dofind(".chat-message-react-btn").clickfind(".chat-emoji-picker [data-emoji=\"#{reaction.emoji}\"]").click- expect(channel).to have_reaction(message_1, reaction)+ expect(channel).to have_reaction(message_1, reaction.emoji)session.quitendusing_session(:tab_2) do |session|- expect(channel).to have_reaction(message_1, reaction)+ expect(channel).to have_reaction(message_1, reaction.emoji)session.quitendend@@ -107,7 +107,7 @@ RSpec.describe "React to message", type: :system, js: true dofind(".chat-message-actions .react-btn").clickfind(".chat-emoji-picker [data-emoji=\"nerd_face\"]").click- expect(channel).to have_reaction(message_1, reaction_1)+ expect(channel).to have_reaction(message_1, reaction_1.emoji)endend@@ -127,6 +127,42 @@ RSpec.describe "React to message", type: :system, js: true doendend+ context "when current user and another have reacted" do+ fab!(:other_user) { Fabricate(:user) }++ fab!(:reaction_1) do+ Chat::MessageReactor.new(current_user, category_channel_1).react!(+ message_id: message_1.id,+ react_action: :add,+ emoji: "female_detective",+ )+ end++ fab!(:reaction_2) do+ Chat::MessageReactor.new(other_user, category_channel_1).react!(+ message_id: message_1.id,+ react_action: :add,+ emoji: "female_detective",+ )+ end++ context "when removing the reaction" do+ it "removes only the reaction from the current user" do+ sign_in(current_user)+ chat.visit_channel(category_channel_1)++ expect(channel).to have_reaction(message_1, "female_detective", "2")++ channel.click_reaction(message_1, "female_detective")++ expect(channel).to have_reaction(message_1, "female_detective", "1")+ expect(+ channel.find_reaction(message_1, "female_detective")["data-tippy-content"],+ ).to include(other_user.username)+ end+ end+ end+context "when current user has reacted" dofab!(:reaction_1) doChat::MessageReactor.new(current_user, category_channel_1).react!(@@ -140,13 +176,13 @@ RSpec.describe "React to message", type: :system, js: true doit "shows existing reactions under the message" dosign_in(current_user)chat.visit_channel(category_channel_1)- expect(channel).to have_reaction(message_1, reaction_1)+ expect(channel).to have_reaction(message_1, reaction_1.emoji)endit "removes it when clicking it" dosign_in(current_user)chat.visit_channel(category_channel_1)- channel.click_reaction(message_1, reaction_1)+ channel.click_reaction(message_1, reaction_1.emoji)expect(channel).to have_no_reactions(message_1)endend@@ -177,7 +213,7 @@ RSpec.describe "React to message", type: :system, js: true doChat::Publisher.publish_reaction!(category_channel_1, message_1, "add", user_1, "heart")channel.send_message("test") # cheap trick to ensure reaction has been processed- expect(channel).to have_reaction(message_1, reaction_2, "1")+ expect(channel).to have_reaction(message_1, reaction_2.emoji, "1")endendend