Skip to content

A platform for community discussion. Free, open, simple.

FIX: Persist default reaction when toggling reaction (#36778)

Isaac Janzen16c3d986bfba
diff --git a/plugins/discourse-reactions/app/services/discourse_reactions/reaction_manager.rb b/plugins/discourse-reactions/app/services/discourse_reactions/reaction_manager.rb
index b3471ec894e..b577b79d745 100644
--- a/plugins/discourse-reactions/app/services/discourse_reactions/reaction_manager.rb
+++ b/plugins/discourse-reactions/app/services/discourse_reactions/reaction_manager.rb
@@ -41,8 +41,13 @@ module DiscourseReactions
private
def toggle_like
- remove_reaction if reaction_user.present?
- @like ? remove_shadow_like : add_shadow_like
+ if reaction_user.present?
+ remove_reaction
+ @reaction = reaction_scope&.first_or_create
+ add_reaction
+ else
+ @like ? remove_shadow_like : add_shadow_like
+ end
end
def toggle_reaction
@@ -81,7 +86,11 @@ module DiscourseReactions
user_id: @user.id,
post_id: @post.id,
)
- search_reaction_user.length > 0 ? search_reaction_user.first : create_reaction_user
+ if search_reaction_user.length > 0
+ search_reaction_user.first
+ else
+ create_reaction_user
+ end
end
def reaction_user
diff --git a/plugins/discourse-reactions/spec/services/reaction_manager_spec.rb b/plugins/discourse-reactions/spec/services/reaction_manager_spec.rb
index b39822a1d44..f4dfb38c0ab 100644
--- a/plugins/discourse-reactions/spec/services/reaction_manager_spec.rb
+++ b/plugins/discourse-reactions/spec/services/reaction_manager_spec.rb
@@ -15,6 +15,36 @@ RSpec.describe DiscourseReactions::ReactionManager do
before { SiteSetting.discourse_reactions_reaction_for_like = "clap" }
describe ".toggle!" do
+ context "when switching to the default reaction" do
+ fab!(:topic)
+ fab!(:op_post) { Fabricate(:post, topic: topic) }
+
+ before do
+ SiteSetting.discourse_reactions_enabled_reactions = "+1|heart"
+ SiteSetting.discourse_reactions_reaction_for_like = "+1"
+ SiteSetting.discourse_reactions_like_icon = "thumbs-up"
+ end
+
+ it "persists the default reaction" do
+ reaction_manager = described_class.new(reaction_value: "heart", user: user, post: op_post)
+ reaction_manager.toggle!
+
+ expect(DiscourseReactions::ReactionUser.exists?(user: user, post: op_post)).to be_truthy
+
+ new_reaction_manager = described_class.new(reaction_value: "+1", user: user, post: op_post)
+ new_reaction_manager.toggle!
+
+ expect(
+ DiscourseReactions::ReactionUser.exists?(
+ user: user,
+ post: op_post,
+ reaction: DiscourseReactions::Reaction.find_by(reaction_value: "+1", post: op_post),
+ ),
+ ).to be_truthy
+ expect(PostAction.exists?(user: user, post: op_post)).to be_truthy
+ end
+ end
+
context "when the user has not yet reacted to the post" do
context "when the new reaction matches discourse_reactions_reaction_for_like" do
it "does create a PostAction record" do