diff --git a/app/controllers/moderation_reports_controller.rb b/app/controllers/moderation_reports_controller.rb index c7209b1ba..4345457c7 100644 --- a/app/controllers/moderation_reports_controller.rb +++ b/app/controllers/moderation_reports_controller.rb @@ -4,19 +4,22 @@ class ModerationReportsController < ApplicationController before_action :moderator_only, only: [:index] def new - check_privilege @moderation_report = ModerationReport.new(moderation_report_params) + check_privilege(@moderation_report) respond_with(@moderation_report) end def index - @moderation_reports = ModerationReport.paginated_search(params).includes(:creator, :model) + @moderation_reports = ModerationReport.paginated_search(params, count_pages: true).includes(:creator, :model) respond_with(@moderation_reports) end def create - check_privilege - @moderation_report = ModerationReport.create(moderation_report_params.merge(creator: CurrentUser.user)) + @moderation_report = ModerationReport.new(moderation_report_params.merge(creator: CurrentUser.user)) + check_privilege(@moderation_report) + @moderation_report.save + + flash.now[:notice] = @moderation_report.valid? ? "Report submitted" : @moderation_report.errors.full_messages.join("; ") respond_with(@moderation_report) end @@ -30,16 +33,8 @@ class ModerationReportsController < ApplicationController params.fetch(:moderation_report, {}).fetch(:model_id) end - def check_privilege - case model_type - when "User" - return if User.find(model_id).reportable_by?(CurrentUser.user) - when "Comment" - return if Comment.find(model_id).reportable_by?(CurrentUser.user) - when "ForumPost" - return if ForumPost.find(model_id).reportable_by?(CurrentUser.user) - end - raise User::PrivilegeError + def check_privilege(moderation_report) + raise User::PrivilegeError unless moderation_report.model.reportable_by?(CurrentUser.user) end def moderation_report_params diff --git a/app/models/application_record.rb b/app/models/application_record.rb index cee3d6133..777ab9011 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -421,6 +421,12 @@ class ApplicationRecord < ActiveRecord::Base end end + concerning :DtextMethods do + def dtext_shortlink + "#{self.class.name.underscore.tr("_", " ")} ##{id}" + end + end + concerning :AttributeMethods do class_methods do # Defines `_string`, `_string=`, and `=` diff --git a/app/models/comment.rb b/app/models/comment.rb index 7df4372b6..106248b16 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -129,7 +129,7 @@ class Comment < ApplicationRecord end def reportable_by?(user) - ModerationReport.enabled? && user.is_builder? && creator_id != user.id && !creator.is_moderator? + creator_id != user.id && !creator.is_moderator? end def voted_by?(user) diff --git a/app/models/dmail.rb b/app/models/dmail.rb index c1a9967b3..eb8ae5085 100644 --- a/app/models/dmail.rb +++ b/app/models/dmail.rb @@ -198,4 +198,8 @@ class Dmail < ApplicationRecord def visible_to?(user, key) owner_id == user.id || (user.is_moderator? && key == self.key) end + + def reportable_by?(user) + is_recipient? && !is_automated? && !from.is_moderator? + end end diff --git a/app/models/forum_post.rb b/app/models/forum_post.rb index 84c7cf9ed..b6eeb0d79 100644 --- a/app/models/forum_post.rb +++ b/app/models/forum_post.rb @@ -95,7 +95,7 @@ class ForumPost < ApplicationRecord end def reportable_by?(user) - ModerationReport.enabled? && user.is_builder? && creator_id != user.id && !creator.is_moderator? + creator_id != user.id && !creator.is_moderator? end def votable? @@ -223,4 +223,8 @@ class ForumPost < ApplicationRecord x.body = x.quoted_response end end + + def dtext_shortlink + "forum ##{id}" + end end diff --git a/app/models/forum_topic.rb b/app/models/forum_topic.rb index 9516c4e6c..bd0288604 100644 --- a/app/models/forum_topic.rb +++ b/app/models/forum_topic.rb @@ -177,8 +177,4 @@ class ForumTopic < ApplicationRecord def update_orignal_post original_post&.update_columns(:updater_id => updater.id, :updated_at => Time.now) end - - def viewable_moderation_reports - CurrentUser.is_moderator? ? moderation_reports.recent : [] - end end diff --git a/app/models/moderation_report.rb b/app/models/moderation_report.rb index 780e653d5..66da90265 100644 --- a/app/models/moderation_report.rb +++ b/app/models/moderation_report.rb @@ -3,9 +3,13 @@ class ModerationReport < ApplicationRecord belongs_to :creator, class_name: "User" validates :reason, presence: true + validates :model_type, inclusion: { in: %w[Comment Dmail ForumPost User] } + validates :creator, uniqueness: { scope: [:model_type, :model_id], message: "have already reported this message." } + after_create :create_forum_post! scope :user, -> { where(model_type: "User") } + scope :dmail, -> { where(model_type: "Dmail") } scope :comment, -> { where(model_type: "Comment") } scope :forum_post, -> { where(model_type: "ForumPost") } scope :recent, -> { where("moderation_reports.created_at >= ?", 1.week.ago) } @@ -35,14 +39,7 @@ class ModerationReport < ApplicationRecord def forum_post_message messages = ["[b]Submitted by:[/b] @#{creator.name}"] - case model_type - when "User" - messages << "[b]Submitted against:[/b] @#{model.name}" - when "Comment" - messages << "[b]Submitted against[/b]: comment ##{model_id}" - when "ForumPost" - messages << "[b]Submitted against[/b]: forum ##{model_id}" - end + messages << "[b]Submitted against:[/b] #{model.dtext_shortlink}" messages << "" messages << "[quote]" messages << "[b]Reason:[/b]" @@ -57,6 +54,10 @@ class ModerationReport < ApplicationRecord updater.update(forum_post_message) end + def self.visible(user = CurrentUser.user) + user.is_moderator? ? all : none + end + def self.search(params) q = super q = q.search_attributes(params, :model_type, :model_id, :creator_id) diff --git a/app/models/post.rb b/app/models/post.rb index 55d3f1d24..bdcfa2632 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1782,8 +1782,4 @@ class Post < ApplicationRecord save end - - def viewable_moderation_reports - CurrentUser.is_moderator? ? moderation_reports.recent : [] - end end diff --git a/app/models/user.rb b/app/models/user.rb index 5bd0e8986..4e1c96c40 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -827,7 +827,7 @@ class User < ApplicationRecord @presenter ||= UserPresenter.new(self) end - def viewable_moderation_reports - CurrentUser.is_moderator? ? moderation_reports.recent : [] + def dtext_shortlink + "<@#{name}>" end end diff --git a/app/views/comments/index_for_post.js.erb b/app/views/comments/index_for_post.js.erb index 622567c51..bc48cb554 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: @post.viewable_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.moderation_reports.visible.recent })) %>"); $(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 20a2f3422..78a9d7d45 100644 --- a/app/views/comments/partials/index/_list.html.erb +++ b/app/views/comments/partials/index/_list.html.erb @@ -3,10 +3,10 @@ <%= render "comments/partials/index/header", :post => post %> <% end %> - <% if post.viewable_moderation_reports.present? %> + <% if post.moderation_reports.visible.recent.present? %>
- This post has comments reported for moderation! (<%= pluralize(post.viewable_moderation_reports.length, "report") %>) + This post has comments reported for moderation! (<%= pluralize(post.moderation_reports.visible.recent.length, "report") %>)
<% end %> @@ -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: post.viewable_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.moderation_reports.visible.recent } %> <% elsif post.last_commented_at.present? %>

There are no visible comments.

<% else %> diff --git a/app/views/comments/partials/show/_comment.html.erb b/app/views/comments/partials/show/_comment.html.erb index 8205ae503..a0ff5f2eb 100644 --- a/app/views/comments/partials/show/_comment.html.erb +++ b/app/views/comments/partials/show/_comment.html.erb @@ -57,7 +57,7 @@ <%= link_to "Unvote", comment_comment_votes_path(comment_id: comment.id), method: :delete, remote: true %> <% if comment.reportable_by?(CurrentUser.user) %> -
  • <%= link_to "Report comment", new_moderation_report_path(moderation_report: { model_type: "Comment", model_id: comment.id }), remote: true %>
  • +
  • <%= link_to "Report", new_moderation_report_path(moderation_report: { model_type: "Comment", model_id: comment.id }), remote: true %>
  • <% end %> <% if comment.editable_by?(CurrentUser.user) %> diff --git a/app/views/dmails/show.html.erb b/app/views/dmails/show.html.erb index 0d24a74b8..28ea7361c 100644 --- a/app/views/dmails/show.html.erb +++ b/app/views/dmails/show.html.erb @@ -33,10 +33,13 @@ <% if @dmail.is_spam? %> | <%= link_to "Not spam", ham_dmail_path(@dmail), remote: :true, method: :post %> <% else %> - | <%= link_to "Mark as spam", spam_dmail_path(@dmail), remote: :true, method: :post %> + | <%= link_to "Spam", spam_dmail_path(@dmail), remote: :true, method: :post %> <% end %> <% end %> + <% if @dmail.reportable_by?(CurrentUser.user) %> + | <%= link_to "Report", new_moderation_report_path(moderation_report: { model_type: "Dmail", model_id: @dmail.id }), remote: true, title: "Report this dmail to the moderators" %> + <% end %>

    diff --git a/app/views/forum_posts/_forum_post.html.erb b/app/views/forum_posts/_forum_post.html.erb index 32fb3c166..47c2bb49e 100644 --- a/app/views/forum_posts/_forum_post.html.erb +++ b/app/views/forum_posts/_forum_post.html.erb @@ -38,7 +38,7 @@ <% end %> <% end %> <% if forum_post.reportable_by?(CurrentUser.user) %> -
  • <%= link_to "Report forum", new_moderation_report_path(moderation_report: { model_type: "ForumPost", model_id: forum_post.id }), remote: true %>
  • +
  • <%= 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" %>
  • <% end %> <% if forum_post.votable? %>
      diff --git a/app/views/forum_topics/show.html.erb b/app/views/forum_topics/show.html.erb index e8bd99d32..4c04b7eb5 100644 --- a/app/views/forum_topics/show.html.erb +++ b/app/views/forum_topics/show.html.erb @@ -26,7 +26,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: @forum_topic.viewable_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.moderation_reports.visible.recent %> <% if CurrentUser.is_member? %> <% if CurrentUser.is_moderator? || !@forum_topic.is_locked? %> diff --git a/app/views/moderation_reports/create.js.erb b/app/views/moderation_reports/create.js.erb index 755f3d075..33e458fc7 100644 --- a/app/views/moderation_reports/create.js.erb +++ b/app/views/moderation_reports/create.js.erb @@ -1 +1 @@ -Danbooru.notice("Report submitted."); \ No newline at end of file +Danbooru.notice("<%= j flash[:notice] %>"); diff --git a/app/views/moderation_reports/index.html.erb b/app/views/moderation_reports/index.html.erb index 93507dbd1..dcf471a19 100644 --- a/app/views/moderation_reports/index.html.erb +++ b/app/views/moderation_reports/index.html.erb @@ -5,10 +5,10 @@ <% t.column "Reported", width: "10%" do |report| %> <% if report.model_type == "User" %> <%= link_to_user report.model %> - <% elsif report.model_type == "Comment" %> - <%= link_to "comment ##{report.model_id}", comment_path(report.model_id) %> - <% elsif report.model_type == "ForumPost" %> - <%= link_to "forum ##{report.model_id}", forum_post_path(report.model_id) %> + <% elsif report.model_type == "Dmail" %> + <%= link_to report.model.dtext_shortlink, dmail_path(report.model, key: report.model.key) %> + <% else %> + <%= link_to report.model.dtext_shortlink, report.model %> <% end %> <% end %> <% t.column "Reason" do |report| %> diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 00cbc3c4d..63fcc8ef3 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -5,10 +5,10 @@

      <%= link_to_user @user %>

      - <% if @user.viewable_moderation_reports.present? %> + <% if @user.moderation_reports.visible.recent.present? %>
      - This user has been reported for moderation! (<%= pluralize(@user.viewable_moderation_reports.length, "report") %>) + This user has been reported for moderation! (<%= pluralize(@user.moderation_reports.visible.recent.length, "report") %>)
      <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 7d3801abc..e6f86ff2e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -37,6 +37,8 @@ en: url: "" forum_post_vote: creator_id: "Your vote" + moderation_report: + creator: "You" post: approver: "You" approver_id: "You"