A platform for community discussion. Free, open, simple.
FIX: Serialize categories for bookmarks (#26606)
Bianca Nenciu9638ce17fa7e
diff --git a/app/assets/javascripts/discourse/app/routes/user-activity-bookmarks.js b/app/assets/javascripts/discourse/app/routes/user-activity-bookmarks.jsindex 3c696a8f7a3..cb455e686ce 100644--- a/app/assets/javascripts/discourse/app/routes/user-activity-bookmarks.js+++ b/app/assets/javascripts/discourse/app/routes/user-activity-bookmarks.js@@ -4,6 +4,7 @@ import $ from "jquery";import { Promise } from "rsvp";import { ajax } from "discourse/lib/ajax";import Bookmark from "discourse/models/bookmark";+import Site from "discourse/models/site";import DiscourseRoute from "discourse/routes/discourse";import I18n from "discourse-i18n";@@ -39,6 +40,10 @@ export default DiscourseRoute.extend({return { bookmarks: [] };}+ response.user_bookmark_list.categories?.forEach((category) =>+ Site.current().updateCategory(category)+ );+const bookmarks = response.user_bookmark_list.bookmarks.map(controller.transform);diff --git a/app/models/user_bookmark_list.rb b/app/models/user_bookmark_list.rbindex a085a5b3ec7..adc2c830d92 100644--- a/app/models/user_bookmark_list.rb+++ b/app/models/user_bookmark_list.rb@@ -36,4 +36,16 @@ class UserBookmarkList@has_more = (@page.to_i + 1) * @per_page < query.count@bookmarksend++ 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+ endenddiff --git a/app/serializers/user_bookmark_list_serializer.rb b/app/serializers/user_bookmark_list_serializer.rbindex b70349a1e83..b1e9af50210 100644--- a/app/serializers/user_bookmark_list_serializer.rb+++ b/app/serializers/user_bookmark_list_serializer.rb@@ -3,6 +3,8 @@class UserBookmarkListSerializer < ApplicationSerializerattributes :more_bookmarks_url, :bookmarks+ has_many :categories, serializer: CategoryBadgeSerializer, embed: :objects+def bookmarksobject.bookmarks.map do |bm|bm.registered_bookmarkable.serializer.new(@@ -17,4 +19,8 @@ class UserBookmarkListSerializer < ApplicationSerializerdef include_more_bookmarks_url?@include_more_bookmarks_url ||= object.has_moreend++ def include_categories?+ scope.can_lazy_load_categories?+ endenddiff --git a/app/services/post_bookmarkable.rb b/app/services/post_bookmarkable.rbindex c11385dd362..82230fa542d 100644--- a/app/services/post_bookmarkable.rb+++ b/app/services/post_bookmarkable.rb@@ -12,7 +12,7 @@ class PostBookmarkable < BaseBookmarkableenddef self.preload_associations- [{ topic: %i[tags category] }, :user]+ [{ topic: [:tags, { category: :parent_category }] }, :user]enddef self.list_query(user, guardian)diff --git a/app/services/topic_bookmarkable.rb b/app/services/topic_bookmarkable.rbindex ef299e87521..5f7b7aee44e 100644--- a/app/services/topic_bookmarkable.rb+++ b/app/services/topic_bookmarkable.rb@@ -12,7 +12,7 @@ class TopicBookmarkable < BaseBookmarkableenddef self.preload_associations- [:category, :tags, { first_post: :user }]+ [{ category: :parent_category }, :tags, { first_post: :user }]enddef self.perform_custom_preload!(topic_bookmarks, guardian)diff --git a/spec/serializers/user_bookmark_list_serializer_spec.rb b/spec/serializers/user_bookmark_list_serializer_spec.rbindex 0567bf73390..64b513d7b6b 100644--- a/spec/serializers/user_bookmark_list_serializer_spec.rb+++ b/spec/serializers/user_bookmark_list_serializer_spec.rb@@ -29,5 +29,16 @@ RSpec.describe UserBookmarkListSerializer do%w[UserTestBookmarkSerializer UserTopicBookmarkSerializer UserPostBookmarkSerializer],)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)+ endendend