Verified Commit a942636a authored by Nicolas's avatar Nicolas

validate create_room/1 request inside room_server.ex

parent 588f7a36
......@@ -7,7 +7,7 @@ defmodule PlasmaHS.RoomServer.Domain.CreateRoomRequest do
embedded_schema do
field :visibility, :string
belongs_to :sender, PlasmaRepo.Users.User
field :sender_id, :binary_id
field :room_version, :string
field :creation_content, :map
field :power_level_content_override, :map
......@@ -21,14 +21,15 @@ defmodule PlasmaHS.RoomServer.Domain.CreateRoomRequest do
end
end
def validate(%CreateRoomRequest{} = request, params \\ %{}) do
request
def validate(params \\ %{}) do
%CreateRoomRequest{}
|> Ecto.Changeset.cast(params, [
:visibility,
:room_version,
:creation_content,
:power_level_content_override,
:name,
:sender_id,
:topic
])
|> Ecto.Changeset.cast_embed(:initial_state, with: &cast_state_event/2)
......
......@@ -7,10 +7,10 @@ defmodule PlasmaHS.RoomServer do
alias PlasmaHS.RoomServer.RoomState
alias PlasmaHS.RoomServer.Domain.{CreateRoomRequest, RoomState}
def create_room(%CreateRoomRequest{} = request) do
with {:ok, room} <- PlasmaRepo.Rooms.create_room(%{mx_room_id: to_string(PlasmaRepo.Channels.Identifier.generate(:room))}),
def create_room(%{} = request) do
with {:ok, request} <- CreateRoomRequest.validate(request),
{:ok, room} <- PlasmaRepo.Rooms.create_room(%{mx_room_id: to_string(PlasmaRepo.Channels.Identifier.generate(:room))}),
{:ok, pid} <- start_room_server(room.id),
{:ok, request} <- CreateRoomRequest.validate(request),
state <- GenServer.call(pid, request) do
#{:ok, _, _state} <- change_preset(pid, request.sender, request.preset, request.visibility) do
#add_initial_state(pid, request.sender, request.initial_state)
......@@ -22,7 +22,7 @@ defmodule PlasmaHS.RoomServer do
#end
{:ok, state, pid}
else
ret -> {:error, ret}
ret -> ret
end
end
......@@ -101,7 +101,6 @@ defmodule PlasmaHS.RoomServer do
with {:ok, authed_event_attrs} <- PlasmaHS.RoomServer.Auth.auth_event(request.room_version, state, event_attrs),
{:ok, create_event_type} <- PlasmaRepo.Events.get_or_create_event_type(PlasmaHS.EventTypes.m_room_create),
{:ok, %{event: create_event}} = PlasmaRepo.Events.create_event(Map.merge(authed_event_attrs, %{event_type_id: create_event_type.id})) do
IO.inspect create_event
case create_event do
%{rejected: true} -> {:rejected, state}
%{rejected: false} -> {:ok, %{state | :state_events => Map.put(state.state_events, PlasmaHS.EventTypes.m_room_create, create_event)}}
......
......@@ -23,19 +23,15 @@ defmodule PlasmaWeb.Controllers.MatrixApi.Client.R0.Room do
}
with {:room_version, true} <- {:room_version, Enum.member?(@supported_room_versions, request_room_version)},
{:room_visibility, true} <- {:room_visibility, Enum.member?(["private", "public"], request_visibility)} do
room_changeset = CreateRoomRequest.validate(
%CreateRoomRequest{
room_attrs = Map.merge(params, %{
visibility: request_visibility,
room_version: request_room_version,
creation_content: Map.get(params, "creation_content", default_creation_content),
power_level_content_override: Map.get(params, "power_level_content_override", %{}),
initial_state: Map.get(params, "initial_state", %{}),
sender_id: user_id
},
params
)
with {:ok, request} <- room_changeset,
{:ok, state, pid} <- PlasmaHS.RoomServer.create_room(request) do
})
with {:ok, state, pid} <- PlasmaHS.RoomServer.create_room(room_attrs) do
conn |> json(%{"room_id" => state.mx_room_id})
else
{:error, errors} -> send_changeset_error_to_json(conn, errors)
......
......@@ -21,4 +21,7 @@ defmodule PlasmaHS.RoomServerTests do
assert {:error, _pid} = res
end
test "create room succeed with valid create request" do
end
end
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment