From 5f880dfbb18363389cdff4c765aa8524cd63ee2b Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Thu, 29 Jun 2017 03:35:41 +1000 Subject: [PATCH 1/8] Use ruma-api-macros for the send endpoints. --- src/lib.rs | 2 +- src/r0/send.rs | 221 +++++++++++++++---------------------------------- 2 files changed, 68 insertions(+), 155 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1ec0e41f..11005431 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,7 +35,7 @@ pub mod r0 { // pub mod redact; // pub mod room; // pub mod search; -// pub mod send; + pub mod send; // pub mod server; // pub mod session; // pub mod sync; diff --git a/src/r0/send.rs b/src/r0/send.rs index 7c7ac4dc..cd6c7a12 100644 --- a/src/r0/send.rs +++ b/src/r0/send.rs @@ -2,193 +2,106 @@ /// [PUT /_matrix/client/r0/rooms/{roomId}/state/{eventType}](https://matrix.org/docs/spec/client_server/r0.2.0.html#put-matrix-client-r0-rooms-roomid-state-eventtype) pub mod send_state_event_for_empty_key { + use ruma_api_macros::ruma_api; use ruma_identifiers::{RoomId, EventId}; use ruma_events::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: EventType - } - - /// This API endpoint's response. - #[derive(Clone, Debug, Deserialize, Serialize)] - pub struct Response { - pub event_id: EventId, - } - - - impl ::Endpoint for Endpoint { - type BodyParams = ::serde_json::Value; - type PathParams = PathParams; - type QueryParams = (); - type Response = Response; - - fn method() -> ::Method { - ::Method::Put + ruma_api! { + metadata { + description: "Send a state event to a room associated with the empty state key.", + method: Method::Put, + name: "send_state_event_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 set the state in. + #[ruma_api(path)] + pub room_id: RoomId, + /// The type of event to send. + #[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 { - "send_state_event_for_empty_key" - } - - fn description() -> &'static str { - "Send a state event to a room associated with the empty state key." - } - - fn requires_authentication() -> bool { - true - } - - fn rate_limited() -> bool { - false + response { + /// A unique identifier for the event. + pub event_id: EventId, } } } /// [PUT /_matrix/client/r0/rooms/{roomId}/state/{eventType}/{stateKey}](https://matrix.org/docs/spec/client_server/r0.2.0.html#put-matrix-client-r0-rooms-roomid-state-eventtype-statekey) pub mod send_state_event_for_key { + use ruma_api_macros::ruma_api; use ruma_identifiers::{RoomId, EventId}; use ruma_events::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: EventType, - pub state_key: String - } - - /// This API endpoint's response. - #[derive(Clone, Debug, Deserialize, Serialize)] - pub struct Response { - pub event_id: EventId, - } - - - impl ::Endpoint for Endpoint { - type BodyParams = ::serde_json::Value; - type PathParams = PathParams; - type QueryParams = (); - type Response = Response; - - fn method() -> ::Method { - ::Method::Put + ruma_api! { + metadata { + description: "Send a state event to a room associated with a given state key.", + method: Method::Put, + name: "send_state_event_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 set the state in. + #[ruma_api(path)] + pub room_id: RoomId, + /// The type of event to send. + #[ruma_api(path)] + pub event_type: EventType, + /// The state_key for the state to send. Defaults to the empty string. + #[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 { - "send_state_event_for_key" - } - - fn description() -> &'static str { - "Send a state event to a room associated with a given state key." - } - - fn requires_authentication() -> bool { - true - } - - fn rate_limited() -> bool { - false + response { + /// A unique identifier for the event. + pub event_id: EventId, } } } /// [PUT /_matrix/client/r0/rooms/{roomId}/send/{eventType}/{txnId}](https://matrix.org/docs/spec/client_server/r0.2.0.html#put-matrix-client-r0-rooms-roomid-send-eventtype-txnid) pub mod send_message_event { + use ruma_api_macros::ruma_api; use ruma_identifiers::{RoomId, EventId}; use ruma_events::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: EventType, - pub txn_id: String - } - - /// This API endpoint's response. - #[derive(Clone, Debug, Deserialize, Serialize)] - pub struct Response { - pub event_id: EventId, - } - - - impl ::Endpoint for Endpoint { - type BodyParams = ::serde_json::Value; - type PathParams = PathParams; - type QueryParams = (); - type Response = Response; - - fn method() -> ::Method { - ::Method::Put + ruma_api! { + metadata { + description: "Send a message event to a room.", + method: Method::Put, + name: "send_message_event", + path: "/_matrix/client/r0/rooms/:room_id/send/:event_type/:txn_id", + rate_limited: false, + requires_authentication: true, } - fn request_path(params: Self::PathParams) -> String { - format!( - "/_matrix/client/r0/rooms/{}/send/{}/{}", - params.room_id, - params.event_type, - params.txn_id - ) + request { + /// The room to send the event to. + #[ruma_api(path)] + pub room_id: RoomId, + /// The type of event to send. + #[ruma_api(path)] + pub event_type: EventType, + /// The transaction ID for this event. + /// + /// Clients should generate an ID unique across requests with the + /// same access token; it will be used by the server to ensure + /// idempotency of requests. + #[ruma_api(path)] + pub txn_id: String, } - fn router_path() -> &'static str { - "/_matrix/client/r0/rooms/:room_id/send/:event_type/:txn_id" - } - - fn name() -> &'static str { - "send_message_event" - } - - fn description() -> &'static str { - "Send a message event to a room." - } - - fn requires_authentication() -> bool { - true - } - - fn rate_limited() -> bool { - false + response { + /// A unique identifier for the event. + pub event_id: EventId, } } } From f715124190cd0f42dd85921a4071d8621783f768 Mon Sep 17 00:00:00 2001 From: Ross Schulman Date: Fri, 30 Jun 2017 18:34:24 -0400 Subject: [PATCH 2/8] 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, } } From a097aa02f21cd0add260a4609eb9abf1d53a90ea Mon Sep 17 00:00:00 2001 From: Ross Schulman Date: Fri, 30 Jun 2017 21:53:30 -0400 Subject: [PATCH 3/8] Port session.rs to ruma_api_macro --- src/r0/session.rs | 124 ++++++++++++---------------------------------- 1 file changed, 33 insertions(+), 91 deletions(-) diff --git a/src/r0/session.rs b/src/r0/session.rs index 7f67babb..dc8d79bb 100644 --- a/src/r0/session.rs +++ b/src/r0/session.rs @@ -2,27 +2,18 @@ /// [POST /_matrix/client/r0/login](https://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-login) pub mod login { - /// Details about this API endpoint. - #[derive(Clone, Copy, Debug)] - pub struct Endpoint; + use ruma_api_macros::ruma_api; - /// Possible login mediums for 3rd party ID - #[derive(Clone, Debug, Deserialize, Serialize)] - pub enum LoginMedium { - #[serde(rename = "email")] - Email + ruma_api! { + metadata { + description: "Login to the homeserver.", + method: ::Method::Post, + name: "login", + path: "/_matrix/client/r0/login", + rate_limited: true, + requires_authentication: false, } - - /// Possible kinds of login - #[derive(Clone, Debug, Deserialize, Serialize)] - pub enum LoginKind { - #[serde(rename = "m.login.password")] - Password - } - - /// The body parameters for this endpoint - #[derive(Clone, Debug, Deserialize, Serialize)] - pub struct BodyParams { + request { /// Password of the user pub password: String, /// Medium of 3rd party login to use @@ -37,10 +28,7 @@ pub mod login { #[serde(skip_serializing_if = "Option::is_none")] pub address: Option } - - /// This API endpoint's response. - #[derive(Clone, Debug, Deserialize, Serialize)] - pub struct Response { + response { pub access_token: String, pub home_server: String, #[serde(skip_serializing_if = "Option::is_none")] @@ -48,80 +36,34 @@ pub mod login { pub user_id: String, } - impl ::Endpoint for Endpoint { - type BodyParams = (); - type PathParams = (); - type QueryParams = (); - type Response = Response; + /// Possible login mediums for 3rd party ID + #[derive(Clone, Debug, Deserialize, Serialize)] + pub enum LoginMedium { + #[serde(rename = "email")] + Email + } - fn method() -> ::Method { - ::Method::Post - } - - fn request_path(_params: Self::PathParams) -> String { - Self::router_path().to_string() - } - - fn router_path() -> &'static str { - "/_matrix/client/r0/login" - } - - fn name() -> &'static str { - "login" - } - - fn description() -> &'static str { - "Login to the homeserver." - } - - fn requires_authentication() -> bool { - false - } - - fn rate_limited() -> bool { - true - } + /// Possible kinds of login + #[derive(Clone, Debug, Deserialize, Serialize)] + pub enum LoginKind { + #[serde(rename = "m.login.password")] + Password } } /// [POST /_matrix/client/r0/logout](https://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-logout) pub mod logout { - /// Details about this API endpoint. - #[derive(Clone, Copy, Debug)] - pub struct Endpoint; + use ruma_api_macros::ruma_api; - impl ::Endpoint for Endpoint { - type BodyParams = (); - type PathParams = (); - type QueryParams = (); - type Response = (); - - fn method() -> ::Method { - ::Method::Post - } - - fn request_path(_params: Self::PathParams) -> String { - Self::router_path().to_string() - } - - fn router_path() -> &'static str { - "/_matrix/client/r0/logout" - } - - fn name() -> &'static str { - "logout" - } - - fn description() -> &'static str { - "Log out of the homeserver." - } - - fn requires_authentication() -> bool { - true - } - - fn rate_limited() -> bool { - false - } + ruma_api! { + metadata { + description: "Log out of the homeserver.", + method: ::Method::Post, + name: "logout", + path: "/_matrix/client/r0/logout", + rate_limited: false, + requires_authentication: true, } + request {} + response {} } From 3a854bf64aafa2ae79d7478059b95dca5f7d0cc2 Mon Sep 17 00:00:00 2001 From: Ross Schulman Date: Sat, 1 Jul 2017 13:32:11 -0400 Subject: [PATCH 4/8] Update lib.rs with newly available calls --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 11005431..f2f6c29e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,8 +37,8 @@ pub mod r0 { // pub mod search; pub mod send; // pub mod server; -// pub mod session; -// pub mod sync; + pub mod session; + pub mod sync; // pub mod tag; // pub mod typing; // pub mod voip; From a1a9064d28d49e4ea7ba35cfc57736709ccb3754 Mon Sep 17 00:00:00 2001 From: Ross Schulman Date: Sat, 1 Jul 2017 15:57:21 -0400 Subject: [PATCH 5/8] Squash a bunch of bugs --- src/r0/session.rs | 82 ++++++++++++++++++++++++----------------------- src/r0/sync.rs | 40 +++++++++++------------ 2 files changed, 61 insertions(+), 61 deletions(-) diff --git a/src/r0/session.rs b/src/r0/session.rs index dc8d79bb..63285be2 100644 --- a/src/r0/session.rs +++ b/src/r0/session.rs @@ -5,49 +5,50 @@ pub mod login { use ruma_api_macros::ruma_api; ruma_api! { - metadata { - description: "Login to the homeserver.", - method: ::Method::Post, - name: "login", - path: "/_matrix/client/r0/login", - rate_limited: true, - requires_authentication: false, - } - request { - /// Password of the user - pub password: String, - /// Medium of 3rd party login to use - #[serde(skip_serializing_if = "Option::is_none")] - pub medium: Option, - /// Type of login to do - #[serde(rename = "type")] - pub kind: LoginKind, - /// Localpart or full matrix user id of the user - pub user: String, - /// 3rd party identifier for the user - #[serde(skip_serializing_if = "Option::is_none")] - pub address: Option - } - response { - pub access_token: String, - pub home_server: String, - #[serde(skip_serializing_if = "Option::is_none")] - pub refresh_token: Option, - pub user_id: String, + metadata { + description: "Login to the homeserver.", + method: Method::Post, + name: "login", + path: "/_matrix/client/r0/login", + rate_limited: true, + requires_authentication: false, + } + request { + /// Password of the user + pub password: String, + /// Medium of 3rd party login to use + #[serde(skip_serializing_if = "Option::is_none")] + pub medium: Option, + /// Type of login to do + #[serde(rename = "type")] + pub kind: LoginKind, + /// Localpart or full matrix user id of the user + pub user: String, + /// 3rd party identifier for the user + #[serde(skip_serializing_if = "Option::is_none")] + pub address: Option + } + response { + pub access_token: String, + pub home_server: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub refresh_token: Option, + pub user_id: String, + } } /// Possible login mediums for 3rd party ID #[derive(Clone, Debug, Deserialize, Serialize)] pub enum LoginMedium { #[serde(rename = "email")] - Email + Email, } /// Possible kinds of login #[derive(Clone, Debug, Deserialize, Serialize)] pub enum LoginKind { #[serde(rename = "m.login.password")] - Password + Password, } } @@ -56,14 +57,15 @@ pub mod logout { use ruma_api_macros::ruma_api; ruma_api! { - metadata { - description: "Log out of the homeserver.", - method: ::Method::Post, - name: "logout", - path: "/_matrix/client/r0/logout", - rate_limited: false, - requires_authentication: true, + metadata { + description: "Log out of the homeserver.", + method: Method::Post, + name: "logout", + path: "/_matrix/client/r0/logout", + rate_limited: false, + requires_authentication: true, + } + request {} + response {} } - request {} - response {} } diff --git a/src/r0/sync.rs b/src/r0/sync.rs index cfa22a5a..87ff903a 100644 --- a/src/r0/sync.rs +++ b/src/r0/sync.rs @@ -20,7 +20,7 @@ pub mod get_state_events { pub room_id: RoomId, } response { - pub Vec, + pub room_state: Vec, } } } @@ -28,12 +28,13 @@ pub mod get_state_events { /// [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_api_macros::ruma_api; - use ruma_identifiers::{RoomId, EventType}; + use ruma_identifiers::RoomId; + use ruma_events::EventType; ruma_api! { metadata { description: "Get state events of a given type associated with the empty key.", - method: ::Method::Get, + method: Method::Get, name: "get_state_events_for_empty_key", path: "/_matrix/client/r0/rooms/:room_id/state/:event_type", rate_limited: false, @@ -61,7 +62,7 @@ pub mod get_state_events_for_key { ruma_api! { metadata { description: "Get state events associated with a given key.", - method: ::Method::Get, + method: Method::Get, name: "get_state_events_for_key", path: "/_matrix/client/r0/rooms/:room_id/state/:event_type/:state_key", rate_limited: false, @@ -70,7 +71,7 @@ pub mod get_state_events_for_key { request { /// The room to look up the state in. #[ruma_api(path)] - pub room_id: RoomID, + pub room_id: RoomId, /// The type of state to look up. #[ruma_api(path)] pub event_type: String, @@ -93,7 +94,7 @@ pub mod get_member_events { ruma_api! { metadata { description: "Get membership events for a room.", - method: ::Method::Get, + method: Method::Get, name: "get_member_events", path: "/_matrix/client/r0/rooms/:room_id/members", rate_limited: false, @@ -105,7 +106,7 @@ pub mod get_member_events { request { /// The room to look up the state in. #[ruma_api(path)] - pub room_id: RoomID, + pub room_id: RoomId, } response { pub chunk: Vec @@ -122,7 +123,7 @@ pub mod get_message_events { ruma_api! { metadata { description: "Get message events for a room.", - method: ::Method::Get, + method: Method::Get, name: "get_message_events", path: "/_matrix/client/r0/rooms/:room_id/messages", rate_limited: false, @@ -133,7 +134,7 @@ pub mod get_message_events { // 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, + 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. @@ -155,6 +156,14 @@ pub mod get_message_events { pub end: String, } } + + #[derive(Clone, Debug, Deserialize, Serialize)] + pub enum Direction { + #[serde(rename="b")] + Backward, + #[serde(rename="f")] + Forward, + } } /// [GET /_matrix/client/r0/sync](https://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-r0-sync) @@ -170,7 +179,7 @@ pub mod sync_events { ruma_api! { metadata { description: "Get all new events from all rooms since the last sync or a given point of time.", - method: ::Method::Get, + method: Method::Get, name: "sync", path: "/_matrix/client/r0/sync", rate_limited: false, @@ -195,9 +204,6 @@ pub mod sync_events { } } - /// Details about this API endpoint. - #[derive(Clone, Copy, Debug)] - pub struct Endpoint; /// Whether to set presence or not during sync. #[derive(Clone, Debug, Deserialize, Serialize)] @@ -290,12 +296,4 @@ pub mod sync_events { pub struct Presence { pub events: only::Event, } - - /// This API endpoint's reponse. - #[derive(Clone, Debug, Deserialize, Serialize)] - pub struct Response { - pub next_batch: String, - pub rooms: Rooms, - pub presence: Presence, - } } From 0a997d28a45eb274dbd6cfb81b125eef8c891e84 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sat, 1 Jul 2017 20:50:39 +1000 Subject: [PATCH 6/8] Remove unused serde attributes --- src/r0/config.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/r0/config.rs b/src/r0/config.rs index 90b467f0..31b5e844 100644 --- a/src/r0/config.rs +++ b/src/r0/config.rs @@ -24,7 +24,6 @@ pub mod set_room_account_data { /// /// Custom types should be namespaced to avoid clashes. #[ruma_api(path)] - #[serde(rename = "type")] pub event_type: String, /// The ID of the room to set account_data on. #[ruma_api(path)] @@ -64,7 +63,6 @@ pub mod set_global_account_data { /// /// Custom types should be namespaced to avoid clashes. #[ruma_api(path)] - #[serde(rename = "type")] pub event_type: String, /// The ID of the user to set account_data for. /// From 5c43a32b007ccd77298136c4a9dd2ce31644f91f Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sun, 2 Jul 2017 06:01:12 +1000 Subject: [PATCH 7/8] Use ruma-api-macros for the create_room endpoint --- src/lib.rs | 2 +- src/r0/room.rs | 97 +++++++++++++++++--------------------------------- 2 files changed, 34 insertions(+), 65 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f2f6c29e..625d1a56 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,7 +33,7 @@ pub mod r0 { // pub mod push; // pub mod receipt; // pub mod redact; -// pub mod room; + pub mod room; // pub mod search; pub mod send; // pub mod server; diff --git a/src/r0/room.rs b/src/r0/room.rs index 38f2f62d..de6fbf13 100644 --- a/src/r0/room.rs +++ b/src/r0/room.rs @@ -3,26 +3,40 @@ /// [POST /_matrix/client/r0/createRoom](https://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-createroom) pub mod create_room { use ruma_identifiers::{RoomId, UserId}; + use ruma_api_macros::ruma_api; - /// The request type. - #[derive(Clone, Debug, Deserialize, Serialize)] - pub struct BodyParams { - #[serde(skip_serializing_if = "Option::is_none")] - pub creation_content: Option, - #[serde(skip_serializing_if = "Vec::is_empty")] - #[serde(default)] - pub invite: Vec, - #[serde(skip_serializing_if = "Option::is_none")] - pub name: Option, - #[serde(skip_serializing_if = "Option::is_none")] - pub preset: Option, - #[serde(skip_serializing_if = "Option::is_none")] - pub room_alias_name: Option, - #[serde(skip_serializing_if = "Option::is_none")] - pub topic: Option, - #[serde(skip_serializing_if = "Option::is_none")] - pub visibility: Option, // TODO: should be an enum ["public", "private"] - // TODO: missing `invite_3pid`, `initial_state` + ruma_api! { + metadata { + description: "Create a new room.", + method: Method::Post, + name: "create_room", + path: "/_matrix/client/r0/createRoom", + rate_limited: false, + requires_authentication: true, + } + + request { + #[serde(skip_serializing_if = "Option::is_none")] + pub creation_content: Option, + #[serde(skip_serializing_if = "Vec::is_empty")] + #[serde(default)] + pub invite: Vec, + #[serde(skip_serializing_if = "Option::is_none")] + pub name: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub preset: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub room_alias_name: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub topic: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub visibility: Option, // TODO: should be an enum ["public", "private"] + // TODO: missing `invite_3pid`, `initial_state` + } + + response { + pub room_id: RoomId, + } } /// Extra options to be added to the `m.room.create` event. @@ -33,16 +47,6 @@ pub mod create_room { pub federate: Option, } - /// Details about this API endpoint. - #[derive(Clone, Copy, Debug)] - pub struct Endpoint; - - /// The response type. - #[derive(Clone, Debug, Deserialize, Serialize)] - pub struct Response { - pub room_id: RoomId, - } - /// A convenience parameter for setting a few default state events. #[derive(Clone, Copy, Debug, Deserialize, Serialize)] pub enum RoomPreset { @@ -56,39 +60,4 @@ pub mod create_room { #[serde(rename="trusted_private_chat")] TrustedPrivateChat, } - - impl ::Endpoint for Endpoint { - type BodyParams = BodyParams; - type PathParams = (); - type QueryParams = (); - type Response = Response; - - fn method() -> ::Method { - ::Method::Post - } - - fn request_path(_params: Self::PathParams) -> String { - Self::router_path().to_string() - } - - fn router_path() -> &'static str { - "/_matrix/client/r0/createRoom" - } - - fn name() -> &'static str { - "create_room" - } - - fn description() -> &'static str { - "Create a new room." - } - - fn requires_authentication() -> bool { - true - } - - fn rate_limited() -> bool { - false - } - } } From 351a6dbd0ceb1aedcb0af80a987dda8bdd113311 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sun, 2 Jul 2017 06:42:48 +1000 Subject: [PATCH 8/8] Add event body to send requests --- src/r0/send.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/r0/send.rs b/src/r0/send.rs index cd6c7a12..ddf26bcc 100644 --- a/src/r0/send.rs +++ b/src/r0/send.rs @@ -5,6 +5,7 @@ pub mod send_state_event_for_empty_key { use ruma_api_macros::ruma_api; use ruma_identifiers::{RoomId, EventId}; use ruma_events::EventType; + use serde_json::Value; ruma_api! { metadata { @@ -23,6 +24,9 @@ pub mod send_state_event_for_empty_key { /// The type of event to send. #[ruma_api(path)] pub event_type: EventType, + /// The event's content. + #[ruma_api(body)] + pub data: Value, } response { @@ -37,6 +41,7 @@ pub mod send_state_event_for_key { use ruma_api_macros::ruma_api; use ruma_identifiers::{RoomId, EventId}; use ruma_events::EventType; + use serde_json::Value; ruma_api! { metadata { @@ -58,6 +63,9 @@ pub mod send_state_event_for_key { /// The state_key for the state to send. Defaults to the empty string. #[ruma_api(path)] pub state_key: String, + /// The event's content. + #[ruma_api(body)] + pub data: Value, } response { @@ -72,6 +80,7 @@ pub mod send_message_event { use ruma_api_macros::ruma_api; use ruma_identifiers::{RoomId, EventId}; use ruma_events::EventType; + use ruma_events::room::message::MessageEventContent; ruma_api! { metadata { @@ -97,6 +106,9 @@ pub mod send_message_event { /// idempotency of requests. #[ruma_api(path)] pub txn_id: String, + /// The event's content. + #[ruma_api(body)] + pub data: MessageEventContent, } response {