architex/lib/matrix_server/room_server.ex

70 lines
2.1 KiB
Elixir
Raw Normal View History

2021-07-10 21:16:00 +00:00
defmodule MatrixServer.RoomServer do
use GenServer
alias MatrixServer.{Repo, Room, Event, Account, StateResolution}
2021-07-10 21:16:00 +00:00
alias MatrixServerWeb.API.CreateRoom
@registry MatrixServer.RoomServer.Registry
@supervisor MatrixServer.RoomServer.Supervisor
def start_link(opts) do
{name, opts} = Keyword.pop(opts, :name)
GenServer.start_link(__MODULE__, opts, name: name)
2021-07-10 21:16:00 +00:00
end
def create_room(input, account) do
%Room{id: room_id} = room = Repo.insert!(Room.create_changeset(input))
opts = [
name: {:via, Registry, {@registry, room_id}},
input: input,
account: account,
room: room
]
DynamicSupervisor.start_child(@supervisor, {__MODULE__, opts})
2021-07-10 21:16:00 +00:00
end
@impl true
def init(opts) do
%Room{id: room_id} = Keyword.fetch!(opts, :room)
input = Keyword.fetch!(opts, :input)
account = Keyword.fetch!(opts, :account)
Repo.transaction(fn ->
with {:ok, state_set} <- insert_create_room_event(account, input, room_id) do
{:ok, %{room_id: room_id, state_set: state_set}}
end
end)
end
defp insert_create_room_event(
%Account{localpart: localpart},
%CreateRoom{room_version: room_version},
room_id
) do
create_room_event = Event.create_room(room_id, MatrixServer.get_mxid(localpart), room_version)
verify_event(create_room_event)
|> IO.inspect()
{:ok, %{}}
end
defp verify_event(event) do
# Check the following things:
# 1. TODO: Is a valid event, otherwise it is dropped.
# 2. TODO: Passes signature checks, otherwise it is dropped.
# 3. TODO: Passes hash checks, otherwise it is redacted before being processed further.
# 4. Passes authorization rules based on the event's auth events, otherwise it is rejected.
# 5. Passes authorization rules based on the state at the event, otherwise it is rejected.
# 6. TODO: Passes authorization rules based on the current state of the room, otherwise it is "soft failed".
if StateResolution.is_authorized_by_auth_events(event) do
state_set = StateResolution.resolve(event, false)
StateResolution.is_authorized(event, state_set)
else
false
end
2021-07-10 21:16:00 +00:00
end
end