2022-05-08 19:37:23 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class TopicBookmarkable < BaseBookmarkable
|
2022-05-10 19:51:03 -04:00
|
|
|
include TopicPostBookmarkableHelper
|
|
|
|
|
2022-05-08 19:37:23 -04:00
|
|
|
def self.model
|
|
|
|
Topic
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.serializer
|
|
|
|
UserTopicBookmarkSerializer
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.preload_associations
|
2024-04-17 10:23:47 -04:00
|
|
|
[{ category: :parent_category }, :tags, { first_post: :user }]
|
2022-08-16 21:40:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
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] }
|
2022-05-08 19:37:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def self.reminder_handler(bookmark)
|
2022-07-17 22:51:57 -04:00
|
|
|
send_reminder_notification(
|
|
|
|
bookmark,
|
2022-05-08 19:37:23 -04:00
|
|
|
topic_id: bookmark.bookmarkable_id,
|
|
|
|
post_number: 1,
|
|
|
|
data: {
|
|
|
|
title: bookmark.bookmarkable.title,
|
|
|
|
bookmarkable_url: bookmark.bookmarkable.first_post.url,
|
2022-07-17 22:51:57 -04:00
|
|
|
},
|
2022-05-08 19:37:23 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.reminder_conditions(bookmark)
|
2023-10-12 21:41:10 -04:00
|
|
|
bookmark.bookmarkable.present? && self.can_see?(bookmark.user.guardian, bookmark)
|
2022-05-08 19:37:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.can_see?(guardian, bookmark)
|
2024-02-28 18:03:49 -05:00
|
|
|
can_see_bookmarkable?(guardian, bookmark.bookmarkable)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.can_see_bookmarkable?(guardian, bookmarkable)
|
|
|
|
guardian.can_see_topic?(bookmarkable)
|
2022-05-08 19:37:23 -04:00
|
|
|
end
|
2022-05-10 19:51:03 -04:00
|
|
|
|
|
|
|
def self.bookmark_metadata(bookmark, user)
|
|
|
|
{ topic_bookmarked: Bookmark.for_user_in_topic(user.id, bookmark.bookmarkable.id).exists? }
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.validate_before_create(guardian, bookmarkable)
|
|
|
|
raise Discourse::InvalidAccess if bookmarkable.blank? || !guardian.can_see_topic?(bookmarkable)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.after_create(guardian, bookmark, opts)
|
|
|
|
sync_topic_user_bookmarked(guardian.user, bookmark.bookmarkable, opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.after_destroy(guardian, bookmark, opts)
|
|
|
|
sync_topic_user_bookmarked(guardian.user, bookmark.bookmarkable, opts)
|
|
|
|
end
|
2022-06-23 00:09:39 -04:00
|
|
|
|
|
|
|
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
|
2022-05-08 19:37:23 -04:00
|
|
|
end
|