mirror of
https://github.com/discourse/discourse.git
synced 2025-05-28 13:51:18 +08:00
DEV: Apply syntax_tree formatting to app/*
This commit is contained in:
@ -1,13 +1,12 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class SearchController < ApplicationController
|
||||
|
||||
before_action :cancel_overloaded_search, only: [:query]
|
||||
skip_before_action :check_xhr, only: :show
|
||||
after_action :add_noindex_header
|
||||
|
||||
def self.valid_context_types
|
||||
%w{user topic category private_messages tag}
|
||||
%w[user topic category private_messages tag]
|
||||
end
|
||||
|
||||
def show
|
||||
@ -16,12 +15,9 @@ class SearchController < ApplicationController
|
||||
|
||||
# a q param has been given but it's not in the correct format
|
||||
# eg: ?q[foo]=bar
|
||||
if params[:q].present? && !@search_term.present?
|
||||
raise Discourse::InvalidParameters.new(:q)
|
||||
end
|
||||
raise Discourse::InvalidParameters.new(:q) if params[:q].present? && !@search_term.present?
|
||||
|
||||
if @search_term.present? &&
|
||||
@search_term.length < SiteSetting.min_search_term_length
|
||||
if @search_term.present? && @search_term.length < SiteSetting.min_search_term_length
|
||||
raise Discourse::InvalidParameters.new(:q)
|
||||
end
|
||||
|
||||
@ -31,21 +27,17 @@ class SearchController < ApplicationController
|
||||
|
||||
page = permitted_params[:page]
|
||||
# check for a malformed page parameter
|
||||
if page && (!page.is_a?(String) || page.to_i.to_s != page)
|
||||
raise Discourse::InvalidParameters
|
||||
end
|
||||
raise Discourse::InvalidParameters if page && (!page.is_a?(String) || page.to_i.to_s != page)
|
||||
|
||||
rate_limit_errors = rate_limit_search
|
||||
|
||||
discourse_expires_in 1.minute
|
||||
|
||||
search_args = {
|
||||
type_filter: 'topic',
|
||||
type_filter: "topic",
|
||||
guardian: guardian,
|
||||
blurb_length: 300,
|
||||
page: if page.to_i <= 10
|
||||
[page.to_i, 1].max
|
||||
end
|
||||
page: ([page.to_i, 1].max if page.to_i <= 10),
|
||||
}
|
||||
|
||||
context, type = lookup_search_context
|
||||
@ -59,19 +51,21 @@ class SearchController < ApplicationController
|
||||
search_args[:user_id] = current_user.id if current_user.present?
|
||||
|
||||
if rate_limit_errors
|
||||
result = Search::GroupedSearchResults.new(
|
||||
type_filter: search_args[:type_filter],
|
||||
term: @search_term,
|
||||
search_context: context
|
||||
)
|
||||
result =
|
||||
Search::GroupedSearchResults.new(
|
||||
type_filter: search_args[:type_filter],
|
||||
term: @search_term,
|
||||
search_context: context,
|
||||
)
|
||||
|
||||
result.error = I18n.t("rate_limiter.slow_down")
|
||||
elsif site_overloaded?
|
||||
result = Search::GroupedSearchResults.new(
|
||||
type_filter: search_args[:type_filter],
|
||||
term: @search_term,
|
||||
search_context: context
|
||||
)
|
||||
result =
|
||||
Search::GroupedSearchResults.new(
|
||||
type_filter: search_args[:type_filter],
|
||||
term: @search_term,
|
||||
search_context: context,
|
||||
)
|
||||
|
||||
result.error = I18n.t("search.extreme_load_error")
|
||||
else
|
||||
@ -83,12 +77,8 @@ class SearchController < ApplicationController
|
||||
serializer = serialize_data(result, GroupedSearchResultSerializer, result: result)
|
||||
|
||||
respond_to do |format|
|
||||
format.html do
|
||||
store_preloaded("search", MultiJson.dump(serializer))
|
||||
end
|
||||
format.json do
|
||||
render_json_dump(serializer)
|
||||
end
|
||||
format.html { store_preloaded("search", MultiJson.dump(serializer)) }
|
||||
format.json { render_json_dump(serializer) }
|
||||
end
|
||||
end
|
||||
|
||||
@ -105,8 +95,8 @@ class SearchController < ApplicationController
|
||||
|
||||
search_args = { guardian: guardian }
|
||||
|
||||
search_args[:type_filter] = params[:type_filter] if params[:type_filter].present?
|
||||
search_args[:search_for_id] = true if params[:search_for_id].present?
|
||||
search_args[:type_filter] = params[:type_filter] if params[:type_filter].present?
|
||||
search_args[:search_for_id] = true if params[:search_for_id].present?
|
||||
|
||||
context, type = lookup_search_context
|
||||
|
||||
@ -118,22 +108,26 @@ class SearchController < ApplicationController
|
||||
search_args[:search_type] = :header
|
||||
search_args[:ip_address] = request.remote_ip
|
||||
search_args[:user_id] = current_user.id if current_user.present?
|
||||
search_args[:restrict_to_archetype] = params[:restrict_to_archetype] if params[:restrict_to_archetype].present?
|
||||
search_args[:restrict_to_archetype] = params[:restrict_to_archetype] if params[
|
||||
:restrict_to_archetype
|
||||
].present?
|
||||
|
||||
if rate_limit_errors
|
||||
result = Search::GroupedSearchResults.new(
|
||||
type_filter: search_args[:type_filter],
|
||||
term: params[:term],
|
||||
search_context: context
|
||||
)
|
||||
result =
|
||||
Search::GroupedSearchResults.new(
|
||||
type_filter: search_args[:type_filter],
|
||||
term: params[:term],
|
||||
search_context: context,
|
||||
)
|
||||
|
||||
result.error = I18n.t("rate_limiter.slow_down")
|
||||
elsif site_overloaded?
|
||||
result = GroupedSearchResults.new(
|
||||
type_filter: search_args[:type_filter],
|
||||
term: params[:term],
|
||||
search_context: context
|
||||
)
|
||||
result =
|
||||
GroupedSearchResults.new(
|
||||
type_filter: search_args[:type_filter],
|
||||
term: params[:term],
|
||||
search_context: context,
|
||||
)
|
||||
else
|
||||
search = Search.new(params[:term], search_args)
|
||||
result = search.execute(readonly_mode: @readonly_mode)
|
||||
@ -163,7 +157,7 @@ class SearchController < ApplicationController
|
||||
|
||||
SearchLog.where(attributes).update_all(
|
||||
search_result_type: SearchLog.search_result_types[search_result_type],
|
||||
search_result_id: search_result_id
|
||||
search_result_id: search_result_id,
|
||||
)
|
||||
end
|
||||
|
||||
@ -173,7 +167,7 @@ class SearchController < ApplicationController
|
||||
protected
|
||||
|
||||
def site_overloaded?
|
||||
queue_time = request.env['REQUEST_QUEUE_SECONDS']
|
||||
queue_time = request.env["REQUEST_QUEUE_SECONDS"]
|
||||
if queue_time
|
||||
threshold = GlobalSetting.disable_search_queue_threshold.to_f
|
||||
threshold > 0 && queue_time > threshold
|
||||
@ -185,10 +179,25 @@ class SearchController < ApplicationController
|
||||
def rate_limit_search
|
||||
begin
|
||||
if current_user.present?
|
||||
RateLimiter.new(current_user, "search-min", SiteSetting.rate_limit_search_user, 1.minute).performed!
|
||||
RateLimiter.new(
|
||||
current_user,
|
||||
"search-min",
|
||||
SiteSetting.rate_limit_search_user,
|
||||
1.minute,
|
||||
).performed!
|
||||
else
|
||||
RateLimiter.new(nil, "search-min-#{request.remote_ip}", SiteSetting.rate_limit_search_anon_user, 1.minute).performed!
|
||||
RateLimiter.new(nil, "search-min-anon-global", SiteSetting.rate_limit_search_anon_global, 1.minute).performed!
|
||||
RateLimiter.new(
|
||||
nil,
|
||||
"search-min-#{request.remote_ip}",
|
||||
SiteSetting.rate_limit_search_anon_user,
|
||||
1.minute,
|
||||
).performed!
|
||||
RateLimiter.new(
|
||||
nil,
|
||||
"search-min-anon-global",
|
||||
SiteSetting.rate_limit_search_anon_global,
|
||||
1.minute,
|
||||
).performed!
|
||||
end
|
||||
rescue RateLimiter::LimitExceeded => e
|
||||
return e
|
||||
@ -197,13 +206,10 @@ class SearchController < ApplicationController
|
||||
end
|
||||
|
||||
def cancel_overloaded_search
|
||||
if site_overloaded?
|
||||
render_json_error I18n.t("search.extreme_load_error"), status: 409
|
||||
end
|
||||
render_json_error I18n.t("search.extreme_load_error"), status: 409 if site_overloaded?
|
||||
end
|
||||
|
||||
def lookup_search_context
|
||||
|
||||
return if params[:skip_context] == "true"
|
||||
|
||||
search_context = params[:search_context]
|
||||
@ -214,30 +220,29 @@ class SearchController < ApplicationController
|
||||
end
|
||||
|
||||
if search_context.present?
|
||||
raise Discourse::InvalidParameters.new(:search_context) unless SearchController.valid_context_types.include?(search_context[:type])
|
||||
unless SearchController.valid_context_types.include?(search_context[:type])
|
||||
raise Discourse::InvalidParameters.new(:search_context)
|
||||
end
|
||||
raise Discourse::InvalidParameters.new(:search_context) if search_context[:id].blank?
|
||||
|
||||
# A user is found by username
|
||||
context_obj = nil
|
||||
if ['user', 'private_messages'].include? search_context[:type]
|
||||
if %w[user private_messages].include? search_context[:type]
|
||||
context_obj = User.find_by(username_lower: search_context[:id].downcase)
|
||||
elsif 'category' == search_context[:type]
|
||||
elsif "category" == search_context[:type]
|
||||
context_obj = Category.find_by(id: search_context[:id].to_i)
|
||||
elsif 'topic' == search_context[:type]
|
||||
elsif "topic" == search_context[:type]
|
||||
context_obj = Topic.find_by(id: search_context[:id].to_i)
|
||||
elsif 'tag' == search_context[:type]
|
||||
elsif "tag" == search_context[:type]
|
||||
context_obj = Tag.where_name(search_context[:name]).first
|
||||
end
|
||||
|
||||
type_filter = nil
|
||||
if search_context[:type] == 'private_messages'
|
||||
type_filter = 'private_messages'
|
||||
end
|
||||
type_filter = "private_messages" if search_context[:type] == "private_messages"
|
||||
|
||||
guardian.ensure_can_see!(context_obj)
|
||||
|
||||
[context_obj, type_filter]
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
Reference in New Issue
Block a user