diff --git a/app/components/application_component.rb b/app/components/application_component.rb
index 3db4d0451..98d567fbd 100644
--- a/app/components/application_component.rb
+++ b/app/components/application_component.rb
@@ -1,2 +1,5 @@
class ApplicationComponent < ViewComponent::Base
+ def policy(subject)
+ Pundit.policy!(current_user, subject)
+ end
end
diff --git a/app/components/comment_component.rb b/app/components/comment_component.rb
index 537bf654f..8dfede7f3 100644
--- a/app/components/comment_component.rb
+++ b/app/components/comment_component.rb
@@ -2,17 +2,8 @@
class CommentComponent < ApplicationComponent
attr_reader :comment, :context, :dtext_data, :show_deleted, :current_user
- delegate :link_to_user, :time_ago_in_words_tagged, :format_text, :policy, to: :helpers
+ delegate :link_to_user, :time_ago_in_words_tagged, :format_text, to: :helpers
- def self.with_collection(comments, current_user:, **options)
- dtext_data = DText.preprocess(comments.map(&:body))
- # XXX
- #comments = comments.includes(:moderation_reports) if Pundit.policy!(current_user, ModerationReport).show?
-
- super(comments, current_user: current_user, dtext_data: dtext_data, **options)
- end
-
- # XXX calls to pundit policy don't respect current_user.
def initialize(comment:, current_user:, context: nil, dtext_data: nil, show_deleted: false)
@comment = comment
@context = context
@@ -25,6 +16,14 @@ class CommentComponent < ApplicationComponent
!comment.is_deleted? || show_deleted || current_user.is_moderator?
end
+ def dimmed?
+ !comment.is_sticky? && comment.score < current_user.comment_threshold/2.0
+ end
+
+ def thresholded?
+ !comment.is_sticky? && comment.score < current_user.comment_threshold
+ end
+
def has_moderation_reports?
policy(ModerationReport).show? && comment.moderation_reports.present?
end
diff --git a/app/components/comment_component/comment_component.html.erb b/app/components/comment_component/comment_component.html.erb
index 8f5ef5e88..45cb60574 100644
--- a/app/components/comment_component/comment_component.html.erb
+++ b/app/components/comment_component/comment_component.html.erb
@@ -8,7 +8,8 @@
data-do-not-bump-post="<%= comment.do_not_bump_post? %>"
data-is-deleted="<%= comment.is_deleted? %>"
data-is-sticky="<%= comment.is_sticky? %>"
- data-below-threshold="<%= comment.score < current_user.comment_threshold %>"
+ data-is-dimmed="<%= dimmed? %>"
+ data-is-thresholded="<%= thresholded? %>"
data-is-reported="<%= has_moderation_reports? %>"
data-is-voted="<%= comment.voted_by?(current_user) %>">
@@ -21,10 +22,14 @@
<%= link_to time_ago_in_words_tagged(comment.created_at), post_path(comment.post, anchor: "comment_#{comment.id}"), class: "message-timestamp" %>
-
+ <% if thresholded? %>
+ <%= link_to "[hidden]", "javascript:void(0)", class: "unhide-comment-link" %>
+ <% end %>
+
+ <%= tag.div class: "body prose", style: ("display: none;" if thresholded?) do %>
<%= format_text(comment.body, data: dtext_data) %>
-
- <%= render "application/update_notice", record: comment %>
+ <%= render "application/update_notice", record: comment %>
+ <% end %>
<% if policy(comment).create? %>
diff --git a/app/views/comments/index_for_post.js.erb b/app/views/comments/index_for_post.js.erb
index e3fbbf04b..6f6a80c1a 100644
--- a/app/views/comments/index_for_post.js.erb
+++ b/app/views/comments/index_for_post.js.erb
@@ -1,5 +1,3 @@
-$("#threshold-comments-notice-for-<%= @post.id %>").hide();
-
-var current_comment_section = $("div.comments-for-post[data-post-id=<%= @post.id %>] div.list-of-comments");
-current_comment_section.html("<%= j render_comment_list(@comments, context: :index_by_post) %>");
+var current_comment_section = $("div.comments-for-post[data-post-id=<%= @post.id %>]");
+current_comment_section.replaceWith("<%= j render_comment_section(@post, limit: nil, current_user: CurrentUser.user) %>");
$(window).trigger("danbooru:index_for_post", [<%= @post.id %>]);
diff --git a/app/views/comments/partials/index/_list.html.erb b/app/views/comments/partials/index/_list.html.erb
deleted file mode 100644
index 8b0311e3d..000000000
--- a/app/views/comments/partials/index/_list.html.erb
+++ /dev/null
@@ -1,30 +0,0 @@
-
diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb
index 7e7edfb14..0a6d50aae 100644
--- a/app/views/posts/show.html.erb
+++ b/app/views/posts/show.html.erb
@@ -105,7 +105,7 @@
<% end %>
diff --git a/test/components/comment_component_test.rb b/test/components/comment_component_test.rb
index bacf675f6..2167dee8d 100644
--- a/test/components/comment_component_test.rb
+++ b/test/components/comment_component_test.rb
@@ -40,5 +40,33 @@ class CommentComponentTest < ViewComponent::TestCase
assert_no_css(".moderation-report-notice")
end
end
+
+ context "for a downvoted comment" do
+ setup do
+ @user = create(:user, comment_threshold: -8)
+ end
+
+ context "that is thresholded" do
+ should "hide the comment" do
+ as(@user) { @comment.update!(score: -9) }
+ render_comment(@comment, current_user: @user)
+
+ assert_css("article.comment[data-is-thresholded=true]")
+ assert_css("article.comment[data-is-dimmed=true]")
+ assert_css("article.comment .unhide-comment-link")
+ end
+ end
+
+ context "that is dimmed" do
+ should "dim the comment" do
+ as(@user) { @comment.update!(score: -5) }
+ render_comment(@comment, current_user: @user)
+
+ assert_css("article.comment[data-is-thresholded=false]")
+ assert_css("article.comment[data-is-dimmed=true]")
+ assert_no_css("article.comment .unhide-comment-link")
+ end
+ end
+ end
end
end
diff --git a/test/components/comment_section_component_test.rb b/test/components/comment_section_component_test.rb
new file mode 100644
index 000000000..6c028c6a8
--- /dev/null
+++ b/test/components/comment_section_component_test.rb
@@ -0,0 +1,50 @@
+require "test_helper"
+
+class CommentSectionComponentTest < ViewComponent::TestCase
+ def render_comment_section(post, current_user: User.anonymous, **options)
+ as(current_user) do
+ render_inline(CommentSectionComponent.new(post: post, current_user: current_user, **options))
+ end
+ end
+
+ context "The CommentSectionComponent" do
+ setup do
+ as(create(:user)) do
+ @post = create(:post)
+ @comment = create_list(:comment, 7, post: @post)
+ end
+ end
+
+ context "for a comment section with comments" do
+ context "without a comment limit" do
+ should "render" do
+ render_comment_section(@post, current_user: User.anonymous)
+
+ assert_css("div.comments-for-post")
+ assert_css("article.comment", count: 7)
+ end
+ end
+
+ context "with a comment limit" do
+ context "higher than the actual number of comments" do
+ should "render" do
+ render_comment_section(@post, current_user: User.anonymous, limit: 8)
+
+ assert_css("div.comments-for-post")
+ assert_css("article.comment", count: 7)
+ end
+ end
+
+ context "lower than the actual number of comments" do
+ should "render" do
+ render_comment_section(@post, current_user: User.anonymous, limit: 6)
+
+ assert_css("div.comments-for-post")
+ assert_css("article.comment", count: 6)
+ assert_css("a.show-all-comments-link", text: "Show 1 more comment")
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/test/functional/comments_controller_test.rb b/test/functional/comments_controller_test.rb
index 048726ed0..859b7c13e 100644
--- a/test/functional/comments_controller_test.rb
+++ b/test/functional/comments_controller_test.rb
@@ -34,40 +34,7 @@ class CommentsControllerTest < ActionDispatch::IntegrationTest
assert_response :success
assert_select "#post_#{@post.id}", 1
assert_select "#post_#{@post.id} .comment", 1
- assert_select "#post_#{@post.id} #show-all-comments-link", 0
- end
-
- should "show the 'Show hidden comments' link on posts with thresholded comments" do
- as(@user) { create(:comment, post: @post, score: -10) }
- get comments_path(group_by: "post")
-
- assert_response :success
- assert_select "#post_#{@post.id}", 1
- assert_select "#post_#{@post.id} #show-all-comments-link", /Show 1 hidden comment/
- assert_select "#post_#{@post.id} .comment", 0
- assert_select "#post_#{@post.id} .list-of-comments", /There are no visible comments/
- end
-
- should "not show the 'Show hidden comments' link on posts with deleted comments to Members" do
- @comment1 = as(@user) { create(:comment, post: @post) }
- @comment2 = as(@user) { create(:comment, post: @post, is_deleted: true) }
- get comments_path(group_by: "post")
-
- assert_response :success
- assert_select "#post_#{@post.id}", 1
- assert_select "#post_#{@post.id} .comment", 1
- assert_select "#post_#{@post.id} #show-all-comments-link", 0
- end
-
- should "show the 'Show hidden comments' link on posts with deleted comments to Moderators" do
- @comment1 = as(@user) { create(:comment, post: @post) }
- @comment2 = as(@user) { create(:comment, post: @post, is_deleted: true) }
- get_auth comments_path(group_by: "post"), @mod
-
- assert_response :success
- assert_select "#post_#{@post.id}", 1
- assert_select "#post_#{@post.id} .comment", 1
- assert_select "#post_#{@post.id} #show-all-comments-link", /Show 1 hidden comment/
+ assert_select "#post_#{@post.id} .show-all-comments-link", 0
end
should "not bump posts with nonbumping comments" do
diff --git a/test/functional/posts_controller_test.rb b/test/functional/posts_controller_test.rb
index 2c307d156..b844d7b7e 100644
--- a/test/functional/posts_controller_test.rb
+++ b/test/functional/posts_controller_test.rb
@@ -560,58 +560,6 @@ class PostsControllerTest < ActionDispatch::IntegrationTest
end
end
- context "with only deleted comments" do
- setup do
- as(@user) { create(:comment, creator: @user, 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 "not show deleted comments to moderators by default, but allow them to be unhidden" do
- mod = create(:mod_user)
- get_auth post_path(@post), mod, 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 comments/
- end
- end
-
- context "with only downvoted comments" do
- should "not show thresholded comments" do
- comment = as(@user) { create(:comment, creator: @user, 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, creator: @user, post: @post, do_not_bump_post: true, body: "good") }
- as(@user) { create(:comment, creator: @user, post: @post, do_not_bump_post: true, body: "bad", score: -10) }
- as(@user) { create(:comment, creator: @user, 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)
<%= link_to "Post comment", new_comment_path(comment: { post_id: post.id }), :class => "expand-comment-response" %>
- <%= render "comments/form", comment: post.comments.new, hidden: true %> -