2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-26 02:42:27 -04:00
|
|
|
module SuggestedTopicsMixin
|
|
|
|
def self.included(klass)
|
2018-11-11 21:04:30 -05:00
|
|
|
klass.attributes :related_messages
|
2017-09-26 02:42:27 -04:00
|
|
|
klass.attributes :suggested_topics
|
2021-09-07 00:30:40 -04:00
|
|
|
klass.attributes :suggested_group_name
|
2017-09-26 02:42:27 -04:00
|
|
|
end
|
|
|
|
|
2018-11-11 21:04:30 -05:00
|
|
|
def include_related_messages?
|
2020-12-10 12:02:07 -05:00
|
|
|
return false if @options[:exclude_suggested_and_related]
|
2020-06-26 15:25:38 -04:00
|
|
|
object.next_page.nil? && object.related_messages&.topics
|
2018-11-11 21:04:30 -05:00
|
|
|
end
|
|
|
|
|
2017-09-26 02:42:27 -04:00
|
|
|
def include_suggested_topics?
|
2020-12-10 12:02:07 -05:00
|
|
|
return false if @options[:exclude_suggested_and_related]
|
2020-06-26 15:25:38 -04:00
|
|
|
object.next_page.nil? && object.suggested_topics&.topics
|
2017-09-26 02:42:27 -04:00
|
|
|
end
|
|
|
|
|
2021-09-07 00:30:40 -04:00
|
|
|
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
|
|
|
|
|
2018-11-11 21:04:30 -05:00
|
|
|
def related_messages
|
|
|
|
object.related_messages.topics.map do |t|
|
|
|
|
SuggestedTopicSerializer.new(t, scope: scope, root: false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-26 02:42:27 -04:00
|
|
|
def suggested_topics
|
|
|
|
object.suggested_topics.topics.map do |t|
|
|
|
|
SuggestedTopicSerializer.new(t, scope: scope, root: false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|