Fix #4525: Show mod report notices next to reported content.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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) %>">
|
||||
<div class="author">
|
||||
<div class="author-name">
|
||||
<%= link_to_user comment.creator %>
|
||||
@@ -56,6 +54,9 @@
|
||||
<% if policy(comment).reportable? %>
|
||||
<li><%= link_to "Report", new_moderation_report_path(moderation_report: { model_type: "Comment", model_id: comment.id }), remote: true %></li>
|
||||
<% end %>
|
||||
<% if has_moderation_reports? %>
|
||||
<li class="moderation-report-notice">This comment has been reported! (<%= link_to pluralize(comment.moderation_reports.length, "report"), moderation_reports_path(search: { model_type: "Comment", model_id: comment.id }) %>)</li>
|
||||
<% end %>
|
||||
</menu>
|
||||
<% if policy(comment).update? %>
|
||||
<%= render "comments/form", comment: comment, hidden: true %>
|
||||
|
||||
@@ -6,7 +6,7 @@ article.comment {
|
||||
}
|
||||
|
||||
&[data-is-reported="true"] {
|
||||
border: var(--moderation-report-border);
|
||||
background-color: var(--moderation-report-background-color);
|
||||
}
|
||||
|
||||
&[data-is-voted="true"] {
|
||||
@@ -28,4 +28,9 @@ article.comment {
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
.moderation-report-notice {
|
||||
font-weight: bold;
|
||||
color: var(--moderation-report-text-color);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,15 +6,28 @@ class ForumPostComponent < ApplicationComponent
|
||||
|
||||
with_collection_parameter :forum_post
|
||||
|
||||
def initialize(forum_post:, original_forum_post_id: nil, dtext_data: nil, moderation_reports: [], current_user: User.anonymous)
|
||||
def self.with_collection(forum_posts, forum_topic:, current_user:)
|
||||
dtext_data = DText.preprocess(forum_posts.map(&:body))
|
||||
original_forum_post_id = forum_topic.original_post&.id
|
||||
|
||||
forum_posts = forum_posts.includes(:creator, :bulk_update_request)
|
||||
forum_posts = forum_posts.includes(:moderation_reports) if Pundit.policy!([current_user, nil], ModerationReport).show?
|
||||
|
||||
super(forum_posts, dtext_data: dtext_data, original_forum_post_id: original_forum_post_id, current_user: current_user)
|
||||
end
|
||||
|
||||
def initialize(forum_post:, original_forum_post_id: nil, dtext_data: nil, current_user: User.anonymous)
|
||||
@forum_post = forum_post
|
||||
@original_forum_post_id = original_forum_post_id
|
||||
@dtext_data = dtext_data
|
||||
@moderation_reports = moderation_reports
|
||||
@current_user = current_user
|
||||
end
|
||||
|
||||
def render?
|
||||
policy(forum_post).show_deleted?
|
||||
end
|
||||
|
||||
def has_moderation_reports?
|
||||
policy(ModerationReport).show? && forum_post.moderation_reports.present?
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
<article class="forum-post message" id="forum_post_<%= forum_post.id %>"
|
||||
data-forum-post-id="<%= forum_post.id %>"
|
||||
<% if policy(moderation_reports).show? %>
|
||||
data-is-reported="<%= moderation_reports.pluck(:model_id).include?(forum_post.id) %>"
|
||||
<% end %>
|
||||
data-is-reported="<%= has_moderation_reports? %>"
|
||||
data-creator="<%= forum_post.creator.name %>">
|
||||
|
||||
<div class="author">
|
||||
@@ -42,6 +40,9 @@
|
||||
<% if policy(forum_post).reportable? %>
|
||||
<li><%= link_to "Report", new_moderation_report_path(moderation_report: { model_type: "ForumPost", model_id: forum_post.id }), remote: true, title: "Report this forum post to the moderators" %></li>
|
||||
<% end %>
|
||||
<% if has_moderation_reports? %>
|
||||
<li class="moderation-report-notice">This post has been reported! (<%= link_to pluralize(forum_post.moderation_reports.length, "report"), moderation_reports_path(search: { model_type: "ForumPost", model_id: forum_post.id }) %>)</li>
|
||||
<% end %>
|
||||
<% if forum_post.bulk_update_request.present? %>
|
||||
<ul class="votes" id="forum-post-votes-for-<%= forum_post.id %>">
|
||||
<%= render "forum_post_votes/list", votes: forum_post.votes, forum_post: forum_post %>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
article.forum-post {
|
||||
&[data-is-reported="true"] {
|
||||
border: var(--moderation-report-border);
|
||||
background-color: var(--moderation-report-background-color);
|
||||
}
|
||||
|
||||
a.voted {
|
||||
@@ -30,4 +30,9 @@ article.forum-post {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.moderation-report-notice {
|
||||
font-weight: bold;
|
||||
color: var(--moderation-report-text-color);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,8 +42,6 @@ class ForumTopicsController < ApplicationController
|
||||
|
||||
if request.format.atom?
|
||||
@forum_posts = @forum_posts.reverse_order.load
|
||||
elsif request.format.html?
|
||||
@forum_posts = @forum_posts.includes(:creator, :bulk_update_request)
|
||||
end
|
||||
|
||||
respond_with(@forum_topic)
|
||||
|
||||
@@ -4,7 +4,6 @@ module CommentsHelper
|
||||
end
|
||||
|
||||
def render_comment_list(comments, **options)
|
||||
dtext_data = DText.preprocess(comments.map(&:body))
|
||||
render CommentComponent.with_collection(comments, dtext_data: dtext_data, **options)
|
||||
render CommentComponent.with_collection(comments, current_user: CurrentUser.user, **options)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -71,7 +71,7 @@
|
||||
--forum-topic-status-rejected-color: hsla(0, 25%, 55%, 1);
|
||||
|
||||
--moderation-report-text-color: red;
|
||||
--moderation-report-border: 2px solid red;
|
||||
--moderation-report-background-color: var(--error-background-color);
|
||||
|
||||
--comment-sticky-background-color: var(--subnav-menu-background-color);
|
||||
|
||||
@@ -349,7 +349,7 @@ body[data-current-user-theme="dark"] {
|
||||
--forum-topic-status-rejected-color: var(--red-3);
|
||||
|
||||
--moderation-report-text-color: var(--red-1);
|
||||
--moderation-report-border: 2px solid var(--red-1);
|
||||
--moderation-report-background-color: var(--red-0);
|
||||
|
||||
--jquery-ui-widget-content-text-color: var(--text-color);
|
||||
--jquery-ui-widget-content-background: var(--grey-2);
|
||||
|
||||
@@ -1,10 +1,3 @@
|
||||
div.list-of-forum-posts {
|
||||
div.moderation-forums-notice {
|
||||
font-weight: bold;
|
||||
color: var(--moderation-report-text-color);
|
||||
}
|
||||
}
|
||||
|
||||
div#c-forum-topics {
|
||||
td.status-column {
|
||||
white-space: nowrap;
|
||||
|
||||
@@ -16,7 +16,6 @@ class ForumTopic < ApplicationRecord
|
||||
has_many :forum_posts, foreign_key: "topic_id", dependent: :destroy, inverse_of: :topic
|
||||
has_many :forum_topic_visits
|
||||
has_one :forum_topic_visit_by_current_user, -> { where(user_id: CurrentUser.id) }, class_name: "ForumTopicVisit"
|
||||
has_many :moderation_reports, through: :forum_posts
|
||||
has_one :original_post, -> { order(id: :asc) }, class_name: "ForumPost", foreign_key: "topic_id", inverse_of: :topic
|
||||
has_many :bulk_update_requests, :foreign_key => "forum_topic_id"
|
||||
has_many :tag_aliases, :foreign_key => "forum_topic_id"
|
||||
|
||||
@@ -50,7 +50,6 @@ class Post < ApplicationRecord
|
||||
has_many :votes, :class_name => "PostVote", :dependent => :destroy
|
||||
has_many :notes, :dependent => :destroy
|
||||
has_many :comments, -> {order("comments.id")}, :dependent => :destroy
|
||||
has_many :moderation_reports, through: :comments
|
||||
has_many :children, -> {order("posts.id")}, :class_name => "Post", :foreign_key => "parent_id"
|
||||
has_many :approvals, :class_name => "PostApproval", :dependent => :destroy
|
||||
has_many :disapprovals, :class_name => "PostDisapproval", :dependent => :destroy
|
||||
|
||||
@@ -3,6 +3,10 @@ class ModerationReportPolicy < ApplicationPolicy
|
||||
user.is_moderator?
|
||||
end
|
||||
|
||||
def show?
|
||||
user.is_moderator?
|
||||
end
|
||||
|
||||
def create?
|
||||
unbanned? && policy(record.model).reportable?
|
||||
end
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
<%= link_to(image_tag(comment.post.preview_file_url), post_path(comment.post)) %>
|
||||
<% end %>
|
||||
</div>
|
||||
<%= render_comment(comment, dtext_data: dtext_data, context: :index_by_comment, show_deleted: params.dig(:search, :is_deleted).to_s.truthy?) %>
|
||||
<%= render_comment(comment, dtext_data: dtext_data, context: :index_by_comment, show_deleted: params.dig(:search, :is_deleted).to_s.truthy?, current_user: CurrentUser.user) %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
$("#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, moderation_reports: @post.moderation_reports.visible(CurrentUser.user).recent) %>");
|
||||
current_comment_section.html("<%= j render_comment_list(@comments, context: :index_by_post) %>");
|
||||
$(window).trigger("danbooru:index_for_post", [<%= @post.id %>]);
|
||||
|
||||
@@ -3,13 +3,6 @@
|
||||
<%= render "comments/partials/index/header", :post => post %>
|
||||
<% end %>
|
||||
|
||||
<% if post.moderation_reports.visible(CurrentUser.user).recent.present? %>
|
||||
<div class="row moderation-comments-notice">
|
||||
<span class="info" id="moderation-comments-notice-for-<%= post.id %>">
|
||||
This post has comments reported for moderation! (<%= pluralize(post.moderation_reports.visible(CurrentUser.user).recent.length, "report") %>)
|
||||
</span>
|
||||
</div>
|
||||
<% end %>
|
||||
<% if post.comments.hidden(CurrentUser.user).any? || (page == :comments && post.comments.size > 6) %>
|
||||
<div class="row hidden-comments-notice">
|
||||
<span class="info" id="threshold-comments-notice-for-<%= post.id %>">
|
||||
@@ -20,7 +13,7 @@
|
||||
|
||||
<div class="list-of-comments list-of-messages">
|
||||
<% if comments.present? %>
|
||||
<%= render_comment_list(comments, context: :index_by_post, moderation_reports: post.moderation_reports.visible(CurrentUser.user).recent) %>
|
||||
<%= render_comment_list(comments, context: :index_by_post) %>
|
||||
<% elsif post.last_commented_at.present? %>
|
||||
<p>There are no visible comments.</p>
|
||||
<% else %>
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
<%- # forum_post %>
|
||||
<%- # original_forum_post_id %>
|
||||
<%- # dtext_data %>
|
||||
<%- # moderation_reports %>
|
||||
|
||||
<div class="list-of-forum-posts list-of-messages">
|
||||
<% if moderation_reports.present? %>
|
||||
<div class="row moderation-forums-notice">
|
||||
<span class="info" id="moderation-forums-notice-for-topic">
|
||||
This topic has forum posts reported for moderation! (<%= pluralize(moderation_reports.length, "report") %>)
|
||||
</span>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<%= render ForumPostComponent.with_collection(forum_posts, original_forum_post_id: original_forum_post_id, dtext_data: dtext_data, moderation_reports: moderation_reports, current_user: CurrentUser.user) %>
|
||||
</div>
|
||||
@@ -27,7 +27,9 @@
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<%= render "forum_posts/listing", forum_posts: @forum_posts, original_forum_post_id: @forum_topic.original_post&.id, dtext_data: DText.preprocess(@forum_posts.map(&:body)), moderation_reports: @forum_topic.moderation_reports.visible(CurrentUser.user).recent %>
|
||||
<div class="list-of-forum-posts list-of-messages">
|
||||
<%= render ForumPostComponent.with_collection(@forum_posts, forum_topic: @forum_topic, current_user: CurrentUser.user) %>
|
||||
</div>
|
||||
|
||||
<% if policy(ForumPost.new(topic: @forum_topic)).create? %>
|
||||
<p><%= link_to "Post reply", new_forum_post_path(topic_id: @forum_topic.id), id: "new-response-link" %></p>
|
||||
|
||||
44
test/components/comment_component_test.rb
Normal file
44
test/components/comment_component_test.rb
Normal file
@@ -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
|
||||
44
test/components/forum_post_component_test.rb
Normal file
44
test/components/forum_post_component_test.rb
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user