<commit_before># encoding: UTF-8
# The filters added to this controller will be run for all controllers in the application.
# Likewise will all the methods added be available for all controllers.

require 'digest/md5'
require "#{Rails.root}/lib/misc"
require "#{Rails.root}/lib/localization"

class ApplicationController < ActionController::Base
  include Misc
  include DateAndTimeHelper


  helper :task_filter
  helper :users
  helper :date_and_time
  helper :todos
  helper :tags
  helper :time_tracking
  helper :resources

#  helper :all

  helper_method :last_active
  helper_method :render_to_string
  helper_method :current_user
  helper_method :tz
  helper_method :current_projects
  helper_method :current_project_ids
  helper_method :completed_milestone_ids
  helper_method :worked_nice
  helper_method :link_to_task
  helper_method :current_task_filter
  helper_method :current_templates
  helper_method :admin?, :logged_in?, :highlight_all

  before_filter :authorize, :except => [ :login, :validate,
                                         :show_logo, :about, :screenshots, :terms, :policy,
                                         :unsubscribe, :igoogle_setup, :igoogle
                                       ]

#  protect_from_forgery :secret => '<KEY>'
  def current_user
    @current_user ||= User.includes(:projects, { :company => :properties }).find(session[:user_id])
  end

  def current_sheet
    unless @current_sheet
      @current_sheet = Sheet.where("user_id = ?", session[:user_id]).order('sheets.id').includes(:task).first
      unless @current_sheet.nil?
        if @current_sheet.task.nil?
          @current_sheet.destroy
          @current_sheet = nil
        end
      end
    end
    @current_sheet
  end

  def tz
    @tz ||= TZInfo::Timezone.get(current_user.time_zone)
  end

  # Make sure the session is logged in
  def authorize
    session[:history] ||= []

    # Remember the previous _important_ page for returning to after an edit / update.
    if( request.fullpath.include?('/list') || request.fullpath.include?('/search') || request.fullpath.include?('/edit_preferences') ||
        request.fullpath.include?('/timeline') || request.fullpath.include?('/gantt') ||
        request.fullpath.include?('/forums') || request.fullpath.include?('/topics') || request.fullpath.include?('/projects') ) &&
        !request.xhr?
      session[:history] = [request.fullpath] + session[:history][0,3] if session[:history][0] != request.fullpath
    end


    logger.info("remember[#{session[:remember_until]}]")

    # We need to re-authenticate
    if session[:user_id] && session[:remember_until] && session[:remember_until] < Time.now.utc
      session[:user_id] = nil
      session[:remember_until] = nil
    end

    if session[:user_id].to_i == 0
      if !(request.fullpath.include?('/login/login') || request.xhr? || request.format.js?)
        session[:redirect] = request.fullpath
      elsif session[:history] && session[:history].size > 0
        session[:redirect] = session[:history][0]
      end

      if request.xhr?
        render :update do |page|
          page.redirect_to :controller => 'login', :action => 'login'
        end
      elsif request.format.js? && request.post?
        #return json for ajax form callback
        render :text => {:status => "session timeout"}.to_json and return
      else
        redirect_to "/login/login"
      end
    else
      session[:remember_until] = Time.now.utc + 3.hours unless (session[:remember].to_i == 1)

      current_sheet

      # Set current locale
      Localization.lang(current_user.locale || 'en_US')

      if session[:redirect]
        redirect_to session[:redirect]
        session[:redirect] = nil
      end
    end
    true
  end

  # Parse <tt>1w 2d 3h 4m</tt> or <tt>1:2:3:4</tt> => minutes or seconds
  def parse_time(input, minutes = false)
    TimeParser.parse_time(current_user, input, minutes)
  end

  delegate :projects, :project_ids, :to => :current_user, :prefix=> :current
  delegate :all_projects, :admin?, :to => :current_user

  # List of completed milestone ids, joined with ,
  def completed_milestone_ids
    unless @milestone_ids
      @milestone_ids ||= Milestone.select("id").where("company_id = ? AND completed_at IS NOT NULL", current_user.company_id).collect{ |m| m.id }
      @milestone_ids = [-1] if @milestone_ids.empty?
    end
    @milestone_ids
  end

  def worked_nice(minutes)<commit_msg>Refactoring ApplicationController: delegate "tz" method to current_user.<commit_after># encoding: UTF-8
# The filters added to this controller will be run for all controllers in the application.
# Likewise will all the methods added be available for all controllers.

require 'digest/md5'
require "#{Rails.root}/lib/misc"
require "#{Rails.root}/lib/localization"

class ApplicationController < ActionController::Base
  include Misc
  include DateAndTimeHelper


  helper :task_filter
  helper :users
  helper :date_and_time
  helper :todos
  helper :tags
  helper :time_tracking
  helper :resources

#  helper :all

  helper_method :last_active
  helper_method :render_to_string
  helper_method :current_user
  helper_method :tz
  helper_method :current_projects
  helper_method :current_project_ids
  helper_method :completed_milestone_ids
  helper_method :worked_nice
  helper_method :link_to_task
  helper_method :current_task_filter
  helper_method :current_templates
  helper_method :admin?, :logged_in?, :highlight_all

  before_filter :authorize, :except => [ :login, :validate,
                                         :show_logo, :about, :screenshots, :terms, :policy,
                                         :unsubscribe, :igoogle_setup, :igoogle
                                       ]

#  protect_from_forgery :secret => '<KEY>'
  def current_user
    @current_user ||= User.includes(:projects, { :company => :properties }).find(session[:user_id])
  end

  def current_sheet
    unless @current_sheet
      @current_sheet = Sheet.where("user_id = ?", session[:user_id]).order('sheets.id').includes(:task).first
      unless @current_sheet.nil?
        if @current_sheet.task.nil?
          @current_sheet.destroy
          @current_sheet = nil
        end
      end
    end
    @current_sheet
  end

  # Make sure the session is logged in
  def authorize
    session[:history] ||= []

    # Remember the previous _important_ page for returning to after an edit / update.
    if( request.fullpath.include?('/list') || request.fullpath.include?('/search') || request.fullpath.include?('/edit_preferences') ||
        request.fullpath.include?('/timeline') || request.fullpath.include?('/gantt') ||
        request.fullpath.include?('/forums') || request.fullpath.include?('/topics') || request.fullpath.include?('/projects') ) &&
        !request.xhr?
      session[:history] = [request.fullpath] + session[:history][0,3] if session[:history][0] != request.fullpath
    end


    logger.info("remember[#{session[:remember_until]}]")

    # We need to re-authenticate
    if session[:user_id] && session[:remember_until] && session[:remember_until] < Time.now.utc
      session[:user_id] = nil
      session[:remember_until] = nil
    end

    if session[:user_id].to_i == 0
      if !(request.fullpath.include?('/login/login') || request.xhr? || request.format.js?)
        session[:redirect] = request.fullpath
      elsif session[:history] && session[:history].size > 0
        session[:redirect] = session[:history][0]
      end

      if request.xhr?
        render :update do |page|
          page.redirect_to :controller => 'login', :action => 'login'
        end
      elsif request.format.js? && request.post?
        #return json for ajax form callback
        render :text => {:status => "session timeout"}.to_json and return
      else
        redirect_to "/login/login"
      end
    else
      session[:remember_until] = Time.now.utc + 3.hours unless (session[:remember].to_i == 1)

      current_sheet

      # Set current locale
      Localization.lang(current_user.locale || 'en_US')

      if session[:redirect]
        redirect_to session[:redirect]
        session[:redirect] = nil
      end
    end
    true
  end

  # Parse <tt>1w 2d 3h 4m</tt> or <tt>1:2:3:4</tt> => minutes or seconds
  def parse_time(input, minutes = false)
    TimeParser.parse_time(current_user, input, minutes)
  end

  delegate :projects, :project_ids, :to => :current_user, :prefix=> :current
  delegate :all_projects, :admin?, :tz,  :to => :current_user

  # List of completed milestone ids, joined with ,
  def completed_milestone_ids
    unless @milestone_ids
      @milestone_ids ||= Milestone.select("id").where("company_id = ? AND completed_at IS NOT NULL", current_user.company_id).collect{ |m| m.id }
      @milestone_ids = [-1] if @milestone_ids.empty?
    end
    @milestone_ids
  end

  def worked_nice(minutes)
{'fraction_non_alphanumeric': 0.11930536761389265, 'fraction_numerical': 0.004285069914298602, 'mean_word_length': 3.2173086067522587, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1677276', 'n_tokens_mistral': 2723, 'n_tokens_neox': 2613, 'n_words': 635}