posts: clean up delete! method.

* Remove unused `ban` and `without_mod_action` options.

* Don't try to set the `is_banned` flag during deletion.

* Don't create modactions for automatic "unapproved in 3 days"
  deletions, only to delete them after the fact.
This commit is contained in:
evazion
2020-08-03 20:00:47 -05:00
parent bca1f122d0
commit 157cb96551
4 changed files with 18 additions and 46 deletions

View File

@@ -61,7 +61,7 @@ class PostsController < ApplicationController
if params[:commit] == "Delete"
move_favorites = params.dig(:post, :move_favorites).to_s.truthy?
@post.delete!(params.dig(:post, :reason), move_favorites: move_favorites)
@post.delete!(params.dig(:post, :reason), move_favorites: move_favorites, user: CurrentUser.user)
flash[:notice] = "Post deleted"
end

View File

@@ -2,36 +2,19 @@ class PostPruner
def prune!
prune_pending!
prune_flagged!
prune_mod_actions!
end
protected
def prune_pending!
CurrentUser.scoped(User.system, "127.0.0.1") do
Post.where("is_deleted = ? and is_pending = ? and created_at < ?", false, true, 3.days.ago).each do |post|
post.delete!("Unapproved in three days")
rescue PostFlag::Error
# swallow
end
Post.pending.expired.each do |post|
post.delete!("Unapproved in three days", user: User.system)
end
end
def prune_flagged!
CurrentUser.scoped(User.system, "127.0.0.1") do
Post.where("is_deleted = ? and is_flagged = ?", false, true).each do |post|
if post.flags.unresolved.old.any?
begin
post.delete!("Unapproved in three days after returning to moderation queue")
rescue PostFlag::Error
# swallow
end
end
Post.flagged.each do |post|
if post.flags.unresolved.old.any?
post.delete!("Unapproved in three days after returning to moderation queue", user: User.system)
end
end
end
def prune_mod_actions!
ModAction.where(["creator_id = ? and description like ?", User.system.id, "deleted post %"]).destroy_all
end
end

View File

@@ -63,6 +63,7 @@ class Post < ApplicationRecord
scope :banned, -> { where(is_banned: true) }
scope :active, -> { where(is_pending: false, is_deleted: false, is_flagged: false) }
scope :pending_or_flagged, -> { pending.or(flagged) }
scope :expired, -> { where("posts.created_at < ?", 3.days.ago) }
scope :unflagged, -> { where(is_flagged: false) }
scope :has_notes, -> { where.not(last_noted_at: nil) }
@@ -950,7 +951,7 @@ class Post < ApplicationRecord
Post.find(parent_id_before_last_save).update_has_children_flag if parent_id_before_last_save.present?
end
def give_favorites_to_parent(options = {})
def give_favorites_to_parent
return if parent.nil?
transaction do
@@ -960,9 +961,7 @@ class Post < ApplicationRecord
end
end
unless options[:without_mod_action]
ModAction.log("moved favorites from post ##{id} to post ##{parent.id}", :post_move_favorites)
end
ModAction.log("moved favorites from post ##{id} to post ##{parent.id}", :post_move_favorites)
end
def has_visible_children?
@@ -1010,29 +1009,19 @@ class Post < ApplicationRecord
ModAction.log("unbanned post ##{id}", :post_unban)
end
def delete!(reason, options = {})
if is_status_locked?
self.errors.add(:is_status_locked, "; cannot delete post")
return false
end
def delete!(reason, move_favorites: false, user: CurrentUser.user)
transaction do
automated = (user == User.system)
Post.transaction do
flag!(reason, is_deletion: true)
update(
is_deleted: true,
is_pending: false,
is_flagged: false,
is_banned: is_banned || options[:ban] || has_tag?("banned_artist")
)
flags.create!(reason: reason, is_deletion: true, creator: user)
update!(is_deleted: true, is_pending: false, is_flagged: false)
# XXX This must happen *after* the `is_deleted` flag is set to true (issue #3419).
give_favorites_to_parent(options) if options[:move_favorites]
give_favorites_to_parent if move_favorites
is_automatic = (reason == "Unapproved in three days")
uploader.upload_limit.update_limit!(self, incremental: is_automatic)
uploader.upload_limit.update_limit!(self, incremental: automated)
unless options[:without_mod_action]
unless automated
ModAction.log("deleted post ##{id}, reason: #{reason}", :post_delete)
end
end

View File

@@ -13,7 +13,7 @@ class PostFlag < ApplicationRecord
validates :reason, presence: true, length: { in: 1..140 }
validate :validate_creator_is_not_limited, on: :create
validate :validate_post
validates_uniqueness_of :creator_id, :scope => :post_id, :on => :create, :unless => :is_deletion, :message => "have already flagged this post"
validates_uniqueness_of :creator_id, scope: :post_id, on: :create, unless: :is_deletion, message: "have already flagged this post"
before_save :update_post
attr_accessor :is_deletion