<commit_before>require_dependency 'search'

class SearchController < ApplicationController

  def self.valid_context_types
    %w{user topic category}
  end

  def query
    params.require(:term)

    search_args = {guardian: guardian}
    search_args[:type_filter] = params[:type_filter] if params[:type_filter].present?

    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

      # A user is found by username
      context_obj = nil
      if search_context[:type] == 'user'
        context_obj = klass.where(username: params[:search_context][:id]).first
      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)
    render_json_dump(search.execute.as_json)
  end

end<commit_msg>FIX: Search wasn't using the lowercase username for finding the context.<commit_after>require_dependency 'search'

class SearchController < ApplicationController

  def self.valid_context_types
    %w{user topic category}
  end

  def query
    params.require(:term)

    search_args = {guardian: guardian}
    search_args[:type_filter] = params[:type_filter] if params[:type_filter].present?

    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

      # A user is found by username
      context_obj = nil
      if search_context[:type] == 'user'
        context_obj = klass.where(username_lower: params[:search_context][:id].downcase).first
      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)
    render_json_dump(search.execute.as_json)
  end

end
{'fraction_non_alphanumeric': 0.09935379644588045, 'fraction_numerical': 0.0, 'mean_word_length': 4.3498920086393085, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3381352', 'n_tokens_mistral': 722, 'n_tokens_neox': 708, 'n_words': 137}