2021-07-10 21:16:00 +00:00
|
|
|
defmodule MatrixServer.RoomServer do
|
2021-08-22 13:19:48 +00:00
|
|
|
@moduledoc """
|
|
|
|
A GenServer to hold and manipulate the state of a Matrix room.
|
|
|
|
|
|
|
|
Each RoomServer corresponds to one Matrix room that the homeserver participates in.
|
|
|
|
The RoomServers are supervised by a DynamicSupervisor RoomServer.Supervisor.
|
|
|
|
"""
|
|
|
|
|
2021-08-24 23:27:03 +00:00
|
|
|
@typep t :: map()
|
|
|
|
|
2021-07-10 21:16:00 +00:00
|
|
|
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-08-24 23:27:03 +00:00
|
|
|
alias MatrixServer.{Repo, Room, Event, StateResolution, Account, JoinedRoom}
|
2021-07-28 15:23:48 +00:00
|
|
|
alias MatrixServer.StateResolution.Authorization
|
2021-08-26 20:09:45 +00:00
|
|
|
alias MatrixServerWeb.Client.Request.{CreateRoom, Kick, Ban}
|
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-08-22 13:19:48 +00:00
|
|
|
@doc """
|
|
|
|
Get the PID of the RoomServer for a room.
|
2021-08-21 09:25:36 +00:00
|
|
|
|
2021-08-22 13:19:48 +00:00
|
|
|
If the given room has no running RoomServer yet, it is created.
|
|
|
|
If the given room does not exist, an error is returned.
|
|
|
|
"""
|
2021-08-18 21:22:04 +00:00
|
|
|
@spec get_room_server(String.t()) :: {:error, :not_found} | DynamicSupervisor.on_start_child()
|
2021-07-28 15:23:48 +00:00
|
|
|
def get_room_server(room_id) do
|
2021-08-22 13:19:48 +00:00
|
|
|
# TODO: Might be wise to use a transaction here to prevent race conditions.
|
2021-07-28 15:23:48 +00:00
|
|
|
case Repo.one(from r in Room, where: r.id == ^room_id) do
|
2021-08-24 23:27:03 +00:00
|
|
|
%Room{state: serialized_state_set} = room ->
|
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-08-24 23:27:03 +00:00
|
|
|
room: room,
|
2021-07-29 14:59:40 +00:00
|
|
|
serialized_state_set: serialized_state_set
|
2021-07-28 15:23:48 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
DynamicSupervisor.start_child(@supervisor, {__MODULE__, opts})
|
|
|
|
end
|
2021-08-22 13:19:48 +00:00
|
|
|
|
|
|
|
nil ->
|
|
|
|
{:error, :not_found}
|
2021-07-28 15:23:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-22 13:19:48 +00:00
|
|
|
@doc """
|
|
|
|
Create a new Matrix room.
|
|
|
|
|
|
|
|
The new room is created with the given `account` as creator.
|
|
|
|
Events are inserted into the new room depending on the input `input` and according
|
|
|
|
to the [Matrix documentation](https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-createroom).
|
|
|
|
"""
|
2021-08-24 23:27:03 +00:00
|
|
|
@spec create_room(pid(), Account.t(), CreateRoom.t()) :: {:ok, String.t()} | {:error, atom()}
|
2021-07-28 15:23:48 +00:00
|
|
|
def create_room(pid, account, input) do
|
|
|
|
GenServer.call(pid, {:create_room, account, input})
|
|
|
|
end
|
|
|
|
|
2021-08-22 13:19:48 +00:00
|
|
|
@doc """
|
|
|
|
Check whether the given server participates in a room.
|
|
|
|
|
|
|
|
Check whether any participant of the room has a server name matching
|
|
|
|
the given `domain`.
|
|
|
|
"""
|
|
|
|
@spec server_in_room?(pid(), String.t()) :: boolean()
|
|
|
|
def server_in_room?(pid, domain) do
|
|
|
|
GenServer.call(pid, {:server_in_room?, domain})
|
2021-08-16 17:30:47 +00:00
|
|
|
end
|
|
|
|
|
2021-08-22 13:19:48 +00:00
|
|
|
@doc """
|
|
|
|
Get the state of a room, before the given event was inserted.
|
|
|
|
|
|
|
|
Return a list of all state events and the auth chain.
|
|
|
|
"""
|
2021-08-22 10:19:47 +00:00
|
|
|
@spec get_state_at_event(pid(), Event.t()) :: {[Event.t()], [Event.t()]}
|
2021-08-21 19:39:28 +00:00
|
|
|
def get_state_at_event(pid, event) do
|
|
|
|
GenServer.call(pid, {:get_state_at_event, event})
|
|
|
|
end
|
|
|
|
|
2021-08-22 13:19:48 +00:00
|
|
|
@doc """
|
|
|
|
Same as `get_state_at_event/2`, except returns the lists as event IDs.
|
|
|
|
"""
|
2021-08-22 10:19:47 +00:00
|
|
|
@spec get_state_ids_at_event(pid(), Event.t()) :: {[String.t()], [String.t()]}
|
|
|
|
def get_state_ids_at_event(pid, event) do
|
|
|
|
GenServer.call(pid, {:get_state_ids_at_event, event})
|
|
|
|
end
|
|
|
|
|
2021-08-24 23:27:03 +00:00
|
|
|
@doc """
|
|
|
|
Invite the a user to this room.
|
|
|
|
"""
|
|
|
|
@spec invite(pid(), Account.t(), String.t()) :: :ok | {:error, atom()}
|
|
|
|
def invite(pid, account, user_id) do
|
|
|
|
GenServer.call(pid, {:invite, account, user_id})
|
|
|
|
end
|
|
|
|
|
2021-08-25 10:25:14 +00:00
|
|
|
@doc """
|
|
|
|
Join a room.
|
|
|
|
"""
|
|
|
|
@spec join(pid(), Account.t()) :: {:ok, String.t()} | {:error, atom()}
|
|
|
|
def join(pid, account) do
|
|
|
|
GenServer.call(pid, {:join, account})
|
|
|
|
end
|
|
|
|
|
2021-08-26 09:01:19 +00:00
|
|
|
@doc """
|
|
|
|
Leave a room.
|
|
|
|
"""
|
|
|
|
@spec leave(pid(), Account.t()) :: :ok | {:error, atom()}
|
|
|
|
def leave(pid, account) do
|
|
|
|
GenServer.call(pid, {:leave, account})
|
|
|
|
end
|
|
|
|
|
2021-08-26 12:32:24 +00:00
|
|
|
@doc """
|
|
|
|
Kick a user from this room.
|
|
|
|
"""
|
|
|
|
@spec kick(pid(), Account.t(), Kick.t()) :: :ok | {:error, atom()}
|
|
|
|
def kick(pid, account, request) do
|
|
|
|
GenServer.call(pid, {:kick, account, request})
|
|
|
|
end
|
|
|
|
|
2021-08-26 20:09:45 +00:00
|
|
|
@doc """
|
|
|
|
Ban a user from this room.
|
|
|
|
"""
|
|
|
|
@spec ban(pid(), Account.t(), Ban.t()) :: :ok | {:error, atom()}
|
|
|
|
def ban(pid, account, request) do
|
|
|
|
GenServer.call(pid, {:ban, account, request})
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Unban a user from this room.
|
|
|
|
"""
|
|
|
|
@spec unban(pid(), Account.t(), String.t()) :: :ok | {:error, atom()}
|
|
|
|
def unban(pid, account, user_id) do
|
|
|
|
GenServer.call(pid, {:unban, account, user_id})
|
|
|
|
end
|
|
|
|
|
2021-07-28 15:23:48 +00:00
|
|
|
### Implementation
|
|
|
|
|
2021-07-10 21:16:00 +00:00
|
|
|
@impl true
|
2021-07-23 19:00:01 +00:00
|
|
|
def init(opts) do
|
2021-08-24 23:27:03 +00:00
|
|
|
room = Keyword.fetch!(opts, :room)
|
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-08-24 23:27:03 +00:00
|
|
|
{:ok, %{room: room, state_set: state_set}}
|
2021-07-28 15:23:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
2021-08-24 23:27:03 +00:00
|
|
|
def handle_call(
|
|
|
|
{:create_room, account, input},
|
|
|
|
_from,
|
|
|
|
%{room: %Room{id: room_id} = room} = state
|
|
|
|
) do
|
2021-08-18 11:54:18 +00:00
|
|
|
# TODO: power_level_content_override, initial_state, invite, invite_3pid
|
2021-08-18 17:04:09 +00:00
|
|
|
case Repo.transaction(create_room_insert_events(room, account, input)) do
|
2021-08-26 12:32:24 +00:00
|
|
|
{:ok, {state_set, room}} ->
|
|
|
|
{:reply, {:ok, room_id}, %{state | state_set: state_set, room: room}}
|
|
|
|
|
|
|
|
{:error, reason} ->
|
|
|
|
{:reply, {:error, reason}, state}
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
{:reply, {:error, :unknown}, state}
|
2021-07-28 15:23:48 +00:00
|
|
|
end
|
2021-07-23 19:00:01 +00:00
|
|
|
end
|
|
|
|
|
2021-08-22 13:19:48 +00:00
|
|
|
def handle_call({:server_in_room?, domain}, _from, %{state_set: state_set} = state) do
|
2021-08-18 11:54:18 +00:00
|
|
|
result =
|
|
|
|
Enum.any?(state_set, fn
|
|
|
|
{{"m.room.member", user_id}, %Event{content: %{"membership" => "join"}}} ->
|
|
|
|
MatrixServer.get_domain(user_id) == domain
|
2021-08-16 17:30:47 +00:00
|
|
|
|
2021-08-18 11:54:18 +00:00
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end)
|
2021-08-16 17:30:47 +00:00
|
|
|
|
|
|
|
{:reply, result, state}
|
|
|
|
end
|
|
|
|
|
2021-08-21 19:39:28 +00:00
|
|
|
def handle_call({:get_state_at_event, %Event{room_id: room_id} = event}, _from, state) do
|
|
|
|
room_events =
|
|
|
|
Event
|
|
|
|
|> where([e], e.room_id == ^room_id)
|
|
|
|
|> select([e], {e.event_id, e})
|
|
|
|
|> Repo.all()
|
|
|
|
|> Enum.into(%{})
|
|
|
|
|
|
|
|
state_set = StateResolution.resolve(event, false)
|
|
|
|
state_events = Map.values(state_set)
|
|
|
|
|
|
|
|
auth_chain =
|
|
|
|
state_set
|
|
|
|
|> Map.values()
|
|
|
|
|> StateResolution.full_auth_chain(room_events)
|
|
|
|
|> Enum.map(&room_events[&1])
|
|
|
|
|
|
|
|
{:reply, {state_events, auth_chain}, state}
|
|
|
|
end
|
|
|
|
|
2021-08-22 10:19:47 +00:00
|
|
|
def handle_call({:get_state_ids_at_event, %Event{room_id: room_id} = event}, _from, state) do
|
|
|
|
room_events =
|
|
|
|
Event
|
|
|
|
|> where([e], e.room_id == ^room_id)
|
|
|
|
|> select([e], {e.event_id, e})
|
|
|
|
|> Repo.all()
|
|
|
|
|> Enum.into(%{})
|
|
|
|
|
|
|
|
state_set = StateResolution.resolve(event, false)
|
|
|
|
state_events = Enum.map(state_set, fn {_, %Event{event_id: event_id}} -> event_id end)
|
|
|
|
|
|
|
|
auth_chain =
|
|
|
|
state_set
|
|
|
|
|> Map.values()
|
|
|
|
|> StateResolution.full_auth_chain(room_events)
|
|
|
|
|> MapSet.to_list()
|
|
|
|
|
|
|
|
{:reply, {state_events, auth_chain}, state}
|
|
|
|
end
|
|
|
|
|
2021-08-24 23:27:03 +00:00
|
|
|
def handle_call({:invite, account, user_id}, _from, %{room: room, state_set: state_set} = state) do
|
2021-08-26 09:08:54 +00:00
|
|
|
invite_event = Event.invite(room, account, user_id)
|
|
|
|
|
|
|
|
case insert_single_event(room, state_set, invite_event) do
|
2021-08-26 12:32:24 +00:00
|
|
|
{:ok, {state_set, room}} -> {:reply, :ok, %{state | state_set: state_set, room: room}}
|
2021-08-24 23:27:03 +00:00
|
|
|
{:error, reason} -> {:reply, {:error, reason}, state}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-25 14:44:39 +00:00
|
|
|
def handle_call(
|
|
|
|
{:join, account},
|
|
|
|
_from,
|
|
|
|
%{room: %Room{id: room_id} = room, state_set: state_set} = state
|
|
|
|
) do
|
2021-08-26 09:08:54 +00:00
|
|
|
join_event = Event.join(room, account)
|
|
|
|
|
|
|
|
case insert_single_event(room, state_set, join_event) do
|
2021-08-26 12:32:24 +00:00
|
|
|
{:ok, {state_set, room}} ->
|
|
|
|
{:reply, {:ok, room_id}, %{state | state_set: state_set, room: room}}
|
|
|
|
|
|
|
|
{:error, reason} ->
|
|
|
|
{:reply, {:error, reason}, state}
|
2021-08-25 10:25:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-26 09:01:19 +00:00
|
|
|
def handle_call({:leave, account}, _from, %{room: room, state_set: state_set} = state) do
|
|
|
|
leave_event = Event.leave(room, account)
|
|
|
|
|
2021-08-26 09:08:54 +00:00
|
|
|
case insert_single_event(room, state_set, leave_event) do
|
2021-08-26 12:32:24 +00:00
|
|
|
{:ok, {state_set, room}} -> {:reply, :ok, %{state | state_set: state_set, room: room}}
|
|
|
|
{:error, reason} -> {:reply, {:error, reason}, state}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_call(
|
|
|
|
{:kick, account, %Kick{user_id: user_id, reason: reason}},
|
|
|
|
_from,
|
|
|
|
%{room: room, state_set: state_set} = state
|
|
|
|
) do
|
|
|
|
kick_event = Event.kick(room, account, user_id, reason)
|
|
|
|
|
|
|
|
case insert_single_event(room, state_set, kick_event) do
|
|
|
|
{:ok, {state_set, room}} -> {:reply, :ok, %{state | state_set: state_set, room: room}}
|
2021-08-26 09:08:54 +00:00
|
|
|
{:error, reason} -> {:reply, {:error, reason}, state}
|
2021-08-25 10:25:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-26 20:09:45 +00:00
|
|
|
def handle_call(
|
|
|
|
{:ban, account, %Kick{user_id: user_id, reason: reason}},
|
|
|
|
_from,
|
|
|
|
%{room: room, state_set: state_set} = state
|
|
|
|
) do
|
|
|
|
ban_event = Event.ban(room, account, user_id, reason)
|
|
|
|
|
|
|
|
case insert_single_event(room, state_set, ban_event) do
|
|
|
|
{:ok, {state_set, room}} -> {:reply, :ok, %{state | state_set: state_set, room: room}}
|
|
|
|
{:error, reason} -> {:reply, {:error, reason}, state}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_call({:unban, account, user_id},_from,%{room: room, state_set: state_set} = state) do
|
|
|
|
unban_event = Event.unban(room, account, user_id)
|
|
|
|
|
|
|
|
case insert_single_event(room, state_set, unban_event) do
|
|
|
|
{:ok, {state_set, room}} -> {:reply, :ok, %{state | state_set: state_set, room: room}}
|
|
|
|
{:error, reason} -> {:reply, {:error, reason}, state}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec insert_single_event(Room.t(), t(), Event.t()) :: {:ok, {t(), Room.t()}} | {:error, atom()}
|
2021-08-26 09:08:54 +00:00
|
|
|
defp insert_single_event(room, state_set, event) do
|
|
|
|
Repo.transaction(fn ->
|
|
|
|
case finalize_and_insert_event(event, state_set, room) do
|
2021-08-24 23:27:03 +00:00
|
|
|
{:ok, state_set, room} ->
|
|
|
|
_ = update_room_state_set(room, state_set)
|
2021-08-26 12:32:24 +00:00
|
|
|
{state_set, room}
|
2021-08-24 23:27:03 +00:00
|
|
|
|
|
|
|
{:error, reason} ->
|
|
|
|
Repo.rollback(reason)
|
|
|
|
end
|
2021-08-26 09:08:54 +00:00
|
|
|
end)
|
2021-08-24 23:27:03 +00:00
|
|
|
end
|
|
|
|
|
2021-08-25 09:46:35 +00:00
|
|
|
# Get a function that inserts all events for room creation.
|
2021-08-24 23:27:03 +00:00
|
|
|
@spec create_room_insert_events(Room.t(), Account.t(), CreateRoom.t()) ::
|
2021-08-26 12:32:24 +00:00
|
|
|
(() -> {:ok, t(), Room.t()} | {:error, atom()})
|
2021-08-18 17:04:09 +00:00
|
|
|
defp create_room_insert_events(room, account, %CreateRoom{
|
2021-08-18 11:54:18 +00:00
|
|
|
room_version: room_version,
|
2021-08-18 17:04:09 +00:00
|
|
|
preset: preset,
|
2021-08-18 11:54:18 +00:00
|
|
|
name: name,
|
2021-08-18 17:04:09 +00:00
|
|
|
topic: topic
|
2021-08-18 11:54:18 +00:00
|
|
|
}) do
|
2021-08-24 23:27:03 +00:00
|
|
|
events =
|
|
|
|
([
|
|
|
|
Event.create_room(room, account, room_version),
|
|
|
|
Event.join(room, account),
|
|
|
|
Event.power_levels(room, account)
|
|
|
|
] ++
|
|
|
|
room_creation_preset(account, preset, room) ++
|
|
|
|
[
|
|
|
|
if(name, do: Event.name(room, account, name)),
|
|
|
|
if(topic, do: Event.topic(room, account, topic))
|
|
|
|
])
|
|
|
|
|> Enum.reject(&Kernel.is_nil/1)
|
|
|
|
|
2021-08-18 11:54:18 +00:00
|
|
|
fn ->
|
2021-08-24 23:27:03 +00:00
|
|
|
result =
|
|
|
|
Enum.reduce_while(events, {%{}, room}, fn event, {state_set, room} ->
|
|
|
|
case finalize_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} ->
|
|
|
|
Repo.rollback(reason)
|
|
|
|
|
|
|
|
{state_set, room} ->
|
|
|
|
_ = update_room_state_set(room, state_set)
|
2021-08-26 12:32:24 +00:00
|
|
|
{state_set, room}
|
2021-08-18 11:54:18 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-25 09:46:35 +00:00
|
|
|
# Update the given room in the database with the given state set.
|
2021-08-24 23:27:03 +00:00
|
|
|
@spec update_room_state_set(Room.t(), t()) :: Room.t()
|
|
|
|
defp update_room_state_set(room, state_set) do
|
2021-08-26 12:32:24 +00:00
|
|
|
# TODO: We might as well hold state in the Room struct,
|
|
|
|
# instead of the state_set state.
|
|
|
|
# Create custom type for this.
|
2021-08-24 23:27:03 +00:00
|
|
|
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))
|
2021-08-23 10:59:12 +00:00
|
|
|
end
|
|
|
|
|
2021-08-25 09:46:35 +00:00
|
|
|
# Get the events for room creation as dictated by the given preset.
|
2021-07-27 10:55:36 +00:00
|
|
|
# TODO: trusted_private_chat:
|
2021-08-25 09:46:35 +00:00
|
|
|
# All invitees are given the same power level as the room creator.
|
2021-08-24 23:27:03 +00:00
|
|
|
@spec room_creation_preset(Account.t(), String.t() | nil, Room.t()) :: [Event.t()]
|
|
|
|
defp room_creation_preset(account, nil, %Room{visibility: visibility} = room) do
|
2021-07-27 10:55:36 +00:00
|
|
|
preset =
|
|
|
|
case visibility do
|
|
|
|
:public -> "public_chat"
|
|
|
|
:private -> "private_chat"
|
|
|
|
end
|
|
|
|
|
2021-08-24 23:27:03 +00:00
|
|
|
room_creation_preset(account, preset, room)
|
2021-07-27 10:55:36 +00:00
|
|
|
end
|
|
|
|
|
2021-08-24 23:27:03 +00:00
|
|
|
defp room_creation_preset(account, preset, room) 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-08-18 17:04:09 +00:00
|
|
|
[
|
2021-08-24 23:27:03 +00:00
|
|
|
Event.join_rules(room, account, join_rule),
|
|
|
|
Event.history_visibility(room, account, his_vis),
|
|
|
|
Event.guest_access(room, account, guest_access)
|
2021-08-18 17:04:09 +00:00
|
|
|
]
|
2021-07-26 21:42:35 +00:00
|
|
|
end
|
|
|
|
|
2021-08-25 09:46:35 +00:00
|
|
|
# Finalize the event struct and insert it into the room's state using state resolution.
|
|
|
|
# The values that are automatically added are:
|
|
|
|
# - Auth events
|
|
|
|
# - Prev events
|
|
|
|
# - Content hash
|
|
|
|
# - Event ID
|
|
|
|
# - Signature
|
2021-08-24 23:27:03 +00:00
|
|
|
@spec finalize_and_insert_event(Event.t(), t(), Room.t()) ::
|
|
|
|
{:ok, t(), Room.t()} | {:error, atom()}
|
|
|
|
defp finalize_and_insert_event(
|
2021-07-29 14:59:40 +00:00
|
|
|
event,
|
2021-08-24 23:27:03 +00:00
|
|
|
state_set,
|
2021-07-26 21:42:35 +00:00
|
|
|
%Room{forward_extremities: forward_extremities} = room
|
|
|
|
) do
|
2021-08-24 23:27:03 +00:00
|
|
|
event =
|
|
|
|
event
|
|
|
|
|> Map.put(:auth_events, auth_events_for_event(event, state_set))
|
|
|
|
|> Map.put(:prev_events, forward_extremities)
|
|
|
|
|
|
|
|
case Event.post_process(event) do
|
2021-08-25 09:46:35 +00:00
|
|
|
{:ok, event} -> authenticate_and_insert_event(event, state_set, room)
|
2021-08-24 23:27:03 +00:00
|
|
|
_ -> {:error, :event_creation}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-25 09:46:35 +00:00
|
|
|
# Get the auth events for an events.
|
2021-08-24 23:27:03 +00:00
|
|
|
@spec auth_events_for_event(Event.t(), t()) :: [{String.t(), String.t()}]
|
|
|
|
defp auth_events_for_event(%Event{type: "m.room.create"}, _), do: []
|
|
|
|
|
|
|
|
defp auth_events_for_event(
|
|
|
|
%Event{sender: sender} = event,
|
|
|
|
state_set
|
|
|
|
) do
|
|
|
|
state_pairs =
|
|
|
|
[{"m.room.create", ""}, {"m.room.power_levels", ""}, {"m.room.member", to_string(sender)}] ++
|
|
|
|
auth_events_for_member_event(event)
|
|
|
|
|
|
|
|
state_set
|
|
|
|
|> Map.take(state_pairs)
|
|
|
|
|> Map.values()
|
|
|
|
|> Enum.map(& &1.event_id)
|
|
|
|
end
|
|
|
|
|
2021-08-25 09:46:35 +00:00
|
|
|
# Get the auth events specific to m.room.member events.
|
2021-08-24 23:27:03 +00:00
|
|
|
@spec auth_events_for_member_event(Event.t()) :: [{String.t(), String.t()}]
|
|
|
|
defp auth_events_for_member_event(
|
|
|
|
%Event{
|
|
|
|
type: "m.room.member",
|
|
|
|
state_key: state_key,
|
|
|
|
content: %{"membership" => membership}
|
|
|
|
} = event
|
|
|
|
) do
|
|
|
|
[
|
|
|
|
{"m.room.member", state_key},
|
|
|
|
if(membership in ["join", "invite"], do: {"m.room.join_rules", ""}),
|
|
|
|
third_party_invite_state_pair(event)
|
|
|
|
]
|
|
|
|
|> Enum.reject(&Kernel.is_nil/1)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp auth_events_for_member_event(_), do: []
|
|
|
|
|
2021-08-25 09:46:35 +00:00
|
|
|
# Get the third party invite state pair for an event, if it exists.
|
2021-08-24 23:27:03 +00:00
|
|
|
@spec third_party_invite_state_pair(Event.t()) :: {String.t(), String.t()} | nil
|
|
|
|
defp third_party_invite_state_pair(%Event{
|
|
|
|
content: %{
|
|
|
|
"membership" => "invite",
|
|
|
|
"third_party_invite" => %{"signed" => %{"token" => token}}
|
|
|
|
}
|
|
|
|
}) do
|
|
|
|
{"m.room.third_party_invite", token}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp third_party_invite_state_pair(_), do: nil
|
|
|
|
|
2021-08-25 09:46:35 +00:00
|
|
|
# Authenticate and insert a new event using state resolution.
|
|
|
|
# Implements the checks as described in the
|
|
|
|
# [Matrix docs](https://matrix.org/docs/spec/server_server/latest#checks-performed-on-receipt-of-a-pdu).
|
|
|
|
@spec authenticate_and_insert_event(Event.t(), t(), Room.t()) ::
|
2021-08-24 23:27:03 +00:00
|
|
|
{:ok, t(), Room.t()} | {:error, atom()}
|
2021-08-25 09:46:35 +00:00
|
|
|
defp authenticate_and_insert_event(event, current_state_set, room) do
|
|
|
|
# TODO: Correctly handle soft fails.
|
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-08-24 23:27:03 +00:00
|
|
|
with true <- Event.prevalidate(event),
|
2021-07-29 20:06:02 +00:00
|
|
|
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)
|
2021-08-25 14:44:39 +00:00
|
|
|
_ = update_joined_rooms(room, state_set)
|
2021-08-24 23:27:03 +00:00
|
|
|
|
|
|
|
{: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
|
2021-08-24 23:27:03 +00:00
|
|
|
|
2021-08-25 09:46:35 +00:00
|
|
|
# Update local accounts' room membership if applicable.
|
2021-08-25 14:44:39 +00:00
|
|
|
# TODO: Could be a background job.
|
|
|
|
@spec update_joined_rooms(Room.t(), t()) :: JoinedRoom.t() | nil
|
|
|
|
defp update_joined_rooms(%Room{id: room_id}, state_set) do
|
|
|
|
server_name = MatrixServer.server_name()
|
2021-08-24 23:27:03 +00:00
|
|
|
|
2021-08-25 14:44:39 +00:00
|
|
|
{joined, not_joined} =
|
|
|
|
state_set
|
|
|
|
|> Enum.filter(fn {{type, state_key}, _} ->
|
|
|
|
type == "m.room.member" and MatrixServer.get_domain(state_key) == server_name
|
|
|
|
end)
|
|
|
|
|> Enum.split_with(fn {_, %Event{content: %{"membership" => membership}}} ->
|
|
|
|
membership == "join"
|
|
|
|
end)
|
|
|
|
|
|
|
|
joined_assocs =
|
|
|
|
Enum.map(joined, fn {{_, state_key}, _} ->
|
|
|
|
%{localpart: MatrixServer.get_localpart(state_key), room_id: room_id}
|
|
|
|
end)
|
|
|
|
|
|
|
|
not_joined_localparts =
|
|
|
|
Enum.map(not_joined, fn {{_, state_key}, _} -> MatrixServer.get_localpart(state_key) end)
|
|
|
|
|
|
|
|
_ = Repo.insert_all(JoinedRoom, joined_assocs, on_conflict: :nothing)
|
|
|
|
|
|
|
|
Repo.delete_all(
|
|
|
|
from jr in JoinedRoom,
|
|
|
|
where: jr.room_id == ^room_id and jr.localpart in ^not_joined_localparts
|
|
|
|
)
|
|
|
|
end
|
2021-07-10 21:16:00 +00:00
|
|
|
end
|