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-08-17 09:40:24 +08:00
|
|
|
preload_polymorphic_associations(bookmarks, object.guardian)
|
2020-03-19 13:48:23 +08:00
|
|
|
@preload.each { |preload| preload.call(bookmarks, object) } if @preload
|
|
|
|
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.
|
2022-08-17 09:40:24 +08:00
|
|
|
def self.preload_polymorphic_associations(bookmarks, guardian)
|
2022-04-22 06:23:42 +08:00
|
|
|
Bookmark.registered_bookmarkables.each do |registered_bookmarkable|
|
2022-08-17 09:40:24 +08:00
|
|
|
registered_bookmarkable.perform_preload(bookmarks, guardian)
|
2022-04-22 06:23:42 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-09-01 18:04:00 +08:00
|
|
|
attr_reader :guardian, :count
|
2022-08-17 09:40:24 +08:00
|
|
|
|
2023-07-28 19:53:46 +08:00
|
|
|
def initialize(user:, guardian: nil, search_term: nil, page: nil, per_page: nil)
|
2020-03-12 13:20:56 +08:00
|
|
|
@user = user
|
2023-07-28 19:53:46 +08:00
|
|
|
@search_term = search_term
|
2020-03-23 19:04:39 +08:00
|
|
|
@guardian = guardian || Guardian.new(@user)
|
2023-07-28 19:53:46 +08:00
|
|
|
@page = page ? page.to_i : 0
|
|
|
|
@per_page = per_page ? per_page.to_i : 20
|
2022-09-01 18:04:00 +08:00
|
|
|
@count = 0
|
2020-03-12 13:20:56 +08:00
|
|
|
end
|
|
|
|
|
2022-08-08 22:24:04 +08:00
|
|
|
def list_all(&blk)
|
2023-07-28 19:53:46 +08:00
|
|
|
ts_query = @search_term.present? ? Search.ts_query(term: @search_term) : nil
|
|
|
|
search_term_wildcard = @search_term.present? ? "%#{@search_term}%" : nil
|
2022-04-22 06:23:42 +08:00
|
|
|
|
|
|
|
queries =
|
|
|
|
Bookmark
|
|
|
|
.registered_bookmarkables
|
|
|
|
.map do |bookmarkable|
|
|
|
|
interim_results = bookmarkable.perform_list_query(@user, @guardian)
|
2020-04-01 12:09:07 +08:00
|
|
|
|
2022-05-12 08:29:01 +08:00
|
|
|
# this could occur if there is some security reason that the user cannot
|
|
|
|
# access the bookmarkables that they have bookmarked, e.g. if they had 1 bookmark
|
|
|
|
# on a topic and that topic was moved into a private category
|
|
|
|
next if interim_results.blank?
|
|
|
|
|
2023-07-28 19:53:46 +08:00
|
|
|
if @search_term.present?
|
2022-04-22 06:23:42 +08:00
|
|
|
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}"
|
2022-05-12 08:29:01 +08:00
|
|
|
end
|
|
|
|
.compact
|
|
|
|
|
|
|
|
# same for interim results being blank, the user might have been locked out
|
|
|
|
# from all their various bookmarks, in which case they will see nothing and
|
|
|
|
# no further pagination/ordering/etc is required
|
|
|
|
return [] if queries.empty?
|
2020-03-19 13:48:23 +08:00
|
|
|
|
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",
|
|
|
|
)
|
|
|
|
|
2022-09-01 18:04:00 +08:00
|
|
|
@count = results.count
|
|
|
|
|
2023-07-28 19:53:46 +08:00
|
|
|
results = results.offset(@page * @per_page) if @page.positive?
|
2020-03-19 13:48:23 +08:00
|
|
|
|
2022-08-08 22:24:04 +08:00
|
|
|
if updated_results = blk&.call(results)
|
|
|
|
results = updated_results
|
|
|
|
end
|
|
|
|
|
2023-07-28 19:53:46 +08:00
|
|
|
results = results.limit(@per_page).to_a
|
|
|
|
|
2022-04-22 06:23:42 +08:00
|
|
|
BookmarkQuery.preload(results, self)
|
|
|
|
results
|
2020-03-12 13:20:56 +08:00
|
|
|
end
|
|
|
|
end
|