2021-08-06 21:14:27 +00:00
|
|
|
defmodule MatrixServerWeb.Client.AccountController do
|
2021-06-22 21:04:37 +00:00
|
|
|
use MatrixServerWeb, :controller
|
2021-06-25 22:29:33 +00:00
|
|
|
|
2021-06-25 23:14:09 +00:00
|
|
|
import MatrixServer
|
|
|
|
import MatrixServerWeb.Plug.Error
|
2021-06-25 22:29:33 +00:00
|
|
|
|
2021-06-27 20:24:54 +00:00
|
|
|
alias MatrixServer.{Account, Repo}
|
2021-06-25 22:29:33 +00:00
|
|
|
alias Plug.Conn
|
2021-06-22 21:04:37 +00:00
|
|
|
|
|
|
|
def available(conn, params) do
|
|
|
|
localpart = Map.get(params, "username", "")
|
|
|
|
|
2021-06-25 23:14:09 +00:00
|
|
|
case Account.available?(localpart) do
|
|
|
|
:ok ->
|
|
|
|
conn
|
|
|
|
|> put_status(200)
|
|
|
|
|> json(%{available: true})
|
2021-06-22 21:04:37 +00:00
|
|
|
|
2021-06-25 23:14:09 +00:00
|
|
|
{:error, error} ->
|
|
|
|
put_error(conn, error)
|
|
|
|
end
|
2021-06-22 21:04:37 +00:00
|
|
|
end
|
2021-06-25 22:29:33 +00:00
|
|
|
|
|
|
|
def whoami(%Conn{assigns: %{account: %Account{localpart: localpart}}} = conn, _params) do
|
|
|
|
data = %{user_id: get_mxid(localpart)}
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_status(200)
|
|
|
|
|> json(data)
|
|
|
|
end
|
2021-06-27 20:24:54 +00:00
|
|
|
|
|
|
|
def logout(%Conn{assigns: %{device: device}} = conn, _params) do
|
|
|
|
case Repo.delete(device) do
|
|
|
|
{:ok, _} ->
|
|
|
|
conn
|
|
|
|
|> put_status(200)
|
|
|
|
|> json(%{})
|
|
|
|
|
|
|
|
{:error, _} ->
|
|
|
|
put_error(conn, :unknown)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def logout_all(%Conn{assigns: %{account: account}} = conn, _params) do
|
|
|
|
Repo.delete_all(Ecto.assoc(account, :devices))
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_status(200)
|
|
|
|
|> json(%{})
|
|
|
|
end
|
2021-06-22 21:04:37 +00:00
|
|
|
end
|