1
0
Fork 0
discourse-custom-wizard-unl.../lib/custom_wizard/realtime_validation.rb
2021-02-15 18:12:32 +05:30

37 Zeilen
1,1 KiB
Ruby

# frozen_string_literal: true
class CustomWizard::RealtimeValidation
cattr_accessor :types
@@types ||= {
similar_topics: { types: [:text], component: "similar-topics-validator", backend: true, required_params: [] }
}
class SimilarTopic
def initialize(topic)
@topic = topic
end
attr_reader :topic
def blurb
Search::GroupedSearchResults.blurb_for(cooked: @topic.try(:blurb))
end
end
def self.similar_topics(params, current_user)
title = params[:title]
raw = params[:raw]
categories = params[:categories]
date_after = params[:date_after]
if title.length < SiteSetting.min_title_similar_length || !Topic.count_exceeds_minimum?
return []
end
topics = Topic.similar_to(title, raw, current_user).to_a
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?
topics.map! { |t| SimilarTopic.new(t) }
{ items: topics, serializer: SimilarTopicSerializer, opts: { root: :similar_topics } }
end
end