Map from state pair to event struct in state sets

This commit is contained in:
Pim Kunis 2021-07-24 22:54:03 +02:00
parent bde8e012d0
commit 937204b73a
2 changed files with 42 additions and 64 deletions

View file

@ -1,8 +1,6 @@
defmodule MatrixServer.RoomServer do defmodule MatrixServer.RoomServer do
use GenServer use GenServer
import Ecto.Query
alias MatrixServer.{Repo, Room, Event, Account, StateResolution} alias MatrixServer.{Repo, Room, Event, Account, StateResolution}
alias MatrixServerWeb.API.CreateRoom alias MatrixServerWeb.API.CreateRoom
@ -46,13 +44,14 @@ defmodule MatrixServer.RoomServer do
room_id room_id
) do ) do
create_room_event = Event.create_room(room_id, MatrixServer.get_mxid(localpart), room_version) create_room_event = Event.create_room(room_id, MatrixServer.get_mxid(localpart), room_version)
verify_event(create_room_event) verify_event(create_room_event)
|> IO.inspect() |> IO.inspect()
{:ok, %{}} {:ok, %{}}
end end
defp verify_event(%Event{auth_events: auth_event_ids} = event) do defp verify_event(event) do
# Check the following things: # Check the following things:
# 1. TODO: Is a valid event, otherwise it is dropped. # 1. TODO: Is a valid event, otherwise it is dropped.
# 2. TODO: Passes signature checks, otherwise it is dropped. # 2. TODO: Passes signature checks, otherwise it is dropped.
@ -61,15 +60,8 @@ defmodule MatrixServer.RoomServer do
# 5. Passes authorization rules based on the state at the event, 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". # 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 if StateResolution.is_authorized_by_auth_events(event) do
auth_events =
Event
|> where([e], e.event_id in ^auth_event_ids)
|> select([e], {e.event_id, e})
|> Repo.all()
|> Enum.into(%{})
# TODO: make the state set a mapping to Event struct.
state_set = StateResolution.resolve(event, false) state_set = StateResolution.resolve(event, false)
StateResolution.is_authorized(event, state_set, auth_events) StateResolution.is_authorized(event, state_set)
else else
false false
end end

View file

@ -24,7 +24,7 @@ defmodule MatrixServer.StateResolution do
end end
def resolve( def resolve(
%Event{type: type, state_key: state_key, event_id: event_id, prev_events: prev_event_ids}, %Event{type: type, state_key: state_key, prev_events: prev_event_ids} = event,
room_events, room_events,
apply_state apply_state
) do ) do
@ -36,7 +36,7 @@ defmodule MatrixServer.StateResolution do
resolved_state = do_resolve(state_sets, room_events) resolved_state = do_resolve(state_sets, room_events)
# TODO: check if state event # TODO: check if state event
if apply_state do if apply_state do
Map.put(resolved_state, {type, state_key}, event_id) Map.put(resolved_state, {type, state_key}, event)
else else
resolved_state resolved_state
end end
@ -66,7 +66,7 @@ defmodule MatrixServer.StateResolution do
conflicted_control_events_with_auth_ids = conflicted_control_events_with_auth_ids =
conflicted_control_event_ids conflicted_control_event_ids
|> MapSet.to_list() |> Enum.map(&room_events[&1])
|> full_auth_chain(room_events) |> full_auth_chain(room_events)
|> MapSet.intersection(full_conflicted_set) |> MapSet.intersection(full_conflicted_set)
|> MapSet.union(conflicted_control_event_ids) |> MapSet.union(conflicted_control_event_ids)
@ -96,13 +96,16 @@ defmodule MatrixServer.StateResolution do
|> MapSet.new() |> MapSet.new()
|> Enum.into(%{}, fn state_pair -> |> Enum.into(%{}, fn state_pair ->
events = events =
Enum.map(state_sets, &Map.get(&1, state_pair)) Enum.map(state_sets, fn
state_set when is_map_key(state_set, state_pair) -> state_set[state_pair].event_id
_ -> nil
end)
|> MapSet.new() |> MapSet.new()
{state_pair, events} {state_pair, events}
end) end)
|> Enum.split_with(fn {_, events} -> |> Enum.split_with(fn {_, event_ids} ->
MapSet.size(events) == 1 MapSet.size(event_ids) == 1
end) end)
unconflicted_state_map = unconflicted_state_map =
@ -136,17 +139,18 @@ defmodule MatrixServer.StateResolution do
MapSet.difference(auth_chain_union, auth_chain_intersection) MapSet.difference(auth_chain_union, auth_chain_intersection)
end end
def full_auth_chain(event_ids, room_events) do def full_auth_chain(events, room_events) do
event_ids events
|> Enum.map(&auth_chain(&1, room_events)) |> Enum.map(&auth_chain(&1, room_events))
|> Enum.reduce(MapSet.new(), &MapSet.union/2) |> Enum.reduce(MapSet.new(), &MapSet.union/2)
end end
def auth_chain(event_id, room_events) do def auth_chain(%Event{auth_events: auth_events}, room_events) do
# TODO: handle when auth event is not found. # TODO: handle when auth event is not found.
room_events[event_id].auth_events auth_events
|> Enum.reduce(MapSet.new(), fn auth_event_id, acc -> |> Enum.map(&room_events[&1])
auth_event_id |> Enum.reduce(MapSet.new(), fn %Event{event_id: auth_event_id} = auth_event, acc ->
auth_event
|> auth_chain(room_events) |> auth_chain(room_events)
|> MapSet.union(acc) |> MapSet.union(acc)
|> MapSet.put(auth_event_id) |> MapSet.put(auth_event_id)
@ -199,9 +203,9 @@ defmodule MatrixServer.StateResolution do
end end
end end
def mainline_order(event_id, room_events) do def mainline_order(event, room_events) do
mainline_map = mainline_map =
room_events[event_id] event
|> mainline(room_events) |> mainline(room_events)
|> Enum.with_index() |> Enum.with_index()
|> Enum.into(%{}) |> Enum.into(%{})
@ -267,67 +271,56 @@ defmodule MatrixServer.StateResolution do
end end
def update_state_set( def update_state_set(
%Event{type: event_type, state_key: state_key, event_id: event_id}, %Event{type: event_type, state_key: state_key} = event,
state_set state_set
) do ) do
Map.put(state_set, {event_type, state_key}, event_id) Map.put(state_set, {event_type, state_key}, event)
end end
def is_authorized2(%Event{auth_events: auth_event_ids} = event, state_set, room_events) do def is_authorized2(%Event{auth_events: auth_event_ids} = event, state_set, room_events) do
state_set = state_set =
auth_event_ids auth_event_ids
|> Enum.map(&room_events[&1]) |> Enum.map(&room_events[&1])
|> Enum.reduce(state_set, fn %Event{ |> Enum.reduce(state_set, &update_state_set/2)
type: event_type,
state_key: state_key,
event_id: event_id
},
acc ->
Map.put_new(acc, {event_type, state_key}, event_id)
end)
is_authorized(event, state_set, room_events) is_authorized(event, state_set)
end end
# TODO: join and power levels events # TODO: join and power levels events
def is_authorized(%Event{type: "m.room.create", prev_events: prev_events}, _, _), def is_authorized(%Event{type: "m.room.create", prev_events: prev_events}, _),
do: prev_events == [] do: prev_events == []
def is_authorized( def is_authorized(
%Event{type: "m.room.member", content: %{"membership" => "join"}, state_key: user}, %Event{type: "m.room.member", content: %{"membership" => "join"}, state_key: user},
state_set, state_set
room_events
) do ) do
allowed_to_join(user, state_set, room_events) allowed_to_join(user, state_set)
end end
def is_authorized(%Event{sender: sender} = event, state_set, room_events) do def is_authorized(%Event{sender: sender} = event, state_set) do
in_room(sender, state_set, room_events) and in_room(sender, state_set) and
has_power_level( has_power_level(
sender, sender,
get_power_levels(state_set, room_events), get_power_levels(state_set),
get_event_power_level(event) get_event_power_level(event)
) )
end end
def in_room(user, state_set, room_events) when is_map_key(state_set, {"m.room.member", user}) do def in_room(user, state_set) when is_map_key(state_set, {"m.room.member", user}) do
event_id = state_set[{"m.room.member", user}] case state_set[{"m.room.member", user}].content["membership"] do
case room_events[event_id].content["membership"] do
"join" -> true "join" -> true
_ -> false _ -> false
end end
end end
def in_room(_, _, _), do: false def in_room(_, _), do: false
def get_power_levels(state_set, room_events) def get_power_levels(state_set)
when is_map_key(state_set, {"m.room.power_levels", ""}) do when is_map_key(state_set, {"m.room.power_levels", ""}) do
event_id = state_set[{"m.room.power_levels", ""}] state_set[{"m.room.power_levels", ""}].content
room_events[event_id].content
end end
def get_power_levels(_, _), do: nil def get_power_levels(_), do: nil
def has_power_level(user, %{"users" => users}, level) do def has_power_level(user, %{"users" => users}, level) do
Map.get(users, user, 0) >= level Map.get(users, user, 0) >= level
@ -341,26 +334,19 @@ defmodule MatrixServer.StateResolution do
defp get_event_power_level(_), do: 50 defp get_event_power_level(_), do: 50
# No join rules specified, allow joining for room creator only. # No join rules specified, allow joining for room creator only.
def allowed_to_join(user, state_set, room_events) def allowed_to_join(user, state_set)
when not is_map_key(state_set, {"m.room.join_rules", ""}) do when not is_map_key(state_set, {"m.room.join_rules", ""}) do
event_id = state_set[{"m.room.create", ""}] state_set[{"m.room.create", ""}].sender == user
room_events[event_id].sender == user
end end
def is_authorized_by_auth_events(%Event{auth_events: auth_event_ids} = event) do def is_authorized_by_auth_events(%Event{auth_events: auth_event_ids} = event) do
# We assume the auth events are validated beforehand. # We assume the auth events are validated beforehand.
auth_events = state_set =
Event Event
|> where([e], e.event_id in ^auth_event_ids) |> where([e], e.event_id in ^auth_event_ids)
|> select([e], {e.event_id, e})
|> Repo.all() |> Repo.all()
|> Enum.into(%{}) |> Enum.reduce(%{}, &update_state_set/2)
# TODO: make the state set a mapping to Event struct. is_authorized(event, state_set)
state_set = Enum.reduce(auth_events, %{}, fn {event_id, %Event{type: type, state_key: state_key}}, acc ->
Map.put(acc, {type, state_key}, event_id)
end)
is_authorized(event, state_set, auth_events)
end end
end end