2013-02-05 14:16:51 -05:00
|
|
|
require_dependency 'search'
|
|
|
|
|
|
|
|
class SearchController < ApplicationController
|
|
|
|
|
2015-07-27 02:13:11 -04:00
|
|
|
skip_before_filter :check_xhr, only: :show
|
|
|
|
|
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
|
|
|
|
|
2015-07-27 02:13:11 -04:00
|
|
|
def show
|
2015-09-07 21:03:51 -04:00
|
|
|
search_args = {
|
|
|
|
type_filter: 'topic',
|
|
|
|
guardian: guardian,
|
|
|
|
include_blurbs: true,
|
|
|
|
blurb_length: 300
|
|
|
|
}
|
|
|
|
|
|
|
|
context, type = lookup_search_context
|
|
|
|
if context
|
|
|
|
search_args[:search_context] = context
|
|
|
|
search_args[:type_filter] = type if type
|
|
|
|
end
|
|
|
|
|
2017-07-13 13:34:31 -04:00
|
|
|
search_args[:search_type] = :full_page
|
|
|
|
search_args[:ip_address] = request.remote_ip
|
|
|
|
search_args[:user_id] = current_user.id if current_user.present?
|
|
|
|
|
2015-09-07 21:03:51 -04:00
|
|
|
search = Search.new(params[:q], search_args)
|
2015-07-27 02:13:11 -04:00
|
|
|
result = search.execute
|
|
|
|
|
2016-03-18 14:07:20 -04:00
|
|
|
result.find_user_data(guardian) if result
|
2016-03-18 01:26:20 -04:00
|
|
|
|
2015-08-19 15:40:43 -04:00
|
|
|
serializer = serialize_data(result, GroupedSearchResultSerializer, result: result)
|
2015-07-27 02:13:11 -04:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
store_preloaded("search", MultiJson.dump(serializer))
|
|
|
|
end
|
|
|
|
format.json do
|
|
|
|
render_json_dump(serializer)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
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
|
|
|
|
2015-08-19 15:40:43 -04:00
|
|
|
search_args = { guardian: guardian }
|
|
|
|
|
|
|
|
search_args[:type_filter] = params[:type_filter] if params[:type_filter].present?
|
|
|
|
search_args[:include_blurbs] = params[:include_blurbs] == "true" if params[:include_blurbs].present?
|
|
|
|
search_args[:search_for_id] = true if params[:search_for_id].present?
|
2013-05-24 14:03:45 -04:00
|
|
|
|
2015-09-07 21:03:51 -04:00
|
|
|
context,type = lookup_search_context
|
|
|
|
if context
|
|
|
|
search_args[:search_context] = context
|
|
|
|
search_args[:type_filter] = type if type
|
|
|
|
end
|
|
|
|
|
2017-07-13 13:34:31 -04:00
|
|
|
search_args[:search_type] = :header
|
|
|
|
search_args[:ip_address] = request.remote_ip
|
|
|
|
search_args[:user_id] = current_user.id if current_user.present?
|
|
|
|
|
|
|
|
search = Search.new(params[:term], search_args)
|
2015-09-07 21:03:51 -04:00
|
|
|
result = search.execute
|
|
|
|
render_serialized(result, GroupedSearchResultSerializer, result: result)
|
|
|
|
end
|
|
|
|
|
2017-07-17 15:42:32 -04:00
|
|
|
def click
|
|
|
|
params.require(:search_log_id)
|
|
|
|
params.require(:search_result_type)
|
|
|
|
params.require(:search_result_id)
|
|
|
|
|
|
|
|
if params[:search_result_type] == 'topic'
|
|
|
|
where = { id: params[:search_log_id] }
|
|
|
|
if current_user.present?
|
|
|
|
where[:user_id] = current_user.id
|
|
|
|
else
|
|
|
|
where[:ip_address] = request.remote_ip
|
|
|
|
end
|
|
|
|
|
|
|
|
SearchLog.where(where).update_all(
|
|
|
|
clicked_topic_id: params[:search_result_id]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
2015-09-07 21:03:51 -04:00
|
|
|
protected
|
|
|
|
|
|
|
|
def lookup_search_context
|
|
|
|
|
|
|
|
return if params[:skip_context] == "true"
|
|
|
|
|
2013-05-24 14:03:45 -04:00
|
|
|
search_context = params[:search_context]
|
2015-09-07 21:03:51 -04:00
|
|
|
unless search_context
|
|
|
|
if (context = params[:context]) && (id = params[:context_id])
|
|
|
|
search_context = {type: context, id: id}
|
|
|
|
end
|
|
|
|
end
|
2015-08-19 15:40:43 -04:00
|
|
|
|
2013-05-24 14:03:45 -04:00
|
|
|
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]
|
2015-09-07 21:03:51 -04:00
|
|
|
context_obj = User.find_by(username_lower: search_context[:id].downcase)
|
2016-06-16 23:46:59 -04:00
|
|
|
elsif 'category' == search_context[:type]
|
|
|
|
context_obj = Category.find_by(id: search_context[:id].to_i)
|
|
|
|
elsif 'topic' == search_context[:type]
|
|
|
|
context_obj = Topic.find_by(id: search_context[:id].to_i)
|
2013-05-24 14:03:45 -04:00
|
|
|
end
|
|
|
|
|
2015-09-07 21:03:51 -04:00
|
|
|
type_filter = nil
|
2014-12-03 21:46:52 -05:00
|
|
|
if search_context[:type] == 'private_messages'
|
2015-09-07 21:03:51 -04:00
|
|
|
type_filter = 'private_messages'
|
2014-12-03 21:46:52 -05:00
|
|
|
end
|
|
|
|
|
2013-05-24 14:03:45 -04:00
|
|
|
guardian.ensure_can_see!(context_obj)
|
|
|
|
|
2015-09-07 21:03:51 -04:00
|
|
|
[context_obj, type_filter]
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|