A platform for community discussion. Free, open, simple.
FIX: reactions shouldn't be visible to users without see_hidden_post permission if a post is hidden (#40216)
small-lovely-cat94e38a8dc744
diff --git a/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-actions-button.gjs b/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-actions-button.gjsindex 78e5a331fbc..d81b45483a3 100644--- a/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-actions-button.gjs+++ b/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-actions-button.gjs@@ -1,12 +1,26 @@+import Component from "@glimmer/component";+import { applyValueTransformer } from "discourse/lib/transformer";+import { reactionsHiddenForUser } from "../lib/hidden-post";import DiscourseReactionsActions from "./discourse-reactions-actions";-const ReactionsActionButton = <template>- <div class="discourse-reactions-actions-button-shim">- <DiscourseReactionsActions- @post={{@post}}- @showLogin={{@buttonActions.showLogin}}- />- </div>-</template>;+export default class ReactionsActionButton extends Component {+ static shouldRender(args) {+ if (reactionsHiddenForUser(args.post)) {+ return false;+ }-export default ReactionsActionButton;+ const show = args.post.showLike || args.post.likeCount > 0;+ return applyValueTransformer("like-button-render-decision", show, {+ post: args.post,+ });+ }++ <template>+ <div class="discourse-reactions-actions-button-shim">+ <DiscourseReactionsActions+ @post={{@post}}+ @showLogin={{@buttonActions.showLogin}}+ />+ </div>+ </template>+}diff --git a/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-actions-summary.gjs b/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-actions-summary.gjsindex 8be86c7bc1e..77efbb07d24 100644--- a/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-actions-summary.gjs+++ b/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-actions-summary.gjs@@ -1,4 +1,5 @@import Component from "@glimmer/component";+import { reactionsHiddenForUser } from "../lib/hidden-post";import DiscourseReactionsActions from "./discourse-reactions-actions";export default class ReactionsActionSummary extends Component {@@ -13,6 +14,10 @@ export default class ReactionsActionSummary extends Component {return false;}+ if (reactionsHiddenForUser(args.post)) {+ return false;+ }+const siteSettings = owner?.lookup("service:site-settings");if (siteSettings?.enable_new_post_reactions_menu) {return true;diff --git a/plugins/discourse-reactions/assets/javascripts/discourse/lib/hidden-post.js b/plugins/discourse-reactions/assets/javascripts/discourse/lib/hidden-post.jsnew file mode 100644index 00000000000..eb216a9fae2--- /dev/null+++ b/plugins/discourse-reactions/assets/javascripts/discourse/lib/hidden-post.js@@ -0,0 +1,6 @@+// When a post is hidden and the viewer lacks permission to see+// it, the reaction affordances (counter and button) must be suppressed, otherwise+// interacting with them hits a 403 with no way to recover.+export function reactionsHiddenForUser(post) {+ return post.hidden && !post.can_see_hidden_post;+}diff --git a/plugins/discourse-reactions/test/javascripts/acceptance/discourse-reactions-post-test.js b/plugins/discourse-reactions/test/javascripts/acceptance/discourse-reactions-post-test.jsindex 53b58d24d41..5e7593a9852 100644--- a/plugins/discourse-reactions/test/javascripts/acceptance/discourse-reactions-post-test.js+++ b/plugins/discourse-reactions/test/javascripts/acceptance/discourse-reactions-post-test.js@@ -94,3 +94,82 @@ acceptance("Post", function (needs) {.doesNotExist("doesn’t allow to toggle the reaction");});});++acceptance("Post - hidden reactions", 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",+ enable_new_post_reactions_menu: true,+ });++ needs.pretender((server, helper) => {+ const topic = structuredClone(ReactionsTopics["/t/374.json"]);+ const post = topic.post_stream.posts[0];+ // A staff member or the author can always see a hidden post, so the denied+ // case is only reachable for a non-owner, non-staff viewer.+ post.yours = false;+ post.admin = false;+ post.staff = false;+ post.moderator = false;+ post.user_id = 999;+ post.hidden = true;+ post.cooked_hidden = true;+ post.can_see_hidden_post = false;++ server.get("/t/374.json", () => helper.response(topic));+ });++ test("hides reaction affordances for hidden posts", async function (assert) {+ await visit("/t/topic_with_reactions_and_likes/374");++ assert+ .dom("#post_1 .discourse-reactions-counter")+ .doesNotExist("hides the counter to prevent opening the reactions list");++ assert+ .dom("#post_1 .discourse-reactions-reaction-button")+ .doesNotExist("hides the reaction button that would hit a 403");+ });+});++acceptance("Post - hidden reactions with hidden-post access", 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",+ enable_new_post_reactions_menu: true,+ });++ needs.pretender((server, helper) => {+ const topic = structuredClone(ReactionsTopics["/t/374.json"]);+ const post = topic.post_stream.posts[0];+ post.yours = false;+ post.admin = false;+ post.staff = false;+ post.moderator = false;+ post.user_id = 999;+ post.hidden = true;+ post.can_see_hidden_post = true;++ server.get("/t/374.json", () => helper.response(topic));+ });++ test("keeps reaction affordances for hidden posts", async function (assert) {+ await visit("/t/topic_with_reactions_and_likes/374");++ assert+ .dom("#post_1 .discourse-reactions-counter .reactions-counter")+ .hasText("209", "allows the reactions list to be opened");++ assert+ .dom("#post_1 .discourse-reactions-reaction-button")+ .exists("keeps the reaction button for viewers who can see the post");+ });+});