Fix create room API endpoint
This commit is contained in:
parent
9e02d5b95c
commit
f033b88121
4 changed files with 38 additions and 33 deletions
|
@ -4,7 +4,7 @@ defmodule MatrixServer.Room do
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
import Ecto.Query
|
import Ecto.Query
|
||||||
|
|
||||||
alias MatrixServer.{Repo, Room, Event}
|
alias MatrixServer.{Repo, Room, Event, RoomServer}
|
||||||
alias MatrixServerWeb.API.CreateRoom
|
alias MatrixServerWeb.API.CreateRoom
|
||||||
|
|
||||||
@primary_key {:id, :string, []}
|
@primary_key {:id, :string, []}
|
||||||
|
@ -39,10 +39,20 @@ defmodule MatrixServer.Room do
|
||||||
) do
|
) do
|
||||||
new_forward_extremities = [event_id | forward_extremities -- prev_event_ids]
|
new_forward_extremities = [event_id | forward_extremities -- prev_event_ids]
|
||||||
|
|
||||||
|
# TODO: might not need to save to DB here.
|
||||||
{_, [room]} =
|
{_, [room]} =
|
||||||
from(r in Room, where: r.id == ^room_id, select: r)
|
from(r in Room, where: r.id == ^room_id, select: r)
|
||||||
|> Repo.update_all(set: [forward_extremities: new_forward_extremities])
|
|> Repo.update_all(set: [forward_extremities: new_forward_extremities])
|
||||||
|
|
||||||
room
|
room
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def create(account, input) do
|
||||||
|
with {:ok, %Room{id: room_id}} <- Repo.insert(create_changeset(input)),
|
||||||
|
{:ok, pid} <- RoomServer.get_room_server(room_id) do
|
||||||
|
RoomServer.create_room(pid, account, input)
|
||||||
|
else
|
||||||
|
_ -> {:error, :unknown}
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -114,8 +114,9 @@ defmodule MatrixServer.RoomServer do
|
||||||
end)
|
end)
|
||||||
|
|
||||||
case result do
|
case result do
|
||||||
{:ok, state_set} -> {:reply, :ok, %{state | state_set: state_set}}
|
{:ok, state_set} -> {:reply, {:ok, room_id}, %{state | state_set: state_set}}
|
||||||
{:error, reason} -> {:reply, {:error, reason}, state}
|
{:error, reason} -> {:reply, {:error, reason}, state}
|
||||||
|
_ -> {:reply, {:error, :unknown}, state}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -160,28 +161,17 @@ defmodule MatrixServer.RoomServer do
|
||||||
# 6. Passes authorization rules based on the current state of the room, otherwise it is "soft failed".
|
# 6. Passes authorization rules based on the current state of the room, otherwise it is "soft failed".
|
||||||
event = %Event{event | prev_events: forward_extremities}
|
event = %Event{event | prev_events: forward_extremities}
|
||||||
|
|
||||||
if Event.prevalidate(event) do
|
with true <- Event.prevalidate(event),
|
||||||
if Authorization.authorized_by_auth_events?(event) do
|
true <- Authorization.authorized_by_auth_events?(event),
|
||||||
state_set = StateResolution.resolve(event, false)
|
state_set <- StateResolution.resolve(event, false),
|
||||||
|
true <- Authorization.authorized?(event, state_set),
|
||||||
if Authorization.authorized?(event, state_set) do
|
true <- Authorization.authorized?(event, current_state_set) do
|
||||||
if Authorization.authorized?(event, current_state_set) do
|
room = Room.update_forward_extremities(event, room)
|
||||||
# We assume here that the event is always a forward extremity.
|
event = Repo.insert!(event)
|
||||||
room = Room.update_forward_extremities(event, room)
|
state_set = StateResolution.resolve_forward_extremities(event)
|
||||||
event = Repo.insert!(event)
|
{:ok, state_set, room}
|
||||||
state_set = StateResolution.resolve_forward_extremities(event)
|
|
||||||
{:ok, state_set, room}
|
|
||||||
else
|
|
||||||
{:error, :soft_failed}
|
|
||||||
end
|
|
||||||
else
|
|
||||||
{:error, :rejected}
|
|
||||||
end
|
|
||||||
else
|
|
||||||
{:error, :rejected}
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
{:error, :invalid}
|
_ -> {:error, :authorization}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,7 +4,7 @@ defmodule MatrixServerWeb.RoomController do
|
||||||
import MatrixServerWeb.Plug.Error
|
import MatrixServerWeb.Plug.Error
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
|
|
||||||
alias MatrixServer.{Repo, Room, RoomServer}
|
alias MatrixServer.Room
|
||||||
alias MatrixServerWeb.API.{CreateRoom}
|
alias MatrixServerWeb.API.{CreateRoom}
|
||||||
alias Ecto.Changeset
|
alias Ecto.Changeset
|
||||||
alias Plug.Conn
|
alias Plug.Conn
|
||||||
|
@ -14,15 +14,18 @@ defmodule MatrixServerWeb.RoomController do
|
||||||
%Changeset{valid?: true} = cs ->
|
%Changeset{valid?: true} = cs ->
|
||||||
input = apply_changes(cs)
|
input = apply_changes(cs)
|
||||||
|
|
||||||
# TODO: refactor
|
case Room.create(account, input) do
|
||||||
# Room.create(account, input)
|
{:ok, room_id} ->
|
||||||
# %Room{id: room_id} = Repo.insert!(Room.create_changeset(input))
|
conn
|
||||||
# {:ok, pid} = RoomServer.get_room_server(room_id)
|
|> put_status(200)
|
||||||
# RoomServer.create_room(pid, account, input)
|
|> json(%{room_id: room_id})
|
||||||
|
|
||||||
conn
|
{:error, :authorization} ->
|
||||||
|> put_status(200)
|
put_error(conn, :invalid_room_state)
|
||||||
|> json(%{})
|
|
||||||
|
{:error, :unknown} ->
|
||||||
|
put_error(conn, :unknown)
|
||||||
|
end
|
||||||
|
|
||||||
_ ->
|
_ ->
|
||||||
put_error(conn, :bad_json)
|
put_error(conn, :bad_json)
|
||||||
|
|
|
@ -9,13 +9,15 @@ defmodule MatrixServerWeb.Plug.Error do
|
||||||
forbidden: {400, "M_FORBIDDEN", "The requested action is forbidden."},
|
forbidden: {400, "M_FORBIDDEN", "The requested action is forbidden."},
|
||||||
unrecognized: {400, "M_UNRECOGNIZED", "Unrecognized request."},
|
unrecognized: {400, "M_UNRECOGNIZED", "Unrecognized request."},
|
||||||
unknown: {400, "M_UNKNOWN", "An unknown error occurred."},
|
unknown: {400, "M_UNKNOWN", "An unknown error occurred."},
|
||||||
|
invalid_room_state:
|
||||||
|
{400, "M_INVALID_ROOM_STATE", "The request would result in and invalid room state."},
|
||||||
unknown_token: {401, "M_UNKNOWN_TOKEN", "Invalid access token."},
|
unknown_token: {401, "M_UNKNOWN_TOKEN", "Invalid access token."},
|
||||||
missing_token: {401, "M_MISSING_TOKEN", "Access token required."}
|
missing_token: {401, "M_MISSING_TOKEN", "Access token required."}
|
||||||
}
|
}
|
||||||
|
|
||||||
def put_error(conn, error, msg \\ nil) do
|
def put_error(conn, error, msg \\ nil) do
|
||||||
{status, errcode, default_msg} = @error_map[error]
|
{status, errcode, default_msg} = @error_map[error]
|
||||||
data = %{errcode: errcode, error: msg or default_msg}
|
data = %{errcode: errcode, error: msg || default_msg}
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|> put_status(status)
|
|> put_status(status)
|
||||||
|
|
Loading…
Reference in a new issue