Implement ruma_api macro

This commit is contained in:
Ross Schulman 2017-06-30 18:34:24 -04:00 committed by Jonas Platte
parent 5f880dfbb1
commit f715124190

View File

@ -2,317 +2,164 @@
/// [GET /_matrix/client/r0/rooms/{roomId}/state](https://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-r0-rooms-roomid-state) /// [GET /_matrix/client/r0/rooms/{roomId}/state](https://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-r0-rooms-roomid-state)
pub mod get_state_events { pub mod get_state_events {
use ruma_api_macros::ruma_api;
use ruma_identifiers::RoomId; use ruma_identifiers::RoomId;
use ruma_events::collections::only; use ruma_events::collections::only;
/// Details about this API endpoint. ruma_api! {
#[derive(Clone, Copy, Debug)] metadata {
pub struct Endpoint; description: "Get state events for a room.",
method: Method::Get,
/// This API endpoint's path parameters. name: "get_state_events",
#[derive(Clone, Debug, Deserialize, Serialize)] path: "/_matrix/client/r0/rooms/:room_id/state",
pub struct PathParams { rate_limited: false,
pub room_id: RoomId requires_authentication: true,
} }
request {
impl ::Endpoint for Endpoint { #[ruma_api(path)]
type BodyParams = (); pub room_id: RoomId,
type PathParams = PathParams;
type QueryParams = ();
type Response = Vec<only::StateEvent>;
fn method() -> ::Method {
::Method::Get
} }
response {
fn request_path(params: Self::PathParams) -> String { pub Vec<only::StateEvent>,
format!(
"/_matrix/client/r0/rooms/{}/state",
params.room_id
)
}
fn router_path() -> &'static str {
"/_matrix/client/r0/rooms/:room_id/state"
}
fn name() -> &'static str {
"get_state_events"
}
fn description() -> &'static str {
"Get state events for a room."
}
fn requires_authentication() -> bool {
true
}
fn rate_limited() -> bool {
false
} }
} }
} }
/// [GET /_matrix/client/r0/rooms/{roomId}/state/{eventType}](https://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-r0-rooms-roomid-state-eventtype) /// [GET /_matrix/client/r0/rooms/{roomId}/state/{eventType}](https://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-r0-rooms-roomid-state-eventtype)
pub mod get_state_events_for_empty_key { pub mod get_state_events_for_empty_key {
use ruma_identifiers::RoomId; use ruma_api_macros::ruma_api;
use ruma_identifiers::{RoomId, EventType};
/// Details about this API endpoint. ruma_api! {
#[derive(Clone, Copy, Debug)] metadata {
pub struct Endpoint; description: "Get state events of a given type associated with the empty key.",
method: ::Method::Get,
/// This API endpoint's path parameters. name: "get_state_events_for_empty_key",
#[derive(Clone, Debug, Deserialize, Serialize)] path: "/_matrix/client/r0/rooms/:room_id/state/:event_type",
pub struct PathParams { rate_limited: false,
requires_authentication: true,
}
request {
/// The room to query for events
#[ruma_api(path)]
pub room_id: RoomId, pub room_id: RoomId,
pub event_type: String /// The type of state to look up
#[ruma_api(path)]
pub event_type: EventType,
} }
response {
impl ::Endpoint for Endpoint { pub content: ::serde_json::Value,
type BodyParams = ();
type PathParams = PathParams;
type QueryParams = ();
type Response = ::serde_json::Value;
fn method() -> ::Method {
::Method::Get
}
fn request_path(params: Self::PathParams) -> String {
format!(
"/_matrix/client/r0/rooms/{}/state/{}",
params.room_id,
params.event_type
)
}
fn router_path() -> &'static str {
"/_matrix/client/r0/rooms/:room_id/state/:event_type"
}
fn name() -> &'static str {
"get_state_events_for_empty_key"
}
fn description() -> &'static str {
"Get state events of a given type associated with the empty key."
}
fn requires_authentication() -> bool {
true
}
fn rate_limited() -> bool {
false
} }
} }
} }
/// [GET /_matrix/client/r0/rooms/{roomId}/state/{eventType}/{stateKey}](https://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-r0-rooms-roomid-state-eventtype-state-key) /// [GET /_matrix/client/r0/rooms/{roomId}/state/{eventType}/{stateKey}](https://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-r0-rooms-roomid-state-eventtype-state-key)
pub mod get_state_events_for_key { pub mod get_state_events_for_key {
use ruma_api_macros::ruma_api;
use ruma_identifiers::RoomId; use ruma_identifiers::RoomId;
/// Details about this API endpoint. ruma_api! {
#[derive(Clone, Copy, Debug)] metadata {
pub struct Endpoint; description: "Get state events associated with a given key.",
method: ::Method::Get,
/// This API endpoint's path parameters. name: "get_state_events_for_key",
#[derive(Clone, Debug, Deserialize, Serialize)] path: "/_matrix/client/r0/rooms/:room_id/state/:event_type/:state_key",
pub struct PathParams { rate_limited: false,
pub room_id: RoomId, requires_authentication: true,
}
request {
/// The room to look up the state in.
#[ruma_api(path)]
pub room_id: RoomID,
/// The type of state to look up.
#[ruma_api(path)]
pub event_type: String, pub event_type: String,
/// The key of the state to look up.
#[ruma_api(path)]
pub state_key: String, pub state_key: String,
} }
response {
impl ::Endpoint for Endpoint { pub content: ::serde_json::Value,
type BodyParams = ();
type PathParams = PathParams;
type QueryParams = ();
type Response = ::serde_json::Value;
fn method() -> ::Method {
::Method::Get
}
fn request_path(params: Self::PathParams) -> String {
format!(
"/_matrix/client/r0/rooms/{}/state/{}/{}",
params.room_id,
params.event_type,
params.state_key
)
}
fn router_path() -> &'static str {
"/_matrix/client/r0/rooms/:room_id/state/:event_type/:state_key"
}
fn name() -> &'static str {
"get_state_events_for_key"
}
fn description() -> &'static str {
"Get state events associated with a given key."
}
fn requires_authentication() -> bool {
true
}
fn rate_limited() -> bool {
false
} }
} }
} }
/// [GET /_matrix/client/r0/rooms/{roomId}/members](https://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-r0-rooms-roomid-members) /// [GET /_matrix/client/r0/rooms/{roomId}/members](https://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-r0-rooms-roomid-members)
pub mod get_member_events { pub mod get_member_events {
use ruma_api_macros::ruma_api;
use ruma_identifiers::RoomId; use ruma_identifiers::RoomId;
use ruma_events::room::member::MemberEvent; use ruma_events::room::member::MemberEvent;
/// Details about this API endpoint. ruma_api! {
#[derive(Clone, Copy, Debug)] metadata {
pub struct Endpoint; description: "Get membership events for a room.",
method: ::Method::Get,
/// This API endpoint's path parameters. name: "get_member_events",
#[derive(Clone, Debug, Deserialize, Serialize)] path: "/_matrix/client/r0/rooms/:room_id/members",
pub struct PathParams { rate_limited: false,
pub room_id: RoomId, requires_authentication: false,
}
/// This API endpoint's reponse.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Response {
pub chunk: Vec<MemberEvent>
}
impl ::Endpoint for Endpoint {
type BodyParams = ();
type PathParams = PathParams;
type QueryParams = ();
type Response = Response;
fn method() -> ::Method {
::Method::Get
}
fn request_path(params: Self::PathParams) -> String {
format!(
"/_matrix/client/r0/rooms/{}/members",
params.room_id,
)
}
fn router_path() -> &'static str {
"/_matrix/client/r0/rooms/:room_id/members"
}
fn name() -> &'static str {
"get_member_events"
}
fn description() -> &'static str {
"Get membership events for a room."
}
fn requires_authentication() -> bool {
// TODO: not marked as requiring auth in the spec, but // TODO: not marked as requiring auth in the spec, but
// will return a 403 error is user is not a member of the // will return a 403 error is user is not a member of the
// room anyway... // room anyway...
false
} }
request {
fn rate_limited() -> bool { /// The room to look up the state in.
false #[ruma_api(path)]
pub room_id: RoomID,
}
response {
pub chunk: Vec<MemberEvent>
} }
} }
} }
/// [GET /_matrix/client/r0/rooms/{roomId}/messages](https://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-r0-rooms-roomid-messages) /// [GET /_matrix/client/r0/rooms/{roomId}/messages](https://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-r0-rooms-roomid-messages)
pub mod get_message_events { pub mod get_message_events {
use ruma_api_macros::ruma_api;
use ruma_identifiers::RoomId; use ruma_identifiers::RoomId;
use ruma_events::collections::only; use ruma_events::collections::only;
/// Details about this API endpoint. ruma_api! {
#[derive(Clone, Copy, Debug)] metadata {
pub struct Endpoint; description: "Get message events for a room.",
method: ::Method::Get,
/// This API endpoint's path parameters. name: "get_message_events",
#[derive(Clone, Debug, Deserialize, Serialize)] path: "/_matrix/client/r0/rooms/:room_id/messages",
pub struct PathParams { rate_limited: false,
pub room_id: RoomId, requires_authentication: true,
pub event_type: String
} }
request {
#[derive(Clone, Debug, Deserialize, Serialize)] // NOTE: The non-macro version of this call included two path params, where the spec only
pub enum Direction { // has one, room_id. I've followed the spec here. -- rschulman 6/30/2017
#[serde(rename="b")] /// The room to look up the state in.
Backward, #[ruma_api(path)]
#[serde(rename="f")] pub room_id: RoomID,
Forward /// Required. The token to start returning events from. This token can be obtained from a
} /// prev_batch token returned for each room by the sync API, or from a start or end token
/// returned by a previous request to this endpoint.
/// This API endpoint's query string parameters.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct QueryParams {
pub from: String, pub from: String,
/// The token to stop returning events at. This token can be obtained from a prev_batch
/// token returned for each room by the sync endpoint, or from a start or end token returned
/// by a previous request to this endpoint.
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub to: Option<String>, pub to: Option<String>,
/// Required. The direction to return events from. One of: ["b", "f"]
pub dir: Direction, pub dir: Direction,
/// The maximum number of events to return. Default: 10.
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub limit: Option<u64> pub limit: Option<u64>,
} }
response {
/// This API endpoint's reponse.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Response {
pub start: String, pub start: String,
pub chunk: Vec<only::RoomEvent>, pub chunk: Vec<only::RoomEvent>,
pub end: String pub end: String,
}
impl ::Endpoint for Endpoint {
type BodyParams = ();
type PathParams = PathParams;
type QueryParams = QueryParams;
type Response = Response;
fn method() -> ::Method {
::Method::Get
}
fn request_path(params: Self::PathParams) -> String {
format!(
"/_matrix/client/r0/rooms/{}/messages",
params.room_id,
)
}
fn router_path() -> &'static str {
"/_matrix/client/r0/rooms/:room_id/messages"
}
fn name() -> &'static str {
"get_message_events"
}
fn description() -> &'static str {
"Get message events for a room."
}
fn requires_authentication() -> bool {
true
}
fn rate_limited() -> bool {
false
} }
} }
} }
/// [GET /_matrix/client/r0/sync](https://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-r0-sync) /// [GET /_matrix/client/r0/sync](https://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-r0-sync)
pub mod sync_events { pub mod sync_events {
use ruma_api_macros::ruma_api;
use ruma_events::collections::only; use ruma_events::collections::only;
use ruma_identifiers::RoomId; use ruma_identifiers::RoomId;
@ -320,6 +167,34 @@ pub mod sync_events {
use r0::filter::FilterDefinition; use r0::filter::FilterDefinition;
ruma_api! {
metadata {
description: "Get all new events from all rooms since the last sync or a given point of time.",
method: ::Method::Get,
name: "sync",
path: "/_matrix/client/r0/sync",
rate_limited: false,
requires_authentication: true,
}
request {
#[serde(skip_serializing_if = "Option::is_none")]
pub filter: Option<Filter>,
#[serde(skip_serializing_if = "Option::is_none")]
pub since: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub full_state: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub set_presence: Option<SetPresence>,
#[serde(skip_serializing_if = "Option::is_none")]
pub timeout: Option<u64>,
}
response {
pub next_batch: String,
pub rooms: Rooms,
pub presence: Presence,
}
}
/// Details about this API endpoint. /// Details about this API endpoint.
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub struct Endpoint; pub struct Endpoint;
@ -328,7 +203,7 @@ pub mod sync_events {
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub enum SetPresence { pub enum SetPresence {
#[serde(rename="offline")] #[serde(rename="offline")]
Offline Offline,
} }
/// A filter represented either as its full JSON definition or the ID of a saved filter. /// A filter represented either as its full JSON definition or the ID of a saved filter.
@ -340,34 +215,19 @@ pub mod sync_events {
FilterId(String), FilterId(String),
} }
/// This API endpoint's query string parameters.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct QueryParams {
#[serde(skip_serializing_if = "Option::is_none")]
pub filter: Option<Filter>,
#[serde(skip_serializing_if = "Option::is_none")]
pub since: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub full_state: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub set_presence: Option<SetPresence>,
#[serde(skip_serializing_if = "Option::is_none")]
pub timeout: Option<u64>
}
/// Updates to rooms /// Updates to rooms
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Rooms { pub struct Rooms {
pub leave: HashMap<RoomId, LeftRoom>, pub leave: HashMap<RoomId, LeftRoom>,
pub join: HashMap<RoomId, JoinedRoom>, pub join: HashMap<RoomId, JoinedRoom>,
pub invite: HashMap<RoomId, InvitedRoom> pub invite: HashMap<RoomId, InvitedRoom>,
} }
/// Historical updates to left rooms /// Historical updates to left rooms
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub struct LeftRoom { pub struct LeftRoom {
pub timeline: Timeline, pub timeline: Timeline,
pub state: State pub state: State,
} }
/// Updates to joined rooms /// Updates to joined rooms
@ -377,14 +237,14 @@ pub mod sync_events {
pub timeline: Timeline, pub timeline: Timeline,
pub state: State, pub state: State,
pub account_data: AccountData, pub account_data: AccountData,
pub ephemeral: Ephemeral pub ephemeral: Ephemeral,
} }
/// unread notifications count /// unread notifications count
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub struct UnreadNotificationsCount { pub struct UnreadNotificationsCount {
pub highlight_count: u64, pub highlight_count: u64,
pub notification_count: u64 pub notification_count: u64,
} }
/// timeline /// timeline
@ -392,43 +252,43 @@ pub mod sync_events {
pub struct Timeline { pub struct Timeline {
pub limited: bool, pub limited: bool,
pub prev_batch: String, pub prev_batch: String,
pub events: only::RoomEvent pub events: only::RoomEvent,
} }
/// state /// state
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub struct State { pub struct State {
pub events: only::StateEvent pub events: only::StateEvent,
} }
/// account data /// account data
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub struct AccountData { pub struct AccountData {
pub events: only::Event pub events: only::Event,
} }
/// ephemeral /// ephemeral
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Ephemeral { pub struct Ephemeral {
pub events: only::Event pub events: only::Event,
} }
/// invited room updates /// invited room updates
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub struct InvitedRoom { pub struct InvitedRoom {
pub invite_state: InviteState pub invite_state: InviteState,
} }
/// invite state /// invite state
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub struct InviteState { pub struct InviteState {
pub events: only::StateEvent pub events: only::StateEvent,
} }
/// presence /// presence
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Presence { pub struct Presence {
pub events: only::Event pub events: only::Event,
} }
/// This API endpoint's reponse. /// This API endpoint's reponse.
@ -436,41 +296,6 @@ pub mod sync_events {
pub struct Response { pub struct Response {
pub next_batch: String, pub next_batch: String,
pub rooms: Rooms, pub rooms: Rooms,
pub presence: Presence pub presence: Presence,
}
impl ::Endpoint for Endpoint {
type BodyParams = ();
type PathParams = ();
type QueryParams = QueryParams;
type Response = Response;
fn method() -> ::Method {
::Method::Get
}
fn request_path(_params: Self::PathParams) -> String {
"/_matrix/client/r0/sync".to_string()
}
fn router_path() -> &'static str {
"/_matrix/client/r0/sync"
}
fn name() -> &'static str {
"sync"
}
fn description() -> &'static str {
"Get all new events from all rooms since the last sync or a given point of time."
}
fn requires_authentication() -> bool {
true
}
fn rate_limited() -> bool {
false
}
} }
} }