diff --git a/app/components/comment_component.rb b/app/components/comment_component.rb index 562a9b5c5..26b1afa31 100644 --- a/app/components/comment_component.rb +++ b/app/components/comment_component.rb @@ -1,14 +1,22 @@ # frozen_string_literal: true class CommentComponent < ApplicationComponent - attr_reader :comment, :context, :dtext_data, :moderation_reports, :show_deleted, :current_user + attr_reader :comment, :context, :dtext_data, :show_deleted, :current_user delegate :link_to_user, :time_ago_in_words_tagged, :format_text, :policy, to: :helpers - def initialize(comment:, context: nil, dtext_data: nil, moderation_reports: [], show_deleted: false, current_user: User.anonymous) + 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, nil], 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 @dtext_data = dtext_data - @moderation_reports = moderation_reports @show_deleted = show_deleted @current_user = current_user end @@ -16,4 +24,8 @@ class CommentComponent < ApplicationComponent def render? !comment.is_deleted? || show_deleted || current_user.is_moderator? end + + def has_moderation_reports? + policy(ModerationReport).show? && comment.moderation_reports.present? + end end diff --git a/app/components/comment_component/comment_component.html.erb b/app/components/comment_component/comment_component.html.erb index ebf3b7995..8f5ef5e88 100644 --- a/app/components/comment_component/comment_component.html.erb +++ b/app/components/comment_component/comment_component.html.erb @@ -8,11 +8,9 @@ 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 < CurrentUser.user.comment_threshold %>" - <% if moderation_reports.present? && policy(moderation_reports).show? %> - data-is-reported="<%= moderation_reports.pluck(:model_id).include?(comment.id) %>" - <% end %> - data-is-voted="<%= comment.voted_by?(CurrentUser.user) %>"> + data-below-threshold="<%= comment.score < current_user.comment_threshold %>" + data-is-reported="<%= has_moderation_reports? %>" + data-is-voted="<%= comment.voted_by?(current_user) %>">
There are no visible comments.
<% else %> diff --git a/app/views/forum_posts/_listing.html.erb b/app/views/forum_posts/_listing.html.erb deleted file mode 100644 index 86130e94f..000000000 --- a/app/views/forum_posts/_listing.html.erb +++ /dev/null @@ -1,16 +0,0 @@ -<%- # forum_post %> -<%- # original_forum_post_id %> -<%- # dtext_data %> -<%- # moderation_reports %> - -<%= link_to "Post reply", new_forum_post_path(topic_id: @forum_topic.id), id: "new-response-link" %>
diff --git a/test/components/comment_component_test.rb b/test/components/comment_component_test.rb new file mode 100644 index 000000000..bacf675f6 --- /dev/null +++ b/test/components/comment_component_test.rb @@ -0,0 +1,44 @@ +require "test_helper" + +class CommentComponentTest < ViewComponent::TestCase + def render_comment(comment, current_user: User.anonymous, **options) + as(current_user) do + render_inline(CommentComponent.new(comment: comment, current_user: current_user, **options)) + end + end + + context "The CommentComponent" do + setup do + @comment = as(create(:user)) { create(:comment) } + end + + context "for a regular comment" do + should "render for Anonymous" do + render_comment(@comment, current_user: User.anonymous) + + assert_css("article#comment_#{@comment.id}") + end + + should "render for a Member" do + render_comment(@comment, current_user: create(:user)) + + assert_css("article#comment_#{@comment.id}") + end + end + + context "for a comment with moderation reports" do + should "show the report notice to moderators" do + create(:moderation_report, model: @comment) + render_comment(@comment, current_user: create(:moderator_user)) + + assert_css(".moderation-report-notice") + end + + should "not show the report notice to regular users" do + render_comment(@comment, current_user: User.anonymous) + + assert_no_css(".moderation-report-notice") + end + end + end +end diff --git a/test/components/forum_post_component_test.rb b/test/components/forum_post_component_test.rb new file mode 100644 index 000000000..eee24ad15 --- /dev/null +++ b/test/components/forum_post_component_test.rb @@ -0,0 +1,44 @@ +require "test_helper" + +class ForumPostComponentTest < ViewComponent::TestCase + def render_forum_post(forum_post, current_user: User.anonymous, **options) + as(current_user) do + render_inline(ForumPostComponent.new(forum_post: forum_post, current_user: current_user, **options)) + end + end + + context "The ForumPostComponent" do + setup do + @forum_post = as(create(:user)) { create(:forum_post) } + end + + context "for a regular forum post" do + should "render for Anonymous" do + render_forum_post(@forum_post, current_user: User.anonymous) + + assert_css("article#forum_post_#{@forum_post.id}") + end + + should "render for a Member" do + render_forum_post(@forum_post, current_user: create(:user)) + + assert_css("article#forum_post_#{@forum_post.id}") + end + end + + context "for a forum post with moderation reports" do + should "show the report notice to moderators" do + create(:moderation_report, model: @forum_post) + render_forum_post(@forum_post, current_user: create(:moderator_user)) + + assert_css(".moderation-report-notice") + end + + should "not show the report notice to regular users" do + render_forum_post(@forum_post, current_user: User.anonymous) + + assert_no_css(".moderation-report-notice") + end + end + end +end