Rename repository
This commit is contained in:
parent
4aeb2d2cd8
commit
232df26b85
71 changed files with 348 additions and 345 deletions
35
lib/architex_web/client/authenticate_client.ex
Normal file
35
lib/architex_web/client/authenticate_client.ex
Normal file
|
@ -0,0 +1,35 @@
|
|||
defmodule ArchitexWeb.Client.Plug.AuthenticateClient do
|
||||
import ArchitexWeb.Error
|
||||
import Plug.Conn
|
||||
|
||||
alias Architex.Account
|
||||
alias Plug.Conn
|
||||
|
||||
def init(opts), do: opts
|
||||
|
||||
def call(%Conn{params: %{"access_token" => access_token}} = conn, _opts) do
|
||||
authenticate(conn, access_token)
|
||||
end
|
||||
|
||||
def call(%Conn{req_headers: headers} = conn, _opts) do
|
||||
case List.keyfind(headers, "authorization", 0) do
|
||||
{_, "Bearer " <> access_token} ->
|
||||
authenticate(conn, access_token)
|
||||
|
||||
_ ->
|
||||
put_error(conn, :missing_token)
|
||||
end
|
||||
end
|
||||
|
||||
defp authenticate(conn, access_token) do
|
||||
case Account.by_access_token(access_token) do
|
||||
{account, device} ->
|
||||
conn
|
||||
|> assign(:account, account)
|
||||
|> assign(:device, device)
|
||||
|
||||
nil ->
|
||||
put_error(conn, :unknown_token)
|
||||
end
|
||||
end
|
||||
end
|
72
lib/architex_web/client/controllers/account_controller.ex
Normal file
72
lib/architex_web/client/controllers/account_controller.ex
Normal file
|
@ -0,0 +1,72 @@
|
|||
defmodule ArchitexWeb.Client.AccountController do
|
||||
use ArchitexWeb, :controller
|
||||
|
||||
import Architex
|
||||
import ArchitexWeb.Error
|
||||
|
||||
alias Architex.{Account, Repo}
|
||||
alias Plug.Conn
|
||||
|
||||
@doc """
|
||||
Checks to see if a username is available, and valid, for the server.
|
||||
|
||||
Action for GET /_matrix/client/r0/register/available.
|
||||
"""
|
||||
def available(conn, params) do
|
||||
localpart = Map.get(params, "username", "")
|
||||
|
||||
case Account.available?(localpart) do
|
||||
:ok ->
|
||||
conn
|
||||
|> put_status(200)
|
||||
|> json(%{available: true})
|
||||
|
||||
{:error, error} ->
|
||||
put_error(conn, error)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets information about the owner of a given access token.
|
||||
|
||||
Action for GET /_matrix/client/r0/account/whoami.
|
||||
"""
|
||||
def whoami(%Conn{assigns: %{account: %Account{localpart: localpart}}} = conn, _params) do
|
||||
data = %{user_id: get_mxid(localpart)}
|
||||
|
||||
conn
|
||||
|> put_status(200)
|
||||
|> json(data)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Invalidates an existing access token, so that it can no longer be used for authorization.
|
||||
|
||||
Action for POST /_matrix/client/r0/logout.
|
||||
"""
|
||||
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
|
||||
|
||||
@doc """
|
||||
Invalidates all access tokens for a user, so that they can no longer be used
|
||||
for authorization.
|
||||
|
||||
Action for POST /_matrix/client/r0/logout/all.
|
||||
"""
|
||||
def logout_all(%Conn{assigns: %{account: account}} = conn, _params) do
|
||||
Repo.delete_all(Ecto.assoc(account, :devices))
|
||||
|
||||
conn
|
||||
|> put_status(200)
|
||||
|> json(%{})
|
||||
end
|
||||
end
|
27
lib/architex_web/client/controllers/aliases_controller.ex
Normal file
27
lib/architex_web/client/controllers/aliases_controller.ex
Normal file
|
@ -0,0 +1,27 @@
|
|||
defmodule ArchitexWeb.Client.AliasesController do
|
||||
use ArchitexWeb, :controller
|
||||
|
||||
import ArchitexWeb.Error
|
||||
|
||||
alias Architex.Alias
|
||||
|
||||
@doc """
|
||||
Create a new mapping from room alias to room ID.
|
||||
|
||||
Action for PUT /_matrix/client/r0/directory/room/{roomAlias}.
|
||||
"""
|
||||
def create(conn, %{"alias" => alias, "room_id" => room_id}) do
|
||||
case Alias.create(alias, room_id) do
|
||||
{:ok, _} ->
|
||||
conn
|
||||
|> put_status(200)
|
||||
|> json(%{})
|
||||
|
||||
{:error, cs} ->
|
||||
put_error(conn, Alias.get_error(cs))
|
||||
end
|
||||
end
|
||||
|
||||
# TODO: create error view for this?
|
||||
def create(conn, _), do: put_error(conn, :bad_json)
|
||||
end
|
24
lib/architex_web/client/controllers/info_controller.ex
Normal file
24
lib/architex_web/client/controllers/info_controller.ex
Normal file
|
@ -0,0 +1,24 @@
|
|||
defmodule ArchitexWeb.Client.InfoController do
|
||||
use ArchitexWeb, :controller
|
||||
|
||||
import ArchitexWeb.Error
|
||||
|
||||
@supported_versions ["r0.6.1"]
|
||||
|
||||
@doc """
|
||||
Gets the versions of the specification supported by the server.
|
||||
|
||||
Action for GET /_matrix/client/versions.
|
||||
"""
|
||||
def versions(conn, _params) do
|
||||
data = %{versions: @supported_versions}
|
||||
|
||||
conn
|
||||
|> put_status(200)
|
||||
|> json(data)
|
||||
end
|
||||
|
||||
def unrecognized(conn, _params) do
|
||||
put_error(conn, :unrecognized)
|
||||
end
|
||||
end
|
70
lib/architex_web/client/controllers/login_controller.ex
Normal file
70
lib/architex_web/client/controllers/login_controller.ex
Normal file
|
@ -0,0 +1,70 @@
|
|||
defmodule ArchitexWeb.Client.LoginController do
|
||||
use ArchitexWeb, :controller
|
||||
|
||||
import ArchitexWeb.Error
|
||||
import Ecto.Changeset
|
||||
|
||||
alias Architex.{Repo, Account, Device}
|
||||
alias ArchitexWeb.Client.Request.Login
|
||||
alias Ecto.Changeset
|
||||
|
||||
@login_type "m.login.password"
|
||||
|
||||
@doc """
|
||||
Gets the homeserver's supported login types to authenticate users.
|
||||
|
||||
Action for GET /_matrix/client/r0/login.
|
||||
"""
|
||||
def login_types(conn, _params) do
|
||||
data = %{flows: [%{type: @login_type}]}
|
||||
|
||||
conn
|
||||
|> put_status(200)
|
||||
|> json(data)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Authenticates the user, and issues an access token they can use to
|
||||
authorize themself in subsequent requests.
|
||||
|
||||
Action for POST /_matrix/client/r0/login.
|
||||
"""
|
||||
def login(
|
||||
conn,
|
||||
%{"type" => @login_type, "identifier" => %{"type" => "m.id.user"}} = params
|
||||
) do
|
||||
case Login.changeset(params) do
|
||||
%Changeset{valid?: true} = cs ->
|
||||
input = apply_changes(cs)
|
||||
|
||||
case Account.login(input) |> Repo.transaction() do
|
||||
{:ok,
|
||||
{%Account{localpart: localpart},
|
||||
%Device{access_token: access_token, device_id: device_id}}} ->
|
||||
data = %{
|
||||
user_id: Architex.get_mxid(localpart),
|
||||
access_token: access_token,
|
||||
device_id: device_id
|
||||
}
|
||||
|
||||
conn
|
||||
|> put_status(200)
|
||||
|> json(data)
|
||||
|
||||
{:error, error} when is_atom(error) ->
|
||||
put_error(conn, error)
|
||||
|
||||
{:error, _} ->
|
||||
put_error(conn, :unknown)
|
||||
end
|
||||
|
||||
_ ->
|
||||
put_error(conn, :bad_json)
|
||||
end
|
||||
end
|
||||
|
||||
def login(conn, _params) do
|
||||
# Other login types and identifiers are unsupported for now.
|
||||
put_error(conn, :unrecognized, "Only m.login.password is supported currently.")
|
||||
end
|
||||
end
|
69
lib/architex_web/client/controllers/register_controller.ex
Normal file
69
lib/architex_web/client/controllers/register_controller.ex
Normal file
|
@ -0,0 +1,69 @@
|
|||
defmodule ArchitexWeb.Client.RegisterController do
|
||||
use ArchitexWeb, :controller
|
||||
|
||||
import ArchitexWeb.Error
|
||||
import Ecto.Changeset
|
||||
|
||||
alias Architex.{Repo, Account, Device}
|
||||
alias ArchitexWeb.Client.Request.Register
|
||||
alias Ecto.Changeset
|
||||
|
||||
@register_type "m.login.dummy"
|
||||
|
||||
@doc """
|
||||
Register for an account on this homeserver.
|
||||
|
||||
Action for POST /_matrix/client/r0/register.
|
||||
"""
|
||||
def register(conn, %{"auth" => %{"type" => @register_type}} = params) do
|
||||
case Register.changeset(params) do
|
||||
%Changeset{valid?: true} = cs ->
|
||||
%Register{inhibit_login: inhibit_login} = input = apply_changes(cs)
|
||||
|
||||
case Account.register(input) |> Repo.transaction() do
|
||||
{:ok,
|
||||
%{
|
||||
account: %Account{localpart: localpart},
|
||||
device: %Device{device_id: device_id, access_token: access_token}
|
||||
}} ->
|
||||
data = %{user_id: Architex.get_mxid(localpart)}
|
||||
|
||||
data =
|
||||
if not inhibit_login do
|
||||
data
|
||||
|> Map.put(:device_id, device_id)
|
||||
|> Map.put(:access_token, access_token)
|
||||
else
|
||||
data
|
||||
end
|
||||
|
||||
conn
|
||||
|> put_status(200)
|
||||
|> json(data)
|
||||
|
||||
{:error, _, cs, _} ->
|
||||
put_error(conn, Register.get_error(cs))
|
||||
end
|
||||
|
||||
_ ->
|
||||
put_error(conn, :bad_json)
|
||||
end
|
||||
end
|
||||
|
||||
def register(conn, %{"auth" => _}) do
|
||||
# Other login types are unsupported for now.
|
||||
put_error(conn, :unrecognized, "Only m.login.dummy is supported currently.")
|
||||
end
|
||||
|
||||
def register(conn, _params) do
|
||||
# User has not started an auth flow.
|
||||
data = %{
|
||||
flows: [%{stages: [@register_type]}],
|
||||
params: %{}
|
||||
}
|
||||
|
||||
conn
|
||||
|> put_status(401)
|
||||
|> json(data)
|
||||
end
|
||||
end
|
235
lib/architex_web/client/controllers/room_controller.ex
Normal file
235
lib/architex_web/client/controllers/room_controller.ex
Normal file
|
@ -0,0 +1,235 @@
|
|||
defmodule ArchitexWeb.Client.RoomController do
|
||||
use ArchitexWeb, :controller
|
||||
|
||||
import ArchitexWeb.Error
|
||||
import Ecto.{Changeset, Query}
|
||||
|
||||
alias Architex.{Repo, Room, RoomServer}
|
||||
alias Architex.Types.UserId
|
||||
alias ArchitexWeb.Client.Request.{CreateRoom, Kick, Ban}
|
||||
alias Ecto.Changeset
|
||||
alias Plug.Conn
|
||||
|
||||
@doc """
|
||||
Create a new room with various configuration options.
|
||||
|
||||
Action for POST /_matrix/client/r0/createRoom.
|
||||
"""
|
||||
def create(%Conn{assigns: %{account: account}} = conn, params) do
|
||||
case CreateRoom.changeset(params) do
|
||||
%Changeset{valid?: true} = cs ->
|
||||
input = apply_changes(cs)
|
||||
|
||||
case Room.create(account, input) do
|
||||
{:ok, room_id} ->
|
||||
conn
|
||||
|> put_status(200)
|
||||
|> json(%{room_id: room_id})
|
||||
|
||||
{:error, :authorization} ->
|
||||
put_error(conn, :invalid_room_state)
|
||||
|
||||
{:error, :unknown} ->
|
||||
put_error(conn, :unknown)
|
||||
end
|
||||
|
||||
_ ->
|
||||
put_error(conn, :bad_json)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
This API returns a list of the user's current rooms.
|
||||
|
||||
Action for GET /_matrix/client/r0/joined_rooms.
|
||||
"""
|
||||
def joined_rooms(%Conn{assigns: %{account: account}} = conn, _params) do
|
||||
joined_room_ids =
|
||||
account
|
||||
|> Ecto.assoc(:joined_rooms)
|
||||
|> select([jr], jr.id)
|
||||
|> Repo.all()
|
||||
|
||||
data = %{
|
||||
joined_rooms: joined_room_ids
|
||||
}
|
||||
|
||||
conn
|
||||
|> put_status(200)
|
||||
|> json(data)
|
||||
end
|
||||
|
||||
@doc """
|
||||
This API invites a user to participate in a particular room.
|
||||
|
||||
Action for POST /_matrix/client/r0/rooms/{roomId}/invite.
|
||||
"""
|
||||
def invite(%Conn{assigns: %{account: account}} = conn, %{
|
||||
"room_id" => room_id,
|
||||
"user_id" => user_id
|
||||
}) do
|
||||
with {:ok, _} <- UserId.cast(user_id),
|
||||
{:ok, pid} <- RoomServer.get_room_server(room_id) do
|
||||
case RoomServer.invite(pid, account, user_id) do
|
||||
:ok ->
|
||||
conn
|
||||
|> send_resp(200, [])
|
||||
|> halt()
|
||||
|
||||
{:error, _} ->
|
||||
put_error(conn, :unknown)
|
||||
end
|
||||
else
|
||||
:error -> put_error(conn, :invalid_param, "Given user ID is invalid.")
|
||||
{:error, :not_found} -> put_error(conn, :not_found, "The given room was not found.")
|
||||
end
|
||||
end
|
||||
|
||||
def invite(conn, _), do: put_error(conn, :missing_param)
|
||||
|
||||
@doc """
|
||||
This API starts a user participating in a particular room, if that user is allowed to participate in that room.
|
||||
|
||||
Action for POST /_matrix/client/r0/rooms/{roomId}/join.
|
||||
TODO: third_party_signed
|
||||
"""
|
||||
def join(%Conn{assigns: %{account: account}} = conn, %{"room_id" => room_id}) do
|
||||
case RoomServer.get_room_server(room_id) do
|
||||
{:ok, pid} ->
|
||||
case RoomServer.join(pid, account) do
|
||||
{:ok, room_id} ->
|
||||
conn
|
||||
|> put_status(200)
|
||||
|> json(%{room_id: room_id})
|
||||
|
||||
{:error, _} ->
|
||||
put_error(conn, :unknown)
|
||||
end
|
||||
|
||||
{:error, :not_found} ->
|
||||
put_error(conn, :not_found, "The given room was not found.")
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
This API stops a user participating in a particular room.
|
||||
|
||||
Action for POST /_matrix/client/r0/rooms/{roomId}/leave.
|
||||
"""
|
||||
def leave(%Conn{assigns: %{account: account}} = conn, %{"room_id" => room_id}) do
|
||||
case RoomServer.get_room_server(room_id) do
|
||||
{:ok, pid} ->
|
||||
case RoomServer.leave(pid, account) do
|
||||
:ok ->
|
||||
conn
|
||||
|> send_resp(200, [])
|
||||
|> halt()
|
||||
|
||||
{:error, _} ->
|
||||
put_error(conn, :unknown)
|
||||
end
|
||||
|
||||
{:error, :not_found} ->
|
||||
put_error(conn, :not_found, "The given room was not found.")
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Kick a user from the room.
|
||||
|
||||
Action for POST /_matrix/client/r0/rooms/{roomId}/kick.
|
||||
"""
|
||||
def kick(%Conn{assigns: %{account: account}} = conn, %{"room_id" => room_id} = params) do
|
||||
with {:ok, request} <- Kick.parse(params),
|
||||
{:ok, pid} <- RoomServer.get_room_server(room_id) do
|
||||
case RoomServer.kick(pid, account, request) do
|
||||
:ok ->
|
||||
conn
|
||||
|> send_resp(200, [])
|
||||
|> halt()
|
||||
|
||||
{:error, _} ->
|
||||
put_error(conn, :unknown)
|
||||
end
|
||||
else
|
||||
{:error, %Ecto.Changeset{}} -> put_error(conn, :bad_json)
|
||||
{:error, :not_found} -> put_error(conn, :not_found, "Room not found.")
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Ban a user in the room.
|
||||
|
||||
Action for POST /_matrix/client/r0/rooms/{roomId}/ban.
|
||||
"""
|
||||
def ban(%Conn{assigns: %{account: account}} = conn, %{"room_id" => room_id} = params) do
|
||||
with {:ok, request} <- Ban.parse(params),
|
||||
{:ok, pid} <- RoomServer.get_room_server(room_id) do
|
||||
case RoomServer.ban(pid, account, request) do
|
||||
:ok ->
|
||||
conn
|
||||
|> send_resp(200, [])
|
||||
|> halt()
|
||||
|
||||
{:error, _} ->
|
||||
put_error(conn, :unknown)
|
||||
end
|
||||
else
|
||||
{:error, %Ecto.Changeset{}} -> put_error(conn, :bad_json)
|
||||
{:error, :not_found} -> put_error(conn, :not_found, "Room not found.")
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Unban a user from the room.
|
||||
|
||||
Action for POST /_matrix/client/r0/rooms/{roomId}/unban.
|
||||
"""
|
||||
def unban(%Conn{assigns: %{account: account}} = conn, %{
|
||||
"room_id" => room_id,
|
||||
"user_id" => user_id
|
||||
}) do
|
||||
case RoomServer.get_room_server(room_id) do
|
||||
{:ok, pid} ->
|
||||
case RoomServer.unban(pid, account, user_id) do
|
||||
:ok ->
|
||||
conn
|
||||
|> send_resp(200, [])
|
||||
|> halt()
|
||||
|
||||
{:error, _} ->
|
||||
put_error(conn, :unknown)
|
||||
end
|
||||
|
||||
{:error, :not_found} ->
|
||||
put_error(conn, :not_found, "The given room was not found.")
|
||||
end
|
||||
end
|
||||
|
||||
def unban(conn, _), do: put_error(conn, :missing_param)
|
||||
|
||||
def send_message(
|
||||
%Conn{assigns: %{account: account, device: device}, body_params: body_params} = conn,
|
||||
%{
|
||||
"room_id" => room_id,
|
||||
"event_type" => event_type,
|
||||
"txn_id" => txn_id
|
||||
}
|
||||
) do
|
||||
case RoomServer.get_room_server(room_id) do
|
||||
{:ok, pid} ->
|
||||
case RoomServer.send_message(pid, account, device, event_type, body_params, txn_id) do
|
||||
{:ok, event_id} ->
|
||||
conn
|
||||
|> put_status(200)
|
||||
|> json(%{event_id: event_id})
|
||||
|
||||
{:error, _} ->
|
||||
put_error(conn, :unknown)
|
||||
end
|
||||
|
||||
{:error, :not_found} ->
|
||||
put_error(conn, :not_found, "The given room was not found.")
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,58 @@
|
|||
defmodule ArchitexWeb.Client.RoomDirectoryController do
|
||||
use ArchitexWeb, :controller
|
||||
|
||||
import ArchitexWeb.Error
|
||||
import Ecto.Query
|
||||
|
||||
alias Architex.{Repo, Room, RoomServer}
|
||||
alias Plug.Conn
|
||||
|
||||
@doc """
|
||||
Gets the visibility of a given room on the server's public room directory.
|
||||
|
||||
Action for GET /_matrix/client/r0/directory/list/room/{roomId}.
|
||||
"""
|
||||
def get_visibility(conn, %{"room_id" => room_id}) do
|
||||
case Repo.one(from r in Room, where: r.id == ^room_id) do
|
||||
%Room{visibility: visibility} ->
|
||||
conn
|
||||
|> put_status(200)
|
||||
|> json(%{visibility: visibility})
|
||||
|
||||
nil ->
|
||||
put_error(conn, :not_found, "The room was not found.")
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Sets the visibility of a given room in the server's public room directory.
|
||||
|
||||
Only allow the creator of the room to change visibility.
|
||||
Action for PUT /_matrix/client/r0/directory/list/room/{roomId}.
|
||||
"""
|
||||
def set_visibility(%Conn{assigns: %{account: account}} = conn, %{"room_id" => room_id} = params) do
|
||||
visibility = Map.get(params, "visibility", "public")
|
||||
|
||||
if visibility in ["public", "private"] do
|
||||
visibility = String.to_atom(visibility)
|
||||
|
||||
with {:ok, pid} <- RoomServer.get_room_server(room_id),
|
||||
:ok <- RoomServer.set_visibility(pid, account, visibility) do
|
||||
conn
|
||||
|> send_resp(200, [])
|
||||
|> halt()
|
||||
else
|
||||
{:error, :not_found} ->
|
||||
put_error(conn, :not_found, "The given room was not found.")
|
||||
|
||||
{:error, :unauthorized} ->
|
||||
put_error(conn, :unauthorized, "Only the room's creator can change visibility.")
|
||||
|
||||
{:error, _} ->
|
||||
put_error(conn, :unknown)
|
||||
end
|
||||
else
|
||||
put_error(conn, :invalid_param, "Invalid visibility.")
|
||||
end
|
||||
end
|
||||
end
|
20
lib/architex_web/client/request/ban.ex
Normal file
20
lib/architex_web/client/request/ban.ex
Normal file
|
@ -0,0 +1,20 @@
|
|||
defmodule ArchitexWeb.Client.Request.Ban do
|
||||
use ArchitexWeb.Request
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
user_id: String.t(),
|
||||
reason: String.t() | nil
|
||||
}
|
||||
|
||||
@primary_key false
|
||||
embedded_schema do
|
||||
field :user_id, :string
|
||||
field :reason, :string
|
||||
end
|
||||
|
||||
def changeset(data, params) do
|
||||
data
|
||||
|> cast(params, [:user_id, :reason])
|
||||
|> validate_required([:user_id])
|
||||
end
|
||||
end
|
48
lib/architex_web/client/request/create_room.ex
Normal file
48
lib/architex_web/client/request/create_room.ex
Normal file
|
@ -0,0 +1,48 @@
|
|||
defmodule ArchitexWeb.Client.Request.CreateRoom do
|
||||
use Ecto.Schema
|
||||
|
||||
import Ecto.Changeset
|
||||
|
||||
alias Ecto.Changeset
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
visibility: String.t(),
|
||||
room_alias_name: String.t(),
|
||||
name: String.t(),
|
||||
topic: String.t(),
|
||||
invite: list(String.t()),
|
||||
room_version: String.t(),
|
||||
preset: String.t()
|
||||
}
|
||||
|
||||
@primary_key false
|
||||
embedded_schema do
|
||||
field :visibility, :string
|
||||
field :room_alias_name, :string
|
||||
field :name, :string
|
||||
field :topic, :string
|
||||
field :invite, {:array, :string}
|
||||
field :room_version, :string
|
||||
field :preset, :string
|
||||
# TODO: unimplemented:
|
||||
# creation_content, initial_state, invite_3pid, initial_state,
|
||||
# is_direct, power_level_content_override
|
||||
end
|
||||
|
||||
def changeset(params) do
|
||||
%__MODULE__{}
|
||||
|> cast(params, [
|
||||
:visibility,
|
||||
:room_alias_name,
|
||||
:name,
|
||||
:topic,
|
||||
:invite,
|
||||
:room_version,
|
||||
:preset
|
||||
])
|
||||
|> validate_inclusion(:preset, ["private_chat", "public_chat", "trusted_private_chat"])
|
||||
end
|
||||
|
||||
def get_error(%Changeset{errors: [error | _]}), do: get_error(error)
|
||||
def get_error(_), do: :bad_json
|
||||
end
|
20
lib/architex_web/client/request/kick.ex
Normal file
20
lib/architex_web/client/request/kick.ex
Normal file
|
@ -0,0 +1,20 @@
|
|||
defmodule ArchitexWeb.Client.Request.Kick do
|
||||
use ArchitexWeb.Request
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
user_id: String.t(),
|
||||
reason: String.t() | nil
|
||||
}
|
||||
|
||||
@primary_key false
|
||||
embedded_schema do
|
||||
field :user_id, :string
|
||||
field :reason, :string
|
||||
end
|
||||
|
||||
def changeset(data, params) do
|
||||
data
|
||||
|> cast(params, [:user_id, :reason])
|
||||
|> validate_required([:user_id])
|
||||
end
|
||||
end
|
38
lib/architex_web/client/request/login.ex
Normal file
38
lib/architex_web/client/request/login.ex
Normal file
|
@ -0,0 +1,38 @@
|
|||
defmodule ArchitexWeb.Client.Request.Login do
|
||||
use Ecto.Schema
|
||||
|
||||
import Ecto.Changeset
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
type: String.t(),
|
||||
password: String.t(),
|
||||
device_id: String.t(),
|
||||
initial_device_display_name: String.t()
|
||||
}
|
||||
|
||||
@primary_key false
|
||||
embedded_schema do
|
||||
field :type, :string
|
||||
field :password, :string
|
||||
field :device_id, :string
|
||||
field :initial_device_display_name, :string
|
||||
|
||||
embeds_one :identifier, Identifier, primary_key: false do
|
||||
field :type, :string
|
||||
field :user, :string
|
||||
end
|
||||
end
|
||||
|
||||
def changeset(params) do
|
||||
%__MODULE__{}
|
||||
|> cast(params, [:type, :password, :device_id, :initial_device_display_name])
|
||||
|> cast_embed(:identifier, with: &identifier_changeset/2, required: true)
|
||||
|> validate_required([:type, :password])
|
||||
end
|
||||
|
||||
def identifier_changeset(identifier, params) do
|
||||
identifier
|
||||
|> cast(params, [:type, :user])
|
||||
|> validate_required([:type, :user])
|
||||
end
|
||||
end
|
41
lib/architex_web/client/request/register.ex
Normal file
41
lib/architex_web/client/request/register.ex
Normal file
|
@ -0,0 +1,41 @@
|
|||
defmodule ArchitexWeb.Client.Request.Register do
|
||||
use Ecto.Schema
|
||||
|
||||
import Ecto.Changeset
|
||||
|
||||
alias Ecto.Changeset
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
device_id: String.t(),
|
||||
initial_device_display_name: String.t(),
|
||||
password: String.t(),
|
||||
username: String.t(),
|
||||
inhibit_login: boolean()
|
||||
}
|
||||
|
||||
@primary_key false
|
||||
embedded_schema do
|
||||
field :device_id, :string
|
||||
field :initial_device_display_name, :string
|
||||
field :password, :string
|
||||
field :username, :string
|
||||
field :inhibit_login, :boolean, default: false
|
||||
end
|
||||
|
||||
def changeset(params) do
|
||||
%__MODULE__{}
|
||||
|> cast(params, [
|
||||
:device_id,
|
||||
:initial_device_display_name,
|
||||
:password,
|
||||
:username,
|
||||
:inhibit_login
|
||||
])
|
||||
|> validate_required([:password])
|
||||
end
|
||||
|
||||
def get_error(%Changeset{errors: [error | _]}), do: get_error(error)
|
||||
def get_error({:localpart, {_, [{:constraint, :unique} | _]}}), do: :user_in_use
|
||||
def get_error({:localpart, {_, [{:validation, _} | _]}}), do: :invalid_username
|
||||
def get_error(_), do: :bad_json
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue