2021-07-10 21:16:00 +00:00
|
|
|
defmodule MatrixServer.RoomServer do
|
|
|
|
use GenServer
|
|
|
|
|
2021-07-25 12:57:52 +00:00
|
|
|
import Ecto.Query
|
|
|
|
|
2021-07-24 20:08:01 +00:00
|
|
|
alias MatrixServer.{Repo, Room, Event, Account, StateResolution}
|
2021-07-10 21:16:00 +00:00
|
|
|
alias MatrixServerWeb.API.CreateRoom
|
|
|
|
|
2021-07-23 19:00:01 +00:00
|
|
|
@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
|
|
|
|
|
2021-07-23 19:00:01 +00:00
|
|
|
def create_room(input, account) do
|
2021-07-23 22:13:12 +00:00
|
|
|
%Room{id: room_id} = room = Repo.insert!(Room.create_changeset(input))
|
2021-07-23 19:00:01 +00:00
|
|
|
|
2021-07-23 22:13:12 +00:00
|
|
|
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
|
2021-07-23 19:00:01 +00:00
|
|
|
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 ->
|
2021-07-23 22:13:12 +00:00
|
|
|
with {:ok, state_set} <- insert_create_room_event(account, input, room_id) do
|
|
|
|
{:ok, %{room_id: room_id, state_set: state_set}}
|
2021-07-23 19:00:01 +00:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp insert_create_room_event(
|
|
|
|
%Account{localpart: localpart},
|
|
|
|
%CreateRoom{room_version: room_version},
|
2021-07-23 22:13:12 +00:00
|
|
|
room_id
|
2021-07-23 19:00:01 +00:00
|
|
|
) do
|
2021-07-24 20:08:01 +00:00
|
|
|
create_room_event = Event.create_room(room_id, MatrixServer.get_mxid(localpart), room_version)
|
2021-07-24 20:54:03 +00:00
|
|
|
|
2021-07-25 12:57:52 +00:00
|
|
|
verify_and_insert_event(create_room_event)
|
2021-07-24 20:08:01 +00:00
|
|
|
|
|
|
|
{:ok, %{}}
|
|
|
|
end
|
2021-07-23 22:13:12 +00:00
|
|
|
|
2021-07-25 12:57:52 +00:00
|
|
|
defp verify_and_insert_event(event) do
|
2021-07-24 20:08:01 +00:00
|
|
|
# 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)
|
2021-07-25 12:57:52 +00:00
|
|
|
|
|
|
|
if StateResolution.is_authorized(event, state_set) do
|
|
|
|
# TODO: Assume the event is a forward extremity, should check this actually.
|
|
|
|
Room.update_forward_extremities(event)
|
|
|
|
{:ok, event} = Repo.insert(event)
|
|
|
|
{:ok, state_set}
|
|
|
|
else
|
|
|
|
{:error, :rejected}
|
|
|
|
end
|
2021-07-24 20:08:01 +00:00
|
|
|
else
|
2021-07-25 12:57:52 +00:00
|
|
|
{:error, :rejected}
|
2021-07-24 20:08:01 +00:00
|
|
|
end
|
2021-07-10 21:16:00 +00:00
|
|
|
end
|
2021-07-25 12:57:52 +00:00
|
|
|
|
|
|
|
def testing do
|
|
|
|
account = Repo.one!(from a in Account, limit: 1)
|
|
|
|
create_room(%CreateRoom{}, account)
|
|
|
|
end
|
2021-07-10 21:16:00 +00:00
|
|
|
end
|