discourse/app/jobs/regular/sync_topic_user_bookmarked.rb
Martin Brennan a86833fe91
FIX: Deleting/recovering a post in topics caused bookmark side effects (#24226)
This commit fixes an issue where when some actions were done
(deleting/recovering post, moving posts) we updated the
topic_users.bookmarked column to the wrong value. This was happening
because the SyncTopicUserBookmarked job was not taking into account
Topic level bookmarks, so if there was a Topic bookmark and no
Post bookmarks for a user in the topic, they would have
topic_users.bookmarked set to false, which meant the bookmark would
no longer show in the /bookmarks list.

To reproduce before the fix:

* Bookmark a topic and don’t bookmark any posts within
* Delete or recover any post in the topic

c.f. https://meta.discourse.org/t/disappearing-bookmarks-and-expected-behavior-of-bookmarks/264670/36
2023-11-07 12:54:05 +10:00

41 lines
1.5 KiB
Ruby

# frozen_string_literal: true
module Jobs
class SyncTopicUserBookmarked < ::Jobs::Base
def execute(args = {})
raise Discourse::InvalidParameters.new(:topic_id) unless args[:topic_id].present?
DB.exec(<<~SQL, topic_id: args[:topic_id])
SELECT bookmarks.user_id, COUNT(*)
INTO TEMP TABLE tmp_sync_topic_user_bookmarks
FROM bookmarks
LEFT JOIN posts ON posts.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Post'
LEFT JOIN topics ON (topics.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Topic') OR
(topics.id = posts.topic_id)
WHERE (topics.id = :topic_id OR posts.topic_id = :topic_id)
AND posts.deleted_at IS NULL AND topics.deleted_at IS NULL
GROUP BY bookmarks.user_id;
UPDATE topic_users
SET bookmarked = true
FROM tmp_sync_topic_user_bookmarks
WHERE topic_users.user_id = tmp_sync_topic_user_bookmarks.user_id AND
topic_users.topic_id = :topic_id AND
tmp_sync_topic_user_bookmarks.count > 0;
UPDATE topic_users
SET bookmarked = false
FROM tmp_sync_topic_user_bookmarks
WHERE topic_users.topic_id = :topic_id AND
topic_users.bookmarked = true AND
topic_users.user_id NOT IN (
SELECT tmp_sync_topic_user_bookmarks.user_id
FROM tmp_sync_topic_user_bookmarks
);
DROP TABLE tmp_sync_topic_user_bookmarks;
SQL
end
end
end