Verified Commit 252fac2f authored by Nicolas's avatar Nicolas

Enable room creation tests

parent f7a21650
......@@ -3,24 +3,4 @@ defmodule PlasmaHS.RoomServer.Domain do
defmodule RoomState do
defstruct [:room_id, :mx_room_id, :create_event, :creator, :power_levels, :join_rule]
end
defmodule CreateRoom do
use Ecto.Schema
embedded_schema do
field :visibility, :string
belongs_to :sender, PlasmaRepo.Users.User
field :room_version, :string
field :creation_content, :map
field :power_level_content_override, :map
end
end
@spec validate(CreateRoom.t()) :: {:ok, CreateRoom.t()} | {:error, Ecto.Changeset.t()}
def validate(%CreateRoom{} = create_room) do
create_room
|> Ecto.Changeset.change()
|> Ecto.Changeset.validate_required([:sender])
|> Ecto.Changeset.apply_action(:insert)
end
end
\ No newline at end of file
defmodule PlasmaRepo.MatrixRooms.CreateRoomRequest do
require Logger
defmodule PlasmaHS.RoomServer.Domain.CreateRoomRequest do
use Ecto.Schema
alias PlasmaHS.RoomServer.Domain.CreateRoomRequest
@visibilities ["public", "private"]
@presets ["private_chat", "public_chat", "trusted_private_chat"]
embedded_schema do
field :visibility, :string
belongs_to :sender, PlasmaRepo.Users.User
field :room_version, :string
field :creation_content, :map
field :power_level_content_override, :map
......@@ -20,7 +21,7 @@ defmodule PlasmaRepo.MatrixRooms.CreateRoomRequest do
end
end
def cast(request, params \\ %{}) do
def validate(%CreateRoomRequest{} = request, params \\ %{}) do
request
|> Ecto.Changeset.cast(params, [
:visibility,
......@@ -31,8 +32,10 @@ defmodule PlasmaRepo.MatrixRooms.CreateRoomRequest do
:topic
])
|> Ecto.Changeset.cast_embed(:initial_state, with: &cast_state_event/2)
|> Ecto.Changeset.validate_required([:sender_id])
|> Ecto.Changeset.validate_inclusion(:visibility, @visibilities, message: "bad_type")
|> Ecto.Changeset.validate_inclusion(:preset, @presets, message: "bad_type")
|> Ecto.Changeset.apply_action(:insert)
end
defp cast_state_event(schema, params) do
......
......@@ -3,21 +3,24 @@ defmodule PlasmaHS.RoomServer do
alias PlasmaRepo.Rooms
alias PlasmaHS.RoomServer.RoomState
alias PlasmaHS.RoomServer.Domain.{CreateRoom, RoomState}
alias PlasmaHS.RoomServer.Domain.{CreateRoomRequest, RoomState}
def create_room(pid, %CreateRoom{} = request) do
with {:ok, request} <- CreateRoom.validate(request),
{:ok, _events, _state} <- GenServer.call(pid, request),
{: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
add_state_event(pid, request.sender, EventTypes.m_room_name, "", request.name)
end
state = if not is_nil(request.topic) do
add_state_event(pid, request.sender, EventTypes.m_room_topic, "", request.topic)
end
state
def create_room(%CreateRoomRequest{} = request) do
with {: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)
#state = if not is_nil(request.name) do
#add_state_event(pid, request.sender, EventTypes.m_room_name, "", request.name)
#end
#state = if not is_nil(request.topic) do
# add_state_event(pid, request.sender, EventTypes.m_room_topic, "", request.topic)
#end
{:ok, state, pid}
else
ret -> {:error, ret}
end
end
......@@ -48,15 +51,6 @@ defmodule PlasmaHS.RoomServer do
content: content})
end
@doc """
Try to start a new room server. A room is created in the repo before starting the server.
The room must exists before starting the room server
"""
@spec start_room_server(String.t()) :: {:ok, pid()} | :ignore | {:error, {:already_started, pid()} | term()}
def start_room_server() do
Rooms.create_room() |> start_room_server
end
@doc """
Try to start a room server. If the room is already started, the existing pid is returned
The room must exists before starting the room server
......@@ -85,8 +79,8 @@ defmodule PlasmaHS.RoomServer do
end
@impl true
def handle_call(%CreateRoom{} = request, _from, state) do
def handle_call(%CreateRoomRequest{} = request, _from, state) do
{:reply, state, state}
end
end
\ No newline at end of file
end
\ No newline at end of file
......@@ -19,7 +19,7 @@ defmodule PlasmaRepo.Rooms.Room do
def changeset(room, attrs \\ %{}) do
room
|> cast(attrs, [:mx_room_id, :visibility])
|> cast(attrs, [:mx_room_id, :is_public])
|> validate_required([:mx_room_id])
|> unique_constraint(:mx_room_id, message: "unique")
end
......
......@@ -2,7 +2,7 @@ defmodule PlasmaRepo.Rooms do
alias PlasmaRepo.Rooms.Room
alias PlasmaRepo.Repo
def create_room(params) do
def create_room(params \\ %{}) do
Room.create_changeset(%Room{}, params) |> Repo.insert
end
......
defmodule PlasmaWeb.Controllers.MatrixApi.Client.R0.Room do
use PlasmaWeb, :controller
use PlasmaWeb.Controllers.MatrixApi.MatrixController
alias PlasmaRepo.MatrixRooms.CreateRoomRequest
alias PlasmaHS.RoomServer.Domain.CreateRoomRequest
import PlasmaWeb.Errors
require Logger
@default_room_version "1"
def create_room(conn, params) do
room_changeset =
CreateRoomRequest.cast(
{:ok, user_id} = Map.fetch(conn.assigns, :user_id)
room_changeset = CreateRoomRequest.validate(
%CreateRoomRequest{
visibility: "private",
room_version: 1,
room_version: @default_room_version,
creation_content: %{},
power_level_content_override: %{},
initial_state: %{}
initial_state: %{},
sender_id: user_id
},
params
)
|> Ecto.Changeset.apply_action(:insert)
case room_changeset do
{:error, errors} ->
send_changeset_error_to_json(conn, errors)
{:ok, request} ->
sender = conn.assigns.user_id
ret = PlasmaRepo.MatrixRooms.create_room(sender, request)
conn |> json(%{"room_id" => ret.room_id})
with {:ok, request} <- room_changeset,
{:ok, state, pid} <- PlasmaHS.RoomServer.create_room(request) do
conn |> json(%{"room_id" => state.mx_room_id})
else
{:error, errors} -> send_changeset_error_to_json(conn, errors)
end
end
......
......@@ -18,6 +18,7 @@
"delta_crdt": {:hex, :delta_crdt, "0.5.1", "309efe887f179db8ac9489606f494d09f60e177b170ecd2f303f3d11667c1774", [:mix], [{:merkle_map, "~> 0.1.1", [hex: :merkle_map, repo: "hexpm", optional: false]}, {:murmur, "~> 1.0", [hex: :murmur, repo: "hexpm", optional: false]}], "hexpm"},
"distillery": {:hex, :distillery, "2.1.1", "f9332afc2eec8a1a2b86f22429e068ef35f84a93ea1718265e740d90dd367814", [:mix], [{:artificery, "~> 0.2", [hex: :artificery, repo: "hexpm", optional: false]}], "hexpm"},
"ecto": {:hex, :ecto, "3.3.0", "9193e261d25c1814324d0b3304fccbadab840b286d270c3b75dfd28c30a3ae15", [:mix], [{:decimal, "~> 1.6", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm"},
"ecto_enum": {:hex, :ecto_enum, "1.4.0", "d14b00e04b974afc69c251632d1e49594d899067ee2b376277efd8233027aec8", [:mix], [{:ecto, ">= 3.0.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "> 3.0.0", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:mariaex, ">= 0.0.0", [hex: :mariaex, repo: "hexpm", optional: true]}, {:postgrex, ">= 0.0.0", [hex: :postgrex, repo: "hexpm", optional: true]}], "hexpm"},
"ecto_sql": {:hex, :ecto_sql, "3.3.2", "92804e0de69bb63e621273c3492252cb08a29475c05d40eeb6f41ad2d483cfd3", [:mix], [{:db_connection, "~> 2.2", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.3", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.3.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.15.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm"},
"elixir_make": {:hex, :elixir_make, "0.6.0", "38349f3e29aff4864352084fc736fa7fa0f2995a819a737554f7ebd28b85aaab", [:mix], [], "hexpm"},
"elixir_uuid": {:hex, :elixir_uuid, "1.2.1", "dce506597acb7e6b0daeaff52ff6a9043f5919a4c3315abb4143f0b00378c097", [:mix], [], "hexpm"},
......
......@@ -21,7 +21,6 @@ defmodule PlasmaWeb.Controllers.MatrixApi.Client.R0.RoomsTest do
[access_token: access_token]
end
@tag :skip
test "create_room with defaults succeed", %{conn: conn, access_token: access_token} do
request = %{}
......@@ -36,7 +35,6 @@ defmodule PlasmaWeb.Controllers.MatrixApi.Client.R0.RoomsTest do
assert %{"room_id" => room_id} = response
end
@tag :skip
test "create_room fails with invalid visibility", %{conn: conn, access_token: access_token} do
request = %{"visibility" => "invisible"}
......
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