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-29 14:59:40 +00:00
|
|
|
import Ecto.Changeset
|
2021-07-25 12:57:52 +00:00
|
|
|
|
2021-07-28 15:23:48 +00:00
|
|
|
alias MatrixServer.{Repo, Room, Event, StateResolution}
|
2021-07-10 21:16:00 +00:00
|
|
|
alias MatrixServerWeb.API.CreateRoom
|
2021-07-28 15:23:48 +00:00
|
|
|
alias MatrixServer.StateResolution.Authorization
|
2021-07-10 21:16:00 +00:00
|
|
|
|
2021-07-23 19:00:01 +00:00
|
|
|
@registry MatrixServer.RoomServer.Registry
|
|
|
|
@supervisor MatrixServer.RoomServer.Supervisor
|
|
|
|
|
2021-07-28 15:23:48 +00:00
|
|
|
### Interface
|
2021-07-10 21:16:00 +00:00
|
|
|
|
2021-07-25 15:39:22 +00:00
|
|
|
def start_link(opts) do
|
|
|
|
{name, opts} = Keyword.pop(opts, :name)
|
|
|
|
GenServer.start_link(__MODULE__, opts, name: name)
|
|
|
|
end
|
|
|
|
|
2021-07-28 15:23:48 +00:00
|
|
|
# Get room server pid, or spin one up for the room.
|
|
|
|
# If the room does not exist, return an error.
|
|
|
|
def get_room_server(room_id) do
|
|
|
|
case Repo.one(from r in Room, where: r.id == ^room_id) do
|
|
|
|
nil ->
|
|
|
|
{:error, :not_found}
|
|
|
|
|
2021-07-29 14:59:40 +00:00
|
|
|
%Room{state: serialized_state_set} ->
|
2021-07-28 15:23:48 +00:00
|
|
|
case Registry.lookup(@registry, room_id) do
|
|
|
|
[{pid, _}] ->
|
|
|
|
{:ok, pid}
|
|
|
|
|
|
|
|
[] ->
|
|
|
|
opts = [
|
|
|
|
name: {:via, Registry, {@registry, room_id}},
|
2021-07-29 14:59:40 +00:00
|
|
|
room_id: room_id,
|
|
|
|
serialized_state_set: serialized_state_set
|
2021-07-28 15:23:48 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
DynamicSupervisor.start_child(@supervisor, {__MODULE__, opts})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_room(pid, account, input) do
|
|
|
|
GenServer.call(pid, {:create_room, account, input})
|
|
|
|
end
|
|
|
|
|
|
|
|
### Implementation
|
|
|
|
|
2021-07-10 21:16:00 +00:00
|
|
|
@impl true
|
2021-07-23 19:00:01 +00:00
|
|
|
def init(opts) do
|
2021-07-28 15:23:48 +00:00
|
|
|
room_id = Keyword.fetch!(opts, :room_id)
|
2021-07-29 14:59:40 +00:00
|
|
|
serialized_state_set = Keyword.fetch!(opts, :serialized_state_set)
|
|
|
|
state_event_ids = Enum.map(serialized_state_set, fn [_, _, event_id] -> event_id end)
|
|
|
|
|
|
|
|
state_set =
|
|
|
|
Event
|
|
|
|
|> where([e], e.event_id in ^state_event_ids)
|
|
|
|
|> Repo.all()
|
|
|
|
|> Enum.into(%{}, fn %Event{type: type, state_key: state_key} = event ->
|
|
|
|
{{type, state_key}, event}
|
|
|
|
end)
|
2021-07-28 15:23:48 +00:00
|
|
|
|
2021-07-29 14:59:40 +00:00
|
|
|
{:ok, %{room_id: room_id, state_set: state_set}}
|
2021-07-28 15:23:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
2021-07-29 14:59:40 +00:00
|
|
|
def handle_call(
|
|
|
|
{:create_room, account,
|
|
|
|
%CreateRoom{room_version: room_version, name: name, topic: topic, preset: preset}},
|
|
|
|
_from,
|
|
|
|
%{room_id: room_id} = state
|
|
|
|
) do
|
2021-07-28 15:23:48 +00:00
|
|
|
result =
|
|
|
|
Repo.transaction(fn ->
|
2021-07-29 14:59:40 +00:00
|
|
|
room = Repo.one!(from r in Room, where: r.id == ^room_id)
|
|
|
|
create_room = Event.create_room(room, account, room_version)
|
|
|
|
join_creator = Event.join(room, account, [create_room.event_id])
|
|
|
|
pls = Event.power_levels(room, account, [create_room.event_id, join_creator.event_id])
|
|
|
|
auth_events = [create_room.event_id, join_creator.event_id, pls.event_id]
|
|
|
|
name_event = if name, do: Event.name(room, account, name, auth_events)
|
|
|
|
topic_event = if topic, do: Event.topic(room, account, topic, auth_events)
|
|
|
|
|
2021-07-28 15:23:48 +00:00
|
|
|
# TODO: power_level_content_override, initial_state, invite, invite_3pid
|
2021-07-29 14:59:40 +00:00
|
|
|
events =
|
|
|
|
[create_room, join_creator, pls] ++
|
|
|
|
room_creation_preset(account, preset, room, auth_events) ++
|
|
|
|
[name_event, topic_event]
|
|
|
|
|
|
|
|
result =
|
|
|
|
events
|
|
|
|
|> Enum.reject(&Kernel.is_nil/1)
|
|
|
|
|> Enum.reduce_while({%{}, room}, fn event, {state_set, room} ->
|
|
|
|
case verify_and_insert_event(event, state_set, room) do
|
|
|
|
{:ok, state_set, room} -> {:cont, {state_set, room}}
|
|
|
|
{:error, reason} -> {:halt, {:error, reason}}
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
case result do
|
|
|
|
{:error, reason} ->
|
2021-07-28 15:23:48 +00:00
|
|
|
Repo.rollback(reason)
|
2021-07-29 14:59:40 +00:00
|
|
|
|
|
|
|
{state_set, room} ->
|
|
|
|
serialized_state_set =
|
|
|
|
Enum.map(state_set, fn {{type, state_key}, event} ->
|
|
|
|
[type, state_key, event.event_id]
|
|
|
|
end)
|
|
|
|
|
|
|
|
Repo.update!(change(room, state: serialized_state_set))
|
|
|
|
state_set
|
2021-07-28 15:23:48 +00:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
case result do
|
2021-07-29 20:06:02 +00:00
|
|
|
{:ok, state_set} -> {:reply, {:ok, room_id}, %{state | state_set: state_set}}
|
2021-07-28 15:23:48 +00:00
|
|
|
{:error, reason} -> {:reply, {:error, reason}, state}
|
2021-07-29 20:06:02 +00:00
|
|
|
_ -> {:reply, {:error, :unknown}, state}
|
2021-07-28 15:23:48 +00:00
|
|
|
end
|
2021-07-23 19:00:01 +00:00
|
|
|
end
|
|
|
|
|
2021-07-27 10:55:36 +00:00
|
|
|
# TODO: trusted_private_chat:
|
|
|
|
# All invitees are given the same power level as the room creator.
|
2021-07-29 14:59:40 +00:00
|
|
|
defp room_creation_preset(account, nil, %Room{visibility: visibility} = room, auth_events) do
|
2021-07-27 10:55:36 +00:00
|
|
|
preset =
|
|
|
|
case visibility do
|
|
|
|
:public -> "public_chat"
|
|
|
|
:private -> "private_chat"
|
|
|
|
end
|
|
|
|
|
2021-07-29 14:59:40 +00:00
|
|
|
room_creation_preset(account, preset, room, auth_events)
|
2021-07-27 10:55:36 +00:00
|
|
|
end
|
|
|
|
|
2021-07-29 14:59:40 +00:00
|
|
|
defp room_creation_preset(account, preset, room, auth_events) do
|
2021-07-27 10:55:36 +00:00
|
|
|
{join_rule, his_vis, guest_access} =
|
|
|
|
case preset do
|
|
|
|
"private_chat" -> {"invite", "shared", "can_join"}
|
|
|
|
"trusted_private_chat" -> {"invite", "shared", "can_join"}
|
|
|
|
"public_chat" -> {"public", "shared", "forbidden"}
|
|
|
|
end
|
|
|
|
|
2021-07-29 14:59:40 +00:00
|
|
|
[
|
|
|
|
Event.join_rules(room, account, join_rule, auth_events),
|
|
|
|
Event.history_visibility(room, account, his_vis, auth_events),
|
|
|
|
Event.guest_access(room, account, guest_access, auth_events)
|
|
|
|
]
|
2021-07-26 21:42:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defp verify_and_insert_event(
|
2021-07-29 14:59:40 +00:00
|
|
|
event,
|
2021-07-26 21:42:35 +00:00
|
|
|
current_state_set,
|
|
|
|
%Room{forward_extremities: forward_extremities} = room
|
|
|
|
) 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.
|
2021-07-25 15:39:22 +00:00
|
|
|
# 6. Passes authorization rules based on the current state of the room, otherwise it is "soft failed".
|
2021-07-26 21:42:35 +00:00
|
|
|
event = %Event{event | prev_events: forward_extremities}
|
|
|
|
|
2021-07-29 20:06:02 +00:00
|
|
|
with true <- Event.prevalidate(event),
|
|
|
|
true <- Authorization.authorized_by_auth_events?(event),
|
|
|
|
state_set <- StateResolution.resolve(event, false),
|
|
|
|
true <- Authorization.authorized?(event, state_set),
|
|
|
|
true <- Authorization.authorized?(event, current_state_set) do
|
|
|
|
room = Room.update_forward_extremities(event, room)
|
|
|
|
event = Repo.insert!(event)
|
|
|
|
state_set = StateResolution.resolve_forward_extremities(event)
|
|
|
|
{:ok, state_set, room}
|
2021-07-24 20:08:01 +00:00
|
|
|
else
|
2021-07-29 20:06:02 +00:00
|
|
|
_ -> {:error, :authorization}
|
2021-07-24 20:08:01 +00:00
|
|
|
end
|
2021-07-10 21:16:00 +00:00
|
|
|
end
|
|
|
|
end
|