Verified Commit 10c5cf82 authored by Nicolas's avatar Nicolas

Implement room server tests (still to be done)

parent 0ee75892
Pipeline #112403083 passed with stages
in 3 minutes and 1 second
......@@ -3,15 +3,16 @@ defmodule PlasmaHS.RoomServer do
require PlasmaHS.EventTypes
alias PlasmaRepo.Rooms
alias PlasmaRepo.Events.Event
alias PlasmaHS.RoomServer.RoomState
alias PlasmaHS.RoomServer.Domain.{CreateRoomRequest, RoomState}
@spec create_room(map) :: {:ok, RoomState.t(), pid} | {:error, RoomState.t()}| {:error, RoomState.t(), Changeset.t()}
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),
state <- GenServer.call(pid, request) do
:ok <- GenServer.cast(pid, request),
state <- get_room_state(pid) do
#{:ok, _, _state} <- change_preset(pid, request.sender, request.preset, request.visibility) do
#add_initial_state(pid, request.sender, request.initial_state)
#state = if not is_nil(request.name) do
......@@ -22,10 +23,15 @@ defmodule PlasmaHS.RoomServer do
#end
{:ok, state, pid}
else
ret -> ret
{:error, changeset} -> {:error, changeset}
end
end
def get_room_state(pid) do
GenServer.call(pid, :get_room_state)
end
defp change_preset(channel, sender, preset, visibility) do
join_rule = case preset do
"private_chat" -> "invite"
......@@ -82,9 +88,17 @@ defmodule PlasmaHS.RoomServer do
end
@impl true
def handle_call(%CreateRoomRequest{} = request, _from, state) do
with {:ok, state} <- create_m_room_create_event(request, state) do
def handle_call(:get_room_state, _from, state) do
{:reply, state, state}
end
@impl true
def handle_cast(%CreateRoomRequest{} = request, state) do
with {:ok, new_state} <- create_m_room_create_event(request, state) do
# IO.inspect new_state
{:noreply, new_state}
else
_ -> {:noreply, state}
end
end
......
......@@ -34,7 +34,8 @@ defmodule PlasmaWeb.Controllers.MatrixApi.Client.R0.Room 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)
{:rejected, state} -> conn |> json_error(:m_invalid_room_state)
{:error, changeset} -> send_changeset_error_to_json(conn, changeset)
end
else
{:room_version, false} -> conn |> json_error(:m_unsupported_room_version)
......
......@@ -21,7 +21,9 @@ defmodule PlasmaHS.RoomServerTests do
assert {:error, _pid} = res
end
test "create room succeed with valid create request" do
test "create room succeed with valid request" do
sender = insert(:user)
res = RoomServer.create_room(%{sender_id: sender.id})
IO.inspect res
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