discourse/app/models/user_bookmark_list.rb
Bianca Nenciu 9638ce17fa
FIX: Serialize categories for bookmarks ()
This is necessary when "lazy load categories" feature is enabled to make
sure the categories are rendered for topics and posts.
2024-04-17 17:23:47 +03:00

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