Merge pull request #4267 from BrokenEagle/add-report-function

Create the ability to send reports to moderators
This commit is contained in:
evazion
2020-01-21 00:08:31 -06:00
committed by GitHub
32 changed files with 418 additions and 7 deletions

View File

@@ -7,6 +7,7 @@ class Comment < ApplicationRecord
belongs_to :post
belongs_to_creator
belongs_to_updater
has_many :moderation_reports, as: :model
has_many :votes, :class_name => "CommentVote", :dependent => :destroy
after_create :update_last_commented_at_on_create
after_update(:if => ->(rec) {(!rec.is_deleted? || !rec.saved_change_to_is_deleted?) && CurrentUser.id != rec.creator_id}) do |rec|
@@ -127,6 +128,10 @@ class Comment < ApplicationRecord
updater_id == user.id || user.is_moderator?
end
def reportable_by?(user)
user.is_builder? && creator_id != user.id && !creator.is_moderator?
end
def voted_by?(user)
return false if user.is_anonymous?
user.id.in?(votes.map(&:user_id))

View File

@@ -6,6 +6,7 @@ class ForumPost < ApplicationRecord
belongs_to_updater
belongs_to :topic, :class_name => "ForumTopic"
has_many :dtext_links, as: :model, dependent: :destroy
has_many :moderation_reports, as: :model
has_many :votes, class_name: "ForumPostVote"
has_one :tag_alias
has_one :tag_implication
@@ -93,6 +94,10 @@ class ForumPost < ApplicationRecord
bulk_update_request || tag_alias || tag_implication
end
def reportable_by?(user)
user.is_builder? && creator_id != user.id && !creator.is_moderator?
end
def votable?
TagAlias.where(forum_post_id: id).exists? ||
TagImplication.where(forum_post_id: id).exists? ||

View File

@@ -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
@@ -181,4 +182,8 @@ class ForumTopic < ApplicationRecord
def update_orignal_post
original_post&.update_columns(:updater_id => CurrentUser.id, :updated_at => Time.now)
end
def viewable_moderation_reports
CurrentUser.is_moderator? ? moderation_reports.recent : []
end
end

View File

@@ -0,0 +1,61 @@
class ModerationReport < ApplicationRecord
belongs_to :model, polymorphic: true
belongs_to_creator
validates :reason, presence: true
after_create :create_forum_post!
scope :user, -> { where(model_type: "User") }
scope :comment, -> { where(model_type: "Comment") }
scope :forum_post, -> { where(model_type: "ForumPost") }
scope :recent, -> { where("moderation_reports.created_at >= ?", 1.week.ago) }
def forum_topic_title
"Reports requiring moderation"
end
def forum_topic_body
"This topic deals with moderation events as reported by Builders. Reports can be filed against users, comments, or forum posts."
end
def forum_topic
topic = ForumTopic.find_by_title(forum_topic_title)
if topic.nil?
topic = CurrentUser.as_system do
ForumTopic.create(title: forum_topic_title, category_id: 0, min_level: User::Levels::MODERATOR, original_post_attributes: {body: forum_topic_body})
end
end
topic
end
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 << ""
messages << "[quote]"
messages << "[b]Reason:[/b]"
messages << ""
messages << reason
messages << "[/quote]"
messages.join("\n")
end
def create_forum_post!
updater = ForumUpdater.new(forum_topic)
updater.update(forum_post_message)
end
def self.search(params)
q = super
q = q.search_attributes(params, :model_type, :model_id, :creator_id)
q.apply_default_order(params)
end
end

View File

@@ -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
@@ -1811,4 +1812,8 @@ class Post < ApplicationRecord
save
end
def viewable_moderation_reports
CurrentUser.is_moderator? ? moderation_reports.recent : []
end
end

View File

@@ -93,6 +93,7 @@ class User < ApplicationRecord
has_many :wiki_page_versions, foreign_key: :updater_id
has_many :feedback, :class_name => "UserFeedback", :dependent => :destroy
has_many :forum_post_votes, dependent: :destroy, foreign_key: :creator_id
has_many :moderation_reports, as: :model
has_many :posts, :foreign_key => "uploader_id"
has_many :post_appeals, foreign_key: :creator_id
has_many :post_approvals, :dependent => :destroy
@@ -791,6 +792,10 @@ class User < ApplicationRecord
end
end
def reportable_by?(user)
user.is_builder? && id != user.id && !is_moderator?
end
def hide_favorites?
!CurrentUser.is_admin? && enable_private_favorites? && CurrentUser.user.id != id
end
@@ -807,4 +812,8 @@ class User < ApplicationRecord
def presenter
@presenter ||= UserPresenter.new(self)
end
def viewable_moderation_reports
CurrentUser.is_moderator? ? moderation_reports.recent : []
end
end