Skip to content

A platform for community discussion. Free, open, simple.

PERF: Serialize only what user menu bookmarks render

Régis Hanol31f877169b17
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index 25a1a537a01..127ff99465a 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -2017,7 +2017,9 @@ class UsersController < ApplicationController
end
reminder_notifications =
- BookmarkQuery.new(user: current_user).unread_notifications(limit: USER_MENU_LIST_LIMIT)
+ BookmarkQuery.new(user: current_user, guardian:).unread_notifications(
+ limit: USER_MENU_LIST_LIMIT,
+ )
if reminder_notifications.size < USER_MENU_LIST_LIMIT
exclude_bookmark_ids =
reminder_notifications.filter_map { |notification| notification.data_hash[:bookmark_id] }
@@ -2045,11 +2047,7 @@ class UsersController < ApplicationController
end
if bookmark_list
- bookmark_list.bookmark_serializer_opts = { link_to_first_unread_post: true }
- serialized_bookmarks =
- serialize_data(bookmark_list, UserBookmarkListSerializer, scope: guardian, root: false)[
- :bookmarks
- ]
+ serialized_bookmarks = serialize_data(bookmark_list.bookmarks, UserMenuBookmarkSerializer)
end
render json: {
diff --git a/app/models/user_bookmark_list.rb b/app/models/user_bookmark_list.rb
index adc2c830d92..377b0cc8c33 100644
--- a/app/models/user_bookmark_list.rb
+++ b/app/models/user_bookmark_list.rb
@@ -6,7 +6,7 @@ class UserBookmarkList
PER_PAGE = 20
attr_reader :bookmarks, :per_page, :has_more
- attr_accessor :more_bookmarks_url, :bookmark_serializer_opts
+ attr_accessor :more_bookmarks_url
def initialize(user:, guardian:, search_term: nil, per_page: nil, page: 0)
@user = user
@@ -19,7 +19,6 @@ class UserBookmarkList
@page = page.to_i
@bookmarks = []
- @bookmark_serializer_opts = {}
end
def load(&blk)
diff --git a/app/serializers/user_bookmark_list_serializer.rb b/app/serializers/user_bookmark_list_serializer.rb
index b1e9af50210..aa32ed9b315 100644
--- a/app/serializers/user_bookmark_list_serializer.rb
+++ b/app/serializers/user_bookmark_list_serializer.rb
@@ -6,14 +6,7 @@ class UserBookmarkListSerializer < ApplicationSerializer
has_many :categories, serializer: CategoryBadgeSerializer, embed: :objects
def bookmarks
- object.bookmarks.map do |bm|
- bm.registered_bookmarkable.serializer.new(
- bm,
- **object.bookmark_serializer_opts,
- scope: scope,
- root: false,
- )
- end
+ object.bookmarks.map { |bm| bm.registered_bookmarkable.serializer.new(bm, scope:, root: false) }
end
def include_more_bookmarks_url?
diff --git a/app/serializers/user_menu_bookmark_serializer.rb b/app/serializers/user_menu_bookmark_serializer.rb
new file mode 100644
index 00000000000..5ebfb046674
--- /dev/null
+++ b/app/serializers/user_menu_bookmark_serializer.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+class UserMenuBookmarkSerializer < ApplicationSerializer
+ attributes :id, :name, :fancy_title, :topic_id, :bookmarkable_url
+
+ has_one :user, serializer: BasicUserSerializer, embed: :objects
+
+ delegate :fancy_title, :topic_id, :bookmarkable_url, to: :bookmarkable_serializer
+
+ def user
+ bookmarkable_serializer.bookmarkable_user
+ end
+
+ def include_topic_id?
+ bookmarkable_serializer.respond_to?(:topic_id)
+ end
+
+ private
+
+ def bookmarkable_serializer
+ @bookmarkable_serializer ||=
+ object.registered_bookmarkable.serializer.new(
+ object,
+ scope:,
+ root: false,
+ link_to_first_unread_post: true,
+ )
+ end
+end
diff --git a/frontend/discourse/tests/fixtures/user-menu.js b/frontend/discourse/tests/fixtures/user-menu.js
index d397d7dda03..d75d6af3f66 100644
--- a/frontend/discourse/tests/fixtures/user-menu.js
+++ b/frontend/discourse/tests/fixtures/user-menu.js
@@ -30,35 +30,11 @@ const userMenuFixtures = {
bookmarks: [
{
id: 6,
- created_at: "2022-08-05T06:09:39.559Z",
- updated_at: "2022-08-05T06:11:27.246Z",
name: "",
- reminder_at: "2022-08-05T06:10:42.223Z",
- reminder_at_ics_start: "20220805T061042Z",
- reminder_at_ics_end: "20220805T071042Z",
- pinned: false,
- title: "Test poll topic hello world",
fancy_title: "Test poll topic hello world",
- excerpt: "poll",
- bookmarkable_id: 1009,
- bookmarkable_type: "Post",
+ topic_id: 227,
bookmarkable_url:
"http://localhost:4200/t/test-poll-topic-hello-world/227/1",
- tags: [],
- tags_descriptions: {},
- truncated: true,
- topic_id: 227,
- linked_post_number: 1,
- deleted: false,
- hidden: false,
- category_id: 1,
- closed: false,
- archived: false,
- archetype: "regular",
- highest_post_number: 1,
- last_read_post_number: 1,
- bumped_at: "2022-04-21T15:14:37.359Z",
- slug: "test-poll-topic-hello-world",
user: {
id: 1,
username: "osama",
diff --git a/frontend/discourse/tests/integration/components/user-menu/menu-item-test.gjs b/frontend/discourse/tests/integration/components/user-menu/menu-item-test.gjs
index 3723c170ea2..fde1a9b80cc 100644
--- a/frontend/discourse/tests/integration/components/user-menu/menu-item-test.gjs
+++ b/frontend/discourse/tests/integration/components/user-menu/menu-item-test.gjs
@@ -482,43 +482,9 @@ module(
function getBookmark(overrides = {}, siteSettings, site) {
const bookmark = deepMerge(
- {
- id: 6,
- created_at: "2022-08-05T06:09:39.559Z",
- updated_at: "2022-08-05T06:11:27.246Z",
- name: "",
- reminder_at: "2022-08-05T06:10:42.223Z",
- reminder_at_ics_start: "20220805T061042Z",
- reminder_at_ics_end: "20220805T071042Z",
- pinned: false,
- title: "Test poll topic hello world",
- fancy_title: "Test poll topic hello world",
- excerpt: "poll",
- bookmarkable_id: 1009,
- bookmarkable_type: "Post",
- bookmarkable_url: "http://localhost:3000/t/this-bookmarkable-url/227/1",
- tags: [],
- tags_descriptions: {},
- truncated: true,
- topic_id: 227,
- linked_post_number: 1,
- deleted: false,
- hidden: false,
- category_id: 1,
- closed: false,
- archived: false,
- archetype: "regular",
- highest_post_number: 45,
- last_read_post_number: 31,
- bumped_at: "2022-04-21T15:14:37.359Z",
- slug: "test-poll-topic-hello-world",
- user: {
- id: 1,
- username: "somebody",
- name: "Mr. Somebody",
- avatar_template: "/letter_avatar_proxy/v4/letter/o/f05b48/{size}.png",
- },
- },
+ cloneJSON(
+ UserMenuFixtures["/u/:username/user-menu-bookmarks"].bookmarks[0]
+ ),
overrides
);
@@ -535,7 +501,7 @@ module(
await render(<template><MenuItem @item={{item}} /></template>);
assert
.dom("li.bookmark a")
- .hasAttribute("href", /\/t\/this-bookmarkable-url\/227\/1$/);
+ .hasAttribute("href", /\/t\/test-poll-topic-hello-world\/227\/1$/);
});
test("item label is the bookmarked post author", async function (assert) {
diff --git a/plugins/discourse-assign/spec/requests/users_controller_spec.rb b/plugins/discourse-assign/spec/requests/users_controller_spec.rb
index dc0ea04c900..93cfee45432 100644
--- a/plugins/discourse-assign/spec/requests/users_controller_spec.rb
+++ b/plugins/discourse-assign/spec/requests/users_controller_spec.rb
@@ -146,22 +146,4 @@ RSpec.describe UsersController do
end
end
end
-
- describe "#user_menu_bookmarks" do
- context "when a bookmarked topic's assignment is inactive" do
- fab!(:topic) { Fabricate(:read_topic, current_user: admin) }
- fab!(:bookmark) { Fabricate(:bookmark, user: admin, bookmarkable: topic) }
-
- fab!(:assignment) do
- Fabricate(:topic_assignment, topic: topic, assigned_to: assignee, active: false)
- end
-
- it "does not include an assigned user" do
- get "/u/#{admin.username}/user-menu-bookmarks"
-
- expect(response.status).to eq(200)
- expect(response.parsed_body.dig("bookmarks", 0)).not_to have_key("assigned_to_user")
- end
- end
- end
end
diff --git a/spec/serializers/user_menu_bookmark_serializer_spec.rb b/spec/serializers/user_menu_bookmark_serializer_spec.rb
new file mode 100644
index 00000000000..8deccdf9786
--- /dev/null
+++ b/spec/serializers/user_menu_bookmark_serializer_spec.rb
@@ -0,0 +1,19 @@
+# frozen_string_literal: true
+
+RSpec.describe UserMenuBookmarkSerializer do
+ fab!(:user)
+ fab!(:bookmarked_user, :user)
+
+ before { register_test_bookmarkable }
+
+ after { DiscoursePluginRegistry.reset_register!(:bookmarkables) }
+
+ it "serializes bookmarkables that are not topics or posts" do
+ bookmark = Fabricate(:bookmark, user:, bookmarkable: bookmarked_user)
+
+ json = described_class.new(bookmark, scope: user.guardian, root: false).as_json
+
+ expect(json[:fancy_title]).to eq(bookmarked_user.username)
+ expect(json[:user][:id]).to eq(bookmarked_user.id)
+ end
+end