FEATURE: Add weekly bookmark cleanup code (#10899)

When posts or topics are deleted we don't want to immediately delete associated bookmarks, so we have a grace period to recover them and their reminders if the post or topic is un-deleted. This PR adds a task to the Weekly scheduled job to go and delete bookmarks attached to posts or topics deleted > 3 days ago.
This commit is contained in:
Martin Brennan
2020-10-14 09:38:57 +10:00
committed by GitHub
parent 57095f0bb7
commit c3cede697d
3 changed files with 58 additions and 0 deletions
+14
View File
@@ -103,6 +103,20 @@ class Bookmark < ActiveRecord::Base
.order('date(bookmarks.created_at)')
.count
end
##
# Deletes bookmarks that are attached to posts/topics that were deleted
# more than X days ago. We don't delete bookmarks instantly when a post/topic
# is deleted so that there is a grace period to un-delete.
def self.cleanup!
grace_time = 3.days.ago
DB.exec(<<~SQL, grace_time: grace_time)
DELETE FROM bookmarks b
USING topics t, posts p
WHERE (b.topic_id = t.id AND b.post_id = p.id)
AND (t.deleted_at < :grace_time OR p.deleted_at < :grace_time)
SQL
end
end
# == Schema Information