A platform for community discussion. Free, open, simple.
FIX: Prevent reactions on posts in archived topics (#39038)
Régis Hanol2c9ca260ce9c
diff --git a/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-actions.gjs b/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-actions.gjsindex 6220d92bdef..86bcaae1644 100644--- a/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-actions.gjs+++ b/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-actions.gjs@@ -292,9 +292,9 @@ export default class DiscourseReactionsActions extends Component {}if (- !this.data.current_user_reaction ||- (this.data.current_user_reaction.can_undo &&- this.data.likeAction?.canToggle)+ this.data.likeAction?.canToggle &&+ (!this.data.current_user_reaction ||+ this.data.current_user_reaction.can_undo)) {if (this.capabilities.userHasBeenActive && this.capabilities.canVibrate) {navigator.vibrate(VIBRATE_DURATION);@@ -493,7 +493,7 @@ export default class DiscourseReactionsActions extends Component {const current_user_reaction = this.data.current_user_reaction;if (- this.data.likeAction &&+ !this.data.likeAction ||!(this.data.likeAction.canToggle || this.data.likeAction.can_undo)) {return;@@ -732,12 +732,7 @@ export default class DiscourseReactionsActions extends Component {return i18n("errors.desc.network");}- if (- xhr.status === 429 &&- xhr.responseJSON &&- xhr.responseJSON.errors &&- xhr.responseJSON.errors[0]- ) {+ if (xhr.responseJSON?.errors?.[0]) {return xhr.responseJSON.errors[0];} else if (xhr.status === 403) {return i18n("discourse_reactions.reaction.forbidden");diff --git a/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-picker.gjs b/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-picker.gjsindex 601b08d392e..60ea8b78f1c 100644--- a/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-picker.gjs+++ b/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-picker.gjs@@ -63,9 +63,9 @@ export default class DiscourseReactionsPicker extends Component {}if (currentUserReaction) {- canUndo = currentUserReaction.can_undo && post.likeAction.canToggle;+ canUndo = currentUserReaction.can_undo && post.likeAction?.canToggle;} else {- canUndo = post.likeAction.canToggle;+ canUndo = post.likeAction?.canToggle;}let title;diff --git a/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-reaction-button.gjs b/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-reaction-button.gjsindex b20414662c6..2d1eaec3b1f 100644--- a/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-reaction-button.gjs+++ b/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-reaction-button.gjs@@ -38,11 +38,15 @@ export default class ReactionsReactionButton extends Component {this.args.cancelCollapse();const likeAction = this.args.post.likeAction;+ if (!likeAction?.canToggle) {+ return;+ }+const currentUserReaction = this.args.post.current_user_reaction;if (currentUserReaction &&!currentUserReaction.can_undo &&- (!likeAction || isBlank(likeAction.can_undo))+ isBlank(likeAction.can_undo)) {return;}diff --git a/plugins/discourse-reactions/config/locales/client.en.yml b/plugins/discourse-reactions/config/locales/client.en.ymlindex 62ed727a720..5b6c5adb683 100644--- a/plugins/discourse-reactions/config/locales/client.en.yml+++ b/plugins/discourse-reactions/config/locales/client.en.yml@@ -15,7 +15,7 @@ en:one: "%{reaction}: %{count} user"other: "%{reaction}: %{count} users"reaction:- forbidden: "That reaction was created too long ago. It can no longer be modified or removed."+ forbidden: "You are not allowed to react or change your reaction to this post."picker:react_with: "React to this post with: %{reaction}"cant_remove_reaction: "You can no longer remove your reaction"diff --git a/plugins/discourse-reactions/spec/system/reactions_post_spec.rb b/plugins/discourse-reactions/spec/system/reactions_post_spec.rbindex f3fdc55035b..20e017f443b 100644--- a/plugins/discourse-reactions/spec/system/reactions_post_spec.rb+++ b/plugins/discourse-reactions/spec/system/reactions_post_spec.rb@@ -18,6 +18,25 @@ describe "Reactions | Post reactions" dosign_in(current_user)end+ context "when topic is archived" do+ fab!(:unliked_post) { Fabricate(:post, topic:) }++ before { topic.update!(archived: true) }++ it "does not allow reacting to a post with no likes" do+ visit unliked_post.url++ selector = "#post_#{unliked_post.post_number}"++ expect(page).to have_no_css("#{selector} .discourse-reactions-actions.can-toggle-reaction")++ find("#{selector} .discourse-reactions-reaction-button").click++ expect(page).to have_no_css(".dialog-body")+ expect(DiscourseReactions::ReactionUser.where(user: current_user).count).to eq(0)+ end+ end+context "when user has reacted but like_count is 0 and undo window passed" dofab!(:reaction) { Fabricate(:reaction, post: post_2) }fab!(:reaction_user) { Fabricate(:reaction_user, reaction:, user: current_user, post: post_2) }diff --git a/plugins/discourse-reactions/test/javascripts/acceptance/discourse-reactions-archived-topic-test.js b/plugins/discourse-reactions/test/javascripts/acceptance/discourse-reactions-archived-topic-test.jsnew file mode 100644index 00000000000..370d4967073--- /dev/null+++ b/plugins/discourse-reactions/test/javascripts/acceptance/discourse-reactions-archived-topic-test.js@@ -0,0 +1,60 @@+import { click, visit } from "@ember/test-helpers";+import { test } from "qunit";+import { acceptance } from "discourse/tests/helpers/qunit-helpers";+import ReactionsTopics from "../fixtures/reactions-topic-fixtures";++acceptance(`Discourse Reactions - Archived topic`, function (needs) {+ needs.user();++ needs.settings({+ discourse_reactions_enabled: true,+ discourse_reactions_enabled_reactions: "otter|open_mouth",+ discourse_reactions_reaction_for_like: "heart",+ discourse_reactions_like_icon: "heart",+ });++ needs.pretender((server, helper) => {+ const topicPath = "/t/374.json";+ const topicData = structuredClone(ReactionsTopics[topicPath]);++ // Make topic archived+ topicData.archived = true;++ // Ensure post_2 (id 1076) has no like action and no reactions+ const post2 = topicData.post_stream.posts.find((p) => p.id === 1076);+ post2.actions_summary = post2.actions_summary.filter((a) => a.id !== 2);+ post2.reactions = [];+ post2.current_user_reaction = null;+ post2.reaction_users_count = 0;+ post2.current_user_used_main_reaction = false;++ server.get(topicPath, () => helper.response(topicData));++ server.put(+ "/discourse-reactions/posts/:post_id/custom-reactions/:reaction/toggle.json",+ () => helper.response(403, { errors: ["forbidden"] })+ );+ });++ test("Cannot toggle reaction on post with no likes in archived topic", async function (assert) {+ await visit("/t/topic_with_reactions_and_likes/374");++ assert+ .dom("#post_2 .discourse-reactions-actions.can-toggle-reaction")+ .doesNotExist("reaction button is not toggleable");++ assert+ .dom("#post_2 .discourse-reactions-reaction-button")+ .exists("reaction button is rendered");++ await click("#post_2 .discourse-reactions-reaction-button");++ assert+ .dom(".dialog-body")+ .doesNotExist("no error dialog is shown after clicking");++ assert+ .dom("#post_2 .discourse-reactions-actions.has-reacted")+ .doesNotExist("no reaction was applied optimistically");+ });+});