2021-04-13 19:10:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Jobs
|
|
|
|
class SyncTopicUserBookmarked < ::Jobs::Base
|
|
|
|
def execute(args = {})
|
2024-05-27 06:27:13 -04:00
|
|
|
raise Discourse::InvalidParameters.new(:topic_id) if args[:topic_id].blank?
|
2021-04-13 19:10:53 -04:00
|
|
|
|
2023-11-06 21:54:05 -05:00
|
|
|
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
|
|
|
|
);
|
2022-05-08 19:37:23 -04:00
|
|
|
|
2023-11-06 21:54:05 -05:00
|
|
|
DROP TABLE tmp_sync_topic_user_bookmarks;
|
2022-05-22 20:07:15 -04:00
|
|
|
SQL
|
2021-04-13 19:10:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|