Implement federation room state snapshot endpoint
This commit is contained in:
parent
e510c3bb6a
commit
ba3e290bf1
8 changed files with 86 additions and 18 deletions
|
@ -1,6 +1,6 @@
|
|||
# https://github.com/michalmuskala/jason/issues/69
|
||||
defmodule MatrixServer.EncodableMap do
|
||||
alias MatrixServer.{EncodableMap, Event}
|
||||
alias MatrixServer.EncodableMap
|
||||
alias MatrixServer.Types.{UserId, RoomId, EventId, GroupId, AliasId}
|
||||
|
||||
defstruct pairs: []
|
||||
|
|
|
@ -60,6 +60,10 @@ defmodule MatrixServer.RoomServer do
|
|||
GenServer.call(pid, {:server_in_room, domain})
|
||||
end
|
||||
|
||||
def get_state_at_event(pid, event) do
|
||||
GenServer.call(pid, {:get_state_at_event, event})
|
||||
end
|
||||
|
||||
### Implementation
|
||||
|
||||
@impl true
|
||||
|
@ -104,6 +108,26 @@ defmodule MatrixServer.RoomServer do
|
|||
{:reply, result, state}
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
defp create_room_insert_events(room, account, %CreateRoom{
|
||||
room_version: room_version,
|
||||
preset: preset,
|
||||
|
|
|
@ -55,6 +55,12 @@ defmodule MatrixServer.StateResolution do
|
|||
|> do_resolve(room_events)
|
||||
end
|
||||
|
||||
def full_auth_chain(events, room_events) do
|
||||
events
|
||||
|> Enum.map(&auth_chain(&1, room_events))
|
||||
|> Enum.reduce(MapSet.new(), &MapSet.union/2)
|
||||
end
|
||||
|
||||
defp do_resolve([], _), do: %{}
|
||||
|
||||
defp do_resolve(state_sets, room_events) do
|
||||
|
@ -150,12 +156,6 @@ defmodule MatrixServer.StateResolution do
|
|||
MapSet.difference(auth_chain_union, auth_chain_intersection)
|
||||
end
|
||||
|
||||
defp full_auth_chain(events, room_events) do
|
||||
events
|
||||
|> Enum.map(&auth_chain(&1, room_events))
|
||||
|> Enum.reduce(MapSet.new(), &MapSet.union/2)
|
||||
end
|
||||
|
||||
defp auth_chain(%Event{auth_events: auth_events}, room_events) do
|
||||
auth_events
|
||||
|> Enum.map(&room_events[&1])
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue