mirror of
https://github.com/discourse/discourse.git
synced 2025-02-07 02:16:31 +08:00
![Bianca Nenciu](/assets/img/avatar_default.png)
This is necessary when "lazy load categories" feature is enabled to make sure the categories are rendered for topics and posts.
52 lines
1.1 KiB
Ruby
52 lines
1.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class UserBookmarkList
|
|
include ActiveModel::Serialization
|
|
|
|
PER_PAGE = 20
|
|
|
|
attr_reader :bookmarks, :per_page, :has_more
|
|
attr_accessor :more_bookmarks_url, :bookmark_serializer_opts
|
|
|
|
def initialize(user:, guardian:, search_term: nil, per_page: nil, page: 0)
|
|
@user = user
|
|
@guardian = guardian
|
|
|
|
@per_page = per_page || PER_PAGE
|
|
@per_page = PER_PAGE if @per_page > PER_PAGE
|
|
|
|
@search_term = search_term
|
|
@page = page.to_i
|
|
|
|
@bookmarks = []
|
|
@bookmark_serializer_opts = {}
|
|
end
|
|
|
|
def load(&blk)
|
|
query =
|
|
BookmarkQuery.new(
|
|
user: @user,
|
|
guardian: @guardian,
|
|
search_term: @search_term,
|
|
page: @page,
|
|
per_page: @per_page,
|
|
)
|
|
|
|
@bookmarks = query.list_all(&blk)
|
|
@has_more = (@page.to_i + 1) * @per_page < query.count
|
|
@bookmarks
|
|
end
|
|
|
|
def categories
|
|
@categories ||=
|
|
@bookmarks
|
|
.map do |bm|
|
|
category = bm.bookmarkable.try(:category) || bm.bookmarkable.try(:topic)&.category
|
|
[category&.parent_category, category]
|
|
end
|
|
.flatten
|
|
.compact
|
|
.uniq
|
|
end
|
|
end
|