2021-04-13 19:10:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Jobs
|
2022-03-29 22:43:11 -04:00
|
|
|
|
|
|
|
# TODO (martin) [POLYBOOK] This will need to be restructured for polymorphic
|
|
|
|
# bookmarks when edge cases are handled.
|
2021-04-13 19:10:53 -04:00
|
|
|
class SyncTopicUserBookmarked < ::Jobs::Base
|
|
|
|
def execute(args = {})
|
|
|
|
topic_id = args[:topic_id]
|
|
|
|
|
|
|
|
DB.exec(<<~SQL, topic_id: topic_id)
|
|
|
|
UPDATE topic_users SET bookmarked = true
|
|
|
|
FROM bookmarks AS b
|
2021-06-15 18:30:40 -04:00
|
|
|
INNER JOIN posts ON posts.id = b.post_id
|
2021-04-13 19:10:53 -04:00
|
|
|
WHERE NOT topic_users.bookmarked AND
|
2021-06-15 18:30:40 -04:00
|
|
|
posts.deleted_at IS NULL AND
|
2021-09-14 20:16:54 -04:00
|
|
|
topic_users.topic_id = posts.topic_id AND
|
2021-04-13 19:10:53 -04:00
|
|
|
topic_users.user_id = b.user_id #{topic_id.present? ? "AND topic_users.topic_id = :topic_id" : ""}
|
|
|
|
SQL
|
|
|
|
|
|
|
|
DB.exec(<<~SQL, topic_id: topic_id)
|
|
|
|
UPDATE topic_users SET bookmarked = false
|
|
|
|
WHERE topic_users.bookmarked AND
|
|
|
|
(
|
2021-06-15 18:30:40 -04:00
|
|
|
SELECT COUNT(*)
|
|
|
|
FROM bookmarks
|
|
|
|
INNER JOIN posts ON posts.id = bookmarks.post_id
|
2021-09-14 20:16:54 -04:00
|
|
|
WHERE posts.topic_id = topic_users.topic_id
|
2021-06-15 18:30:40 -04:00
|
|
|
AND bookmarks.user_id = topic_users.user_id
|
|
|
|
AND posts.deleted_at IS NULL
|
2021-04-13 19:10:53 -04:00
|
|
|
) = 0 #{topic_id.present? ? "AND topic_users.topic_id = :topic_id" : ""}
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|