2021-07-10 21:16:00 +00:00
|
|
|
defmodule MatrixServer.StateResolution do
|
2021-07-23 19:00:01 +00:00
|
|
|
import Ecto.Query
|
2021-07-10 21:16:00 +00:00
|
|
|
|
2021-07-25 15:39:22 +00:00
|
|
|
alias MatrixServer.{Repo, Event, Room}
|
2021-07-28 15:23:48 +00:00
|
|
|
alias MatrixServer.StateResolution.Authorization
|
2021-07-25 15:39:22 +00:00
|
|
|
|
2021-08-19 21:42:24 +00:00
|
|
|
@type state_set :: map()
|
|
|
|
|
2021-07-25 15:39:22 +00:00
|
|
|
def resolve(event), do: resolve(event, true)
|
2021-07-10 21:16:00 +00:00
|
|
|
|
2021-07-24 20:08:01 +00:00
|
|
|
def resolve(%Event{room_id: room_id} = event, apply_state) do
|
2021-07-23 20:50:09 +00:00
|
|
|
room_events =
|
|
|
|
Event
|
|
|
|
|> where([e], e.room_id == ^room_id)
|
|
|
|
|> select([e], {e.event_id, e})
|
2021-07-23 19:00:01 +00:00
|
|
|
|> Repo.all()
|
2021-07-23 20:50:09 +00:00
|
|
|
|> Enum.into(%{})
|
|
|
|
|
2021-07-23 22:13:12 +00:00
|
|
|
resolve(event, room_events, apply_state)
|
2021-07-23 20:50:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def resolve(
|
2021-07-24 20:54:03 +00:00
|
|
|
%Event{type: type, state_key: state_key, prev_events: prev_event_ids} = event,
|
2021-07-23 22:13:12 +00:00
|
|
|
room_events,
|
|
|
|
apply_state
|
2021-07-23 20:50:09 +00:00
|
|
|
) do
|
|
|
|
state_sets =
|
|
|
|
prev_event_ids
|
|
|
|
|> Enum.map(&room_events[&1])
|
2021-07-26 09:50:18 +00:00
|
|
|
|> Enum.map(&resolve(&1, room_events, true))
|
2021-07-23 20:50:09 +00:00
|
|
|
|
2021-07-23 22:13:12 +00:00
|
|
|
resolved_state = do_resolve(state_sets, room_events)
|
2021-07-25 12:57:52 +00:00
|
|
|
|
|
|
|
if apply_state and Event.is_state_event(event) do
|
2021-07-24 20:54:03 +00:00
|
|
|
Map.put(resolved_state, {type, state_key}, event)
|
2021-07-23 22:13:12 +00:00
|
|
|
else
|
|
|
|
resolved_state
|
|
|
|
end
|
2021-07-10 21:16:00 +00:00
|
|
|
end
|
|
|
|
|
2021-07-27 15:19:08 +00:00
|
|
|
def resolve_forward_extremities(%Event{room_id: room_id}) do
|
|
|
|
room_events =
|
|
|
|
Event
|
|
|
|
|> where([e], e.room_id == ^room_id)
|
|
|
|
|> select([e], {e.event_id, e})
|
|
|
|
|> Repo.all()
|
|
|
|
|> Enum.into(%{})
|
|
|
|
|
|
|
|
Event
|
|
|
|
|> where([e], e.room_id == ^room_id)
|
|
|
|
|> join(:inner, [e], r in Room, on: e.room_id == r.id)
|
|
|
|
|> where([e, r], e.event_id == fragment("ANY(?)", r.forward_extremities))
|
|
|
|
|> Repo.all()
|
|
|
|
|> Enum.map(&resolve/1)
|
|
|
|
|> do_resolve(room_events)
|
|
|
|
end
|
|
|
|
|
2021-08-21 19:39:28 +00:00
|
|
|
def full_auth_chain(events, room_events) do
|
|
|
|
events
|
|
|
|
|> Enum.map(&auth_chain(&1, room_events))
|
|
|
|
|> Enum.reduce(MapSet.new(), &MapSet.union/2)
|
|
|
|
end
|
|
|
|
|
2021-07-27 15:19:08 +00:00
|
|
|
defp do_resolve([], _), do: %{}
|
|
|
|
|
|
|
|
defp do_resolve(state_sets, room_events) do
|
2021-07-26 09:50:18 +00:00
|
|
|
{unconflicted_state_map, conflicted_state_set} = calculate_conflict(state_sets, room_events)
|
2021-07-23 20:50:09 +00:00
|
|
|
|
2021-07-23 19:00:01 +00:00
|
|
|
if MapSet.size(conflicted_state_set) == 0 do
|
|
|
|
unconflicted_state_map
|
|
|
|
else
|
2021-07-23 22:13:12 +00:00
|
|
|
do_resolve(state_sets, room_events, unconflicted_state_map, conflicted_state_set)
|
|
|
|
end
|
|
|
|
end
|
2021-07-10 21:16:00 +00:00
|
|
|
|
2021-07-27 15:19:08 +00:00
|
|
|
defp do_resolve(state_sets, room_events, unconflicted_state_map, conflicted_state_set) do
|
2021-07-23 22:13:12 +00:00
|
|
|
full_conflicted_set =
|
|
|
|
MapSet.union(conflicted_state_set, auth_difference(state_sets, room_events))
|
2021-07-10 21:16:00 +00:00
|
|
|
|
2021-07-23 22:13:12 +00:00
|
|
|
conflicted_control_event_ids =
|
|
|
|
full_conflicted_set
|
2021-07-25 12:57:52 +00:00
|
|
|
|> Enum.filter(&Event.is_control_event(room_events[&1]))
|
2021-07-23 22:13:12 +00:00
|
|
|
|> MapSet.new()
|
2021-07-10 21:16:00 +00:00
|
|
|
|
2021-07-23 22:13:12 +00:00
|
|
|
conflicted_control_events_with_auth_ids =
|
|
|
|
conflicted_control_event_ids
|
2021-07-24 20:54:03 +00:00
|
|
|
|> Enum.map(&room_events[&1])
|
2021-07-23 22:13:12 +00:00
|
|
|
|> full_auth_chain(room_events)
|
|
|
|
|> MapSet.intersection(full_conflicted_set)
|
|
|
|
|> MapSet.union(conflicted_control_event_ids)
|
2021-07-10 21:16:00 +00:00
|
|
|
|
2021-07-23 22:13:12 +00:00
|
|
|
sorted_control_events =
|
|
|
|
conflicted_control_events_with_auth_ids
|
|
|
|
|> Enum.map(&room_events[&1])
|
|
|
|
|> Enum.sort(rev_top_pow_order(room_events))
|
2021-07-10 21:16:00 +00:00
|
|
|
|
2021-07-23 22:13:12 +00:00
|
|
|
partial_resolved_state =
|
|
|
|
iterative_auth_checks(sorted_control_events, unconflicted_state_map, room_events)
|
2021-07-10 21:16:00 +00:00
|
|
|
|
2021-07-23 22:13:12 +00:00
|
|
|
resolved_power_levels = partial_resolved_state[{"m.room.power_levels", ""}]
|
2021-07-10 21:16:00 +00:00
|
|
|
|
2021-07-23 22:13:12 +00:00
|
|
|
conflicted_control_events_with_auth_ids
|
|
|
|
|> MapSet.difference(full_conflicted_set)
|
|
|
|
|> Enum.sort(mainline_order(resolved_power_levels, room_events))
|
2021-07-24 20:08:01 +00:00
|
|
|
|> Enum.map(&room_events[&1])
|
2021-07-23 22:13:12 +00:00
|
|
|
|> iterative_auth_checks(partial_resolved_state, room_events)
|
|
|
|
|> Map.merge(unconflicted_state_map)
|
2021-07-10 21:16:00 +00:00
|
|
|
end
|
|
|
|
|
2021-07-27 15:19:08 +00:00
|
|
|
defp calculate_conflict(state_sets, room_events) do
|
2021-07-23 19:00:01 +00:00
|
|
|
{unconflicted, conflicted} =
|
2021-07-10 21:16:00 +00:00
|
|
|
state_sets
|
2021-07-23 19:00:01 +00:00
|
|
|
|> Enum.flat_map(&Map.keys/1)
|
2021-07-10 21:16:00 +00:00
|
|
|
|> MapSet.new()
|
2021-07-23 19:00:01 +00:00
|
|
|
|> Enum.into(%{}, fn state_pair ->
|
2021-07-10 21:16:00 +00:00
|
|
|
events =
|
2021-07-24 20:54:03 +00:00
|
|
|
Enum.map(state_sets, fn
|
|
|
|
state_set when is_map_key(state_set, state_pair) -> state_set[state_pair].event_id
|
|
|
|
_ -> nil
|
|
|
|
end)
|
2021-07-10 21:16:00 +00:00
|
|
|
|> MapSet.new()
|
|
|
|
|
2021-07-23 19:00:01 +00:00
|
|
|
{state_pair, events}
|
2021-07-10 21:16:00 +00:00
|
|
|
end)
|
2021-07-24 20:54:03 +00:00
|
|
|
|> Enum.split_with(fn {_, event_ids} ->
|
|
|
|
MapSet.size(event_ids) == 1
|
2021-07-10 21:16:00 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
unconflicted_state_map =
|
2021-07-26 09:50:18 +00:00
|
|
|
Enum.into(unconflicted, %{}, fn {state_pair, event_ids} ->
|
|
|
|
event_id = MapSet.to_list(event_ids) |> hd()
|
2021-07-10 21:16:00 +00:00
|
|
|
|
2021-07-26 09:50:18 +00:00
|
|
|
{state_pair, room_events[event_id]}
|
2021-07-10 21:16:00 +00:00
|
|
|
end)
|
|
|
|
|
2021-07-23 19:00:01 +00:00
|
|
|
conflicted_state_set =
|
|
|
|
Enum.reduce(conflicted, MapSet.new(), fn {_, events}, acc ->
|
|
|
|
MapSet.union(acc, events)
|
2021-07-10 21:16:00 +00:00
|
|
|
end)
|
2021-07-23 19:00:01 +00:00
|
|
|
|> MapSet.delete(nil)
|
2021-07-10 21:16:00 +00:00
|
|
|
|
2021-07-23 19:00:01 +00:00
|
|
|
{unconflicted_state_map, conflicted_state_set}
|
2021-07-10 21:16:00 +00:00
|
|
|
end
|
|
|
|
|
2021-07-27 15:19:08 +00:00
|
|
|
defp auth_difference(state_sets, room_events) do
|
2021-07-10 21:16:00 +00:00
|
|
|
full_auth_chains =
|
|
|
|
Enum.map(state_sets, fn state_set ->
|
|
|
|
state_set
|
|
|
|
|> Map.values()
|
2021-07-23 20:50:09 +00:00
|
|
|
|> full_auth_chain(room_events)
|
2021-07-10 21:16:00 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
auth_chain_union = Enum.reduce(full_auth_chains, MapSet.new(), &MapSet.union/2)
|
|
|
|
auth_chain_intersection = Enum.reduce(full_auth_chains, MapSet.new(), &MapSet.intersection/2)
|
|
|
|
|
|
|
|
MapSet.difference(auth_chain_union, auth_chain_intersection)
|
|
|
|
end
|
|
|
|
|
2021-07-27 15:19:08 +00:00
|
|
|
defp auth_chain(%Event{auth_events: auth_events}, room_events) do
|
2021-07-24 20:54:03 +00:00
|
|
|
auth_events
|
|
|
|
|> Enum.map(&room_events[&1])
|
|
|
|
|> Enum.reduce(MapSet.new(), fn %Event{event_id: auth_event_id} = auth_event, acc ->
|
|
|
|
auth_event
|
2021-07-23 20:50:09 +00:00
|
|
|
|> auth_chain(room_events)
|
2021-07-23 19:00:01 +00:00
|
|
|
|> MapSet.union(acc)
|
|
|
|
|> MapSet.put(auth_event_id)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2021-07-27 15:19:08 +00:00
|
|
|
defp rev_top_pow_order(room_events) do
|
2021-07-23 20:50:09 +00:00
|
|
|
fn %Event{origin_server_ts: timestamp1, event_id: event_id1} = event1,
|
|
|
|
%Event{origin_server_ts: timestamp2, event_id: event_id2} = event2 ->
|
|
|
|
power1 = get_power_level(event1, room_events)
|
|
|
|
power2 = get_power_level(event2, room_events)
|
2021-07-10 21:16:00 +00:00
|
|
|
|
2021-07-23 20:50:09 +00:00
|
|
|
if power1 == power2 do
|
|
|
|
if timestamp1 == timestamp2 do
|
|
|
|
event_id1 <= event_id2
|
|
|
|
else
|
|
|
|
timestamp1 < timestamp2
|
|
|
|
end
|
2021-07-10 21:16:00 +00:00
|
|
|
else
|
2021-07-23 20:50:09 +00:00
|
|
|
power1 < power2
|
2021-07-10 21:16:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-27 15:19:08 +00:00
|
|
|
defp get_power_level(%Event{sender: sender, auth_events: auth_event_ids}, room_events) do
|
2021-07-23 20:50:09 +00:00
|
|
|
pl_event_id =
|
|
|
|
Enum.find(auth_event_ids, fn id ->
|
|
|
|
room_events[id].type == "m.room.power_levels"
|
|
|
|
end)
|
2021-07-23 19:00:01 +00:00
|
|
|
|
2021-07-27 15:19:08 +00:00
|
|
|
# TODO: refactor
|
2021-07-23 20:50:09 +00:00
|
|
|
case room_events[pl_event_id] do
|
2021-08-19 21:42:24 +00:00
|
|
|
%Event{content: %{"users" => pl_users}} -> Map.get(pl_users, to_string(sender), 0)
|
2021-07-23 19:00:01 +00:00
|
|
|
nil -> 0
|
2021-07-10 21:16:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-27 15:19:08 +00:00
|
|
|
defp mainline_order(event, room_events) do
|
2021-07-10 21:16:00 +00:00
|
|
|
mainline_map =
|
2021-07-24 20:54:03 +00:00
|
|
|
event
|
2021-07-23 20:50:09 +00:00
|
|
|
|> mainline(room_events)
|
2021-07-10 21:16:00 +00:00
|
|
|
|> Enum.with_index()
|
|
|
|
|> Enum.into(%{})
|
|
|
|
|
2021-07-24 20:08:01 +00:00
|
|
|
fn event_id1, event_id2 ->
|
|
|
|
%Event{origin_server_ts: timestamp1} = event1 = room_events[event_id1]
|
|
|
|
%Event{origin_server_ts: timestamp2} = event2 = room_events[event_id2]
|
2021-07-23 20:50:09 +00:00
|
|
|
mainline_depth1 = get_mainline_depth(mainline_map, event1, room_events)
|
|
|
|
mainline_depth2 = get_mainline_depth(mainline_map, event2, room_events)
|
2021-07-10 21:16:00 +00:00
|
|
|
|
|
|
|
if mainline_depth1 == mainline_depth2 do
|
|
|
|
if timestamp1 == timestamp2 do
|
|
|
|
event_id1 <= event_id2
|
|
|
|
else
|
|
|
|
timestamp1 < timestamp2
|
|
|
|
end
|
|
|
|
else
|
|
|
|
mainline_depth1 < mainline_depth2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-23 20:50:09 +00:00
|
|
|
defp get_mainline_depth(mainline_map, event, room_events) do
|
|
|
|
mainline = mainline(event, room_events)
|
2021-07-10 21:16:00 +00:00
|
|
|
|
|
|
|
result =
|
|
|
|
Enum.find_value(mainline, fn mainline_event ->
|
|
|
|
if Map.has_key?(mainline_map, mainline_event) do
|
|
|
|
{:ok, mainline_map[mainline_event]}
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
case result do
|
|
|
|
{:ok, index} -> -index
|
|
|
|
nil -> nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-27 15:19:08 +00:00
|
|
|
defp mainline(event, room_events) do
|
2021-07-23 19:00:01 +00:00
|
|
|
event
|
2021-07-23 20:50:09 +00:00
|
|
|
|> mainline([], room_events)
|
2021-07-23 19:00:01 +00:00
|
|
|
|> Enum.reverse()
|
|
|
|
end
|
2021-07-10 21:16:00 +00:00
|
|
|
|
2021-07-27 15:19:08 +00:00
|
|
|
defp mainline(%Event{auth_events: auth_event_ids} = event, acc, room_events) do
|
2021-07-23 20:50:09 +00:00
|
|
|
pl_event_id =
|
|
|
|
Enum.find(auth_event_ids, fn id ->
|
|
|
|
room_events[id].type == "m.room.power_levels"
|
|
|
|
end)
|
2021-07-10 21:16:00 +00:00
|
|
|
|
2021-07-23 20:50:09 +00:00
|
|
|
case room_events[pl_event_id] do
|
|
|
|
%Event{} = pl_event -> mainline(pl_event, [event | acc], room_events)
|
2021-07-23 19:00:01 +00:00
|
|
|
nil -> [event | acc]
|
|
|
|
end
|
|
|
|
end
|
2021-07-10 21:16:00 +00:00
|
|
|
|
2021-07-27 15:19:08 +00:00
|
|
|
defp iterative_auth_checks(events, state_set, room_events) do
|
2021-07-23 19:00:01 +00:00
|
|
|
Enum.reduce(events, state_set, fn event, acc ->
|
2021-07-28 15:23:48 +00:00
|
|
|
if authorized?(event, acc, room_events), do: update_state_set(event, acc), else: acc
|
2021-07-23 19:00:01 +00:00
|
|
|
end)
|
|
|
|
end
|
2021-07-10 21:16:00 +00:00
|
|
|
|
2021-07-28 15:23:48 +00:00
|
|
|
def update_state_set(
|
2021-07-29 14:59:40 +00:00
|
|
|
%Event{type: event_type, state_key: state_key} = event,
|
|
|
|
state_set
|
|
|
|
) do
|
2021-07-24 20:54:03 +00:00
|
|
|
Map.put(state_set, {event_type, state_key}, event)
|
2021-07-23 19:00:01 +00:00
|
|
|
end
|
2021-07-10 21:16:00 +00:00
|
|
|
|
2021-07-28 15:23:48 +00:00
|
|
|
defp authorized?(%Event{auth_events: auth_event_ids} = event, state_set, room_events) do
|
2021-07-23 19:00:01 +00:00
|
|
|
state_set =
|
2021-07-23 20:50:09 +00:00
|
|
|
auth_event_ids
|
|
|
|
|> Enum.map(&room_events[&1])
|
2021-07-24 20:54:03 +00:00
|
|
|
|> Enum.reduce(state_set, &update_state_set/2)
|
2021-07-10 21:16:00 +00:00
|
|
|
|
2021-07-28 15:23:48 +00:00
|
|
|
Authorization.authorized?(event, state_set)
|
2021-07-10 21:16:00 +00:00
|
|
|
end
|
|
|
|
end
|