2013-07-13 02:38:20 +08:00
|
|
|
require_dependency 'topic_list'
|
|
|
|
|
|
|
|
class SuggestedTopicsBuilder
|
|
|
|
|
|
|
|
attr_reader :excluded_topic_ids
|
|
|
|
|
|
|
|
def initialize(topic)
|
|
|
|
@excluded_topic_ids = [topic.id]
|
2013-08-28 08:51:49 +08:00
|
|
|
@category_id = topic.category_id
|
2013-11-22 05:43:22 +08:00
|
|
|
@category_topic_ids = Category.pluck(:topic_id).compact
|
2013-07-13 02:38:20 +08:00
|
|
|
@results = []
|
|
|
|
end
|
|
|
|
|
2013-08-28 08:51:49 +08:00
|
|
|
|
|
|
|
def add_results(results, priority=:low)
|
2013-07-13 02:38:20 +08:00
|
|
|
|
2013-07-30 07:54:29 +08:00
|
|
|
# WARNING .blank? will execute an Active Record query
|
|
|
|
return unless results
|
2013-07-13 02:38:20 +08:00
|
|
|
|
|
|
|
# Only add results if we don't have those topic ids already
|
|
|
|
results = results.where('topics.id NOT IN (?)', @excluded_topic_ids)
|
2014-02-05 01:26:38 +08:00
|
|
|
.where(visible: true)
|
2014-06-04 05:48:18 +08:00
|
|
|
|
|
|
|
# If limit suggested to category is enabled, restrict to that category
|
|
|
|
if @category_id && SiteSetting.limit_suggested_to_category?
|
|
|
|
results = results.where(category_id: @category_id)
|
|
|
|
end
|
|
|
|
results = results.to_a.reject { |topic| @category_topic_ids.include?(topic.id) }
|
2013-07-13 02:38:20 +08:00
|
|
|
|
2013-07-30 07:54:29 +08:00
|
|
|
unless results.empty?
|
|
|
|
# Keep track of the ids we've added
|
|
|
|
@excluded_topic_ids.concat results.map {|r| r.id}
|
2013-08-28 08:51:49 +08:00
|
|
|
splice_results(results,priority)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def splice_results(results, priority)
|
2014-01-14 04:02:08 +08:00
|
|
|
if @category_id && priority == :high
|
2013-08-28 08:51:49 +08:00
|
|
|
|
2014-01-14 04:02:08 +08:00
|
|
|
# Topics from category @category_id need to be first in the list, all others after.
|
2013-08-28 08:51:49 +08:00
|
|
|
|
2014-01-14 04:02:08 +08:00
|
|
|
other_category_index = @results.index { |r| r.category_id != @category_id }
|
|
|
|
category_results, other_category_results = results.partition{ |r| r.category_id == @category_id }
|
|
|
|
|
|
|
|
if other_category_index
|
|
|
|
@results.insert other_category_index, *category_results
|
|
|
|
else
|
|
|
|
@results.concat category_results
|
|
|
|
end
|
|
|
|
@results.concat other_category_results
|
2013-08-28 08:51:49 +08:00
|
|
|
else
|
2013-07-30 07:54:29 +08:00
|
|
|
@results.concat results
|
|
|
|
end
|
2013-07-13 02:38:20 +08:00
|
|
|
end
|
|
|
|
|
2013-08-28 08:51:49 +08:00
|
|
|
def results
|
|
|
|
@results.first(SiteSetting.suggested_topics)
|
|
|
|
end
|
|
|
|
|
2013-07-13 02:38:20 +08:00
|
|
|
def results_left
|
|
|
|
SiteSetting.suggested_topics - @results.size
|
|
|
|
end
|
|
|
|
|
|
|
|
def full?
|
2013-08-28 08:51:49 +08:00
|
|
|
results_left <= 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def category_results_left
|
|
|
|
SiteSetting.suggested_topics - @results.count{|r| r.category_id == @category_id}
|
|
|
|
end
|
|
|
|
|
2013-07-13 02:38:20 +08:00
|
|
|
def size
|
|
|
|
@results.size
|
|
|
|
end
|
|
|
|
|
2013-07-30 07:54:29 +08:00
|
|
|
end
|