<commit_before>require 'json'
#require 'google/api_client'
#require 'google/api_client/auth/file_storage'
#require 'google/api_client/auth/installed_app'
require 'securerandom'

class FlashTeamsController < ApplicationController
  helper_method :get_tasks
  before_filter :valid_user?, only: [:panels, :hire_form, :send_task_available, :task_acceptance, :send_task_acceptance, :task_rejection, :send_task_rejection]

	def new
	# check to see if the user id exists in the database 
		#if User.where(:id => params[:user_id]).blank? #user id does not exist 
		if !session[:user].nil?
			#@user = User.find(params[:id])
			@user = session[:user]
			@flash_team = FlashTeam.new
		else
			@user = nil 
			@title = "Invalid User ID"
			flash[:notice] = "You must be logged in to create a team."
			redirect_to(:controller => :users, :action => :new)
			return 
		end
			
	end

  def create 
  	if !session[:user].nil?
    name = flash_team_params(params[:flash_team])[:name]

    author = flash_team_params(params[:flash_team])[:author]
    
    @user = session[:user]
    
    @flash_team = FlashTeam.create(:name => name, :author => author, :user_id => @user.id)

    # get id
    id = @flash_team[:id]

    # store in flash team
    @flash_team.json = '{"title": "' + name + '","id": ' + id.to_s + ',"events": [],"members": [],"interactions": [], "author": "' + author + '"}'

    if @flash_team.save
      #redirect_to @flash_team
      redirect_to edit_flash_team_path(id)
    else
      render 'new'
    end
    end #end if session user not nil 
  end

  def show
  	if session[:user].nil?
		@user = nil 
		@title = "Invalid User ID"
		@flash_team = nil
		redirect_to(welcome_index_path)				
  	else
    	@flash_team = FlashTeam.find(params[:id])
    
		if @flash_team.user_id != session[:user].id
			flash[:notice] = 'You cannot access this flash team.' 
    		redirect_to(flash_teams_path)
		end
	end
  end


  def duplicate
  	if !session[:user].nil?
	  	@user = session[:user]
	  	
	    # Locate data from the original
	    original = FlashTeam.find(params[:id])
	
	    # Then create a copy from the original data
	    copy = FlashTeam.create(:name => original.name + " Copy", :author => original.author, :user_id => @user.id)
	    copy.json = '{"title": "' + copy.name + '","id": ' + copy.id.to_s + ',"events": [],"members": [],"interactions": [], "author": "' + copy.author + '"}'
	    copy.status = original.status
	    copy.save
	
	    # Redirect to the list of things
	    redirect_to :action => 'index'   
    end #end if session not nil
  end


  def index
  		# check to see if the user id exists in the database 
		if session[:user].nil?
			@user = nil 
			@title = "Invalid User ID"
			@flash_teams = nil
			redirect_to(welcome_index_path)
		else
			@user = User.find(session[:user].id)
			
			@flash_teams = FlashTeam.where(:user_id => @user.id).order(:id).reverse_order	
		end
  end


rescue_from ActiveRecord::RecordNotFound do
  #flash[:notice] = 'The object you tried to access does not exist'
  render 'member_doesnt_exist'   # or e.g. redirect_to :action => :index
end
 
  def edit
  
  if !params.has_key?("uniq") #if in author view
	  	if session[:user].nil? 
			@user = nil 
			@title = "Invalid User ID"
			@flash_team = nil
			redirect_to(welcome_index_path)			
		else 
			@flash_team = FlashTeam.find(params[:id])
			
			if @flash_team.user_id != session[:user].id 
				flash[:notice] = 'You cannot access this flash team.' 
				redirect_to(flash_teams_path)
			end
		end
			
  	else #else it is in worker view 
    	@flash_team = FlashTeam.find(params[:id])
    end 
    
	#note: member info is stored in status json in flash_teams_json
		
    #customize user views
    status = @flash_team.status 
    if status == nil
      @author_runtime=false
    else
      json_status= JSON.parse(status)
      if json_status["flash_team_in_progress"] == nil
        @author_runtime=false
      else<commit_msg>updated the the edit action so that if user is not logged in and can't access the edit page for the team it redirects to the login instead of raising an error<commit_after>require 'json'
#require 'google/api_client'
#require 'google/api_client/auth/file_storage'
#require 'google/api_client/auth/installed_app'
require 'securerandom'

class FlashTeamsController < ApplicationController
  helper_method :get_tasks
  before_filter :valid_user?, only: [:panels, :hire_form, :send_task_available, :task_acceptance, :send_task_acceptance, :task_rejection, :send_task_rejection]

	def new
	# check to see if the user id exists in the database 
		#if User.where(:id => params[:user_id]).blank? #user id does not exist 
		if !session[:user].nil?
			#@user = User.find(params[:id])
			@user = session[:user]
			@flash_team = FlashTeam.new
		else
			@user = nil 
			@title = "Invalid User ID"
			flash[:notice] = "You must be logged in to create a team."
			redirect_to(:controller => :users, :action => :new)
			return 
		end
			
	end

  def create 
  	if !session[:user].nil?
    name = flash_team_params(params[:flash_team])[:name]

    author = flash_team_params(params[:flash_team])[:author]
    
    @user = session[:user]
    
    @flash_team = FlashTeam.create(:name => name, :author => author, :user_id => @user.id)

    # get id
    id = @flash_team[:id]

    # store in flash team
    @flash_team.json = '{"title": "' + name + '","id": ' + id.to_s + ',"events": [],"members": [],"interactions": [], "author": "' + author + '"}'

    if @flash_team.save
      #redirect_to @flash_team
      redirect_to edit_flash_team_path(id)
    else
      render 'new'
    end
    end #end if session user not nil 
  end

  def show
  	if session[:user].nil?
		@user = nil 
		@title = "Invalid User ID"
		@flash_team = nil
		redirect_to(welcome_index_path)				
  	else
    	@flash_team = FlashTeam.find(params[:id])
    
		if @flash_team.user_id != session[:user].id
			flash[:notice] = 'You cannot access this flash team.' 
    		redirect_to(flash_teams_path)
		end
	end
  end


  def duplicate
  	if !session[:user].nil?
	  	@user = session[:user]
	  	
	    # Locate data from the original
	    original = FlashTeam.find(params[:id])
	
	    # Then create a copy from the original data
	    copy = FlashTeam.create(:name => original.name + " Copy", :author => original.author, :user_id => @user.id)
	    copy.json = '{"title": "' + copy.name + '","id": ' + copy.id.to_s + ',"events": [],"members": [],"interactions": [], "author": "' + copy.author + '"}'
	    copy.status = original.status
	    copy.save
	
	    # Redirect to the list of things
	    redirect_to :action => 'index'   
    end #end if session not nil
  end


  def index
  		# check to see if the user id exists in the database 
		if session[:user].nil?
			@user = nil 
			@title = "Invalid User ID"
			@flash_teams = nil
			redirect_to(welcome_index_path)
		else
			@user = User.find(session[:user].id)
			
			@flash_teams = FlashTeam.where(:user_id => @user.id).order(:id).reverse_order	
		end
  end


rescue_from ActiveRecord::RecordNotFound do
  #flash[:notice] = 'The object you tried to access does not exist'
  render 'member_doesnt_exist'   # or e.g. redirect_to :action => :index
end
 
  def edit
  
  if !params.has_key?("uniq") #if in author view
	  	if session[:user].nil? 
			@user = nil 
			@title = "Invalid User ID"
			@flash_team = nil
			redirect_to(welcome_index_path) and return		
		else 
			@flash_team = FlashTeam.find(params[:id])
			
			if @flash_team.user_id != session[:user].id 
				flash[:notice] = 'You cannot access this flash team.' 
				redirect_to(flash_teams_path) and return 
			end
		end
			
  	else #else it is in worker view 
    	@flash_team = FlashTeam.find(params[:id])
    end 
    
	#note: member info is stored in status json in flash_teams_json
		
    #customize user views
    status = @flash_team.status 
    if status == nil
      @author_runtime=false
    else
      json_status= JSON.parse(status)
      if json_status["flash_team_in_progress"] == nil
        @author_runtime=false
      else
{'fraction_non_alphanumeric': 0.12626199675931696, 'fraction_numerical': 0.0, 'mean_word_length': 3.241014799154334, 'pattern_counts': {'":': 24, '<': 5, '<?xml version=': 0, '>': 27, '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': '811110', 'n_tokens_mistral': 2909, 'n_tokens_neox': 2662, 'n_words': 726}