diff --git a/app/models/forum_topic.rb b/app/models/forum_topic.rb index 2f47dfe30..4383f618d 100644 --- a/app/models/forum_topic.rb +++ b/app/models/forum_topic.rb @@ -14,6 +14,7 @@ class ForumTopic < ApplicationRecord belongs_to_creator belongs_to_updater has_many :posts, -> {order("forum_posts.id asc")}, :class_name => "ForumPost", :foreign_key => "topic_id", :dependent => :destroy + has_many :moderation_reports, through: :posts has_one :original_post, -> {order("forum_posts.id asc")}, class_name: "ForumPost", foreign_key: "topic_id", inverse_of: :topic has_many :subscriptions, :class_name => "ForumSubscription" before_validation :initialize_is_deleted, :on => :create @@ -182,8 +183,7 @@ class ForumTopic < ApplicationRecord original_post&.update_columns(:updater_id => CurrentUser.id, :updated_at => Time.now) end - def moderation_reports - posts_with_reports = posts.joins(:moderation_reports).includes(:moderation_reports).distinct - posts_with_reports.reduce([]) {|arr,post| arr + post.moderation_reports} + def viewable_moderation_reports + CurrentUser.is_moderator? ? moderation_reports : [] end end diff --git a/app/models/post.rb b/app/models/post.rb index 3f66bc21c..f93c7309e 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -49,6 +49,7 @@ 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 @@ -1812,10 +1813,7 @@ class Post < ApplicationRecord save end - def moderation_reports - @moderation_reports ||= begin - comments_with_reports = comments.joins(:moderation_reports).includes(:moderation_reports).distinct - comments_with_reports.reduce([]) {|arr,comment| arr + comment.moderation_reports} - end + def viewable_moderation_reports + CurrentUser.is_moderator? ? moderation_reports : [] end end diff --git a/app/models/user.rb b/app/models/user.rb index 6733a8b15..0ae440aca 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -812,4 +812,8 @@ class User < ApplicationRecord def presenter @presenter ||= UserPresenter.new(self) end + + def viewable_moderation_reports + !is_moderator? && CurrentUser.is_moderator? ? moderation_reports : [] + end end diff --git a/app/views/comments/index_for_post.js.erb b/app/views/comments/index_for_post.js.erb index b92960a47..622567c51 100644 --- a/app/views/comments/index_for_post.js.erb +++ b/app/views/comments/index_for_post.js.erb @@ -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(partial: 'comments/partials/show/comment', collection: @comments, locals: { context: :index_for_post, dtext_data: DText.preprocess(@comments.map(&:body)), moderation_reports: (CurrentUser.is_moderator? ? @post.moderation_reports : []) })) %>"); +current_comment_section.html("<%= j(render(partial: 'comments/partials/show/comment', collection: @comments, locals: { context: :index_for_post, dtext_data: DText.preprocess(@comments.map(&:body)), moderation_reports: @post.viewable_moderation_reports })) %>"); $(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 index b9ab4de0e..9a02546eb 100644 --- a/app/views/comments/partials/index/_list.html.erb +++ b/app/views/comments/partials/index/_list.html.erb @@ -3,7 +3,7 @@ <%= render "comments/partials/index/header", :post => post %> <% end %> - <% if CurrentUser.is_moderator? && post.moderation_reports.present? %> + <% if post.viewable_moderation_reports.present? %>
This post has comments reported for moderation! (<%= post.moderation_reports.length %> <%= (post.moderation_reports.length == 1 ? "report" : "reports") %>) @@ -20,7 +20,7 @@
<% if comments.present? %> - <%= render partial: "comments/partials/show/comment", collection: comments, locals: { context: :index_by_post, dtext_data: DText.preprocess(comments.map(&:body)), moderation_reports: (CurrentUser.is_moderator? ? post.moderation_reports : []) } %> + <%= render partial: "comments/partials/show/comment", collection: comments, locals: { context: :index_by_post, dtext_data: DText.preprocess(comments.map(&:body)), moderation_reports: post.viewable_moderation_reports } %> <% elsif post.last_commented_at.present? %>

There are no visible comments.

<% else %> diff --git a/app/views/forum_posts/_listing.html.erb b/app/views/forum_posts/_listing.html.erb index 7242801a5..778ce0fb1 100644 --- a/app/views/forum_posts/_listing.html.erb +++ b/app/views/forum_posts/_listing.html.erb @@ -4,7 +4,7 @@ <%- # moderation_reports %>
- <% if CurrentUser.is_moderator? && moderation_reports.present? %> + <% if moderation_reports.present? %>
This topic has forum posts reported for moderation! (<%= moderation_reports.length %> <%= (moderation_reports.length == 1 ? "report" : "reports") %>) diff --git a/app/views/forum_topics/show.html.erb b/app/views/forum_topics/show.html.erb index 8d662875c..5bab6d482 100644 --- a/app/views/forum_topics/show.html.erb +++ b/app/views/forum_topics/show.html.erb @@ -20,7 +20,7 @@
<% 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: (CurrentUser.is_moderator? ? @forum_topic.moderation_reports : []) %> + <%= 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.viewable_moderation_reports %> <% if CurrentUser.is_member? %> <% if CurrentUser.is_moderator? || !@forum_topic.is_locked? %> diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index c28a5eae8..82ddeccdc 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -2,7 +2,7 @@

<%= link_to_user @user %>

- <% if !@user.is_moderator? && CurrentUser.is_moderator? && @user.moderation_reports.present? %> + <% if @user.viewable_moderation_reports.present? %>
This user has been reported for moderation! (<%= @user.moderation_reports.length %> <%= (@user.moderation_reports.length == 1 ? "report" : "reports") %>)