<commit_before>defmodule AtomStyleTweaks.SlidingSessionTimeout do
  @moduledoc """
  Times out the session after a period of inactivity.

  The default timeout is one hour. This can be configured in the application config where the
  timeout is given as a number of seconds.

  ```
  config :my_app, AtomStyleTweaks.SlidingSessionTimeout,
    timeout: 1_234
  ```
  """
  import Plug.Conn

  alias Phoenix.Controller
  alias Timex.Duration

  require Logger

  def init, do: init([])
  def init(nil), do: init([])

  def init(opts) do
    defaults = init_defaults()
    Keyword.merge(defaults, opts)
  end

  def call(conn, opts) do
    timeout_at = get_session(conn, :timeout_at)

    if timeout_at && now() > timeout_at do
      conn
      |> logout_user
      |> Controller.redirect(to: "/auth?from=#{conn.request_path}")
      |> halt
    else
      new_timeout = calculate_timeout(opts[:timeout])

      put_session(conn, :timeout_at, new_timeout)
    end
  end

  defp get_app, do: Application.get_application(__MODULE__)

  defp init_defaults do
    [timeout: 3_600]
    |> Keyword.merge(Application.get_env(get_app(), __MODULE__) || [])
  end

  defp logout_user(conn) do
    conn
    |> clear_session
    |> configure_session([:renew])
    |> assign(:timed_out?, true)
  end

  defp now, do: DateTime.to_unix(DateTime.utc_now())

  defp calculate_timeout(timeout), do: now() + timeout

  defp from_now(timestamp) do
    Timex.now()
    |> Timex.diff(Timex.from_unix(timestamp), :seconds)
    |> Duration.from_seconds()
    |> Timex.format_duration(:humanized)
  end
end<commit_msg>Remove unused code warnings<commit_after>defmodule AtomStyleTweaks.SlidingSessionTimeout do
  @moduledoc """
  Times out the session after a period of inactivity.

  The default timeout is one hour. This can be configured in the application config where the
  timeout is given as a number of seconds.

  ```
  config :my_app, AtomStyleTweaks.SlidingSessionTimeout,
    timeout: 1_234
  ```
  """
  import Plug.Conn

  alias Phoenix.Controller

  require Logger

  def init, do: init([])
  def init(nil), do: init([])

  def init(opts) do
    defaults = init_defaults()
    Keyword.merge(defaults, opts)
  end

  def call(conn, opts) do
    timeout_at = get_session(conn, :timeout_at)

    if timeout_at && now() > timeout_at do
      conn
      |> logout_user
      |> Controller.redirect(to: "/auth?from=#{conn.request_path}")
      |> halt
    else
      new_timeout = calculate_timeout(opts[:timeout])

      put_session(conn, :timeout_at, new_timeout)
    end
  end

  defp get_app, do: Application.get_application(__MODULE__)

  defp init_defaults do
    [timeout: 3_600]
    |> Keyword.merge(Application.get_env(get_app(), __MODULE__) || [])
  end

  defp logout_user(conn) do
    conn
    |> clear_session
    |> configure_session([:renew])
    |> assign(:timed_out?, true)
  end

  defp now, do: DateTime.to_unix(DateTime.utc_now())

  defp calculate_timeout(timeout), do: now() + timeout
end
{'fraction_non_alphanumeric': 0.10869565217391304, 'fraction_numerical': 0.005351170568561873, 'mean_word_length': 3.566412213740458, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, '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': '5004837', 'n_tokens_mistral': 1020, 'n_tokens_neox': 956, 'n_words': 253}