2013-02-05 13:16:51 -06:00
|
|
|
require_dependency 'search'
|
|
|
|
|
|
|
|
class SearchController < ApplicationController
|
|
|
|
|
2013-05-24 13:03:45 -05:00
|
|
|
def self.valid_context_types
|
|
|
|
%w{user topic category}
|
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
def query
|
2013-06-06 02:14:32 -05:00
|
|
|
params.require(:term)
|
2014-04-11 15:07:39 -05:00
|
|
|
|
2013-05-24 13:03:45 -05:00
|
|
|
search_args = {guardian: guardian}
|
|
|
|
search_args[:type_filter] = params[:type_filter] if params[:type_filter].present?
|
2014-04-11 15:07:39 -05:00
|
|
|
if params[:include_blurbs].present?
|
|
|
|
search_args[:include_blurbs] = params[:include_blurbs] == "true"
|
|
|
|
end
|
2013-05-24 13:03:45 -05:00
|
|
|
|
|
|
|
search_context = params[:search_context]
|
|
|
|
if search_context.present?
|
|
|
|
raise Discourse::InvalidParameters.new(:search_context) unless SearchController.valid_context_types.include?(search_context[:type])
|
|
|
|
raise Discourse::InvalidParameters.new(:search_context) if search_context[:id].blank?
|
|
|
|
|
|
|
|
klass = search_context[:type].classify.constantize
|
2013-05-22 13:36:14 -05:00
|
|
|
|
2013-05-24 13:03:45 -05:00
|
|
|
# A user is found by username
|
|
|
|
context_obj = nil
|
|
|
|
if search_context[:type] == 'user'
|
2013-06-10 09:42:06 -05:00
|
|
|
context_obj = klass.where(username_lower: params[:search_context][:id].downcase).first
|
2013-05-24 13:03:45 -05:00
|
|
|
else
|
|
|
|
context_obj = klass.where(id: params[:search_context][:id]).first
|
|
|
|
end
|
|
|
|
|
|
|
|
guardian.ensure_can_see!(context_obj)
|
|
|
|
search_args[:search_context] = context_obj
|
|
|
|
end
|
|
|
|
|
|
|
|
search = Search.new(params[:term], search_args.symbolize_keys)
|
2013-05-22 13:36:14 -05:00
|
|
|
render_json_dump(search.execute.as_json)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|