From a3d748e300db8deaaccd2308f72affa2b1d4af28 Mon Sep 17 00:00:00 2001 From: evazion Date: Mon, 26 Aug 2019 13:18:54 -0500 Subject: [PATCH] Fix #4145: Unable to view deleted comments from post page. Bug: if all the comments on a post were deleted then the deleted comments wouldn't be visible to moderators. This was because we assumed that if `last_commented_at` was nil it meant that the post had no comments, but this was wrong. `last_commented_at` only counts undeleted comments. It's reset to nil if all the commnets have been deleted. --- app/controllers/posts_controller.rb | 2 +- .../comments/partials/index/_list.html.erb | 4 +- test/functional/posts_controller_test.rb | 50 +++++++++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 772385292..768bb2415 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -23,7 +23,7 @@ class PostsController < ApplicationController def show @post = Post.find(params[:id]) - @comments = @post.last_commented_at.present? ? @post.comments : Comment.none + @comments = @post.comments @comments = @comments.includes(:creator) @comments = @comments.includes(:votes) if CurrentUser.is_member? @comments = @comments.select { |c| c.visible_by?(CurrentUser.user) } diff --git a/app/views/comments/partials/index/_list.html.erb b/app/views/comments/partials/index/_list.html.erb index a5d7810dc..a424094c8 100644 --- a/app/views/comments/partials/index/_list.html.erb +++ b/app/views/comments/partials/index/_list.html.erb @@ -4,9 +4,9 @@ <% end %>
- <% if (post.last_commented_at.present? && post.comments.any? { |c| !c.visible_by?(CurrentUser.user) }) || (page == :comments && post.comments.size > 6) %> + <% if post.comments.any? { |c| !c.visible_by?(CurrentUser.user, show_deleted: true) } || (page == :comments && post.comments.size > 6) %> - <%= link_to "Show all comments", comments_path(:post_id => post.id), :remote => true %> + <%= link_to "Show all comments", comments_path(post_id: post.id), id: "show-all-comments-link", remote: true %> <% end %>
diff --git a/test/functional/posts_controller_test.rb b/test/functional/posts_controller_test.rb index 42130d116..39d0e1626 100644 --- a/test/functional/posts_controller_test.rb +++ b/test/functional/posts_controller_test.rb @@ -143,6 +143,56 @@ class PostsControllerTest < ActionDispatch::IntegrationTest assert_response :success end + context "with only deleted comments" do + setup do + as(@user) { create(:comment, post: @post, is_deleted: true) } + end + + should "not show deleted comments to regular members" do + get_auth post_path(@post), @user, params: { id: @post.id } + + assert_response :success + assert_select "article.comment", 0 + assert_select "a#show-all-comments-link", 0 + assert_select "div.list-of-comments p", /There are no comments/ + end + + should "show deleted comments to moderators" do + mod = create(:mod_user) + get_auth post_path(@post), mod, params: { id: @post.id } + + assert_response :success + assert_select "article.comment", 1 + end + end + + context "with only downvoted comments" do + should "not show thresholded comments" do + comment = as(@user) { create(:comment, post: @post, score: -10) } + get_auth post_path(@post), @user, params: { id: @post.id } + + assert_response :success + assert_select "article.comment", 0 + assert_select "a#show-all-comments-link", 1 + assert_select "div.list-of-comments p", /There are no visible comments/ + end + end + + context "with a mix of comments" do + should "not show deleted or thresholded comments " do + as(@user) { create(:comment, post: @post, do_not_bump_post: true, body: "good") } + as(@user) { create(:comment, post: @post, do_not_bump_post: true, body: "bad", score: -10) } + as(@user) { create(:comment, post: @post, do_not_bump_post: true, body: "ugly", is_deleted: true) } + + get_auth post_path(@post), @user, params: { id: @post.id } + + assert_response :success + assert_select "article.comment", 1 + assert_select "article.comment", /good/ + assert_select "a#show-all-comments-link", 1 + end + end + context "when the recommend service is enabled" do setup do @post2 = create(:post)