forum: delete posts when topic is deleted.

Fix it so that when a forum topic is deleted, all posts in the topic are
deleted too. Also make it so that when a forum topic is undeleted, all
posts in it are undeleted too.

Before when a topic was deleted, only the topic itself was marked as
deleted, not the posts inside the topic. This meant that when a spam
topic was deleted, the OP wouldn't be marked as deleted, so any
modreports against it wouldn't be marked as handled.

Also change it so that it's not possible to undelete a post in a deleted
topic, or to delete the OP of a topic without deleting the topic itself.

Finally, add a fix script to delete all active posts in deleted topics,
and to undelete all deleted OPs in active topics.
This commit is contained in:
evazion
2022-01-21 20:16:32 -06:00
parent befdb87bd5
commit 56722df753
9 changed files with 104 additions and 39 deletions

View File

@@ -24,12 +24,13 @@ class ForumTopic < ApplicationRecord
has_many :tag_implications
validates :title, presence: true, length: { maximum: 200 }, if: :title_changed?
validates_associated :original_post
validates :category_id, inclusion: { in: CATEGORIES.keys }
validates :min_level, inclusion: { in: MIN_LEVELS.values }
accepts_nested_attributes_for :original_post
after_update :update_orignal_post
after_update :update_posts_on_deletion_or_undeletion
after_update :update_original_post
after_save(:if => ->(rec) {rec.is_locked? && rec.saved_change_to_is_locked?}) do |rec|
ModAction.log("locked forum topic ##{id} (title: #{title})", :forum_topic_lock)
end
@@ -179,7 +180,14 @@ class ForumTopic < ApplicationRecord
(response_count / Danbooru.config.posts_per_page.to_f).ceil
end
def update_orignal_post
# Delete all posts when the topic is deleted. Undelete all posts when the topic is undeleted.
def update_posts_on_deletion_or_undeletion
if saved_change_to_is_deleted?
forum_posts.update!(is_deleted: is_deleted) # XXX depends on current user
end
end
def update_original_post
original_post&.update_columns(:updater_id => updater.id, :updated_at => Time.now)
end