2015-06-24 15:08:22 -04:00
|
|
|
require_dependency 'similar_topic_serializer'
|
|
|
|
require_dependency 'search/grouped_search_results'
|
|
|
|
|
|
|
|
class SimilarTopicsController < ApplicationController
|
|
|
|
|
|
|
|
class SimilarTopic
|
|
|
|
def initialize(topic)
|
|
|
|
@topic = topic
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :topic
|
|
|
|
|
|
|
|
def blurb
|
|
|
|
Search::GroupedSearchResults.blurb_for(@topic.try(:blurb))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def index
|
|
|
|
params.require(:title)
|
|
|
|
params.require(:raw)
|
|
|
|
title, raw = params[:title], params[:raw]
|
2015-08-31 10:54:45 -04:00
|
|
|
invalid_length = [:title, :raw].any? { |key| check_invalid_length(key, params[key]) }
|
2015-06-24 15:08:22 -04:00
|
|
|
|
2015-08-31 10:54:45 -04:00
|
|
|
# Only suggest similar topics if the site has a minimum amount of topics present
|
|
|
|
# and params are long enough.
|
|
|
|
return render json: [] if invalid_length || !Topic.count_exceeds_minimum?
|
2015-06-24 15:08:22 -04:00
|
|
|
|
|
|
|
topics = Topic.similar_to(title, raw, current_user).to_a
|
2017-07-27 21:20:09 -04:00
|
|
|
topics.map! { |t| SimilarTopic.new(t) }
|
2015-06-24 15:08:22 -04:00
|
|
|
render_serialized(topics, SimilarTopicSerializer, root: :similar_topics, rest_serializer: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2015-08-31 10:54:45 -04:00
|
|
|
def check_invalid_length(key, attr)
|
2015-06-24 15:08:22 -04:00
|
|
|
str = (key == :raw) ? "body" : key.to_s
|
2015-08-31 10:54:45 -04:00
|
|
|
attr.length < SiteSetting.send("min_#{str}_similar_length")
|
2015-06-24 15:08:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|