Implement federation room state snapshot endpoint
This commit is contained in:
parent
e510c3bb6a
commit
ba3e290bf1
8 changed files with 86 additions and 18 deletions
|
@ -13,6 +13,7 @@ defmodule MatrixServerWeb.Error do
|
|||
{400, "M_INVALID_ROOM_STATE", "The request would leave the room in an invalid state."},
|
||||
unauthorized: {400, "M_UNAUTHORIZED", "The request was unauthorized."},
|
||||
invalid_param: {400, "M_INVALID_PARAM", "A request parameter was invalid."},
|
||||
missing_param: {400, "M_MISSING_PARAM", "A request parameter is missing."},
|
||||
unknown_token: {401, "M_UNKNOWN_TOKEN", "Invalid access token."},
|
||||
missing_token: {401, "M_MISSING_TOKEN", "Access token required."},
|
||||
not_found: {404, "M_NOT_FOUND", "The requested resource was not found."},
|
||||
|
|
|
@ -25,11 +25,7 @@ defmodule MatrixServerWeb.Federation.EventController do
|
|||
|> put_status(200)
|
||||
|> json(data)
|
||||
else
|
||||
put_error(
|
||||
conn,
|
||||
:unauthorized,
|
||||
"Origin server is not allowed to see requested event."
|
||||
)
|
||||
put_error(conn, :unauthorized, "Origin server is not participating in room.")
|
||||
end
|
||||
|
||||
_ ->
|
||||
|
@ -41,5 +37,44 @@ defmodule MatrixServerWeb.Federation.EventController do
|
|||
end
|
||||
end
|
||||
|
||||
def event(conn, _), do: put_error(conn, :bad_json)
|
||||
def event(conn, _), do: put_error(conn, :missing_param)
|
||||
|
||||
def state(%Plug.Conn{assigns: %{origin: origin}} = conn, %{
|
||||
"event_id" => event_id,
|
||||
"room_id" => room_id
|
||||
}) do
|
||||
query =
|
||||
Event
|
||||
|> where([e], e.event_id == ^event_id and e.room_id == ^room_id)
|
||||
|> preload(:room)
|
||||
|
||||
case Repo.one(query) do
|
||||
%Event{room: room} = event ->
|
||||
case RoomServer.get_room_server(room) do
|
||||
{:ok, pid} ->
|
||||
if RoomServer.server_in_room(pid, origin) do
|
||||
{state_events, auth_chain} = RoomServer.get_state_at_event(pid, event)
|
||||
|
||||
data = %{
|
||||
auth_chain: auth_chain,
|
||||
pdus: state_events
|
||||
}
|
||||
|
||||
conn
|
||||
|> put_status(200)
|
||||
|> json(data)
|
||||
else
|
||||
put_error(conn, :unauthorized, "Origin server is not participating in room.")
|
||||
end
|
||||
|
||||
_ ->
|
||||
put_error(conn, :unknown)
|
||||
end
|
||||
|
||||
nil ->
|
||||
put_error(conn, :not_found, "Event or room not found.")
|
||||
end
|
||||
end
|
||||
|
||||
def state(conn, _), do: put_error(conn, :missing_param)
|
||||
end
|
||||
|
|
|
@ -66,6 +66,13 @@ defmodule MatrixServerWeb.Federation.HTTPClient do
|
|||
Tesla.get(client, path)
|
||||
end
|
||||
|
||||
def get_state(client, room_id, event_id) do
|
||||
path =
|
||||
RouteHelpers.event_path(Endpoint, :state, room_id) |> Tesla.build_url(event_id: event_id)
|
||||
|
||||
Tesla.get(client, path)
|
||||
end
|
||||
|
||||
defp tesla_request(method, client, path, request_schema) do
|
||||
with {:ok, %Tesla.Env{body: body}} <- Tesla.request(client, url: path, method: method),
|
||||
%Ecto.Changeset{valid?: true} = cs <- apply(request_schema, :changeset, [body]) do
|
||||
|
|
|
@ -6,11 +6,11 @@ defmodule MatrixServerWeb.Federation.Transaction do
|
|||
@type edu :: any()
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
origin: String.t(),
|
||||
origin_server_ts: integer(),
|
||||
pdus: [Event.t()],
|
||||
edus: [edu()] | nil
|
||||
}
|
||||
origin: String.t(),
|
||||
origin_server_ts: integer(),
|
||||
pdus: [Event.t()],
|
||||
edus: [edu()] | nil
|
||||
}
|
||||
|
||||
defstruct [:origin, :origin_server_ts, :pdus, :edus]
|
||||
|
||||
|
|
|
@ -65,6 +65,7 @@ defmodule MatrixServerWeb.Router do
|
|||
scope "/v1" do
|
||||
get "/query/profile", QueryController, :profile
|
||||
get "/event/:event_id", EventController, :event
|
||||
get "/state/:room_id", EventController, :state
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue