mirror of
https://github.com/discourse/discourse.git
synced 2024-12-24 01:13:44 +08:00
21bb28df91
This commit improves `TopicsController#show` to not load suggested and related topics unless it is the last page of the topic's view. Previously, we avoided loading suggested and related topics by the use of conditionals in the `TopicViewSerializer` to avoid calling `TopicView#suggested_topics` and `TopicView#related_topics`. However, this pattern is not reliable as the methods can still be called from other spots in the code base. Instead, we ensure that `TopicView#include_suggested` and `TopicView#include_related` is set correctly on the instance of `TopicView` which ensures that for the given instance, `TopicView#suggested_topics` and `TopicView#related_topics` will be a noop.
50 lines
1.2 KiB
Ruby
50 lines
1.2 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module SuggestedTopicsMixin
|
|
def self.included(klass)
|
|
klass.attributes :related_messages
|
|
klass.attributes :suggested_topics
|
|
klass.attributes :suggested_group_name
|
|
end
|
|
|
|
def include_related_messages?
|
|
object.related_messages&.topics
|
|
end
|
|
|
|
def include_suggested_topics?
|
|
object.suggested_topics&.topics
|
|
end
|
|
|
|
def include_suggested_group_name?
|
|
return false unless include_suggested_topics?
|
|
object.topic.private_message? && scope.user
|
|
end
|
|
|
|
def suggested_group_name
|
|
return if object.topic.topic_allowed_users.exists?(user_id: scope.user.id)
|
|
|
|
if object.topic_allowed_group_ids.present?
|
|
Group
|
|
.joins(:group_users)
|
|
.where(
|
|
"group_users.group_id IN (?) AND group_users.user_id = ?",
|
|
object.topic_allowed_group_ids,
|
|
scope.user.id,
|
|
)
|
|
.pick(:name)
|
|
end
|
|
end
|
|
|
|
def related_messages
|
|
object.related_messages.topics.map do |t|
|
|
SuggestedTopicSerializer.new(t, scope: scope, root: false)
|
|
end
|
|
end
|
|
|
|
def suggested_topics
|
|
object.suggested_topics.topics.map do |t|
|
|
SuggestedTopicSerializer.new(t, scope: scope, root: false)
|
|
end
|
|
end
|
|
end
|