mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
group the "suggested topics" by category correctly.
in the past new topics were not prioritizing current category and new topics in a category were not being inserted before other unread topics in other categories
This commit is contained in:
@@ -3,14 +3,15 @@ require_dependency 'topic_list'
|
||||
class SuggestedTopicsBuilder
|
||||
|
||||
attr_reader :excluded_topic_ids
|
||||
attr_reader :results
|
||||
|
||||
def initialize(topic)
|
||||
@excluded_topic_ids = [topic.id]
|
||||
@category_id = topic.category_id
|
||||
@results = []
|
||||
end
|
||||
|
||||
def add_results(results)
|
||||
|
||||
def add_results(results, priority=:low)
|
||||
|
||||
# WARNING .blank? will execute an Active Record query
|
||||
return unless results
|
||||
@@ -23,16 +24,46 @@ class SuggestedTopicsBuilder
|
||||
unless results.empty?
|
||||
# Keep track of the ids we've added
|
||||
@excluded_topic_ids.concat results.map {|r| r.id}
|
||||
splice_results(results,priority)
|
||||
end
|
||||
end
|
||||
|
||||
def splice_results(results, priority)
|
||||
if @category_id &&
|
||||
priority == :high &&
|
||||
non_category_index = @results.index{|r| r.category_id != @category_id}
|
||||
|
||||
category_results, non_category_results = results.partition{|r| r.category_id == @category_id}
|
||||
|
||||
@results.insert non_category_index, *category_results
|
||||
@results.concat non_category_results
|
||||
else
|
||||
@results.concat results
|
||||
end
|
||||
end
|
||||
|
||||
def results
|
||||
@results.first(SiteSetting.suggested_topics)
|
||||
end
|
||||
|
||||
def results_left
|
||||
SiteSetting.suggested_topics - @results.size
|
||||
end
|
||||
|
||||
def full?
|
||||
results_left == 0
|
||||
results_left <= 0
|
||||
end
|
||||
|
||||
def category_results_left
|
||||
SiteSetting.suggested_topics - @results.count{|r| r.category_id == @category_id}
|
||||
end
|
||||
|
||||
def category_full?
|
||||
if @category_id
|
||||
|
||||
else
|
||||
full?
|
||||
end
|
||||
end
|
||||
|
||||
def size
|
||||
|
||||
Reference in New Issue
Block a user