Add creator join event when creating room

Fix event set format bug in state resolution
This commit is contained in:
Pim Kunis 2021-07-26 11:50:18 +02:00
parent d3c16dd7aa
commit 857aabd007
2 changed files with 30 additions and 13 deletions

View file

@ -29,12 +29,17 @@ defmodule MatrixServer.RoomServer do
@impl true @impl true
def init(opts) do def init(opts) do
%Room{id: room_id} = Keyword.fetch!(opts, :room) %Room{id: room_id} = room = Keyword.fetch!(opts, :room)
input = Keyword.fetch!(opts, :input) input = Keyword.fetch!(opts, :input)
account = Keyword.fetch!(opts, :account) account = Keyword.fetch!(opts, :account)
state_set = %{}
Repo.transaction(fn -> Repo.transaction(fn ->
with {:ok, state_set} <- insert_create_room_event(account, input, room_id) do with {:ok, create_room_event, state_set} <-
room_creation_create_room(account, input, room, state_set),
{:ok, _join_creator_event, state_set} <-
room_creation_join_creator(account, room, state_set, create_room_event) do
{:ok, %{room_id: room_id, state_set: state_set}} {:ok, %{room_id: room_id, state_set: state_set}}
else else
_ -> {:error, :something} _ -> {:error, :something}
@ -42,14 +47,26 @@ defmodule MatrixServer.RoomServer do
end) end)
end end
defp insert_create_room_event( defp room_creation_create_room(
%Account{localpart: localpart}, %Account{localpart: localpart},
%CreateRoom{room_version: room_version}, %CreateRoom{room_version: room_version},
room_id %Room{id: room_id},
_state_set
) do ) do
create_room_event = Event.create_room(room_id, MatrixServer.get_mxid(localpart), room_version) Event.create_room(room_id, MatrixServer.get_mxid(localpart), room_version)
|> verify_and_insert_event(%{})
end
verify_and_insert_event(create_room_event, %{}) defp room_creation_join_creator(
%Account{localpart: localpart},
%Room{id: room_id},
state_set,
%Event{event_id: create_room_event_id}
) do
Event.join(room_id, MatrixServer.get_mxid(localpart))
|> Map.put(:auth_events, [create_room_event_id])
|> Map.put(:prev_events, [create_room_event_id])
|> verify_and_insert_event(state_set)
end end
defp verify_and_insert_event(event, current_state_set) do defp verify_and_insert_event(event, current_state_set) do
@ -69,7 +86,7 @@ defmodule MatrixServer.RoomServer do
Room.update_forward_extremities(event) Room.update_forward_extremities(event)
{:ok, event} = Repo.insert(event) {:ok, event} = Repo.insert(event)
state_set = StateResolution.resolve_forward_extremities(event) state_set = StateResolution.resolve_forward_extremities(event)
{:ok, state_set} {:ok, event, state_set}
else else
{:error, :soft_failed} {:error, :soft_failed}
end end

View file

@ -24,7 +24,7 @@ defmodule MatrixServer.StateResolution do
state_sets = state_sets =
prev_event_ids prev_event_ids
|> Enum.map(&room_events[&1]) |> Enum.map(&room_events[&1])
|> Enum.map(&resolve(&1, room_events, apply_state)) |> Enum.map(&resolve(&1, room_events, true))
resolved_state = do_resolve(state_sets, room_events) resolved_state = do_resolve(state_sets, room_events)
@ -38,7 +38,7 @@ defmodule MatrixServer.StateResolution do
def do_resolve([], _), do: %{} def do_resolve([], _), do: %{}
def do_resolve(state_sets, room_events) do def do_resolve(state_sets, room_events) do
{unconflicted_state_map, conflicted_state_set} = calculate_conflict(state_sets) {unconflicted_state_map, conflicted_state_set} = calculate_conflict(state_sets, room_events)
if MapSet.size(conflicted_state_set) == 0 do if MapSet.size(conflicted_state_set) == 0 do
unconflicted_state_map unconflicted_state_map
@ -81,7 +81,7 @@ defmodule MatrixServer.StateResolution do
|> Map.merge(unconflicted_state_map) |> Map.merge(unconflicted_state_map)
end end
def calculate_conflict(state_sets) do def calculate_conflict(state_sets, room_events) do
{unconflicted, conflicted} = {unconflicted, conflicted} =
state_sets state_sets
|> Enum.flat_map(&Map.keys/1) |> Enum.flat_map(&Map.keys/1)
@ -101,10 +101,10 @@ defmodule MatrixServer.StateResolution do
end) end)
unconflicted_state_map = unconflicted_state_map =
Enum.into(unconflicted, %{}, fn {state_pair, events} -> Enum.into(unconflicted, %{}, fn {state_pair, event_ids} ->
event = MapSet.to_list(events) |> hd() event_id = MapSet.to_list(event_ids) |> hd()
{state_pair, event} {state_pair, room_events[event_id]}
end) end)
conflicted_state_set = conflicted_state_set =