From af76f291e6a2ab91b98cabcde4c7ca5dd3905f27 Mon Sep 17 00:00:00 2001 From: Alan Guo Xiang Tan Date: Tue, 3 Jan 2023 10:00:36 +0800 Subject: [PATCH] PERF: Avoid running redundant bookmarks query for anon viewing topic (#19659) The `TopicView#bookmarks` method is called by `TopicViewSerializer` and `PostSerializer` so we want to avoid running a meaningless query when user is not present. --- lib/topic_view.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/topic_view.rb b/lib/topic_view.rb index eeccb8bae89..63b6ec7580a 100644 --- a/lib/topic_view.rb +++ b/lib/topic_view.rb @@ -412,12 +412,13 @@ class TopicView end def has_bookmarks? - return false if @user.blank? - return false if @topic.trashed? bookmarks.any? end def bookmarks + return [] if @user.blank? + return [] if @topic.trashed? + @bookmarks ||= Bookmark.for_user_in_topic(@user, @topic.id).select( :id, :bookmarkable_id, :bookmarkable_type, :reminder_at, :name, :auto_delete_preference )