Class: SearchController

Inherits:
ApplicationController show all
Defined in:
app/controllers/search_controller.rb

Constant Summary

Constants inherited from ApplicationController

ApplicationController::LEGACY_NO_THEMES, ApplicationController::LEGACY_NO_UNOFFICIAL_PLUGINS, ApplicationController::NO_PLUGINS, ApplicationController::NO_THEMES, ApplicationController::NO_UNOFFICIAL_PLUGINS, ApplicationController::SAFE_MODE

Constants included from CanonicalURL::ControllerExtensions

CanonicalURL::ControllerExtensions::ALLOWED_CANONICAL_PARAMS

Instance Attribute Summary

Attributes inherited from ApplicationController

#theme_id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationController

#application_layout, #can_cache_content?, #clear_notifications, #conditionally_allow_site_embedding, #current_homepage, #discourse_expires_in, #dont_cache_page, #ember_cli_required?, #fetch_user_from_params, #guardian, #handle_permalink, #handle_theme, #handle_unverified_request, #has_escaped_fragment?, #immutable_for, #no_cookies, #perform_refresh_session, #post_ids_including_replies, #preload_json, #rate_limit_second_factor!, #redirect_with_client_support, #render_json_dump, #render_serialized, requires_plugin, #rescue_discourse_actions, #resolve_safe_mode, #secure_session, #serialize_data, #set_current_user_for_logs, #set_layout, #set_mobile_view, #set_mp_snapshot_fields, #show_browser_update?, #store_preloaded, #use_crawler_layout?, #with_resolved_locale

Methods included from VaryHeader

#ensure_vary_header

Methods included from ReadOnlyMixin

#add_readonly_header, #allowed_in_staff_writes_only_mode?, #block_if_readonly_mode, #check_readonly_mode, included, #staff_writes_only_mode?

Methods included from Hijack

#hijack

Methods included from GlobalPath

#cdn_path, #cdn_relative_path, #full_cdn_url, #path, #upload_cdn_path

Methods included from JsonError

#create_errors_json

Methods included from CanonicalURL::ControllerExtensions

#canonical_url, #default_canonical, included

Methods included from CurrentUser

#clear_current_user, #current_user, has_auth_cookie?, #is_api?, #is_user_api?, #log_off_user, #log_on_user, lookup_from_env, #refresh_session

Class Method Details

.valid_context_typesObject



8
9
10
# File 'app/controllers/search_controller.rb', line 8

def self.valid_context_types
  %w[user topic category private_messages tag]
end

Instance Method Details

#clickObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'app/controllers/search_controller.rb', line 126

def click
  params.require(:search_log_id)
  params.require(:search_result_type)
  params.require(:search_result_id)

  search_result_type = params[:search_result_type].downcase.to_sym
  if SearchLog.search_result_types.has_key?(search_result_type)
    attributes = { id: params[:search_log_id] }
    if current_user.present?
      attributes[:user_id] = current_user.id
    else
      attributes[:ip_address] = request.remote_ip
    end

    if search_result_type == :tag
      search_result_id = Tag.find_by_name(params[:search_result_id])&.id
    else
      search_result_id = params[:search_result_id]
    end

    SearchLog.where(attributes).update_all(
      search_result_type: SearchLog.search_result_types[search_result_type],
      search_result_id: search_result_id,
    )
  end

  render json: success_json
end

#queryObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/controllers/search_controller.rb', line 79

def query
  params.require(:term)

  if params[:term].include?("\u0000")
    raise Discourse::InvalidParameters.new("string contains null byte")
  end

  discourse_expires_in 1.minute

  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?

  context, type = lookup_search_context

  if context
    search_args[:search_context] = context
    search_args[:type_filter] = type if type
  end

  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?

  if rate_limit_search
    return(
      render json: failed_json.merge(message: I18n.t("rate_limiter.slow_down")),
             status: :too_many_requests
    )
  elsif site_overloaded?
    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)
  end
  render_serialized(result, GroupedSearchResultSerializer, result: result)
end

#showObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/search_controller.rb', line 12

def show
  permitted_params = params.permit(:q, :page)
  @search_term = permitted_params[:q]

  # a q param has been given but it's not in the correct format
  # eg: ?q[foo]=bar
  raise Discourse::InvalidParameters.new(:q) if params[:q].present? && !@search_term.present?

  if @search_term.present? && @search_term.length < SiteSetting.min_search_term_length
    raise Discourse::InvalidParameters.new(:q)
  end

  if @search_term.present? && @search_term.include?("\u0000")
    raise Discourse::InvalidParameters.new("string contains null byte")
  end

  page = permitted_params[:page]
  # check for a malformed page parameter
  raise Discourse::InvalidParameters if page && (!page.is_a?(String) || page.to_i.to_s != page)

  discourse_expires_in 1.minute

  search_args = {
    type_filter: "topic",
    guardian: guardian,
    blurb_length: 300,
    page: ([page.to_i, 1].max if page.to_i <= 10),
  }

  context, type = lookup_search_context
  if context
    search_args[:search_context] = context
    search_args[:type_filter] = type if type
  end

  search_args[:search_type] = :full_page
  search_args[:ip_address] = request.remote_ip
  search_args[:user_id] = current_user.id if current_user.present?

  if rate_limit_search
    return(
      render json: failed_json.merge(message: I18n.t("rate_limiter.slow_down")),
             status: :too_many_requests
    )
  elsif site_overloaded?
    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
    search = Search.new(@search_term, search_args)
    result = search.execute(readonly_mode: @readonly_mode)
    result.find_user_data(guardian) if result
  end

  serializer = serialize_data(result, GroupedSearchResultSerializer, result: result)

  respond_to do |format|
    format.html { store_preloaded("search", MultiJson.dump(serializer)) }
    format.json { render_json_dump(serializer) }
  end
end