114 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
			
		
		
	
	
			114 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
class MessagesController < ApplicationController
 | 
						|
  include CompletionsConcern
 | 
						|
 | 
						|
  skip_before_action :verify_authenticity_token, only: %i[ create connect cors_preflight_check ]
 | 
						|
  skip_before_action :require_authentication, only: %i[ create connect cors_preflight_check ]
 | 
						|
 | 
						|
  before_action :cors_set_access_control_headers, only: %i[ create connect cors_preflight_check ]
 | 
						|
  before_action :set_contest, only: %i[ convert destroy ]
 | 
						|
  before_action :set_data, only: %i[ convert ]
 | 
						|
 | 
						|
  def self.local_prefixes
 | 
						|
    super + [ "completions" ]
 | 
						|
  end
 | 
						|
 | 
						|
  def cors_set_access_control_headers
 | 
						|
    response.set_header("Access-Control-Allow-Origin", "*")
 | 
						|
    response.set_header("Access-Control-Allow-Credentials", "true")
 | 
						|
    response.set_header("Access-Control-Allow-Methods", "POST")
 | 
						|
    response.set_header("Access-Control-Allow-Headers", "*")
 | 
						|
    response.set_header("Access-Control-Max-Age", "86400")
 | 
						|
  end
 | 
						|
 | 
						|
  def cors_preflight_check
 | 
						|
    skip_authorization
 | 
						|
  end
 | 
						|
 | 
						|
  def connect
 | 
						|
    skip_authorization
 | 
						|
 | 
						|
    if !params.key?(:token)
 | 
						|
      respond_to do |format|
 | 
						|
        format.json  { render json: { error: "no token provided" }, status: 400 }
 | 
						|
      end
 | 
						|
    else
 | 
						|
      @contest = Contest.find_by_token_for(:token, params[:token])
 | 
						|
      if @contest
 | 
						|
        respond_to do |format|
 | 
						|
          format.json  { render json: { name: @contest.name }, status: 200 }
 | 
						|
        end
 | 
						|
      else
 | 
						|
        respond_to do |format|
 | 
						|
          format.json  { render json: { error: "invalid token" }, status: 400 }
 | 
						|
        end
 | 
						|
      end
 | 
						|
    end
 | 
						|
  end
 | 
						|
 | 
						|
  def create
 | 
						|
    skip_authorization
 | 
						|
 | 
						|
    begin
 | 
						|
      @contest = Contest.find_by_token_for(:token, params[:token])
 | 
						|
      @message = Message.new(text: params[:text], author: params[:author], time_seconds: params[:time_seconds],
 | 
						|
          display_time: display_time(params[:time_seconds]), contest: @contest)
 | 
						|
      if @contest && @message.save
 | 
						|
        respond_to do |format|
 | 
						|
          format.json  { render json: {}, status: 200 }
 | 
						|
        end
 | 
						|
      else
 | 
						|
        respond_to do |format|
 | 
						|
          format.json  { render json: { error: "invalid token" }, status: 400 }
 | 
						|
        end
 | 
						|
      end
 | 
						|
    rescue
 | 
						|
      respond_to do |format|
 | 
						|
        format.json  { render json: { error: "invalid token" }, status: 400 }
 | 
						|
      end
 | 
						|
    end
 | 
						|
  end
 | 
						|
 | 
						|
  def convert
 | 
						|
    authorize @contest
 | 
						|
 | 
						|
    @action_name = t("helpers.buttons.back")
 | 
						|
    @action_path = contest_path(@contest)
 | 
						|
 | 
						|
    @completion = Completion.new()
 | 
						|
    @completion.display_time_from_start = @message.display_time
 | 
						|
 | 
						|
    render "completions/new"
 | 
						|
  end
 | 
						|
 | 
						|
  def destroy
 | 
						|
    authorize @contest
 | 
						|
 | 
						|
    @message = Message.find(params[:id])
 | 
						|
    @message.destroy
 | 
						|
    redirect_to contest_path(@contest), notice: t("messages.destroy.notice")
 | 
						|
  end
 | 
						|
 | 
						|
  private
 | 
						|
 | 
						|
  def set_contest
 | 
						|
    @contest = Contest.find(params[:contest_id])
 | 
						|
  end
 | 
						|
 | 
						|
  def set_data
 | 
						|
    @message = Message.find(params[:message_id])
 | 
						|
    @puzzles = @contest.puzzles
 | 
						|
    @contestants = @contest.contestants.order(:name)
 | 
						|
    if @contestants.size > 0
 | 
						|
      @closest_contestant = @contestants.first
 | 
						|
      closest_distance = 10000
 | 
						|
      @contestants.each do |contestant|
 | 
						|
        distance = DamerauLevenshtein.distance(@message.author, contestant.name)
 | 
						|
        if distance < closest_distance
 | 
						|
          closest_distance = distance
 | 
						|
          @closest_contestant = contestant
 | 
						|
        end
 | 
						|
      end
 | 
						|
    end
 | 
						|
  end
 | 
						|
end
 |