996 B · text History 7bff5e7
defmodule VigilWeb.Api.V0.IndexController do
  @moduledoc """
  The Sentry-compatible `/api/0/` root. sentry-cli's `info` and `login` read
  `user.email`, `user.id` (a string) and `auth.scopes` here. Anything else
  under `/api/0/` that isn't routed answers `{"detail": "Not found"}`, as
  GlitchTip's catch-all, which sentry-cli reads as "not supported".
  """
  use VigilWeb, :controller

  alias VigilWeb.Api.Auth

  def index(conn, _params) do
    scope = conn.assigns.current_scope
    json(conn, %{version: "0", user: user(scope), auth: auth(scope)})
  end

  def not_found(conn, _params), do: Auth.not_found(conn)

  defp user(nil), do: nil

  defp user(%{user: u}) do
    %{
      id: u.id,
      email: u.email,
      name: Vigil.Accounts.display_name(u),
      username: u.email,
      isActive: true,
      dateJoined: u.inserted_at
    }
  end

  defp auth(%{token: %{} = t}),
    do: %{id: t.id, label: t.label, scopes: t.scopes, created: t.inserted_at}

  defp auth(_), do: nil
end