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
31
32
33
34
35
36
37
38
# File 'app/jobs/regular/sync_topic_user_bookmarked.rb', line 5

def execute(args = {})
  raise Discourse::InvalidParameters.new(:topic_id) if args[:topic_id].blank?

  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