mirror of
https://github.com/discourse/discourse.git
synced 2025-05-28 01:56:58 +08:00
FIX: N1 issues for bookmark list (#9236)
* Preload custom fields for BookmarkQuery and add preload callback. Copy TopicQuery preload methodology to allow plugins to preload data for the BookmarkQuery. This fixes assigned plugin custom fields N1 * Include topic tags in initial query to avoid tags N1 Related: discourse/discourse-assign#63
This commit is contained in:
@ -5,6 +5,19 @@
|
||||
# in the user/activity/bookmarks page.
|
||||
|
||||
class BookmarkQuery
|
||||
cattr_accessor :preloaded_custom_fields
|
||||
self.preloaded_custom_fields = Set.new
|
||||
|
||||
def self.on_preload(&blk)
|
||||
(@preload ||= Set.new) << blk
|
||||
end
|
||||
|
||||
def self.preload(bookmarks, object)
|
||||
if @preload
|
||||
@preload.each { |preload| preload.call(bookmarks, object) }
|
||||
end
|
||||
end
|
||||
|
||||
def initialize(user, params = {})
|
||||
@user = user
|
||||
@params = params
|
||||
@ -15,18 +28,28 @@ class BookmarkQuery
|
||||
.joins('INNER JOIN topics ON topics.id = bookmarks.topic_id')
|
||||
.joins('INNER JOIN posts ON posts.id = bookmarks.post_id')
|
||||
.joins('INNER JOIN users ON users.id = posts.user_id')
|
||||
.order('created_at DESC')
|
||||
.order('bookmarks.created_at DESC')
|
||||
|
||||
if @params[:limit]
|
||||
results = results.limit(@params[:limit])
|
||||
end
|
||||
|
||||
if BookmarkQuery.preloaded_custom_fields.any?
|
||||
Topic.preload_custom_fields(
|
||||
results.map(&:topic), BookmarkQuery.preloaded_custom_fields
|
||||
)
|
||||
end
|
||||
|
||||
BookmarkQuery.preload(results, self)
|
||||
|
||||
results
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def user_bookmarks
|
||||
Bookmark.where(user: @user).includes(:topic).includes(post: :user)
|
||||
Bookmark.where(user: @user)
|
||||
.includes(topic: :tags)
|
||||
.includes(post: :user)
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user