2021-03-11 07:30:15 +01:00
|
|
|
# frozen_string_literal: true
|
2021-02-16 01:43:00 +01:00
|
|
|
class CustomWizard::RealtimeValidation::SimilarTopics
|
|
|
|
attr_accessor :user
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2021-02-16 01:43:00 +01:00
|
|
|
def initialize(user)
|
|
|
|
@user = user
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2021-02-16 01:43:00 +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-03-11 07:30:15 +01:00
|
|
|
|
2021-02-16 01:43:00 +01:00
|
|
|
def perform(params)
|
|
|
|
title = params[:title]
|
|
|
|
raw = params[:raw]
|
|
|
|
categories = params[:categories]
|
2021-02-25 11:29:42 +01:00
|
|
|
time_n_value = params[:time_n_value]
|
2021-02-25 11:06:43 +01:00
|
|
|
time_unit = params[:time_unit]
|
|
|
|
|
2021-02-16 01:43:00 +01:00
|
|
|
result = CustomWizard::RealtimeValidation::Result.new(:similar_topic)
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-05-02 14:57:56 +02:00
|
|
|
if title.length < SiteSetting.min_title_similar_length
|
2021-02-16 01:43:00 +01:00
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
topics = Topic.similar_to(title, raw, user).to_a
|
|
|
|
topics.select! { |t| categories.include?(t.category.id.to_s) } if categories.present?
|
2021-02-25 11:06:43 +01:00
|
|
|
|
2021-02-26 05:16:23 +01:00
|
|
|
if time_n_value.to_i > 0 && time_unit.present?
|
|
|
|
topics.select! { |t| t.created_at >= time_n_value.to_i.send(time_unit).ago }
|
2021-02-25 11:06:43 +01:00
|
|
|
end
|
2021-02-26 05:23:39 +01:00
|
|
|
|
2021-02-16 01:43:00 +01:00
|
|
|
topics.map! { |t| SimilarTopic.new(t) }
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2021-02-16 01:43:00 +01:00
|
|
|
result.items = topics
|
|
|
|
result.serializer_opts = { root: :similar_topics }
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2021-02-16 01:43:00 +01:00
|
|
|
result
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
end
|