federation-api: Add retrieval endpoints

This commit is contained in:
Julian Tescher 2020-09-11 10:12:48 -07:00 committed by GitHub
parent c3a074059f
commit ebd1387bac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 188 additions and 0 deletions

View File

@ -1,3 +1,6 @@
//! Endpoints to get general information about events
pub mod get_event;
pub mod get_missing_events;
pub mod get_room_state;
pub mod get_room_state_ids;

View File

@ -0,0 +1,3 @@
//! Retrieves a single event.
pub mod v1;

View File

@ -0,0 +1,52 @@
//! [GET /_matrix/federation/v1/event/{eventId}](https://matrix.org/docs/spec/server_server/r0.1.4#get-matrix-federation-v1-event-eventid)
use ruma_api::ruma_api;
use ruma_events::pdu::Pdu;
use ruma_identifiers::{EventId, ServerNameBox};
use std::time::SystemTime;
ruma_api! {
metadata: {
description: "Retrieves a single event.",
method: GET,
name: "get_event",
path: "/_matrix/federation/v1/event/:event_id",
rate_limited: false,
requires_authentication: true,
}
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
request: {
/// The event ID to get.
#[ruma_api(path)]
pub event_id: &'a EventId,
}
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
response: {
/// The `server_name` of the homeserver sending this transaction.
pub origin: ServerNameBox,
/// Time on originating homeserver when this transaction started.
#[serde(with = "ruma_serde::time::ms_since_unix_epoch")]
pub origin_server_ts: SystemTime,
/// The event.
#[serde(rename = "pdus", with = "ruma_serde::single_element_seq")]
pub pdu: Pdu,
}
}
impl<'a> Request<'a> {
/// Creates a new `Request` with the given event id.
pub fn new(event_id: &'a EventId) -> Self {
Self { event_id }
}
}
impl Response {
/// Creates a new `Response` with the given server name, timestamp, and event.
pub fn new(origin: ServerNameBox, origin_server_ts: SystemTime, pdu: Pdu) -> Self {
Self { origin, origin_server_ts, pdu }
}
}

View File

@ -0,0 +1,3 @@
//! Retrieves a snapshot of a room's state at a given event.
pub mod v1;

View File

@ -0,0 +1,51 @@
//! [GET /_matrix/federation/v1/state/{roomId}](https://matrix.org/docs/spec/server_server/r0.1.4#get-matrix-federation-v1-state-roomid)
use ruma_api::ruma_api;
use ruma_events::pdu::Pdu;
use ruma_identifiers::{EventId, RoomId};
ruma_api! {
metadata: {
description: "Retrieves a snapshot of a room's state at a given event.",
method: GET,
name: "get_room_state",
path: "/_matrix/federation/v1/state/:room_id",
rate_limited: false,
requires_authentication: true,
}
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
request: {
/// The room ID to get state for.
#[ruma_api(path)]
pub room_id: &'a RoomId,
/// An event ID in the room to retrieve the state at.
#[ruma_api(query)]
pub event_id: &'a EventId,
}
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
response: {
/// The full set of authorization events that make up the state of the
/// room, and their authorization events, recursively.
pub auth_chain: Vec<Pdu>,
/// The fully resolved state of the room at the given event.
pub pdus: Vec<Pdu>,
}
}
impl<'a> Request<'a> {
/// Creates a new `Request` with the given event ID and room ID.
pub fn new(event_id: &'a EventId, room_id: &'a RoomId) -> Self {
Self { event_id, room_id }
}
}
impl Response {
/// Creates a new `Response` with the given auth chain and room state.
pub fn new(auth_chain: Vec<Pdu>, pdus: Vec<Pdu>) -> Self {
Self { auth_chain, pdus }
}
}

View File

@ -0,0 +1,3 @@
//! Retrieves a snapshot of a room's state at a given event, in the form of event IDs.
pub mod v1;

View File

@ -0,0 +1,50 @@
//! [GET /_matrix/federation/v1/state_ids/{roomId}](https://matrix.org/docs/spec/server_server/r0.1.4#get-matrix-federation-v1-state-ids-roomid)
use ruma_api::ruma_api;
use ruma_identifiers::{EventId, RoomId};
ruma_api! {
metadata: {
description: "Retrieves a snapshot of a room's state at a given event, in the form of event IDs",
method: GET,
name: "get_room_state_ids",
path: "/_matrix/federation/v1/state_ids/:room_id",
rate_limited: false,
requires_authentication: true,
}
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
request: {
/// The room ID to get state for.
#[ruma_api(path)]
pub room_id: &'a RoomId,
/// An event ID in the room to retrieve the state at.
#[ruma_api(query)]
pub event_id: &'a EventId,
}
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
response: {
/// The full set of authorization events that make up the state of the
/// room, and their authorization events, recursively.
pub auth_chain_ids: Vec<EventId>,
/// The fully resolved state of the room at the given event.
pub pdu_ids: Vec<EventId>,
}
}
impl<'a> Request<'a> {
/// Creates a new `Request` with the given event id and room id.
pub fn new(event_id: &'a EventId, room_id: &'a RoomId) -> Self {
Self { event_id, room_id }
}
}
impl Response {
/// Creates a new `Response` with the given auth chain IDs and room state IDs.
pub fn new(auth_chain_ids: Vec<EventId>, pdu_ids: Vec<EventId>) -> Self {
Self { auth_chain_ids, pdu_ids }
}
}

View File

@ -6,6 +6,7 @@ pub mod can_be_empty;
pub mod duration;
pub mod empty;
pub mod json_string;
pub mod single_element_seq;
pub mod test;
pub mod time;
pub mod urlencoded;

View File

@ -0,0 +1,22 @@
//! De-/serialization functions to and from single element sequences.
use serde::{
de::{Deserialize, Deserializer},
ser::{Serialize, Serializer},
};
pub fn serialize<T, S>(element: &T, serializer: S) -> Result<S::Ok, S::Error>
where
T: Serialize,
S: Serializer,
{
[element].serialize(serializer)
}
pub fn deserialize<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where
T: Deserialize<'de>,
D: Deserializer<'de>,
{
<[_; 1]>::deserialize(deserializer).map(|[first]| first)
}