<commit_before>class SearchController < ApplicationController
  skip_before_filter :verify_authenticity_token, only: [:slack]

  def index
  end

  def show
    @found = Gif.getgifs(params[:search][:query])
  end

  def slack
    text, channel, username, domain = params[:text].downcase, params[:channel_name], params[:user_name], params[:team_domain]
    @team = Team.find_by_domain domain
    return render json: 'Team not found' unless @team
    if !!(text =~ /^(upvote|downvote)$/)
      vote text, domain, channel, username
    else
      found = find_gifs_for text
      post_gif_to_slack(found, text, channel, username)
    end
    return render nothing: true
  end

  private

  def find_gifs_for(query)
    @gifs = Gif.getgifs(query)
    return no_gifs if (@gifs.empty?) || (!@gifs.sample.instance_of? Gif)
    @team.gifs << @gifs.reject { |gif| @team.gifs.include?(gif) }
    @gif = pick_random
    @gif
  end

  # a pick raondom gif function that takes the votes into account
  def pick_random
    id_array = @gifs.map(&:id)
    @teamgifs = @team.teamgifs.where(gif_id: id_array )
    @total_votes = @teamgifs.map(&:votes).inject(0, &:+)
    @total = 0 # makes this variable aviable in the map
    # hacky way to pair gif_ids with their total of votes as a %
    range_pairs = @teamgifs.collect.map{ |gif| [gif.id, @total += gif.votes * 100 / @total_votes] }.to_h
    @random_number = rand(0..100) # make the random number avaible to the select
    @selected_id = range_pairs.select { |_key, value| @random_number <= value }.keys.first
    choosen_gif = @gifs.select{ |gif| gif.id == @selected_id }.first
    choosen_gif
  end
  
  def post_gif_to_slack(image, text, channel, username)
    store_last_gif_data @team.domain, channel, image.id
    responselink = '<' + image.url + '?' + Random.rand(500).to_s + '|' + ' /reactif ' + text + '>'
    post_to_slack @team.webhook, username, channel, responselink
  end

  def no_gifs
    render json: 'No gifs found'
  end

  def store_last_gif_data(team_domain, channel, gif_id)
    last = Lastgif.find(team_domain: team_domain, channel: channel).first
    last.delete unless last.nil? 
    Lastgif.create team_domain: team_domain, channel: channel, gif_id: gif_id
  end

  def get_last_gif(team_domain, channel)
    last_gif = Lastgif.find(team_domain: team_domain, channel: channel).first
    last_gif
  end

  def vote(query, domain, channel, _username)<commit_msg>fix bug that couldn't find gifs when there were more than 1 team<commit_after>class SearchController < ApplicationController
  skip_before_filter :verify_authenticity_token, only: [:slack]

  def index
  end

  def show
    @found = Gif.getgifs(params[:search][:query])
  end

  def slack
    text, channel, username, domain = params[:text].downcase, params[:channel_name], params[:user_name], params[:team_domain]
    @team = Team.find_by_domain domain
    return render json: 'Team not found' unless @team
    if !!(text =~ /^(upvote|downvote)$/)
      vote text, domain, channel, username
    else
      found = find_gifs_for text
      post_gif_to_slack(found, text, channel, username)
    end
    return render nothing: true
  end

  private

  def find_gifs_for(query)
    @gifs = Gif.getgifs(query)
    return no_gifs if (@gifs.empty?) || (!@gifs.sample.instance_of? Gif)
    @team.gifs << @gifs.reject { |gif| @team.gifs.include?(gif) }
    @gif = pick_random
    @gif
  end

  # a pick raondom gif function that takes the votes into account
  def pick_random
    id_array = @gifs.map(&:id)
    @teamgifs = @team.teamgifs.where(gif_id: id_array )
    @total_votes = @teamgifs.map(&:votes).inject(0, &:+)
    @total = 0 # makes this variable aviable in the map
    # hacky way to pair gif_ids with their total of votes as a %
    range_pairs = @teamgifs.collect.map{ |gif| [gif.gif_id, @total += gif.votes * 100 / @total_votes] }.to_h
    @random_number = rand(0..100) # make the random number avaible to the select
    @selected_id = range_pairs.select { |_key, value| @random_number <= value }.keys.first
    choosen_gif = @gifs.select{ |gif| gif.id == @selected_id }.first
    choosen_gif
  end
  
  def post_gif_to_slack(image, text, channel, username)
    store_last_gif_data @team.domain, channel, image.id
    responselink = '<' + image.url + '?' + Random.rand(500).to_s + '|' + ' /reactif ' + text + '>'
    post_to_slack @team.webhook, username, channel, responselink
  end

  def no_gifs
    render json: 'No gifs found'
  end

  def store_last_gif_data(team_domain, channel, gif_id)
    last = Lastgif.find(team_domain: team_domain, channel: channel).first
    last.delete unless last.nil? 
    Lastgif.create team_domain: team_domain, channel: channel, gif_id: gif_id
  end

  def get_last_gif(team_domain, channel)
    last_gif = Lastgif.find(team_domain: team_domain, channel: channel).first
    last_gif
  end

  def vote(query, domain, channel, _username)
{'fraction_non_alphanumeric': 0.11528259538869619, 'fraction_numerical': 0.005100999795960008, 'mean_word_length': 3.7179980750721846, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 5, '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': '653395', 'n_tokens_mistral': 1749, 'n_tokens_neox': 1701, 'n_words': 441}