Hide super low score comments if they are child comments (#20612)

This commit is contained in:
Anna Buianova 2024-02-09 19:24:13 +03:00 committed by GitHub
parent 05f75c396e
commit e5064b418f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 19 additions and 11 deletions

View file

@ -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

View file

@ -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) %>

View file

@ -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 %>

View file

@ -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)