2013-02-05 14:16:51 -05:00
|
|
|
require_dependency 'search'
|
|
|
|
|
|
|
|
class SearchController < ApplicationController
|
|
|
|
|
2013-05-24 14:03:45 -04:00
|
|
|
def self.valid_context_types
|
2014-12-03 21:46:52 -05:00
|
|
|
%w{user topic category private_messages}
|
2013-05-24 14:03:45 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def query
|
2013-06-06 03:14:32 -04:00
|
|
|
params.require(:term)
|
2014-08-28 03:15:53 -04:00
|
|
|
|
2013-05-24 14:03:45 -04:00
|
|
|
search_args = {guardian: guardian}
|
|
|
|
search_args[:type_filter] = params[:type_filter] if params[:type_filter].present?
|
2014-04-11 16:07:39 -04:00
|
|
|
if params[:include_blurbs].present?
|
|
|
|
search_args[:include_blurbs] = params[:include_blurbs] == "true"
|
|
|
|
end
|
2014-08-28 15:42:29 -04:00
|
|
|
search_args[:search_for_id] = true if params[:search_for_id].present?
|
2013-05-24 14:03:45 -04: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?
|
|
|
|
|
|
|
|
# A user is found by username
|
|
|
|
context_obj = nil
|
2014-12-03 21:46:52 -05:00
|
|
|
if ['user','private_messages'].include? search_context[:type]
|
|
|
|
context_obj = User.find_by(username_lower: params[:search_context][:id].downcase)
|
2013-05-24 14:03:45 -04:00
|
|
|
else
|
2014-12-03 21:46:52 -05:00
|
|
|
klass = search_context[:type].classify.constantize
|
2014-05-06 09:41:59 -04:00
|
|
|
context_obj = klass.find_by(id: params[:search_context][:id])
|
2013-05-24 14:03:45 -04:00
|
|
|
end
|
|
|
|
|
2014-12-03 21:46:52 -05:00
|
|
|
if search_context[:type] == 'private_messages'
|
|
|
|
search_args[:type_filter] = 'private_messages'
|
|
|
|
end
|
|
|
|
|
2013-05-24 14:03:45 -04:00
|
|
|
guardian.ensure_can_see!(context_obj)
|
|
|
|
search_args[:search_context] = context_obj
|
|
|
|
end
|
|
|
|
|
|
|
|
search = Search.new(params[:term], search_args.symbolize_keys)
|
2014-09-02 05:15:08 -04:00
|
|
|
result = search.execute
|
|
|
|
render_serialized(result, GroupedSearchResultSerializer, :result => result)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|