discourse/app/serializers/suggested_topics_mixin.rb
Alan Guo Xiang Tan fc1fd1b416
FEATURE: Display new/unread count in browse more messages for PMs. (#14188)
In order to include the new/unread count in the browse more message
under suggested topics, a couple of technical changes have to be made.

1. `PrivateMessageTopicTrackingState` is now auto-injected which is
   similar to how it is done for `TopicTrackingState`. This is done so
we don't have to attempt to pass the `PrivateMessageTopicTrackingState`
object multiple levels down into the suggested-topics component. While
the object is auto-injected, we only fetch the initial state and start
tracking when the relevant private messages routes has been hit and only
when a private message's suggested topics is loaded. This is
done as we do not want to add the extra overhead of fetching the inital
state to all page loads but instead wait till the private messages
routes are hit.

2. Previously, we would stop tracking once the `user-private-messages`
   route has been deactivated. However, that is not ideal since
navigating out of the route and back means we send an API call to the
server each time. Since `PrivateMessageTopicTrackingState` is kept in
sync cheaply via messageBus, we can just continue to track the state
even if the user has navigated away from the relevant stages.
2021-09-07 12:30:40 +08:00

50 lines
1.3 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?
return false if @options[:exclude_suggested_and_related]
object.next_page.nil? && object.related_messages&.topics
end
def include_suggested_topics?
return false if @options[:exclude_suggested_and_related]
object.next_page.nil? && 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
)
.pluck_first(: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