tweaks to related message list generation

- exclude users in groups I am in from related message search
- correctly limit number of related messages
This commit is contained in:
Sam 2018-10-29 16:09:58 +11:00
parent 9933059426
commit 5c86e2d749
2 changed files with 55 additions and 25 deletions

View File

@ -17,7 +17,8 @@ class SuggestedTopicsBuilder
return unless results
# Only add results if we don't have those topic ids already
results = results.where('topics.id NOT IN (?)', @excluded_topic_ids)
results = results
.where('topics.id NOT IN (?)', @excluded_topic_ids)
.where(visible: true)
# If limit suggested to category is enabled, restrict to that category
@ -29,17 +30,27 @@ class SuggestedTopicsBuilder
results.reject! { |topic| @category_topic_ids.include?(topic.id) }
unless results.empty?
# Keep track of the ids we've added
@excluded_topic_ids.concat results.map { |r| r.id }
# protect against dupes
temp = results
results = []
temp.each do |r|
if !@excluded_topic_ids.include?(r.id)
results << r
@excluded_topic_ids << r.id
end
end
splice_results(results, priority)
end
end
def splice_results(results, priority)
if @category_id && priority == :high
if priority == :ultra_high
@results.insert 0, *results
elsif @category_id && priority == :high
# Topics from category @category_id need to be first in the list, all others after.
other_category_index = @results.index { |r| r.category_id != @category_id }
category_results, other_category_results = results.partition { |r| r.category_id == @category_id }

View File

@ -148,20 +148,37 @@ class TopicQuery
pm_params =
if topic.private_message?
group_ids = topic.topic_allowed_groups
my_group_ids = topic.topic_allowed_groups
.joins("
LEFT JOIN group_users gu
ON topic_allowed_groups.group_id = gu.group_id
AND user_id = #{@user.id.to_i}
AND gu.user_id = #{@user.id.to_i}
")
.where("gu.group_id IS NOT NULL")
.pluck(:group_id)
target_group_ids = topic.topic_allowed_groups.pluck(:group_id)
target_users = topic
.topic_allowed_users
if my_group_ids.present?
# strip out users in groups you already belong to
target_users = target_users
.joins("LEFT JOIN group_users gu ON gu.user_id = topic_allowed_users.user_id AND gu.group_id IN (#{sanitize_sql_array(my_group_ids)})")
.where('gu.group_id IS NULL')
end
target_user_ids = target_users
.where('NOT topic_allowed_users.user_id = ?', @user.id)
.pluck(:user_id)
{
topic: topic,
my_group_ids: group_ids,
target_group_ids: topic.topic_allowed_groups.pluck(:group_id),
target_user_ids: topic.topic_allowed_users.pluck(:user_id) - [@user.id]
my_group_ids: my_group_ids,
target_group_ids: target_group_ids,
target_user_ids: target_user_ids
}
end
@ -169,19 +186,6 @@ class TopicQuery
if @user
if topic.private_message?
# we start with related conversations cause they are the most relevant
if pm_params[:my_group_ids].present?
builder.add_results(related_messages_group(
pm_params.merge(count: [3, builder.results_left].max,
exclude: builder.excluded_topic_ids)
))
else
builder.add_results(related_messages_user(
pm_params.merge(count: [3, builder.results_left].max,
exclude: builder.excluded_topic_ids)
))
end
builder.add_results(new_messages(
pm_params.merge(count: builder.results_left)
)) unless builder.full?
@ -190,6 +194,18 @@ class TopicQuery
pm_params.merge(count: builder.results_left)
)) unless builder.full?
if pm_params[:my_group_ids].present?
builder.add_results(related_messages_group(
pm_params.merge(count: [3, builder.results_left].max,
exclude: builder.excluded_topic_ids)
), :ultra_high)
else
builder.add_results(related_messages_user(
pm_params.merge(count: [3, builder.results_left].max,
exclude: builder.excluded_topic_ids)
), :ultra_high)
end
else
builder.add_results(unread_results(topic: topic, per_page: builder.results_left), :high)
builder.add_results(new_results(topic: topic, per_page: builder.category_results_left)) unless builder.full?
@ -830,16 +846,19 @@ class TopicQuery
end
def new_messages(params)
TopicQuery.new_filter(messages_for_groups_or_user(params[:my_group_ids]), Time.at(SiteSetting.min_new_topics_time).to_datetime)
query = TopicQuery
.new_filter(messages_for_groups_or_user(params[:my_group_ids]), Time.at(SiteSetting.min_new_topics_time).to_datetime)
.limit(params[:count])
query
end
def unread_messages(params)
TopicQuery.unread_filter(
query = TopicQuery.unread_filter(
messages_for_groups_or_user(params[:my_group_ids]),
@user&.id,
staff: @user&.staff?)
.limit(params[:count])
query
end
def related_messages_user(params)