Class: TopicBookmarkable

Inherits:
BaseBookmarkable show all
Includes:
TopicPostBookmarkableHelper
Defined in:
app/services/topic_bookmarkable.rb

Instance Attribute Summary

Attributes inherited from BaseBookmarkable

#model, #preload_associations, #serializer

Class Method Summary collapse

Methods inherited from BaseBookmarkable

has_preloads?, send_reminder_notification

Class Method Details

.after_create(guardian, bookmark, opts) ⇒ Object



76
77
78
# File 'app/services/topic_bookmarkable.rb', line 76

def self.after_create(guardian, bookmark, opts)
  sync_topic_user_bookmarked(guardian.user, bookmark.bookmarkable, opts)
end

.after_destroy(guardian, bookmark, opts) ⇒ Object



80
81
82
# File 'app/services/topic_bookmarkable.rb', line 80

def self.after_destroy(guardian, bookmark, opts)
  sync_topic_user_bookmarked(guardian.user, bookmark.bookmarkable, opts)
end

.bookmark_metadata(bookmark, user) ⇒ Object



68
69
70
# File 'app/services/topic_bookmarkable.rb', line 68

def self.(bookmark, user)
  { topic_bookmarked: Bookmark.for_user_in_topic(user.id, bookmark.bookmarkable.id).exists? }
end

.can_see?(guardian, bookmark) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'app/services/topic_bookmarkable.rb', line 64

def self.can_see?(guardian, bookmark)
  guardian.can_see_topic?(bookmark.bookmarkable)
end

.cleanup_deletedObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/services/topic_bookmarkable.rb', line 84

def self.cleanup_deleted
  related_topics = DB.query(<<~SQL, grace_time: 3.days.ago)
    DELETE FROM bookmarks b
    USING topics t
    WHERE b.bookmarkable_id = t.id AND b.bookmarkable_type = 'Topic'
    AND (t.deleted_at < :grace_time)
    RETURNING t.id AS topic_id
  SQL

  related_topics_ids = related_topics.map(&:topic_id).uniq
  related_topics_ids.each do |topic_id|
    Jobs.enqueue(:sync_topic_user_bookmarked, topic_id: topic_id)
  end
end

.list_query(user, guardian) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/services/topic_bookmarkable.rb', line 25

def self.list_query(user, guardian)
  topics = Topic.listable_topics.secured(guardian)
  pms = Topic.private_messages_for_user(user)
  topic_bookmarks =
    user
      .bookmarks_of_type("Topic")
      .joins(
        "INNER JOIN topics ON topics.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Topic'",
      )
      .joins("LEFT JOIN topic_users ON topic_users.topic_id = topics.id")
      .where("topic_users.user_id = ?", user.id)
  guardian.filter_allowed_categories(topic_bookmarks.merge(topics.or(pms)))
end

.modelObject



6
7
8
# File 'app/services/topic_bookmarkable.rb', line 6

def self.model
  Topic
end

.perform_custom_preload!(topic_bookmarks, guardian) ⇒ Object



18
19
20
21
22
23
# File 'app/services/topic_bookmarkable.rb', line 18

def self.perform_custom_preload!(topic_bookmarks, guardian)
  topics = topic_bookmarks.map(&:bookmarkable)
  topic_user_lookup = TopicUser.lookup_for(guardian.user, topics)

  topics.each { |topic| topic.user_data = topic_user_lookup[topic.id] }
end

.preload_associationsObject



14
15
16
# File 'app/services/topic_bookmarkable.rb', line 14

def self.preload_associations
  [:tags, { first_post: :user }]
end

.reminder_conditions(bookmark) ⇒ Object



60
61
62
# File 'app/services/topic_bookmarkable.rb', line 60

def self.reminder_conditions(bookmark)
  bookmark.bookmarkable.present?
end

.reminder_handler(bookmark) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'app/services/topic_bookmarkable.rb', line 48

def self.reminder_handler(bookmark)
  send_reminder_notification(
    bookmark,
    topic_id: bookmark.bookmarkable_id,
    post_number: 1,
    data: {
      title: bookmark.bookmarkable.title,
      bookmarkable_url: bookmark.bookmarkable.first_post.url,
    },
  )
end

.search_query(bookmarks, query, ts_query, &bookmarkable_search) ⇒ Object



39
40
41
42
43
44
45
46
# File 'app/services/topic_bookmarkable.rb', line 39

def self.search_query(bookmarks, query, ts_query, &bookmarkable_search)
  bookmarkable_search.call(
    bookmarks.joins(
      "LEFT JOIN posts ON posts.topic_id = topics.id AND posts.post_number = 1",
    ).joins("LEFT JOIN post_search_data ON post_search_data.post_id = posts.id"),
    "#{ts_query} @@ post_search_data.search_data",
  )
end

.serializerObject



10
11
12
# File 'app/services/topic_bookmarkable.rb', line 10

def self.serializer
  UserTopicBookmarkSerializer
end

.validate_before_create(guardian, bookmarkable) ⇒ Object



72
73
74
# File 'app/services/topic_bookmarkable.rb', line 72

def self.validate_before_create(guardian, bookmarkable)
  raise Discourse::InvalidAccess if bookmarkable.blank? || !guardian.can_see_topic?(bookmarkable)
end