discourse/lib/suggested_topics_builder.rb
Jarek Radosz a54e3208cb
DEV: Hand-pick Rails/WhereNot autofixes (#35117)
We can't enable `Rails/WhereNot` lint/autofix, because it would break
code that uses mini_sql instead of AR (which rubocop, and tbh also we,
can't easily differentiate)

Those are safe because they either:
* are executed in AR model scope definitions
* are clearly chained starting from a AR model
* are less-clearly chained, but still can be traced to a AR model/scope

---------

Co-authored-by: Loïc Guitaut <loic@discourse.org>
2025-10-03 13:29:22 +02:00

83 lines
2.2 KiB
Ruby

# frozen_string_literal: true
class SuggestedTopicsBuilder
attr_reader :excluded_topic_ids
def initialize(topic)
@excluded_topic_ids = [topic.id]
@category_id = topic.category_id
@category_topic_ids = Category.topic_ids
@results = []
end
def add_results(results, priority = :low)
# WARNING .blank? will execute an Active Record query
return unless results
# Only add results if we don't have those topic ids already
results = results.where.not(topics: { id: @excluded_topic_ids }).where(visible: true)
# 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
results.reject! { |topic| @category_topic_ids.include?(topic.id) }
unless results.empty?
# 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
results = DiscoursePluginRegistry.apply_modifier(:suggested_topics_add_results, results)
splice_results(results, priority)
end
end
def splice_results(results, priority)
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 }
if other_category_index
@results.insert other_category_index, *category_results
else
@results.concat category_results
end
@results.concat other_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
end
def category_results_left
SiteSetting.suggested_topics - @results.count { |r| r.category_id == @category_id }
end
def size
@results.size
end
end