A platform for community discussion. Free, open, simple.
FIX: Hide ignored user posts from nested topic view (#39504)
Mark VanLandinghama32f887725a2
diff --git a/app/assets/stylesheets/common/nested-view.scss b/app/assets/stylesheets/common/nested-view.scssindex fe074e0f16d..ed4dd33f526 100644--- a/app/assets/stylesheets/common/nested-view.scss+++ b/app/assets/stylesheets/common/nested-view.scss@@ -629,32 +629,42 @@color: var(--tertiary);}- &__deleted-placeholder {+ &__placeholder {grid-column: 2;grid-row: 1;- padding: 0 0 2em;- margin-top: -0.35em;++ &--deleted {+ padding: 0 0 2em;+ margin-top: -0.35em;+ }++ &--ignored {+ min-height: var(--nested-avatar-size);+ display: flex;+ align-items: center;+ padding-bottom: 1em;+ }}- &__deleted-actions {+ &__placeholder-actions {display: flex;align-items: center;gap: 0.25em;}- &__deleted-label {+ &__placeholder-label {color: var(--primary-medium);font-size: var(--font-down-1);font-style: italic;}- &__deleted-content {+ &__placeholder-reveal {background-color: var(--danger-low-mid);padding: 0.5em;width: 100%;}- &__deleted-content-header {+ &__placeholder-reveal-header {display: flex;align-items: center;gap: 0.5em;@@ -677,7 +687,7 @@}}- &__deleted-avatar-placeholder {+ &__placeholder-avatar {width: var(--nested-avatar-size);height: var(--nested-avatar-size);display: flex;@@ -689,6 +699,27 @@width: 14px;height: 14px;}++ &--reveal {+ background: transparent;+ border: none;+ padding: 0;+ cursor: pointer;+ border-radius: 50%;+ transition:+ background-color 0.15s ease,+ color 0.15s ease;++ &:hover:not(:disabled),+ &:focus-visible {+ background-color: var(--primary-low);+ color: var(--primary);+ }++ &:disabled {+ cursor: default;+ }+ }}&__continue-link {diff --git a/app/models/ignored_user.rb b/app/models/ignored_user.rbindex 5794864709a..36521737723 100644--- a/app/models/ignored_user.rb+++ b/app/models/ignored_user.rb@@ -5,6 +5,21 @@ class IgnoredUser < ActiveRecord::Basebelongs_to :userbelongs_to :ignored_user, class_name: "User"++ # Excludes self and staff to match topic post filtering.+ def self.ignored_ids_for(user)+ return [] unless user++ DB.query_single(<<~SQL, current_user_id: user.id)+ SELECT ignored_user_id+ FROM ignored_users as ig+ INNER JOIN users as u ON u.id = ig.ignored_user_id+ WHERE ig.user_id = :current_user_id+ AND ig.ignored_user_id <> :current_user_id+ AND NOT u.admin+ AND NOT u.moderator+ SQL+ endend# == Schema Informationdiff --git a/config/locales/client.en.yml b/config/locales/client.en.ymlindex 72800658455..0b5bd6e8e0e 100644--- a/config/locales/client.en.yml+++ b/config/locales/client.en.yml@@ -3943,6 +3943,8 @@ en:default_nested_view: "Use nested view as default for topics in this category"no_replies: "No replies yet."deleted_post_placeholder: "[deleted]"+ ignored_post_placeholder: "[ignored]"+ toggle_ignored_content: "Show ignored content"op_badge: "OP"pinned_reply: "Pinned"pin_reply: "Pin to top"diff --git a/frontend/discourse/app/components/nested/post.gjs b/frontend/discourse/app/components/nested/post.gjsindex 00ae680edb4..f033e91ac4b 100644--- a/frontend/discourse/app/components/nested/post.gjs+++ b/frontend/discourse/app/components/nested/post.gjs@@ -18,6 +18,7 @@ import PostMetaData from "discourse/components/post/meta-data";import concatClass from "discourse/helpers/concat-class";import icon from "discourse/helpers/d-icon";import lazyHash from "discourse/helpers/lazy-hash";+import { ajax } from "discourse/lib/ajax";import { popupAjaxError } from "discourse/lib/ajax-error";import { isTesting } from "discourse/lib/environment";import getURL, { getAbsoluteURL } from "discourse/lib/get-url";@@ -41,6 +42,8 @@ export default class NestedPost extends Component {@tracked lineHighlighted = false;@tracked collapsed;@tracked showDeletedContent = false;+ @tracked showIgnoredContent = false;+ @tracked loadingIgnoredContent = false;restoreScroll = modifier((element) => {const anchor = this.args.scrollAnchor;@@ -63,7 +66,8 @@ export default class NestedPost extends Component {} else {this.expanded =(this.args.children?.length ?? 0) > 0 ||- this.args.post.deleted_post_placeholder === true;+ this.args.post.deleted_post_placeholder === true ||+ this.args.post.ignored_post_placeholder === true;this.collapsed = false;}@@ -156,6 +160,14 @@ export default class NestedPost extends Component {return this.args.post.deleted_post_placeholder === true;}+ get isIgnoredPlaceholder() {+ return this.args.post.ignored_post_placeholder === true;+ }++ get renderIgnoredPlaceholder() {+ return this.isIgnoredPlaceholder && !this.showIgnoredContent;+ }+get showContinueThread() {return (this.atMaxDepth &&@@ -209,6 +221,25 @@ export default class NestedPost extends Component {this.showDeletedContent = !this.showDeletedContent;}+ @action+ async revealIgnoredContent() {+ if (this.showIgnoredContent || this.loadingIgnoredContent) {+ return;+ }++ const post = this.args.post;+ try {+ this.loadingIgnoredContent = true;+ const result = await ajax(`/posts/${post.id}/cooked.json`);+ post.set("cooked", result.cooked);+ this.showIgnoredContent = true;+ } catch (e) {+ popupAjaxError(e);+ } finally {+ this.loadingIgnoredContent = false;+ }+ }+@actionhighlightLine() {if (!this.site.mobileView) {@@ -317,9 +348,24 @@ export default class NestedPost extends Component {<div class="nested-post__gutter">{{#unless this.isMobile}}{{#if this.isDeletedPlaceholder}}- <div class="nested-post__deleted-avatar-placeholder">+ <div class="nested-post__placeholder-avatar">{{icon "trash-can"}}</div>+ {{else if this.renderIgnoredPlaceholder}}+ <button+ type="button"+ class="nested-post__placeholder-avatar nested-post__placeholder-avatar--reveal"+ data-post-number={{@post.post_number}}+ aria-label={{i18n "nested_replies.toggle_ignored_content"}}+ disabled={{this.loadingIgnoredContent}}+ {{on "click" this.revealIgnoredContent}}+ >+ {{#if this.loadingIgnoredContent}}+ {{icon "spinner" class="fa-spin"}}+ {{else}}+ {{icon "far-eye-slash"}}+ {{/if}}+ </button>{{else}}<PostAvatar @post={{@post}} @size="small" />{{/if}}@@ -362,6 +408,10 @@ export default class NestedPost extends Component {<span class="nested-post__collapsed-username">{{i18n"nested_replies.deleted_post_placeholder"}}</span>+ {{else if this.renderIgnoredPlaceholder}}+ <span class="nested-post__collapsed-username">{{i18n+ "nested_replies.ignored_post_placeholder"+ }}</span>{{else}}<spanclass="nested-post__collapsed-username"@@ -377,11 +427,11 @@ export default class NestedPost extends Component {</button>{{else if this.isDeletedPlaceholder}}<div- class="nested-post__deleted-placeholder"+ class="nested-post__placeholder nested-post__placeholder--deleted"data-post-number={{@post.post_number}}>- <div class="nested-post__deleted-actions">- <span class="nested-post__deleted-label">{{i18n+ <div class="nested-post__placeholder-actions">+ <span class="nested-post__placeholder-label">{{i18n"nested_replies.deleted_post_placeholder"}}</span>{{#if this.currentUser.staff}}@@ -406,8 +456,8 @@ export default class NestedPost extends Component {{{/if}}</div>{{#if this.showDeletedContent}}- <div class="nested-post__deleted-content">- <div class="nested-post__deleted-content-header">+ <div class="nested-post__placeholder-reveal">+ <div class="nested-post__placeholder-reveal-header"><PostAvatar @post={{@post}} @size="small" /><PostMetaData @post={{@post}} /></div>@@ -415,6 +465,17 @@ export default class NestedPost extends Component {</div>{{/if}}</div>+ {{else if this.renderIgnoredPlaceholder}}+ <div+ class="nested-post__placeholder nested-post__placeholder--ignored"+ data-post-number={{@post.post_number}}+ >+ <div class="nested-post__placeholder-actions">+ <span class="nested-post__placeholder-label">{{i18n+ "nested_replies.ignored_post_placeholder"+ }}</span>+ </div>+ </div>{{else}}{{#let (lazyHash post=@post) as |postOutletArgs|}}<PluginOutlet @name="post-article" @outletArgs={{postOutletArgs}}>diff --git a/lib/nested_replies/post_tree_serializer.rb b/lib/nested_replies/post_tree_serializer.rbindex 2fe80c2c85f..5bf06a985c5 100644--- a/lib/nested_replies/post_tree_serializer.rb+++ b/lib/nested_replies/post_tree_serializer.rb@@ -6,6 +6,7 @@ module NestedReplies@topic = topic@topic_view = topic_view@guardian = guardian+ @ignored_user_ids = IgnoredUser.ignored_ids_for(guardian.user)endSUGGESTED_AND_RELATED_KEYS = %i[@@ -60,6 +61,11 @@ module NestedReplies:total_descendant_count,)end+ elsif post.post_number != 1 && @ignored_user_ids.include?(post.user_id)+ json[:ignored_post_placeholder] = true+ json[:cooked] = ""+ json[:raw] = nil+ json[:actions_summary] = []endjsondiff --git a/lib/topic_view.rb b/lib/topic_view.rbindex 8a8f2dd74ed..9bd38d1ccca 100644--- a/lib/topic_view.rb+++ b/lib/topic_view.rb@@ -1050,17 +1050,7 @@ class TopicView@filtered_posts = unfiltered_postsif @user- sql = <<~SQL- SELECT ignored_user_id- FROM ignored_users as ig- INNER JOIN users as u ON u.id = ig.ignored_user_id- WHERE ig.user_id = :current_user_id- AND ig.ignored_user_id <> :current_user_id- AND NOT u.admin- AND NOT u.moderator- SQL-- ignored_user_ids = DB.query_single(sql, current_user_id: @user.id)+ ignored_user_ids = IgnoredUser.ignored_ids_for(@user)if ignored_user_ids.present?@filtered_posts =diff --git a/spec/lib/nested_replies/post_tree_serializer_spec.rb b/spec/lib/nested_replies/post_tree_serializer_spec.rbnew file mode 100644index 00000000000..91a5676a6d7--- /dev/null+++ b/spec/lib/nested_replies/post_tree_serializer_spec.rb@@ -0,0 +1,83 @@+# frozen_string_literal: true++RSpec.describe NestedReplies::PostTreeSerializer do+ fab!(:user)+ fab!(:ignored_user, :user)+ fab!(:topic) { Fabricate(:topic, user: user) }+ fab!(:op) { Fabricate(:post, topic: topic, user: user, post_number: 1) }++ let(:topic_view) do+ TopicView.new(topic.id, user, skip_custom_fields: true, skip_post_loading: true)+ end++ before { SiteSetting.nested_replies_enabled = true }++ def serializer_for(viewer)+ described_class.new(topic: topic, topic_view: topic_view, guardian: Guardian.new(viewer))+ end++ describe "#serialize_post" do+ fab!(:reply) do+ Fabricate(:post, topic: topic, user: ignored_user, reply_to_post_number: 1, raw: "secret")+ end++ context "when the viewer has ignored the author" do+ before { Fabricate(:ignored_user, user: user, ignored_user: ignored_user) }++ it "marks it as an ignored_post_placeholder, clears content, keeps metadata" do+ json = serializer_for(user).serialize_post(reply, {})++ expect(json[:ignored_post_placeholder]).to eq(true)+ expect(json[:cooked]).to eq("")+ expect(json[:raw]).to be_nil+ expect(json[:actions_summary]).to eq([])+ expect(json[:username]).to eq(ignored_user.username)+ expect(json).to include(:id, :post_number, :reply_to_post_number, :avatar_template)+ end+ end++ context "when the viewer has not ignored the author" do+ it "returns the full post JSON" do+ json = serializer_for(user).serialize_post(reply, {})++ expect(json).not_to have_key(:ignored_post_placeholder)+ expect(json[:cooked]).to include("secret")+ end+ end++ context "when the viewer is anonymous" do+ it "returns the full post JSON" do+ json = serializer_for(nil).serialize_post(reply, {})++ expect(json).not_to have_key(:ignored_post_placeholder)+ expect(json[:cooked]).to include("secret")+ end+ end++ context "when the ignored author wrote the OP (post_number == 1)" do+ before { Fabricate(:ignored_user, user: user, ignored_user: ignored_user) }++ it "does not placeholder the OP (client-side firstPost handles it)" do+ op.update!(user: ignored_user)++ json = serializer_for(user).serialize_post(op, {})++ expect(json).not_to have_key(:ignored_post_placeholder)+ expect(json[:cooked]).to be_present+ end+ end++ context "when the post is both deleted and from an ignored author" do+ before { Fabricate(:ignored_user, user: user, ignored_user: ignored_user) }++ it "prefers the deleted_post_placeholder path" do+ reply.trash!(user)++ json = serializer_for(user).serialize_post(reply.reload, {})++ expect(json[:deleted_post_placeholder]).to eq(true)+ expect(json).not_to have_key(:ignored_post_placeholder)+ end+ end+ end+enddiff --git a/spec/models/ignored_user_spec.rb b/spec/models/ignored_user_spec.rbnew file mode 100644index 00000000000..fb4c22efbe9--- /dev/null+++ b/spec/models/ignored_user_spec.rb@@ -0,0 +1,24 @@+# frozen_string_literal: true++RSpec.describe IgnoredUser do+ describe ".ignored_ids_for" do+ fab!(:user)+ fab!(:target, :user)+ fab!(:admin) { Fabricate(:user, admin: true) }++ before { Fabricate(:ignored_user, user: user, ignored_user: target) }++ it "returns ids of users ignored by the given user" do+ expect(described_class.ignored_ids_for(user)).to contain_exactly(target.id)+ end++ it "excludes staff even if ignored" do+ Fabricate(:ignored_user, user: user, ignored_user: admin)+ expect(described_class.ignored_ids_for(user)).not_to include(admin.id)+ end++ it "returns an empty array when user is nil" do+ expect(described_class.ignored_ids_for(nil)).to eq([])+ end+ end+enddiff --git a/spec/system/nested_ignored_users_spec.rb b/spec/system/nested_ignored_users_spec.rbnew file mode 100644index 00000000000..872b2646514--- /dev/null+++ b/spec/system/nested_ignored_users_spec.rb@@ -0,0 +1,55 @@+# frozen_string_literal: true++RSpec.describe "Nested view ignored users" do+ fab!(:viewer) { Fabricate(:user, refresh_auto_groups: true) }+ fab!(:ignored_author) { Fabricate(:user, refresh_auto_groups: true) }+ fab!(:topic) { Fabricate(:topic, user: viewer) }+ fab!(:op) { Fabricate(:post, topic: topic, user: viewer, post_number: 1) }++ fab!(:ignored_reply) do+ Fabricate(+ :post,+ topic: topic,+ user: ignored_author,+ reply_to_post_number: 1,+ raw: "Secret reply content from an ignored user",+ )+ end++ let(:nested_view) { PageObjects::Pages::NestedView.new }++ before do+ SiteSetting.nested_replies_enabled = true+ Fabricate(:ignored_user, user: viewer, ignored_user: ignored_author)+ sign_in(viewer)+ end++ it "shows an [ignored] placeholder in place of the reply body" do+ nested_view.visit_nested(topic)++ expect(nested_view).to have_ignored_placeholder_for(ignored_reply)+ expect(page).to have_no_content("Secret reply content from an ignored user")+ end++ it "reveals the real content when the eye-slash avatar button is clicked" do+ nested_view.visit_nested(topic)++ expect(nested_view).to have_ignored_placeholder_for(ignored_reply)++ nested_view.click_reveal_ignored(ignored_reply)++ expect(nested_view).to have_no_ignored_placeholder_for(ignored_reply)+ expect(page).to have_css(+ "[data-post-number='#{ignored_reply.post_number}']",+ text: "Secret reply content from an ignored user",+ )+ end++ it "does not render an ignored placeholder for the OP even if the OP author is ignored" do+ op.update!(user: ignored_author)++ nested_view.visit_nested(topic)++ expect(nested_view).to have_no_ignored_placeholder_for(op)+ end+enddiff --git a/spec/system/nested_post_lifecycle_spec.rb b/spec/system/nested_post_lifecycle_spec.rbindex 46f4f415ff4..02a53a5abf1 100644--- a/spec/system/nested_post_lifecycle_spec.rb+++ b/spec/system/nested_post_lifecycle_spec.rb@@ -190,7 +190,7 @@ RSpec.describe "Nested view post lifecycle" doexpect(nested_view).to have_deleted_content_visible_for(root_reply)expect(page).to have_css(- ".nested-post__deleted-content",+ ".nested-post__placeholder-reveal",text: "Secret deleted content here",)enddiff --git a/spec/system/page_objects/pages/nested_view.rb b/spec/system/page_objects/pages/nested_view.rbindex aa61b177508..565b43f4c42 100644--- a/spec/system/page_objects/pages/nested_view.rb+++ b/spec/system/page_objects/pages/nested_view.rb@@ -340,11 +340,11 @@ module PageObjects# ── Deletion/recovery assertions ─────────────────────────────def has_deleted_placeholder_for?(post)- has_css?("[data-post-number='#{post.post_number}'].nested-post__deleted-placeholder")+ has_css?("[data-post-number='#{post.post_number}'].nested-post__placeholder--deleted")enddef has_no_deleted_placeholder_for?(post)- has_no_css?("[data-post-number='#{post.post_number}'].nested-post__deleted-placeholder")+ has_no_css?("[data-post-number='#{post.post_number}'].nested-post__placeholder--deleted")enddef has_deleted_post_class_for?(post)@@ -365,11 +365,28 @@ module PageObjectsenddef has_deleted_content_visible_for?(post)- has_css?(wrapper_selector(post, ".nested-post__deleted-content"))+ has_css?(wrapper_selector(post, ".nested-post__placeholder-reveal"))enddef has_no_deleted_content_visible_for?(post)- has_no_css?(wrapper_selector(post, ".nested-post__deleted-content"))+ has_no_css?(wrapper_selector(post, ".nested-post__placeholder-reveal"))+ end++ # ── Ignored-user placeholder assertions ──────────────────────++ def has_ignored_placeholder_for?(post)+ has_css?("[data-post-number='#{post.post_number}'].nested-post__placeholder--ignored")+ end++ def has_no_ignored_placeholder_for?(post)+ has_no_css?("[data-post-number='#{post.post_number}'].nested-post__placeholder--ignored")+ end++ def click_reveal_ignored(post)+ find(+ "button.nested-post__placeholder-avatar--reveal[data-post-number='#{post.post_number}']",+ ).click+ selfend# ── Post actions ────────────────────────────────────────────