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.
This commit is contained in:
Alan Guo Xiang Tan 2023-01-03 10:00:36 +08:00 committed by GitHub
parent 24db6fbb73
commit af76f291e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 2 deletions

View File

@ -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
)