<commit_before>class ConversationsController < ApplicationController
  before_action :load_account

  def index
    if params['q'].blank?
      @conversations = @account.conversations.open.includes(:messages)

      # TODO move analytics to javascript?
      # TODO standardise analytics event names
      Analytics.track(user_id: current_user.id, event: 'Read Conversations Index') if signed_in?

    else
      @query = params['q'].to_s.strip

      message_ids = query_messages(@query)
      @conversations = @account.conversations.joins(:messages).where(messages: { id: message_ids })

      if signed_in?
        Analytics.track(user_id: current_user.id, event: 'Searched For', properties: { query: @query.to_s })
      end
    end
  end

  private

  def query_messages(query)
    response = elasticsearch.search body: { query: { match: { content: query } } }
    response['hits']['hits'].map { |x| x["_id"] }
  end

  def elasticsearch
    @es ||= Elasticsearch::Client.new hosts: [ ENV['ELASTICSEARCH_URL'] ]
  end

  def load_account
    @account = Account.first! # simpfy until for now
  end

end<commit_msg>raising a 404 on record not found<commit_after>class ConversationsController < ApplicationController
  before_action :load_account

  def index
    if params['q'].blank?
      @conversations = @account.conversations.open.includes(:messages)

      # TODO move analytics to javascript?
      # TODO standardise analytics event names
      Analytics.track(user_id: current_user.id, event: 'Read Conversations Index') if signed_in?

    else
      @query = params['q'].to_s.strip

      message_ids = query_messages(@query)
      @conversations = @account.conversations.joins(:messages).where(messages: { id: message_ids })

      if signed_in?
        Analytics.track(user_id: current_user.id, event: 'Searched For', properties: { query: @query.to_s })
      end
    end
  end

  private

  def query_messages(query)
    response = elasticsearch.search body: { query: { match: { content: query } } }
    response['hits']['hits'].map { |x| x["_id"] }
  end

  def elasticsearch
    @es ||= Elasticsearch::Client.new hosts: [ ENV['ELASTICSEARCH_URL'] ]
  end

  def load_account
    @account = Account.find_by_slug!(params['account'])
  end

  rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found
  def record_not_found
    raise ActionController::RoutingError.new('Not Found')
  end

end
{'fraction_non_alphanumeric': 0.11290322580645161, 'fraction_numerical': 0.0012406947890818859, 'mean_word_length': 3.715399610136452, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, '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': '3214621', 'n_tokens_mistral': 761, 'n_tokens_neox': 739, 'n_words': 175}