<commit_before>require 'httparty'

module Pakyow::Helpers
  CONSOLE_SESSION_KEY = :console_user

  def using_platform?
    Pakyow::Config.console.use_pakyow_platform == true
  end

  def console_setup?
    platform_setup? || Pakyow::Console.model(:user).count > 0
  end

  def platform_setup?
    File.exists?(platform_file_path)
  end

  def console_authed?
    (!session[CONSOLE_SESSION_KEY].nil? || platform_authed?) && current_console_user.console?
  end

  def platform_authed?
    return false if req.nil?
    !session[:platform_email].nil? && !session[:platform_token].nil?
  end

  def console_auth(user)
    session[CONSOLE_SESSION_KEY] = user.id
  end

  def console_unauth
    session[CONSOLE_SESSION_KEY] = nil
    session[:platform_email] = nil
    session[:platform_token] = nil
    $socket = nil
  end

  def current_console_user
    if platform?
      { email: session[:platform_email] }
    else
      Pakyow::Console.model(:user)[session[CONSOLE_SESSION_KEY]]
    end
  end

  def platform?
    return unless console_authed?
    (platform_setup? && platform_client.valid?)
  end

  def platform_token?<commit_msg>Refactor console user auth check<commit_after>require 'httparty'

module Pakyow::Helpers
  CONSOLE_SESSION_KEY = :console_user

  def using_platform?
    Pakyow::Config.console.use_pakyow_platform == true
  end

  def console_setup?
    platform_setup? || Pakyow::Console.model(:user).count > 0
  end

  def platform_setup?
    File.exists?(platform_file_path)
  end

  def console_authed?
    !session[CONSOLE_SESSION_KEY].nil? || platform_authed?
  end

  def platform_authed?
    return false if req.nil?
    !session[:platform_email].nil? && !session[:platform_token].nil?
  end

  def console_auth(user)
    session[CONSOLE_SESSION_KEY] = user.id
  end

  def console_unauth
    session[CONSOLE_SESSION_KEY] = nil
    session[:platform_email] = nil
    session[:platform_token] = nil
    $socket = nil
  end

  def current_console_user
    if platform?
      { email: session[:platform_email] }
    else
      user = Pakyow::Console.model(:user)[session[CONSOLE_SESSION_KEY]]
      return user if user && user.console?
    end
  end

  def platform?
    return unless console_authed?
    (platform_setup? && platform_client.valid?)
  end

  def platform_token?
{'fraction_non_alphanumeric': 0.09625435540069686, 'fraction_numerical': 0.0008710801393728223, 'mean_word_length': 3.8976545842217485, 'pattern_counts': {'":': 0, '<': 3, '<?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': '7197526', 'n_tokens_mistral': 772, 'n_tokens_neox': 753, 'n_words': 152}