2020-04-01 12:09:07 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class UserBookmarkList
|
|
|
|
include ActiveModel::Serialization
|
|
|
|
|
|
|
|
PER_PAGE = 20
|
|
|
|
|
2022-09-01 18:04:00 +08:00
|
|
|
attr_reader :bookmarks, :per_page, :has_more
|
2022-08-08 22:24:04 +08:00
|
|
|
attr_accessor :more_bookmarks_url, :bookmark_serializer_opts
|
2020-04-01 12:09:07 +08:00
|
|
|
|
2023-07-28 19:53:46 +08:00
|
|
|
def initialize(user:, guardian:, search_term: nil, per_page: nil, page: 0)
|
2020-04-01 12:09:07 +08:00
|
|
|
@user = user
|
|
|
|
@guardian = guardian
|
2021-01-19 06:53:49 +08:00
|
|
|
|
2023-07-28 19:53:46 +08:00
|
|
|
@per_page = per_page || PER_PAGE
|
|
|
|
@per_page = PER_PAGE if @per_page > PER_PAGE
|
|
|
|
|
|
|
|
@search_term = search_term
|
|
|
|
@page = page.to_i
|
2021-01-19 06:53:49 +08:00
|
|
|
|
2020-04-01 12:09:07 +08:00
|
|
|
@bookmarks = []
|
2022-08-08 22:24:04 +08:00
|
|
|
@bookmark_serializer_opts = {}
|
2020-04-01 12:09:07 +08:00
|
|
|
end
|
|
|
|
|
2022-08-08 22:24:04 +08:00
|
|
|
def load(&blk)
|
2023-07-28 19:53:46 +08:00
|
|
|
query =
|
|
|
|
BookmarkQuery.new(
|
|
|
|
user: @user,
|
|
|
|
guardian: @guardian,
|
|
|
|
search_term: @search_term,
|
|
|
|
page: @page,
|
|
|
|
per_page: @per_page,
|
|
|
|
)
|
|
|
|
|
2022-09-01 18:04:00 +08:00
|
|
|
@bookmarks = query.list_all(&blk)
|
2023-07-28 19:53:46 +08:00
|
|
|
@has_more = (@page.to_i + 1) * @per_page < query.count
|
2020-04-01 12:09:07 +08:00
|
|
|
@bookmarks
|
|
|
|
end
|
2024-04-17 22:23:47 +08:00
|
|
|
|
|
|
|
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
|
2020-04-01 12:09:07 +08:00
|
|
|
end
|