mirror of
https://github.com/discourse/discourse.git
synced 2025-05-31 01:27:15 +08:00
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:
@ -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
|
Reference in New Issue
Block a user