Add working implementation of state resolution
Add some database seeds
This commit is contained in:
parent
2d34f78a0b
commit
8a2ef0d079
7 changed files with 848 additions and 410 deletions
|
@ -1,41 +1,76 @@
|
|||
defmodule MatrixServer.RoomServer do
|
||||
use GenServer
|
||||
|
||||
alias MatrixServer.{Repo, Room, Event}
|
||||
alias MatrixServer.{Repo, Room, Event, Account}
|
||||
alias MatrixServerWeb.API.CreateRoom
|
||||
alias Ecto.Multi
|
||||
|
||||
def start_link(_opts) do
|
||||
GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
|
||||
@registry MatrixServer.RoomServer.Registry
|
||||
@supervisor MatrixServer.RoomServer.Supervisor
|
||||
|
||||
def get_room_server(room_id) do
|
||||
case Registry.lookup(@registry, room_id) do
|
||||
[{pid, _}] ->
|
||||
{:ok, pid}
|
||||
|
||||
[] ->
|
||||
opts = [
|
||||
room_id: room_id,
|
||||
name: {:via, Registry, {@registry, room_id}}
|
||||
]
|
||||
|
||||
DynamicSupervisor.start_child(@supervisor, {__MODULE__, opts})
|
||||
end
|
||||
end
|
||||
|
||||
def create_room(%CreateRoom{} = input, account) do
|
||||
GenServer.call(__MODULE__, {:create_room, input, account})
|
||||
def start_link(opts) do
|
||||
{name, opts} = Keyword.pop(opts, :name)
|
||||
GenServer.start_link(__MODULE__, opts, name: name)
|
||||
end
|
||||
|
||||
def create_room(input, account) do
|
||||
Multi.new()
|
||||
|> Multi.insert(:room, Room.create_changeset(input))
|
||||
|> Multi.run(:room_server, fn _repo, %{room: %Room{id: room_id} = room} ->
|
||||
opts = [
|
||||
name: {:via, Registry, {@registry, room_id}},
|
||||
input: input,
|
||||
account: account,
|
||||
room: room
|
||||
]
|
||||
|
||||
DynamicSupervisor.start_child(@supervisor, {__MODULE__, opts})
|
||||
end)
|
||||
|> Repo.transaction()
|
||||
end
|
||||
|
||||
@impl true
|
||||
def init(:ok) do
|
||||
{:ok, %{}}
|
||||
def init(opts) do
|
||||
%Room{id: room_id} = Keyword.fetch!(opts, :room)
|
||||
input = Keyword.fetch!(opts, :input)
|
||||
account = Keyword.fetch!(opts, :account)
|
||||
|
||||
state = %{
|
||||
room_id: room_id,
|
||||
state_set: %{}
|
||||
}
|
||||
|
||||
Repo.transaction(fn ->
|
||||
with {:ok, create_room_event} <- insert_create_room_event(account, input, state) do
|
||||
{:ok, state}
|
||||
end
|
||||
end)
|
||||
|
||||
{:ok, state}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_call({:create_room, input, account}, _from, state) do
|
||||
# TODO: preset events, initial_state events, invite, invite_3pid
|
||||
result =
|
||||
Multi.new()
|
||||
|> Multi.put(:input, input)
|
||||
|> Multi.put(:account, account)
|
||||
|> Multi.insert(:room, Room.create_changeset(input))
|
||||
|> Multi.run(:create_room_event, &Event.room_creation_create_room/2)
|
||||
|> Multi.run(:join_creator_event, &Event.room_creation_join_creator/2)
|
||||
|> Multi.run(:power_levels_event, &Event.room_creation_power_levels/2)
|
||||
|> Multi.run(:name_event, &Event.room_creation_name/2)
|
||||
|> Multi.run(:topic_event, &Event.room_creation_topic/2)
|
||||
|> Multi.run(:temp, fn _, _ ->
|
||||
{:error, :lol}
|
||||
end)
|
||||
|> Repo.transaction()
|
||||
|
||||
{:reply, result, state}
|
||||
defp insert_create_room_event(
|
||||
%Account{localpart: localpart},
|
||||
%CreateRoom{room_version: room_version},
|
||||
%{room_id: room_id, state_set: state_set}
|
||||
) do
|
||||
create_room_event = Event.create_room(room_id, MatrixServer.get_mxid(localpart), room_version)
|
||||
MatrixServer.StateResolution.resolve(create_room_event)
|
||||
{:ok, create_room_event}
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue