2021-08-17 20:50:15 +00:00
|
|
|
defmodule MatrixServerWeb.Federation.EventController do
|
|
|
|
use MatrixServerWeb, :controller
|
|
|
|
use MatrixServerWeb.Federation.AuthenticateServer
|
|
|
|
|
2021-08-21 09:25:36 +00:00
|
|
|
import MatrixServerWeb.Error
|
|
|
|
import Ecto.Query
|
|
|
|
|
|
|
|
alias MatrixServer.{Repo, Event, RoomServer}
|
|
|
|
alias MatrixServerWeb.Federation.Transaction
|
|
|
|
|
2021-08-24 23:27:03 +00:00
|
|
|
@doc """
|
|
|
|
Retrieves a single event.
|
|
|
|
|
|
|
|
Action for GET /_matrix/federation/v1/event/{eventId}.
|
|
|
|
"""
|
2021-08-21 09:25:36 +00:00
|
|
|
def event(%Plug.Conn{assigns: %{origin: origin}} = conn, %{"event_id" => event_id}) do
|
|
|
|
query =
|
|
|
|
Event
|
|
|
|
|> where([e], e.event_id == ^event_id)
|
|
|
|
|> preload(:room)
|
|
|
|
|
|
|
|
case Repo.one(query) do
|
|
|
|
%Event{room: room} = event ->
|
|
|
|
case RoomServer.get_room_server(room) do
|
|
|
|
{:ok, pid} ->
|
2021-08-22 13:19:48 +00:00
|
|
|
if RoomServer.server_in_room?(pid, origin) do
|
2021-08-21 09:25:36 +00:00
|
|
|
data = Transaction.new([event])
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_status(200)
|
|
|
|
|> json(data)
|
|
|
|
else
|
2021-08-21 19:39:28 +00:00
|
|
|
put_error(conn, :unauthorized, "Origin server is not participating in room.")
|
2021-08-21 09:25:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
put_error(conn, :unknown)
|
|
|
|
end
|
|
|
|
|
|
|
|
nil ->
|
|
|
|
put_error(conn, :not_found, "Event or room not found.")
|
|
|
|
end
|
2021-08-17 20:50:15 +00:00
|
|
|
end
|
2021-08-21 09:25:36 +00:00
|
|
|
|
2021-08-21 19:39:28 +00:00
|
|
|
def event(conn, _), do: put_error(conn, :missing_param)
|
|
|
|
|
2021-08-24 23:27:03 +00:00
|
|
|
@doc """
|
|
|
|
Retrieves a snapshot of a room's state at a given event.
|
|
|
|
|
|
|
|
Action for GET /_matrix/federation/v1/state/{roomId}.
|
|
|
|
"""
|
2021-08-21 19:39:28 +00:00
|
|
|
def state(%Plug.Conn{assigns: %{origin: origin}} = conn, %{
|
|
|
|
"event_id" => event_id,
|
|
|
|
"room_id" => room_id
|
|
|
|
}) do
|
2021-08-22 10:19:47 +00:00
|
|
|
get_state_or_state_ids(conn, :state, origin, event_id, room_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def state(conn, _), do: put_error(conn, :missing_param)
|
|
|
|
|
2021-08-24 23:27:03 +00:00
|
|
|
@doc """
|
|
|
|
Retrieves a snapshot of a room's state at a given event, in the form of event IDs.
|
|
|
|
|
|
|
|
Action for GET /_matrix/federation/v1/state_ids/{roomId}.
|
|
|
|
"""
|
2021-08-22 10:19:47 +00:00
|
|
|
def state_ids(%Plug.Conn{assigns: %{origin: origin}} = conn, %{
|
|
|
|
"event_id" => event_id,
|
|
|
|
"room_id" => room_id
|
|
|
|
}) do
|
|
|
|
get_state_or_state_ids(conn, :state_ids, origin, event_id, room_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def state_ids(conn, _), do: put_error(conn, :missing_param)
|
|
|
|
|
2021-08-22 13:19:48 +00:00
|
|
|
@spec get_state_or_state_ids(
|
|
|
|
Plug.Conn.t(),
|
|
|
|
:state | :state_ids,
|
|
|
|
String.t(),
|
|
|
|
String.t(),
|
|
|
|
String.t()
|
|
|
|
) :: Plug.Conn.t()
|
2021-08-22 10:19:47 +00:00
|
|
|
defp get_state_or_state_ids(conn, state_or_state_ids, origin, event_id, room_id) do
|
2021-08-21 19:39:28 +00:00
|
|
|
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} ->
|
2021-08-22 13:19:48 +00:00
|
|
|
if RoomServer.server_in_room?(pid, origin) do
|
2021-08-22 10:19:47 +00:00
|
|
|
{state_events, auth_chain} =
|
|
|
|
case state_or_state_ids do
|
|
|
|
:state -> RoomServer.get_state_at_event(pid, event)
|
|
|
|
:state_ids -> RoomServer.get_state_ids_at_event(pid, event)
|
|
|
|
end
|
2021-08-21 19:39:28 +00:00
|
|
|
|
|
|
|
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
|
2021-08-17 20:50:15 +00:00
|
|
|
end
|