<commit_before>require 'net/http'
require 'action_controller/railtie'
require 'cancan'
require 'pavlov'

class ApplicationController < ActionController::Base

  include Pavlov::Helpers
  include Devise::Controllers::Rememberable

  DEFAULT_LAYOUT = 'frontend'

  def pavlov_options
    Util::PavlovContextSerialization.pavlov_context_by_user current_user, current_ability
  end

  # expose query to views, so we can rewrite inline
  # retrieval to proper queries. The queries should
  # be pulled back to controllers, and then to interactors
  helper_method :query # TODO: remove me ASAP

  rescue_from CanCan::AccessDenied, Pavlov::AccessDenied do |exception|
    respond_to do |format|
      if not current_user
        format.html do
          flash[:alert] = t('devise.failure.unauthenticated')
          redirect_to root_path(return_to: request.original_url)
        end
      end

      format.json { render json: {error: "You don't have the correct credentials to execute this operation", code: 'login'}, status: :forbidden }
      format.any  { fail exception }
    end
  end

  protect_from_forgery

  helper :all

  after_filter :set_access_control

  def seen_the_tour user
    view_context.tour_steps.last.to_s == user.seen_tour_step
  end

  def start_the_tour_path
    step = current_user.seen_tour_step || view_context.first_real_tour_step

    send(:"#{step}_path")
  end

  def after_sign_in_path_for(user)
    return setup_account_path unless user.set_up
    return start_the_tour_path unless seen_the_tour(user)

    safe_return_to_path || feed_path(current_user.username)
  end

  def safe_return_to_path
    return nil unless params[:return_to]

    uri = URI.parse(params[:return_to].to_s)
    if FactlinkUI::Application.config.hostname == uri.host
      uri.to_s
    else
      nil
    end
  end

  ##########
  # Set the Access Control, so XHR request from other domains are allowed.
  def set_access_control
    headers['Access-Control-Allow-Origin'] = '*'
    headers['Access-Control-Request-Origin'] = '*'
  end

  def current_graph_user
    return unless current_user
    @current_graph_user ||= current_user.graph_user
  end
  helper_method :current_graph_user

  def raise_404(message="Not Found")
    fail ActionController::RoutingError.new(message)
  end

  class HackAttempt < StandardError
  end

  rescue_from HackAttempt, with: :rescue_hacker

  def rescue_hacker
    render nothing: true, status: 500
  end

  def backbone_responder &block
    respond_to do |format|
      format.html do
        render inline: '', layout: 'channels'
      end
      format.json { yield } if block_given?
    end
  end

  def mp_track(event, opts={})
    user = opts.fetch(:current_user) { current_user }
    opts.delete :current_user

    new_opts = if user
                 opts.update mp_name_tag: user.username,
                             distinct_id: user.id
               else
                 opts
               end

    new_opts[:time] = Time.now.utc.to_i

    req_env = MixpanelRequestPresenter.new(request).to_hash

    Resque.enqueue(Mixpanel::TrackEventJob, event, new_opts, req_env)
  end

  def mp_track_people_event(opts={})
    if current_user
      req_env = MixpanelRequestPresenter.new(request).to_hash
      Resque.enqueue(Mixpanel::TrackPeopleEventJob, current_user.id, opts, req_env)
    end
  end

  before_filter :initialize_mixpanel
  def initialize_mixpanel
    return unless user_signed_in? and request.format == "text/html"

    mixpanel = FactlinkUI::Application.config.mixpanel.new(request.env, true)

    # the following two commands set data in the frontend using js
    mixpanel.append_api('name_tag', current_user.username)
    mixpanel.append_identify(current_user.id.to_s)
  end

  before_filter :set_last_interaction_for_user
  def set_last_interaction_for_user
    return unless user_signed_in? and request.format == "text/html"
<commit_msg>made consistent with other mixpanel checks<commit_after>require 'net/http'
require 'action_controller/railtie'
require 'cancan'
require 'pavlov'

class ApplicationController < ActionController::Base

  include Pavlov::Helpers
  include Devise::Controllers::Rememberable

  DEFAULT_LAYOUT = 'frontend'

  def pavlov_options
    Util::PavlovContextSerialization.pavlov_context_by_user current_user, current_ability
  end

  # expose query to views, so we can rewrite inline
  # retrieval to proper queries. The queries should
  # be pulled back to controllers, and then to interactors
  helper_method :query # TODO: remove me ASAP

  rescue_from CanCan::AccessDenied, Pavlov::AccessDenied do |exception|
    respond_to do |format|
      if not current_user
        format.html do
          flash[:alert] = t('devise.failure.unauthenticated')
          redirect_to root_path(return_to: request.original_url)
        end
      end

      format.json { render json: {error: "You don't have the correct credentials to execute this operation", code: 'login'}, status: :forbidden }
      format.any  { fail exception }
    end
  end

  protect_from_forgery

  helper :all

  after_filter :set_access_control

  def seen_the_tour user
    view_context.tour_steps.last.to_s == user.seen_tour_step
  end

  def start_the_tour_path
    step = current_user.seen_tour_step || view_context.first_real_tour_step

    send(:"#{step}_path")
  end

  def after_sign_in_path_for(user)
    return setup_account_path unless user.set_up
    return start_the_tour_path unless seen_the_tour(user)

    safe_return_to_path || feed_path(current_user.username)
  end

  def safe_return_to_path
    return nil unless params[:return_to]

    uri = URI.parse(params[:return_to].to_s)
    if FactlinkUI::Application.config.hostname == uri.host
      uri.to_s
    else
      nil
    end
  end

  ##########
  # Set the Access Control, so XHR request from other domains are allowed.
  def set_access_control
    headers['Access-Control-Allow-Origin'] = '*'
    headers['Access-Control-Request-Origin'] = '*'
  end

  def current_graph_user
    return unless current_user
    @current_graph_user ||= current_user.graph_user
  end
  helper_method :current_graph_user

  def raise_404(message="Not Found")
    fail ActionController::RoutingError.new(message)
  end

  class HackAttempt < StandardError
  end

  rescue_from HackAttempt, with: :rescue_hacker

  def rescue_hacker
    render nothing: true, status: 500
  end

  def backbone_responder &block
    respond_to do |format|
      format.html do
        render inline: '', layout: 'channels'
      end
      format.json { yield } if block_given?
    end
  end

  def mp_track(event, opts={})
    user = opts.fetch(:current_user) { current_user }
    opts.delete :current_user

    new_opts = if user
                 opts.update mp_name_tag: user.username,
                             distinct_id: user.id
               else
                 opts
               end

    new_opts[:time] = Time.now.utc.to_i

    req_env = MixpanelRequestPresenter.new(request).to_hash

    Resque.enqueue(Mixpanel::TrackEventJob, event, new_opts, req_env)
  end

  def mp_track_people_event(opts={})
    return unless user_signed_in?

    req_env = MixpanelRequestPresenter.new(request).to_hash
    Resque.enqueue(Mixpanel::TrackPeopleEventJob, current_user.id, opts, req_env)
  end

  before_filter :initialize_mixpanel
  def initialize_mixpanel
    return unless user_signed_in? and request.format == "text/html"

    mixpanel = FactlinkUI::Application.config.mixpanel.new(request.env, true)

    # the following two commands set data in the frontend using js
    mixpanel.append_api('name_tag', current_user.username)
    mixpanel.append_identify(current_user.id.to_s)
  end

  before_filter :set_last_interaction_for_user
  def set_last_interaction_for_user
    return unless user_signed_in? and request.format == "text/html"

{'fraction_non_alphanumeric': 0.0779535595815259, 'fraction_numerical': 0.0015310028068384791, 'mean_word_length': 3.5181556195965418, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '6638216', 'n_tokens_mistral': 2507, 'n_tokens_neox': 2397, 'n_words': 585}