2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-07-12 14:38:20 -04:00
|
|
|
class SuggestedTopicsBuilder
|
|
|
|
|
|
|
|
attr_reader :excluded_topic_ids
|
|
|
|
|
|
|
|
def initialize(topic)
|
|
|
|
@excluded_topic_ids = [topic.id]
|
2013-08-27 20:51:49 -04:00
|
|
|
@category_id = topic.category_id
|
2016-07-18 22:34:54 -04:00
|
|
|
@category_topic_ids = Category.topic_ids
|
2013-07-12 14:38:20 -04:00
|
|
|
@results = []
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def add_results(results, priority = :low)
|
2013-07-12 14:38:20 -04:00
|
|
|
|
2013-07-29 19:54:29 -04:00
|
|
|
# WARNING .blank? will execute an Active Record query
|
|
|
|
return unless results
|
2013-07-12 14:38:20 -04:00
|
|
|
|
|
|
|
# Only add results if we don't have those topic ids already
|
2018-10-29 01:09:58 -04:00
|
|
|
results = results
|
|
|
|
.where('topics.id NOT IN (?)', @excluded_topic_ids)
|
2017-07-27 21:20:09 -04:00
|
|
|
.where(visible: true)
|
2014-06-03 17:48:18 -04: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
|
2017-09-08 01:10:37 -04:00
|
|
|
|
|
|
|
results = results.to_a
|
|
|
|
results.reject! { |topic| @category_topic_ids.include?(topic.id) }
|
2013-07-12 14:38:20 -04:00
|
|
|
|
2013-07-29 19:54:29 -04:00
|
|
|
unless results.empty?
|
2018-10-29 01:09:58 -04:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
splice_results(results, priority)
|
2013-08-27 20:51:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def splice_results(results, priority)
|
2018-10-29 01:09:58 -04:00
|
|
|
if priority == :ultra_high
|
|
|
|
@results.insert 0, *results
|
2013-08-27 20:51:49 -04:00
|
|
|
|
2018-10-29 01:09:58 -04:00
|
|
|
elsif @category_id && priority == :high
|
2014-01-13 15:02:08 -05:00
|
|
|
# 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 }
|
2017-07-27 21:20:09 -04:00
|
|
|
category_results, other_category_results = results.partition { |r| r.category_id == @category_id }
|
2014-01-13 15:02:08 -05:00
|
|
|
|
|
|
|
if other_category_index
|
|
|
|
@results.insert other_category_index, *category_results
|
|
|
|
else
|
|
|
|
@results.concat category_results
|
|
|
|
end
|
|
|
|
@results.concat other_category_results
|
2013-08-27 20:51:49 -04:00
|
|
|
else
|
2013-07-29 19:54:29 -04:00
|
|
|
@results.concat results
|
|
|
|
end
|
2013-07-12 14:38:20 -04:00
|
|
|
end
|
|
|
|
|
2013-08-27 20:51:49 -04:00
|
|
|
def results
|
|
|
|
@results.first(SiteSetting.suggested_topics)
|
|
|
|
end
|
|
|
|
|
2013-07-12 14:38:20 -04:00
|
|
|
def results_left
|
|
|
|
SiteSetting.suggested_topics - @results.size
|
|
|
|
end
|
|
|
|
|
|
|
|
def full?
|
2013-08-27 20:51:49 -04:00
|
|
|
results_left <= 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def category_results_left
|
2017-07-27 21:20:09 -04:00
|
|
|
SiteSetting.suggested_topics - @results.count { |r| r.category_id == @category_id }
|
2013-08-27 20:51:49 -04:00
|
|
|
end
|
|
|
|
|
2013-07-12 14:38:20 -04:00
|
|
|
def size
|
|
|
|
@results.size
|
|
|
|
end
|
|
|
|
|
2013-07-29 19:54:29 -04:00
|
|
|
end
|