Merge pull request #3496 from BrokenEagle/feat-mod-action-event-ids

Add categories to mod actions
This commit is contained in:
Albert Yi
2018-01-15 11:09:38 -08:00
committed by GitHub
25 changed files with 132 additions and 37 deletions

View File

@@ -67,6 +67,7 @@ class ForumTopicsController < ApplicationController
def destroy def destroy
check_privilege(@forum_topic) check_privilege(@forum_topic)
@forum_topic.delete! @forum_topic.delete!
@forum_topic.create_mod_action_for_delete
flash[:notice] = "Topic deleted" flash[:notice] = "Topic deleted"
respond_with(@forum_topic) respond_with(@forum_topic)
end end
@@ -74,6 +75,7 @@ class ForumTopicsController < ApplicationController
def undelete def undelete
check_privilege(@forum_topic) check_privilege(@forum_topic)
@forum_topic.undelete! @forum_topic.undelete!
@forum_topic.create_mod_action_for_undelete
flash[:notice] = "Topic undeleted" flash[:notice] = "Topic undeleted"
respond_with(@forum_topic) respond_with(@forum_topic)
end end

View File

@@ -7,7 +7,7 @@ class BulkRevert
def process(creator, constraints = {}) def process(creator, constraints = {})
@constraints = constraints @constraints = constraints
ModAction.log("Processed bulk revert for #{constraints.inspect} by #{creator.name}") ModAction.log("Processed bulk revert for #{constraints.inspect} by #{creator.name}",:bulk_revert)
CurrentUser.scoped(creator) do CurrentUser.scoped(creator) do
ApplicationRecord.without_timeout do ApplicationRecord.without_timeout do

View File

@@ -31,7 +31,7 @@ module Moderator
end end
end end
ModAction.log("processed mass update: #{antecedent} -> #{consequent}") ModAction.log("processed mass update: #{antecedent} -> #{consequent}",:mass_update)
end end
end end
end end

View File

@@ -30,7 +30,7 @@ class UserDeletion
private private
def create_mod_action def create_mod_action
ModAction.log("user ##{user.id} deleted") ModAction.log("user ##{user.id} deleted",:user_delete)
end end
def clear_saved_searches def clear_saved_searches

View File

@@ -37,11 +37,11 @@ private
def create_mod_actions def create_mod_actions
if old_can_approve_posts != user.can_approve_posts? if old_can_approve_posts != user.can_approve_posts?
ModAction.log("\"#{promoter.name}\":/users/#{promoter.id} changed approval privileges for \"#{user.name}\":/users/#{user.id} from #{old_can_approve_posts} to [b]#{user.can_approve_posts?}[/b]") ModAction.log("\"#{promoter.name}\":/users/#{promoter.id} changed approval privileges for \"#{user.name}\":/users/#{user.id} from #{old_can_approve_posts} to [b]#{user.can_approve_posts?}[/b]",:user_approval_privilege)
end end
if old_can_upload_free != user.can_upload_free? if old_can_upload_free != user.can_upload_free?
ModAction.log("\"#{promoter.name}\":/users/#{promoter.id} changed unlimited upload privileges for \"#{user.name}\":/users/#{user.id} from #{old_can_upload_free} to [b]#{user.can_upload_free?}[/b]") ModAction.log("\"#{promoter.name}\":/users/#{promoter.id} changed unlimited upload privileges for \"#{user.name}\":/users/#{user.id} from #{old_can_upload_free} to [b]#{user.can_upload_free?}[/b]",:user_upload_privilege)
end end
end end

View File

@@ -436,6 +436,7 @@ class Artist < ApplicationRecord
end end
update_column(:is_banned, false) update_column(:is_banned, false)
ModAction.log("unbanned artist ##{id}",:artist_unban)
end end
end end
end end
@@ -458,6 +459,7 @@ class Artist < ApplicationRecord
end end
update_column(:is_banned, true) update_column(:is_banned, true)
ModAction.log("banned artist ##{id}",:artist_ban)
end end
end end
end end

View File

@@ -126,6 +126,6 @@ class Ban < ApplicationRecord
end end
def create_mod_action def create_mod_action
ModAction.log(%{Banned "#{user_name}":/users/#{user_id} until #{expires_at}}) ModAction.log(%{Banned "#{user_name}":/users/#{user_id} until #{expires_at}},:user_ban)
end end
end end

View File

@@ -11,12 +11,12 @@ class Comment < ApplicationRecord
before_validation :initialize_creator, :on => :create before_validation :initialize_creator, :on => :create
before_validation :initialize_updater before_validation :initialize_updater
after_create :update_last_commented_at_on_create after_create :update_last_commented_at_on_create
after_update(:if => lambda {|rec| CurrentUser.id != rec.creator_id}) do |rec| after_update(:if => lambda {|rec| (!rec.is_deleted? || !rec.is_deleted_changed?) && CurrentUser.id != rec.creator_id}) do |rec|
ModAction.log("comment ##{rec.id} updated by #{CurrentUser.name}") ModAction.log("comment ##{rec.id} updated by #{CurrentUser.name}",:comment_update)
end end
after_save :update_last_commented_at_on_destroy, :if => lambda {|rec| rec.is_deleted? && rec.is_deleted_changed?} after_save :update_last_commented_at_on_destroy, :if => lambda {|rec| rec.is_deleted? && rec.is_deleted_changed?}
after_save(:if => lambda {|rec| rec.is_deleted? && rec.is_deleted_changed? && CurrentUser.id != rec.creator_id}) do |rec| after_save(:if => lambda {|rec| rec.is_deleted? && rec.is_deleted_changed? && CurrentUser.id != rec.creator_id}) do |rec|
ModAction.log("comment ##{rec.id} deleted by #{CurrentUser.name}") ModAction.log("comment ##{rec.id} deleted by #{CurrentUser.name}",:comment_delete)
end end
attr_accessible :body, :post_id, :do_not_bump_post, :is_deleted, :as => [:member, :gold, :platinum, :builder, :moderator, :admin] attr_accessible :body, :post_id, :do_not_bump_post, :is_deleted, :as => [:member, :gold, :platinum, :builder, :moderator, :admin]
attr_accessible :is_sticky, :as => [:moderator, :admin] attr_accessible :is_sticky, :as => [:moderator, :admin]

View File

@@ -20,10 +20,10 @@ class ForumPost < ApplicationRecord
before_destroy :validate_topic_is_unlocked before_destroy :validate_topic_is_unlocked
after_save :delete_topic_if_original_post after_save :delete_topic_if_original_post
after_update(:if => lambda {|rec| rec.updater_id != rec.creator_id}) do |rec| after_update(:if => lambda {|rec| rec.updater_id != rec.creator_id}) do |rec|
ModAction.log("#{CurrentUser.name} updated forum ##{rec.id}") ModAction.log("#{CurrentUser.name} updated forum ##{rec.id}",:forum_post_update)
end end
after_destroy(:if => lambda {|rec| rec.updater_id != rec.creator_id}) do |rec| after_destroy(:if => lambda {|rec| rec.updater_id != rec.creator_id}) do |rec|
ModAction.log("#{CurrentUser.name} deleted forum ##{rec.id}") ModAction.log("#{CurrentUser.name} deleted forum ##{rec.id}",:forum_post_delete)
end end
mentionable( mentionable(
:message_field => :body, :message_field => :body,

View File

@@ -28,6 +28,9 @@ class ForumTopic < ApplicationRecord
validates :title, :length => {:maximum => 255} validates :title, :length => {:maximum => 255}
accepts_nested_attributes_for :original_post accepts_nested_attributes_for :original_post
after_update :update_orignal_post after_update :update_orignal_post
after_save(:if => lambda {|rec| rec.is_locked? && rec.is_locked_changed?}) do |rec|
ModAction.log("locked forum topic ##{id} (title: #{title})",:forum_topic_lock)
end
module CategoryMethods module CategoryMethods
extend ActiveSupport::Concern extend ActiveSupport::Concern
@@ -154,6 +157,14 @@ class ForumTopic < ApplicationRecord
user.level >= min_level user.level >= min_level
end end
def create_mod_action_for_delete
ModAction.log("deleted forum topic ##{id} (title: #{title})",:forum_topic_delete)
end
def create_mod_action_for_undelete
ModAction.log("undeleted forum topic ##{id} (title: #{title})",:forum_topic_undelete)
end
def initialize_is_deleted def initialize_is_deleted
self.is_deleted = false if is_deleted.nil? self.is_deleted = false if is_deleted.nil?
end end

View File

@@ -7,10 +7,10 @@ class IpBan < ApplicationRecord
validates_uniqueness_of :ip_addr, :if => lambda {|rec| rec.ip_addr =~ IP_ADDR_REGEX} validates_uniqueness_of :ip_addr, :if => lambda {|rec| rec.ip_addr =~ IP_ADDR_REGEX}
attr_accessible :ip_addr, :reason attr_accessible :ip_addr, :reason
after_create do |rec| after_create do |rec|
ModAction.log("#{CurrentUser.name} created ip ban for #{rec.ip_addr}") ModAction.log("#{CurrentUser.name} created ip ban for #{rec.ip_addr}",:ip_ban_create)
end end
after_destroy do |rec| after_destroy do |rec|
ModAction.log("#{CurrentUser.name} deleted ip ban for #{rec.ip_addr}") ModAction.log("#{CurrentUser.name} deleted ip ban for #{rec.ip_addr}",:ip_ban_delete)
end end
def self.is_banned?(ip_addr) def self.is_banned?(ip_addr)

View File

@@ -2,7 +2,54 @@ class ModAction < ApplicationRecord
belongs_to :creator, :class_name => "User" belongs_to :creator, :class_name => "User"
before_validation :initialize_creator, :on => :create before_validation :initialize_creator, :on => :create
validates_presence_of :creator_id validates_presence_of :creator_id
attr_accessible :description attr_accessible :description, :category
#####DIVISIONS#####
#Groups: 0-999
#Individual: 1000-1999
#####Actions#####
#Create: 0
#Update: 1
#Delete: 2
#Undelete: 3
#Ban: 4
#Unban: 5
#Misc: 6-19
enum category: {
user_delete: 2,
user_ban: 4,
user_name_change: 6,
user_level: 7,
user_approval_privilege: 8,
user_upload_privilege: 9,
user_feedback_update: 21,
user_feedback_delete: 22,
post_delete: 42,
post_undelete: 43,
post_ban: 44,
post_unban: 45,
post_permanent_delete: 46,
pool_delete: 62,
pool_undelete: 63,
artist_ban: 184,
artist_unban: 185,
comment_update: 81,
comment_delete: 82,
forum_topic_delete: 202,
forum_topic_undelete: 203,
forum_topic_lock: 206,
forum_post_update: 101,
forum_post_delete: 102,
tag_alias_create: 120,
tag_alias_update: 121,
tag_implication_create: 140,
tag_implication_update: 141,
ip_ban_create: 160,
ip_ban_delete: 162,
mass_update: 1000,
bulk_revert: 1001,
other: 2000
}
def self.search(params = {}) def self.search(params = {})
q = where("true") q = where("true")
@@ -12,11 +59,27 @@ class ModAction < ApplicationRecord
q = q.where("creator_id = ?", params[:creator_id].to_i) q = q.where("creator_id = ?", params[:creator_id].to_i)
end end
if params[:creator_name].present?
q = q.where("creator_id = (select _.id from users _ where lower(_.name) = ?)", params[:creator_name].mb_chars.downcase)
end
if params[:category].present?
q = q.attribute_matches(:category, params[:category])
end
q q
end end
def self.log(desc) def category_id
create(:description => desc) self.class.categories[category]
end
def method_attributes
super + [:category_id]
end
def self.log(desc, cat = :other)
create(:description => desc,:category => categories[cat])
end end
def initialize_creator def initialize_creator

View File

@@ -218,15 +218,11 @@ class Pool < ApplicationRecord
end end
def create_mod_action_for_delete def create_mod_action_for_delete
ModAction.log("deleted pool ##{id} (name: #{name})") ModAction.log("deleted pool ##{id} (name: #{name})",:pool_delete)
end end
def create_mod_action_for_undelete def create_mod_action_for_undelete
ModAction.log("undeleted pool ##{id} (name: #{name})") ModAction.log("undeleted pool ##{id} (name: #{name})",:pool_undelete)
end
def create_mod_action_for_destroy
ModAction.log("permanently deleted pool ##{id} name=#{name} post_ids=#{post_ids}")
end end
def add!(post) def add!(post)

View File

@@ -1362,7 +1362,7 @@ class Post < ApplicationRecord
transaction do transaction do
Post.without_timeout do Post.without_timeout do
ModAction.log("permanently deleted post ##{id}") ModAction.log("permanently deleted post ##{id}",:post_permanent_delete)
give_favorites_to_parent give_favorites_to_parent
update_children_on_destroy update_children_on_destroy
@@ -1378,12 +1378,12 @@ class Post < ApplicationRecord
def ban! def ban!
update_column(:is_banned, true) update_column(:is_banned, true)
ModAction.log("banned post ##{id}") ModAction.log("banned post ##{id}",:post_ban)
end end
def unban! def unban!
update_column(:is_banned, false) update_column(:is_banned, false)
ModAction.log("unbanned post ##{id}") ModAction.log("unbanned post ##{id}",:post_unban)
end end
def delete!(reason, options = {}) def delete!(reason, options = {})
@@ -1406,7 +1406,7 @@ class Post < ApplicationRecord
give_favorites_to_parent if options[:move_favorites] give_favorites_to_parent if options[:move_favorites]
unless options[:without_mod_action] unless options[:without_mod_action]
ModAction.log("deleted post ##{id}, reason: #{reason}") ModAction.log("deleted post ##{id}, reason: #{reason}",:post_delete)
end end
end end
end end
@@ -1429,7 +1429,7 @@ class Post < ApplicationRecord
self.approver_id = CurrentUser.id self.approver_id = CurrentUser.id
flags.each {|x| x.resolve!} flags.each {|x| x.resolve!}
save save
ModAction.log("undeleted post ##{id}") ModAction.log("undeleted post ##{id}",:post_undelete)
end end
def replace!(params) def replace!(params)

View File

@@ -28,7 +28,7 @@ class PostApproval < ApplicationRecord
end end
def approve_post def approve_post
ModAction.log("undeleted post ##{post_id}") if post.is_deleted ModAction.log("undeleted post ##{post_id}",:post_undelete) if post.is_deleted
post.flags.each(&:resolve!) post.flags.each(&:resolve!)
post.update({ approver: user, is_flagged: false, is_pending: false, is_deleted: false }, without_protection: true) post.update({ approver: user, is_flagged: false, is_pending: false, is_deleted: false }, without_protection: true)

View File

@@ -253,7 +253,7 @@ class TagAlias < TagRelationship
alias_desc = %Q("tag alias ##{id}":[#{Rails.application.routes.url_helpers.tag_alias_path(self)}]: [[#{antecedent_name}]] -> [[#{consequent_name}]]) alias_desc = %Q("tag alias ##{id}":[#{Rails.application.routes.url_helpers.tag_alias_path(self)}]: [[#{antecedent_name}]] -> [[#{consequent_name}]])
if id_changed? if id_changed?
ModAction.log("created #{status} #{alias_desc}") ModAction.log("created #{status} #{alias_desc}",:tag_alias_create)
else else
# format the changes hash more nicely. # format the changes hash more nicely.
change_desc = changes.except(:updated_at).map do |attribute, values| change_desc = changes.except(:updated_at).map do |attribute, values|
@@ -265,7 +265,7 @@ class TagAlias < TagRelationship
end end
end.join(", ") end.join(", ")
ModAction.log("updated #{alias_desc}\n#{change_desc}") ModAction.log("updated #{alias_desc}\n#{change_desc}",:tag_alias_update)
end end
end end
end end

View File

@@ -194,7 +194,7 @@ class TagImplication < TagRelationship
implication = %Q("tag implication ##{id}":[#{Rails.application.routes.url_helpers.tag_implication_path(self)}]: [[#{antecedent_name}]] -> [[#{consequent_name}]]) implication = %Q("tag implication ##{id}":[#{Rails.application.routes.url_helpers.tag_implication_path(self)}]: [[#{antecedent_name}]] -> [[#{consequent_name}]])
if id_changed? if id_changed?
ModAction.log("created #{status} #{implication}") ModAction.log("created #{status} #{implication}",:tag_implication_create)
else else
# format the changes hash more nicely. # format the changes hash more nicely.
change_desc = changes.except(:updated_at).map do |attribute, values| change_desc = changes.except(:updated_at).map do |attribute, values|
@@ -206,7 +206,7 @@ class TagImplication < TagRelationship
end end
end.join(", ") end.join(", ")
ModAction.log("updated #{implication}\n#{change_desc}") ModAction.log("updated #{implication}\n#{change_desc}",:tag_implication_update)
end end
end end

View File

@@ -439,10 +439,10 @@ class User < ApplicationRecord
def create_mod_action def create_mod_action
if level_changed? if level_changed?
ModAction.log(%{"#{name}":/users/#{id} level changed #{level_string_was} -> #{level_string}}) ModAction.log(%{"#{name}":/users/#{id} level changed #{level_string_was} -> #{level_string}},:user_level)
end end
end end
def set_per_page def set_per_page
if per_page.nil? || !is_gold? if per_page.nil? || !is_gold?
self.per_page = Danbooru.config.posts_per_page self.per_page = Danbooru.config.posts_per_page

View File

@@ -11,10 +11,10 @@ class UserFeedback < ApplicationRecord
validate :user_is_not_creator validate :user_is_not_creator
after_create :create_dmail after_create :create_dmail
after_update(:if => lambda {|rec| CurrentUser.id != rec.creator_id}) do |rec| after_update(:if => lambda {|rec| CurrentUser.id != rec.creator_id}) do |rec|
ModAction.log(%{#{CurrentUser.name} updated user feedback for "#{rec.user_name}":/users/#{rec.user_id}}) ModAction.log(%{#{CurrentUser.name} updated user feedback for "#{rec.user_name}":/users/#{rec.user_id}},:user_feedback_update)
end end
after_destroy(:if => lambda {|rec| CurrentUser.id != rec.creator_id}) do |rec| after_destroy(:if => lambda {|rec| CurrentUser.id != rec.creator_id}) do |rec|
ModAction.log(%{#{CurrentUser.name} deleted user feedback for "#{rec.user_name}":/users/#{rec.user_id}}) ModAction.log(%{#{CurrentUser.name} deleted user feedback for "#{rec.user_name}":/users/#{rec.user_id}},:user_feedback_delete)
end end
module SearchMethods module SearchMethods

View File

@@ -0,0 +1,5 @@
<%= simple_form_for(:search, method: :get, url: mod_actions_path, defaults: { required: false }, html: { class: "inline-form" }) do |f| %>
<%= f.input :creator_name, label: "Creator", input_html: { value: params[:search][:creator_name] } %>
<%= f.input :category, label: "Category", collection: ModAction.categories.map {|k,v| [k.capitalize.tr("_"," "), v]}, include_blank: true,selected: params[:search][:category] %>
<%= f.submit "Search" %>
<% end %>

View File

@@ -1,6 +1,9 @@
<div id="c-mod-actions"> <div id="c-mod-actions">
<div id="a-index"> <div id="a-index">
<h1>Mod Actions</h1> <h1>Mod Actions</h1>
<%= render "search" %>
<table class="striped" width="100%"> <table class="striped" width="100%">
<thead> <thead>
<tr> <tr>

View File

@@ -0,0 +1,7 @@
class AddCategoryToModActions < ActiveRecord::Migration
def change
ModAction.without_timeout do
add_column :mod_actions, :category, :integer
end
end
end

View File

@@ -2315,7 +2315,8 @@ CREATE TABLE mod_actions (
creator_id integer NOT NULL, creator_id integer NOT NULL,
description text NOT NULL, description text NOT NULL,
created_at timestamp without time zone, created_at timestamp without time zone,
updated_at timestamp without time zone updated_at timestamp without time zone,
category integer
); );
@@ -7547,3 +7548,5 @@ INSERT INTO schema_migrations (version) VALUES ('20171219001521');
INSERT INTO schema_migrations (version) VALUES ('20171230220225'); INSERT INTO schema_migrations (version) VALUES ('20171230220225');
INSERT INTO schema_migrations (version) VALUES ('20180113211343');

View File

@@ -477,6 +477,7 @@ class PostTest < ActiveSupport::TestCase
should "create a mod action" do should "create a mod action" do
@post.undelete! @post.undelete!
assert_equal("undeleted post ##{@post.id}", ModAction.last.description) assert_equal("undeleted post ##{@post.id}", ModAction.last.description)
assert_equal("post_undelete", ModAction.last.category)
end end
end end
@@ -489,6 +490,7 @@ class PostTest < ActiveSupport::TestCase
should "create a mod action" do should "create a mod action" do
@post.approve! @post.approve!
assert_equal("undeleted post ##{@post.id}", ModAction.last.description) assert_equal("undeleted post ##{@post.id}", ModAction.last.description)
assert_equal("post_undelete", ModAction.last.category)
end end
end end

View File

@@ -55,6 +55,7 @@ class UserTest < ActiveSupport::TestCase
@user.invite!(User::Levels::BUILDER, "1") @user.invite!(User::Levels::BUILDER, "1")
end end
assert_equal(%{"#{@user.name}":/users/#{@user.id} level changed Member -> Builder}, ModAction.last.description) assert_equal(%{"#{@user.name}":/users/#{@user.id} level changed Member -> Builder}, ModAction.last.description)
assert_equal("user_level", ModAction.last.category)
end end
end end