From f715124190cd0f42dd85921a4071d8621783f768 Mon Sep 17 00:00:00 2001 From: Ross Schulman Date: Fri, 30 Jun 2017 18:34:24 -0400 Subject: [PATCH] Implement ruma_api macro --- src/r0/sync.rs | 465 +++++++++++++++---------------------------------- 1 file changed, 145 insertions(+), 320 deletions(-) diff --git a/src/r0/sync.rs b/src/r0/sync.rs index 16a5e925..cfa22a5a 100644 --- a/src/r0/sync.rs +++ b/src/r0/sync.rs @@ -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) pub mod get_state_events { + use ruma_api_macros::ruma_api; use ruma_identifiers::RoomId; use ruma_events::collections::only; - /// Details about this API endpoint. - #[derive(Clone, Copy, Debug)] - pub struct Endpoint; - - /// This API endpoint's path parameters. - #[derive(Clone, Debug, Deserialize, Serialize)] - pub struct PathParams { - pub room_id: RoomId - } - - impl ::Endpoint for Endpoint { - type BodyParams = (); - type PathParams = PathParams; - type QueryParams = (); - type Response = Vec; - - fn method() -> ::Method { - ::Method::Get + ruma_api! { + metadata { + description: "Get state events for a room.", + method: Method::Get, + name: "get_state_events", + path: "/_matrix/client/r0/rooms/:room_id/state", + rate_limited: false, + requires_authentication: true, } - - fn request_path(params: Self::PathParams) -> String { - format!( - "/_matrix/client/r0/rooms/{}/state", - params.room_id - ) + request { + #[ruma_api(path)] + pub room_id: RoomId, } - - 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 + response { + pub Vec, } } } /// [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 { - use ruma_identifiers::RoomId; + use ruma_api_macros::ruma_api; + use ruma_identifiers::{RoomId, EventType}; - /// Details about this API endpoint. - #[derive(Clone, Copy, Debug)] - pub struct Endpoint; - - /// This API endpoint's path parameters. - #[derive(Clone, Debug, Deserialize, Serialize)] - pub struct PathParams { - pub room_id: RoomId, - pub event_type: String - } - - impl ::Endpoint for Endpoint { - type BodyParams = (); - type PathParams = PathParams; - type QueryParams = (); - type Response = ::serde_json::Value; - - fn method() -> ::Method { - ::Method::Get + ruma_api! { + metadata { + description: "Get state events of a given type associated with the empty key.", + method: ::Method::Get, + name: "get_state_events_for_empty_key", + path: "/_matrix/client/r0/rooms/:room_id/state/:event_type", + rate_limited: false, + requires_authentication: true, } - - fn request_path(params: Self::PathParams) -> String { - format!( - "/_matrix/client/r0/rooms/{}/state/{}", - params.room_id, - params.event_type - ) + request { + /// The room to query for events + #[ruma_api(path)] + pub room_id: RoomId, + /// The type of state to look up + #[ruma_api(path)] + pub event_type: EventType, } - - 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 + response { + pub content: ::serde_json::Value, } } } /// [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 { + use ruma_api_macros::ruma_api; use ruma_identifiers::RoomId; - /// Details about this API endpoint. - #[derive(Clone, Copy, Debug)] - pub struct Endpoint; - - /// This API endpoint's path parameters. - #[derive(Clone, Debug, Deserialize, Serialize)] - pub struct PathParams { - pub room_id: RoomId, - pub event_type: String, - pub state_key: String, - } - - impl ::Endpoint for Endpoint { - type BodyParams = (); - type PathParams = PathParams; - type QueryParams = (); - type Response = ::serde_json::Value; - - fn method() -> ::Method { - ::Method::Get + ruma_api! { + metadata { + description: "Get state events associated with a given key.", + method: ::Method::Get, + name: "get_state_events_for_key", + path: "/_matrix/client/r0/rooms/:room_id/state/:event_type/:state_key", + rate_limited: false, + requires_authentication: true, } - - fn request_path(params: Self::PathParams) -> String { - format!( - "/_matrix/client/r0/rooms/{}/state/{}/{}", - params.room_id, - params.event_type, - params.state_key - ) + 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, + /// The key of the state to look up. + #[ruma_api(path)] + pub state_key: String, } - - 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 + response { + pub content: ::serde_json::Value, } } } /// [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 { + use ruma_api_macros::ruma_api; use ruma_identifiers::RoomId; use ruma_events::room::member::MemberEvent; - /// Details about this API endpoint. - #[derive(Clone, Copy, Debug)] - pub struct Endpoint; - - /// This API endpoint's path parameters. - #[derive(Clone, Debug, Deserialize, Serialize)] - pub struct PathParams { - pub room_id: RoomId, - } - - /// This API endpoint's reponse. - #[derive(Clone, Debug, Deserialize, Serialize)] - pub struct Response { - pub chunk: Vec - } - - 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 { + ruma_api! { + metadata { + description: "Get membership events for a room.", + method: ::Method::Get, + name: "get_member_events", + path: "/_matrix/client/r0/rooms/:room_id/members", + rate_limited: false, + requires_authentication: false, // TODO: not marked as requiring auth in the spec, but // will return a 403 error is user is not a member of the // room anyway... - false } - - fn rate_limited() -> bool { - false + request { + /// The room to look up the state in. + #[ruma_api(path)] + pub room_id: RoomID, + } + response { + pub chunk: Vec } } } /// [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 { + use ruma_api_macros::ruma_api; use ruma_identifiers::RoomId; use ruma_events::collections::only; - /// Details about this API endpoint. - #[derive(Clone, Copy, Debug)] - pub struct Endpoint; - - /// This API endpoint's path parameters. - #[derive(Clone, Debug, Deserialize, Serialize)] - pub struct PathParams { - pub room_id: RoomId, - pub event_type: String - } - - #[derive(Clone, Debug, Deserialize, Serialize)] - pub enum Direction { - #[serde(rename="b")] - Backward, - #[serde(rename="f")] - Forward - } - - /// This API endpoint's query string parameters. - #[derive(Clone, Debug, Deserialize, Serialize)] - pub struct QueryParams { - pub from: String, - #[serde(skip_serializing_if = "Option::is_none")] - pub to: Option, - pub dir: Direction, - #[serde(skip_serializing_if = "Option::is_none")] - pub limit: Option - } - - /// This API endpoint's reponse. - #[derive(Clone, Debug, Deserialize, Serialize)] - pub struct Response { - pub start: String, - pub chunk: Vec, - pub end: String - } - - impl ::Endpoint for Endpoint { - type BodyParams = (); - type PathParams = PathParams; - type QueryParams = QueryParams; - type Response = Response; - - fn method() -> ::Method { - ::Method::Get + ruma_api! { + metadata { + description: "Get message events for a room.", + method: ::Method::Get, + name: "get_message_events", + path: "/_matrix/client/r0/rooms/:room_id/messages", + rate_limited: false, + requires_authentication: true, } - - fn request_path(params: Self::PathParams) -> String { - format!( - "/_matrix/client/r0/rooms/{}/messages", - params.room_id, - ) + request { + // NOTE: The non-macro version of this call included two path params, where the spec only + // has one, room_id. I've followed the spec here. -- rschulman 6/30/2017 + /// The room to look up the state in. + #[ruma_api(path)] + pub room_id: RoomID, + /// 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. + 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")] + pub to: Option, + /// Required. The direction to return events from. One of: ["b", "f"] + pub dir: Direction, + /// The maximum number of events to return. Default: 10. + #[serde(skip_serializing_if = "Option::is_none")] + pub limit: Option, } - - 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 + response { + pub start: String, + pub chunk: Vec, + pub end: String, } } } /// [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 { + use ruma_api_macros::ruma_api; use ruma_events::collections::only; use ruma_identifiers::RoomId; @@ -320,6 +167,34 @@ pub mod sync_events { 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, + #[serde(skip_serializing_if = "Option::is_none")] + pub since: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub full_state: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub set_presence: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub timeout: Option, + } + response { + pub next_batch: String, + pub rooms: Rooms, + pub presence: Presence, + } + + } /// Details about this API endpoint. #[derive(Clone, Copy, Debug)] pub struct Endpoint; @@ -328,7 +203,7 @@ pub mod sync_events { #[derive(Clone, Debug, Deserialize, Serialize)] pub enum SetPresence { #[serde(rename="offline")] - Offline + Offline, } /// 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), } - /// 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, - #[serde(skip_serializing_if = "Option::is_none")] - pub since: Option, - #[serde(skip_serializing_if = "Option::is_none")] - pub full_state: Option, - #[serde(skip_serializing_if = "Option::is_none")] - pub set_presence: Option, - #[serde(skip_serializing_if = "Option::is_none")] - pub timeout: Option - } - /// Updates to rooms #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Rooms { pub leave: HashMap, pub join: HashMap, - pub invite: HashMap + pub invite: HashMap, } /// Historical updates to left rooms #[derive(Clone, Debug, Deserialize, Serialize)] pub struct LeftRoom { pub timeline: Timeline, - pub state: State + pub state: State, } /// Updates to joined rooms @@ -377,14 +237,14 @@ pub mod sync_events { pub timeline: Timeline, pub state: State, pub account_data: AccountData, - pub ephemeral: Ephemeral + pub ephemeral: Ephemeral, } /// unread notifications count #[derive(Clone, Debug, Deserialize, Serialize)] pub struct UnreadNotificationsCount { pub highlight_count: u64, - pub notification_count: u64 + pub notification_count: u64, } /// timeline @@ -392,43 +252,43 @@ pub mod sync_events { pub struct Timeline { pub limited: bool, pub prev_batch: String, - pub events: only::RoomEvent + pub events: only::RoomEvent, } /// state #[derive(Clone, Debug, Deserialize, Serialize)] pub struct State { - pub events: only::StateEvent + pub events: only::StateEvent, } /// account data #[derive(Clone, Debug, Deserialize, Serialize)] pub struct AccountData { - pub events: only::Event + pub events: only::Event, } /// ephemeral #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Ephemeral { - pub events: only::Event + pub events: only::Event, } /// invited room updates #[derive(Clone, Debug, Deserialize, Serialize)] pub struct InvitedRoom { - pub invite_state: InviteState + pub invite_state: InviteState, } /// invite state #[derive(Clone, Debug, Deserialize, Serialize)] pub struct InviteState { - pub events: only::StateEvent + pub events: only::StateEvent, } /// presence #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Presence { - pub events: only::Event + pub events: only::Event, } /// This API endpoint's reponse. @@ -436,41 +296,6 @@ pub mod sync_events { pub struct Response { pub next_batch: String, pub rooms: Rooms, - 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 - } + pub presence: Presence, } }