From 58bd07d729795eb0dcde3f043b6afeedd131ffef Mon Sep 17 00:00:00 2001 From: Anna Buianova Date: Fri, 26 Jan 2024 20:21:18 +0300 Subject: [PATCH] Render 404 when root comment has low score and no children (#20560) --- app/controllers/comments_controller.rb | 5 +++++ app/views/comments/index.html.erb | 2 +- spec/requests/comments_spec.rb | 21 +++++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 7980000c4..23fe6445f 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -16,6 +16,11 @@ class CommentsController < ApplicationController @root_comment = Comment.find(params[:id_code].to_i(26)) if params[:id_code].present? + # hide low quality comments w/o children + if @root_comment && @root_comment.decorate.low_quality && !@root_comment.has_children? + not_found + end + if @podcast @user = @podcast @commentable = @user.podcast_episodes.find_by(slug: params[:slug]) if @user.podcast_episodes diff --git a/app/views/comments/index.html.erb b/app/views/comments/index.html.erb index 60f02416a..71e7e43d5 100644 --- a/app/views/comments/index.html.erb +++ b/app/views/comments/index.html.erb @@ -1,5 +1,5 @@ <%= javascript_include_tag "postCommentsPage", defer: true %> -<% if @root_comment.present? %> +<% if @root_comment.present? && !@root_comment.decorate.low_quality %> <% title(@root_comment.title.to_s) %> <% else %> <% title(t("views.comments.meta.title_root", title: @commentable.title)) %> diff --git a/spec/requests/comments_spec.rb b/spec/requests/comments_spec.rb index e80706ef4..e9b225e7b 100644 --- a/spec/requests/comments_spec.rb +++ b/spec/requests/comments_spec.rb @@ -161,6 +161,27 @@ RSpec.describe "Comments" do end end + context "when the comment is low quality" do + let(:low_comment) do + create(:comment, commentable: article, user: user, score: -1000, body_markdown: "low-comment") + end + + it "raises 404 when no children" do + expect do + get low_comment.path + end.to raise_error(ActiveRecord::RecordNotFound) + end + + it "is displayed as deleted when has children", :aggregate_failures do + create(:comment, commentable: article, user: user, parent: low_comment, + body_markdown: "child of a low-quality comment") + get low_comment.path + expect(response).to have_http_status(:ok) + expect(response.body).to include("Comment deleted") + expect(response.body).to include("child of a low-quality comment") + end + end + context "when the comment is for a podcast's episode" do it "is successful" do podcast_comment = create(:comment, commentable: podcast_episode, user: user)