2021-01-27 06:08:26 +01:00
|
|
|
class CustomWizard::RealtimeValidation
|
|
|
|
cattr_accessor :types
|
|
|
|
@@types ||= {
|
2021-02-11 18:05:04 +01:00
|
|
|
similar_topics: { types: [:text], component: "similar-topics-validator", backend: true, required_params: [] }
|
2021-01-27 06:08:26 +01:00
|
|
|
}
|
2021-02-01 14:58:37 +01:00
|
|
|
|
|
|
|
class SimilarTopic
|
|
|
|
def initialize(topic)
|
|
|
|
@topic = topic
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :topic
|
|
|
|
|
|
|
|
def blurb
|
|
|
|
Search::GroupedSearchResults.blurb_for(cooked: @topic.try(:blurb))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-05 13:59:30 +01:00
|
|
|
def self.similar_topics(params, current_user)
|
2021-02-01 14:58:37 +01:00
|
|
|
title = params[:title]
|
|
|
|
raw = params[:raw]
|
2021-02-05 13:59:30 +01:00
|
|
|
categories = params[:categories]
|
|
|
|
date_after = params[:date_after]
|
2021-02-01 14:58:37 +01:00
|
|
|
|
|
|
|
if title.length < SiteSetting.min_title_similar_length || !Topic.count_exceeds_minimum?
|
|
|
|
return []
|
|
|
|
end
|
|
|
|
|
|
|
|
topics = Topic.similar_to(title, raw, current_user).to_a
|
2021-02-05 13:59:30 +01:00
|
|
|
topics.select! { |t| categories.include?(t.category.id.to_s) } if categories.present?
|
|
|
|
topics.select! { |t| t.created_at > DateTime.parse(date_after) } if date_after.present?
|
2021-02-01 14:58:37 +01:00
|
|
|
topics.map! { |t| SimilarTopic.new(t) }
|
|
|
|
::ActiveModel::ArraySerializer.new(topics, each_serializer: SimilarTopicSerializer, root: :similar_topics, rest_serializer: true, scope: ::Guardian.new(current_user))
|
|
|
|
end
|
|
|
|
end
|