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:
@@ -61,7 +61,7 @@ class PostsController < ApplicationController
|
|||||||
|
|
||||||
if params[:commit] == "Delete"
|
if params[:commit] == "Delete"
|
||||||
move_favorites = params.dig(:post, :move_favorites).to_s.truthy?
|
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"
|
flash[:notice] = "Post deleted"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -2,36 +2,19 @@ class PostPruner
|
|||||||
def prune!
|
def prune!
|
||||||
prune_pending!
|
prune_pending!
|
||||||
prune_flagged!
|
prune_flagged!
|
||||||
prune_mod_actions!
|
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
|
||||||
|
|
||||||
def prune_pending!
|
def prune_pending!
|
||||||
CurrentUser.scoped(User.system, "127.0.0.1") do
|
Post.pending.expired.each do |post|
|
||||||
Post.where("is_deleted = ? and is_pending = ? and created_at < ?", false, true, 3.days.ago).each do |post|
|
post.delete!("Unapproved in three days", user: User.system)
|
||||||
post.delete!("Unapproved in three days")
|
|
||||||
rescue PostFlag::Error
|
|
||||||
# swallow
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def prune_flagged!
|
def prune_flagged!
|
||||||
CurrentUser.scoped(User.system, "127.0.0.1") do
|
Post.flagged.each do |post|
|
||||||
Post.where("is_deleted = ? and is_flagged = ?", false, true).each do |post|
|
if post.flags.unresolved.old.any?
|
||||||
if post.flags.unresolved.old.any?
|
post.delete!("Unapproved in three days after returning to moderation queue", user: User.system)
|
||||||
begin
|
|
||||||
post.delete!("Unapproved in three days after returning to moderation queue")
|
|
||||||
rescue PostFlag::Error
|
|
||||||
# swallow
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def prune_mod_actions!
|
|
||||||
ModAction.where(["creator_id = ? and description like ?", User.system.id, "deleted post %"]).destroy_all
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -63,6 +63,7 @@ class Post < ApplicationRecord
|
|||||||
scope :banned, -> { where(is_banned: true) }
|
scope :banned, -> { where(is_banned: true) }
|
||||||
scope :active, -> { where(is_pending: false, is_deleted: false, is_flagged: false) }
|
scope :active, -> { where(is_pending: false, is_deleted: false, is_flagged: false) }
|
||||||
scope :pending_or_flagged, -> { pending.or(flagged) }
|
scope :pending_or_flagged, -> { pending.or(flagged) }
|
||||||
|
scope :expired, -> { where("posts.created_at < ?", 3.days.ago) }
|
||||||
|
|
||||||
scope :unflagged, -> { where(is_flagged: false) }
|
scope :unflagged, -> { where(is_flagged: false) }
|
||||||
scope :has_notes, -> { where.not(last_noted_at: nil) }
|
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?
|
Post.find(parent_id_before_last_save).update_has_children_flag if parent_id_before_last_save.present?
|
||||||
end
|
end
|
||||||
|
|
||||||
def give_favorites_to_parent(options = {})
|
def give_favorites_to_parent
|
||||||
return if parent.nil?
|
return if parent.nil?
|
||||||
|
|
||||||
transaction do
|
transaction do
|
||||||
@@ -960,9 +961,7 @@ class Post < ApplicationRecord
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
unless options[:without_mod_action]
|
ModAction.log("moved favorites from post ##{id} to post ##{parent.id}", :post_move_favorites)
|
||||||
ModAction.log("moved favorites from post ##{id} to post ##{parent.id}", :post_move_favorites)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def has_visible_children?
|
def has_visible_children?
|
||||||
@@ -1010,29 +1009,19 @@ class Post < ApplicationRecord
|
|||||||
ModAction.log("unbanned post ##{id}", :post_unban)
|
ModAction.log("unbanned post ##{id}", :post_unban)
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete!(reason, options = {})
|
def delete!(reason, move_favorites: false, user: CurrentUser.user)
|
||||||
if is_status_locked?
|
transaction do
|
||||||
self.errors.add(:is_status_locked, "; cannot delete post")
|
automated = (user == User.system)
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
Post.transaction do
|
flags.create!(reason: reason, is_deletion: true, creator: user)
|
||||||
flag!(reason, is_deletion: true)
|
update!(is_deleted: true, is_pending: false, is_flagged: false)
|
||||||
|
|
||||||
update(
|
|
||||||
is_deleted: true,
|
|
||||||
is_pending: false,
|
|
||||||
is_flagged: false,
|
|
||||||
is_banned: is_banned || options[:ban] || has_tag?("banned_artist")
|
|
||||||
)
|
|
||||||
|
|
||||||
# XXX This must happen *after* the `is_deleted` flag is set to true (issue #3419).
|
# 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: automated)
|
||||||
uploader.upload_limit.update_limit!(self, incremental: is_automatic)
|
|
||||||
|
|
||||||
unless options[:without_mod_action]
|
unless automated
|
||||||
ModAction.log("deleted post ##{id}, reason: #{reason}", :post_delete)
|
ModAction.log("deleted post ##{id}, reason: #{reason}", :post_delete)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ class PostFlag < ApplicationRecord
|
|||||||
validates :reason, presence: true, length: { in: 1..140 }
|
validates :reason, presence: true, length: { in: 1..140 }
|
||||||
validate :validate_creator_is_not_limited, on: :create
|
validate :validate_creator_is_not_limited, on: :create
|
||||||
validate :validate_post
|
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
|
before_save :update_post
|
||||||
attr_accessor :is_deletion
|
attr_accessor :is_deletion
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user