FIX: Ensure topic user bookmarked synced on bookmark auto-delete (#10323)

For the following conditions, the TopicUser.bookmarked column was not updated correctly:

* When a bookmark was auto-deleted because the reminder was sent
* When a bookmark was auto-deleted because the owner of the bookmark replied to the topic

This adds another migration to fix the out-of-sync column and also some refactors to BookmarkManager to allow for more of these delete cases. BookmarkManager is used instead of directly destroying the bookmark in PostCreator and BookmarkReminderNotificationHandler.
This commit is contained in:
Martin Brennan
2020-07-29 09:43:32 +10:00
committed by GitHub
parent 2ea17c06dd
commit 9e5b213089
7 changed files with 92 additions and 23 deletions

View File

@ -0,0 +1,30 @@
# frozen_string_literal: true
class SyncTopicUserBookmarkedColumnWithBookmarks < ActiveRecord::Migration[6.0]
def up
should_be_bookmarked_sql = <<~SQL
UPDATE topic_users SET bookmarked = true WHERE id IN (
SELECT topic_users.id
FROM topic_users
INNER JOIN bookmarks ON bookmarks.user_id = topic_users.user_id AND
bookmarks.topic_id = topic_users.topic_id
WHERE NOT topic_users.bookmarked
) AND NOT bookmarked
SQL
DB.exec(should_be_bookmarked_sql)
# post_action_type_id 1 is bookmark
should_not_be_bookmarked_sql = <<~SQL
UPDATE topic_users SET bookmarked = FALSE WHERE ID IN (
SELECT DISTINCT topic_users.id FROM topic_users
LEFT JOIN bookmarks ON bookmarks.topic_id = topic_users.topic_id AND bookmarks.user_id = topic_users.user_id
LEFT JOIN post_actions ON post_actions.user_id = topic_users.user_id AND post_actions.post_action_type_id = 1 AND post_actions.post_id IN (SELECT id FROM posts WHERE posts.topic_id = topic_users.topic_id)
WHERE topic_users.bookmarked = true AND (bookmarks.id IS NULL AND post_actions.id IS NULL))
SQL
DB.exec(should_not_be_bookmarked_sql)
end
def down
raise ActiveRecord::IrreversibleMigration
end
end