Class: Jobs::SyncTopicUserBookmarked

Inherits:
Base
  • Object
show all
Defined in:
app/jobs/regular/sync_topic_user_bookmarked.rb

Instance Method Summary collapse

Methods inherited from Base

acquire_cluster_concurrency_lock!, clear_cluster_concurrency_lock!, cluster_concurrency, cluster_concurrency_redis_key, delayed_perform, #error_context, get_cluster_concurrency, #last_db_duration, #log, #perform, #perform_immediately

Instance Method Details

#execute(args = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/jobs/regular/sync_topic_user_bookmarked.rb', line 5

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
    INNER JOIN posts ON posts.id = b.bookmarkable_id AND b.bookmarkable_type = 'Post'
    WHERE NOT topic_users.bookmarked AND
      posts.deleted_at IS NULL AND
      topic_users.topic_id = posts.topic_id AND
      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
      (
        SELECT COUNT(*)
        FROM bookmarks
        INNER JOIN posts ON posts.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Post'
        WHERE posts.topic_id = topic_users.topic_id
        AND bookmarks.user_id = topic_users.user_id
        AND posts.deleted_at IS NULL
    ) = 0 #{topic_id.present? ? "AND topic_users.topic_id = :topic_id" : ""}
  SQL
end