FIX: Serialize categories for bookmarks (#26606)

This is necessary when "lazy load categories" feature is enabled to make
sure the categories are rendered for topics and posts.
This commit is contained in:
Bianca Nenciu 2024-04-17 17:23:47 +03:00 committed by GitHub
parent c9a46cfdda
commit 9638ce17fa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 36 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import $ from "jquery";
import { Promise } from "rsvp"; import { Promise } from "rsvp";
import { ajax } from "discourse/lib/ajax"; import { ajax } from "discourse/lib/ajax";
import Bookmark from "discourse/models/bookmark"; import Bookmark from "discourse/models/bookmark";
import Site from "discourse/models/site";
import DiscourseRoute from "discourse/routes/discourse"; import DiscourseRoute from "discourse/routes/discourse";
import I18n from "discourse-i18n"; import I18n from "discourse-i18n";
@ -39,6 +40,10 @@ export default DiscourseRoute.extend({
return { bookmarks: [] }; return { bookmarks: [] };
} }
response.user_bookmark_list.categories?.forEach((category) =>
Site.current().updateCategory(category)
);
const bookmarks = response.user_bookmark_list.bookmarks.map( const bookmarks = response.user_bookmark_list.bookmarks.map(
controller.transform controller.transform
); );

View File

@ -36,4 +36,16 @@ class UserBookmarkList
@has_more = (@page.to_i + 1) * @per_page < query.count @has_more = (@page.to_i + 1) * @per_page < query.count
@bookmarks @bookmarks
end 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 end

View File

@ -3,6 +3,8 @@
class UserBookmarkListSerializer < ApplicationSerializer class UserBookmarkListSerializer < ApplicationSerializer
attributes :more_bookmarks_url, :bookmarks attributes :more_bookmarks_url, :bookmarks
has_many :categories, serializer: CategoryBadgeSerializer, embed: :objects
def bookmarks def bookmarks
object.bookmarks.map do |bm| object.bookmarks.map do |bm|
bm.registered_bookmarkable.serializer.new( bm.registered_bookmarkable.serializer.new(
@ -17,4 +19,8 @@ class UserBookmarkListSerializer < ApplicationSerializer
def include_more_bookmarks_url? def include_more_bookmarks_url?
@include_more_bookmarks_url ||= object.has_more @include_more_bookmarks_url ||= object.has_more
end end
def include_categories?
scope.can_lazy_load_categories?
end
end end

View File

@ -12,7 +12,7 @@ class PostBookmarkable < BaseBookmarkable
end end
def self.preload_associations def self.preload_associations
[{ topic: %i[tags category] }, :user] [{ topic: [:tags, { category: :parent_category }] }, :user]
end end
def self.list_query(user, guardian) def self.list_query(user, guardian)

View File

@ -12,7 +12,7 @@ class TopicBookmarkable < BaseBookmarkable
end end
def self.preload_associations def self.preload_associations
[:category, :tags, { first_post: :user }] [{ category: :parent_category }, :tags, { first_post: :user }]
end end
def self.perform_custom_preload!(topic_bookmarks, guardian) def self.perform_custom_preload!(topic_bookmarks, guardian)

View File

@ -29,5 +29,16 @@ RSpec.describe UserBookmarkListSerializer do
%w[UserTestBookmarkSerializer UserTopicBookmarkSerializer UserPostBookmarkSerializer], %w[UserTestBookmarkSerializer UserTopicBookmarkSerializer UserPostBookmarkSerializer],
) )
end end
it "serializes categories" do
topic_category = Fabricate(:category)
topic_bookmark.bookmarkable.update!(category: topic_category)
post_category = Fabricate(:category)
post_bookmark.bookmarkable.topic.update!(category: post_category)
serializer = run_serializer
expect(serializer.categories).to contain_exactly(topic_category, post_category)
end
end end
end end