DEV: New option to always destroy posts. (#11898)

This commit is contained in:
Roman Rizzi
2021-02-01 18:57:31 -03:00
committed by GitHub
parent 4b3d34d3d4
commit e040de0c2c
4 changed files with 44 additions and 29 deletions

View File

@ -35,32 +35,9 @@ class UserDestroyer
category_topic_ids = Category.where("topic_id IS NOT NULL").pluck(:topic_id)
if opts[:delete_posts]
user.posts.each do |post|
# agree with flags
if opts[:delete_as_spammer] && reviewable = post.reviewable_flag
reviewable.perform(@actor, :agree_and_keep)
end
# block all external urls
if opts[:block_urls]
post.topic_links.each do |link|
next if link.internal
next if Oneboxer.engine(link.url) != Onebox::Engine::AllowlistedGenericOnebox
ScreenedUrl.watch(link.url, link.domain, ip_address: user.ip_address)&.record_match!
end
end
if post.is_first_post? && category_topic_ids.include?(post.topic_id)
post.update!(user: Discourse.system_user)
else
PostDestroyer.new(@actor.staff? ? @actor : Discourse.system_user, post).destroy
end
if post.topic && post.is_first_post?
Topic.unscoped.where(id: post.topic_id).update_all(user_id: nil)
end
end
agree_with_flags(user) if opts[:delete_as_spammer]
block_external_urls(user) if opts[:block_urls]
delete_posts(user, category_topic_ids, opts)
end
user.post_actions.each do |post_action|
@ -131,6 +108,33 @@ class UserDestroyer
protected
def block_external_urls(user)
TopicLink.where(user: user, internal: false).find_each do |link|
next if Oneboxer.engine(link.url) != Onebox::Engine::AllowlistedGenericOnebox
ScreenedUrl.watch(link.url, link.domain, ip_address: user.ip_address)&.record_match!
end
end
def agree_with_flags(user)
ReviewableFlaggedPost.where(target_created_by: user).find_each do |reviewable|
reviewable.perform(@actor, :agree_and_keep)
end
end
def delete_posts(user, category_topic_ids, opts)
user.posts.find_each do |post|
if post.is_first_post? && category_topic_ids.include?(post.topic_id)
post.update!(user: Discourse.system_user)
else
PostDestroyer.new(@actor.staff? ? @actor : Discourse.system_user, post).destroy
end
if post.topic && post.is_first_post?
Topic.unscoped.where(id: post.topic_id).update_all(user_id: nil)
end
end
end
def prepare_for_destroy(user)
PostAction.where(user_id: user.id).delete_all
UserAction.where('user_id = :user_id OR target_user_id = :user_id OR acting_user_id = :user_id', user_id: user.id).delete_all