From e5064b418f714f3581ac7d6f22a56ee2348c7ae7 Mon Sep 17 00:00:00 2001 From: Anna Buianova Date: Fri, 9 Feb 2024 19:24:13 +0300 Subject: [PATCH] Hide super low score comments if they are child comments (#20612) --- app/helpers/comments_helper.rb | 12 +++++++----- app/views/articles/_comment_tree.html.erb | 4 +--- app/views/comments/index.html.erb | 4 +--- spec/requests/comments_spec.rb | 10 ++++++++++ 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb index 5f8e15664..6fee321ea 100644 --- a/app/helpers/comments_helper.rb +++ b/app/helpers/comments_helper.rb @@ -94,12 +94,14 @@ module CommentsHelper private def nested_comments(tree:, commentable:, is_view_root: false) - comments = tree.map do |comment, sub_comments| - render("comments/comment", comment: comment, commentable: commentable, - is_view_root: is_view_root, is_childless: sub_comments.empty?, - subtree_html: nested_comments(tree: sub_comments, commentable: commentable)) + comments = tree.filter_map do |comment, sub_comments| + is_childless = sub_comments.empty? + unless comment.decorate.super_low_quality && is_childless + render("comments/comment", comment: comment, commentable: commentable, + is_view_root: is_view_root, is_childless: is_childless, + subtree_html: nested_comments(tree: sub_comments, commentable: commentable)) + end end - safe_join(comments) end end diff --git a/app/views/articles/_comment_tree.html.erb b/app/views/articles/_comment_tree.html.erb index bac08c262..e46f155c7 100644 --- a/app/views/articles/_comment_tree.html.erb +++ b/app/views/articles/_comment_tree.html.erb @@ -1,4 +1,2 @@ <% comment, sub_comments = comment_node %> -<% unless comment.decorate.super_low_quality && sub_comments.empty? %> - <%= nested_comments(tree: { comment => sub_comments }, commentable: @article, is_view_root: true) %> -<% end %> +<%= nested_comments(tree: { comment => sub_comments }, commentable: @article, is_view_root: true) %> diff --git a/app/views/comments/index.html.erb b/app/views/comments/index.html.erb index 8f0d0a45a..4daff88a6 100644 --- a/app/views/comments/index.html.erb +++ b/app/views/comments/index.html.erb @@ -144,9 +144,7 @@ <% else %> <% Comments::Tree.for_commentable(@commentable, include_negative: user_signed_in?).each do |comment, sub_comments| %> <% cache ["comment_root_#{user_signed_in?}", comment] do %> - <% unless comment.decorate.super_low_quality && sub_comments.empty? %> - <%= nested_comments(tree: { comment => sub_comments }, commentable: @commentable, is_view_root: true) %> - <% end %> + <%= nested_comments(tree: { comment => sub_comments }, commentable: @commentable, is_view_root: true) %> <% end %> <% end %> <% end %> diff --git a/spec/requests/comments_spec.rb b/spec/requests/comments_spec.rb index 81aea1748..33bc59e36 100644 --- a/spec/requests/comments_spec.rb +++ b/spec/requests/comments_spec.rb @@ -80,6 +80,16 @@ RSpec.describe "Comments" do end end + context "when there are child spam comments" do + it "hides child spam comment if it has no children" do + create(:comment, commentable: article, score: -500, body_markdown: "child-spam-comment", parent: comment) + sign_in user + get "#{article.path}/comments" + expect(response.body).not_to include("child-spam-comment") + expect(response.body).not_to include("Comment deleted") + end + end + context "when the comment is a root" do it "displays the comment hidden message if the comment is hidden" do comment.update(hidden_by_commentable_user: true)