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)
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
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,
}
impl ::Endpoint for Endpoint {
type BodyParams = ();
type PathParams = PathParams;
type QueryParams = ();
type Response = Vec<only::StateEvent>;
fn method() -> ::Method {
::Method::Get
request {
#[ruma_api(path)]
pub room_id: RoomId,
}
fn request_path(params: Self::PathParams) -> String {
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
response {
pub Vec<only::StateEvent>,
}
}
}
/// [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 {
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,
}
request {
/// The room to query for events
#[ruma_api(path)]
pub room_id: RoomId,
pub event_type: String
/// The type of state to look up
#[ruma_api(path)]
pub event_type: EventType,
}
impl ::Endpoint for Endpoint {
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
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,
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,
}
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,
}
impl ::Endpoint for Endpoint {
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
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<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 {
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<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)
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
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,
}
#[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 {
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<String>,
/// 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<u64>
pub limit: Option<u64>,
}
/// This API endpoint's reponse.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Response {
response {
pub start: String,
pub chunk: Vec<only::RoomEvent>,
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
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<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.
#[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<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
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Rooms {
pub leave: HashMap<RoomId, LeftRoom>,
pub join: HashMap<RoomId, JoinedRoom>,
pub invite: HashMap<RoomId, InvitedRoom>
pub invite: HashMap<RoomId, InvitedRoom>,
}
/// 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,
}
}