2021-08-06 21:14:27 +00:00
|
|
|
defmodule MatrixServerWeb.Client.RoomController do
|
2021-07-10 21:16:00 +00:00
|
|
|
use MatrixServerWeb, :controller
|
|
|
|
|
2021-08-14 13:20:42 +00:00
|
|
|
import MatrixServerWeb.Error
|
2021-07-10 21:16:00 +00:00
|
|
|
import Ecto.Changeset
|
|
|
|
|
2021-07-29 20:06:02 +00:00
|
|
|
alias MatrixServer.Room
|
2021-08-14 13:20:42 +00:00
|
|
|
alias MatrixServerWeb.Client.Request.CreateRoom
|
2021-07-10 21:16:00 +00:00
|
|
|
alias Ecto.Changeset
|
2021-07-17 15:38:20 +00:00
|
|
|
alias Plug.Conn
|
2021-07-10 21:16:00 +00:00
|
|
|
|
2021-07-17 15:38:20 +00:00
|
|
|
def create(%Conn{assigns: %{account: account}} = conn, params) do
|
2021-07-10 21:16:00 +00:00
|
|
|
case CreateRoom.changeset(params) do
|
|
|
|
%Changeset{valid?: true} = cs ->
|
2021-07-28 15:23:48 +00:00
|
|
|
input = apply_changes(cs)
|
|
|
|
|
2021-07-29 20:06:02 +00:00
|
|
|
case Room.create(account, input) do
|
|
|
|
{:ok, room_id} ->
|
|
|
|
conn
|
|
|
|
|> put_status(200)
|
|
|
|
|> json(%{room_id: room_id})
|
2021-07-10 21:16:00 +00:00
|
|
|
|
2021-07-29 20:06:02 +00:00
|
|
|
{:error, :authorization} ->
|
|
|
|
put_error(conn, :invalid_room_state)
|
|
|
|
|
|
|
|
{:error, :unknown} ->
|
|
|
|
put_error(conn, :unknown)
|
|
|
|
end
|
2021-07-10 21:16:00 +00:00
|
|
|
|
|
|
|
_ ->
|
|
|
|
put_error(conn, :bad_json)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|