2020-03-12 13:20:56 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
##
|
|
|
|
# Allows us to query Bookmark records for lists. Used mainly
|
|
|
|
# in the user/activity/bookmarks page.
|
|
|
|
|
|
|
|
class BookmarkQuery
|
2020-03-19 13:48:23 +08:00
|
|
|
def self.on_preload(&blk)
|
|
|
|
(@preload ||= Set.new) << blk
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.preload(bookmarks, object)
|
2022-04-22 06:23:42 +08:00
|
|
|
if SiteSetting.use_polymorphic_bookmarks
|
|
|
|
preload_polymorphic_associations(bookmarks)
|
|
|
|
end
|
2020-03-19 13:48:23 +08:00
|
|
|
if @preload
|
|
|
|
@preload.each { |preload| preload.call(bookmarks, object) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-22 06:23:42 +08:00
|
|
|
# These polymorphic associations are loaded to make the UserBookmarkListSerializer's
|
|
|
|
# life easier, which conditionally chooses the bookmark serializer to use based
|
|
|
|
# on the type, and we want the associations all loaded ahead of time to make
|
|
|
|
# sure we are not doing N+1s.
|
|
|
|
def self.preload_polymorphic_associations(bookmarks)
|
|
|
|
Bookmark.registered_bookmarkables.each do |registered_bookmarkable|
|
|
|
|
registered_bookmarkable.perform_preload(bookmarks)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-23 19:04:39 +08:00
|
|
|
def initialize(user:, guardian: nil, params: {})
|
2020-03-12 13:20:56 +08:00
|
|
|
@user = user
|
|
|
|
@params = params
|
2020-03-23 19:04:39 +08:00
|
|
|
@guardian = guardian || Guardian.new(@user)
|
2020-04-01 12:09:07 +08:00
|
|
|
@page = @params[:page].to_i
|
|
|
|
@limit = @params[:limit].present? ? @params[:limit].to_i : @params[:per_page]
|
2020-03-12 13:20:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def list_all
|
2022-04-22 06:23:42 +08:00
|
|
|
return polymorphic_list_all if SiteSetting.use_polymorphic_bookmarks
|
2020-03-12 13:20:56 +08:00
|
|
|
|
2020-03-23 19:04:39 +08:00
|
|
|
topics = Topic.listable_topics.secured(@guardian)
|
|
|
|
pms = Topic.private_messages_for_user(@user)
|
2022-04-22 06:23:42 +08:00
|
|
|
results = list_all_results(topics, pms)
|
2020-03-23 19:04:39 +08:00
|
|
|
|
2022-04-22 06:23:42 +08:00
|
|
|
results = results.order(
|
|
|
|
"(CASE WHEN bookmarks.pinned THEN 0 ELSE 1 END),
|
|
|
|
bookmarks.reminder_at ASC,
|
|
|
|
bookmarks.updated_at DESC"
|
|
|
|
)
|
2020-03-23 19:04:39 +08:00
|
|
|
|
2020-07-14 12:43:41 +08:00
|
|
|
if @params[:q].present?
|
2022-04-22 06:23:42 +08:00
|
|
|
results = search(results, @params[:q])
|
2020-07-14 12:43:41 +08:00
|
|
|
end
|
|
|
|
|
2020-04-01 12:09:07 +08:00
|
|
|
if @page.positive?
|
|
|
|
results = results.offset(@page * @params[:per_page])
|
2020-03-12 13:20:56 +08:00
|
|
|
end
|
|
|
|
|
2022-04-22 06:23:42 +08:00
|
|
|
results = results.limit(@limit).to_a
|
|
|
|
BookmarkQuery.preload(results, self)
|
|
|
|
results
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def polymorphic_list_all
|
|
|
|
search_term = @params[:q]
|
|
|
|
ts_query = search_term.present? ? Search.ts_query(term: search_term) : nil
|
|
|
|
search_term_wildcard = search_term.present? ? "%#{search_term}%" : nil
|
|
|
|
|
|
|
|
queries = Bookmark.registered_bookmarkables.map do |bookmarkable|
|
|
|
|
interim_results = bookmarkable.perform_list_query(@user, @guardian)
|
2020-04-01 12:09:07 +08:00
|
|
|
|
2022-04-22 06:23:42 +08:00
|
|
|
if search_term.present?
|
|
|
|
interim_results = bookmarkable.perform_search_query(
|
|
|
|
interim_results, search_term_wildcard, ts_query
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
# this is purely to make the query easy to read and debug, otherwise it's
|
|
|
|
# all mashed up into a massive ball in MiniProfiler :)
|
|
|
|
"---- #{bookmarkable.model.to_s} bookmarkable ---\n\n #{interim_results.to_sql}"
|
2020-03-19 13:48:23 +08:00
|
|
|
end
|
|
|
|
|
2022-04-22 06:23:42 +08:00
|
|
|
union_sql = queries.join("\n\nUNION\n\n")
|
|
|
|
results = Bookmark.select("bookmarks.*").from("(\n\n#{union_sql}\n\n) as bookmarks")
|
|
|
|
results = results.order(
|
|
|
|
"(CASE WHEN bookmarks.pinned THEN 0 ELSE 1 END),
|
|
|
|
bookmarks.reminder_at ASC,
|
|
|
|
bookmarks.updated_at DESC"
|
|
|
|
)
|
|
|
|
|
|
|
|
if @page.positive?
|
|
|
|
results = results.offset(@page * @params[:per_page])
|
|
|
|
end
|
2020-03-19 13:48:23 +08:00
|
|
|
|
2022-04-22 06:23:42 +08:00
|
|
|
results = results.limit(@limit).to_a
|
|
|
|
BookmarkQuery.preload(results, self)
|
|
|
|
results
|
2020-03-12 13:20:56 +08:00
|
|
|
end
|
|
|
|
|
2022-04-22 06:23:42 +08:00
|
|
|
def list_all_results(topics, pms)
|
|
|
|
results = base_bookmarks.merge(topics.or(pms))
|
|
|
|
results = results.merge(Post.secured(@guardian))
|
|
|
|
results = @guardian.filter_allowed_categories(results)
|
|
|
|
results
|
|
|
|
end
|
2020-03-12 13:20:56 +08:00
|
|
|
|
2022-04-22 06:23:42 +08:00
|
|
|
def base_bookmarks
|
2020-03-19 13:48:23 +08:00
|
|
|
Bookmark.where(user: @user)
|
|
|
|
.includes(post: :user)
|
2021-09-15 08:16:54 +08:00
|
|
|
.includes(post: { topic: :tags })
|
2021-09-21 11:49:56 +08:00
|
|
|
.includes(topic: :topic_users)
|
2020-03-23 19:04:39 +08:00
|
|
|
.references(:post)
|
2021-09-21 11:49:56 +08:00
|
|
|
.where(topic_users: { user_id: @user.id })
|
2020-03-12 13:20:56 +08:00
|
|
|
end
|
2022-04-22 06:23:42 +08:00
|
|
|
|
|
|
|
def search(results, term)
|
|
|
|
bookmark_ts_query = Search.ts_query(term: term)
|
|
|
|
results
|
|
|
|
.joins("LEFT JOIN post_search_data ON post_search_data.post_id = bookmarks.post_id")
|
|
|
|
.where("bookmarks.name ILIKE :q OR #{bookmark_ts_query} @@ post_search_data.search_data", q: "%#{term}%")
|
|
|
|
end
|
2020-03-12 13:20:56 +08:00
|
|
|
end
|