From ed47f9eb1cda75c28c94b46d82e9ccbf1f546f32 Mon Sep 17 00:00:00 2001 From: Jimmy Cuadra Date: Tue, 23 Jul 2019 00:36:26 -0700 Subject: [PATCH 01/27] ruma-federation-api --- .gitignore | 2 ++ .travis.yml | 15 +++++++++++++++ Cargo.toml | 15 +++++++++++++++ LICENSE | 19 +++++++++++++++++++ README.md | 14 ++++++++++++++ src/lib.rs | 0 6 files changed, 65 insertions(+) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 Cargo.toml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 src/lib.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..fa8d85ac --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +Cargo.lock +target diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..a9eb5899 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,15 @@ +language: "rust" +before_script: + - "rustup component add rustfmt" + - "rustup component add clippy" +script: + - "cargo fmt --all -- --check" + - "cargo clippy --all-targets --all-features -- -D warnings" + - "cargo build --verbose" + - "cargo test --verbose" +notifications: + email: false + irc: + channels: + - secure: "QCqLuvSHYRxTj28ze9lu5K4mgyAQBfBQA105GlVJLcG4x2rfkUXJR538qXuIt5IUBwXePgu8RzXRLjKd3Ev0bFnCDGzK2OTXIBg2517vuLmmhbJ97yVtLk6F0nfQ8IPMJq67EkfB5ecZYgNHmmzv1/+F4M7xU0BC4QwkpJparYTwJLrsC8B/pPQds45aZFmkfbe5Mh5nNcFlx7ueP/FQLrOxPWthgpKBZreS8PX7l/6uQYZdu0HmX8xP6HZk8GAdLj8oL8H2WcJ0PrmiQ7JdCJ+V50WgiB7beiqaRieag75XWmIxNpEF2s12dZD8FEUC2aDPyysGT/SUU8gAqsI6DUDwySeCEoad7dpr+CfmOu621X9Yw2ukOkQXH2EbHq/gKcr5GOaIteh0ZD56iZmzAxmo/4UOgJ1qaP14o936ry5+Vz4QP7Iphg9y0owVRB7BZPszN8PS9g70NDkd7Pvwe8V7OypckSHcIGRhAI0iKV8i4FhNNBJbKVcyjGUQpq2YsAQcBGB07JrkrfDfwkiuy2Eh6dzkv9U5VsMKSZtSLpKNAr/ZSGOn74iHVxlRrC4LKmGO2CqmPCW/7g2cBqH7cogWVJS+v/FRuec40ueKiarh8daacAmBggKuCsAqMJbhm3AF7Q8NWom+44Q+VO1upwTu9hT9laQq08HMXOT/uP8=" + use_notice: true diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 00000000..ca29e539 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,15 @@ +[package] +authors = ["Jimmy Cuadra "] +categories = ["api-bindings", "web-programming"] +description = "Types for the endpoints in the Matrix server-server API." +documentation = "https://docs.rs/ruma-federation-api" +edition = "2018" +homepage = "https://github.com/ruma/ruma-federation-api" +keywords = ["matrix", "chat", "messaging", "ruma"] +license = "MIT" +name = "ruma-federation-api" +readme = "README.md" +repository = "https://github.com/ruma/ruma-federation-api" +version = "0.1.0" + +[dependencies] diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..1ea21301 --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2019 Jimmy Cuadra + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 00000000..1a84a6ec --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +# ruma-federation-api + +[![Build Status](https://travis-ci.org/ruma/ruma-federation-api.svg?branch=master)](https://travis-ci.org/ruma/ruma-federation-api) + +**ruma-federation-api** contains serializable types for the requests and responses for each endpoint in the [Matrix](https://matrix.org/) federation API specification. +These types can be shared by client and server code. + +## Minimum Rust version + +ruma-federation-api requires Rust 1.34 or later. + +## License + +[MIT](http://opensource.org/licenses/MIT) diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 00000000..e69de29b From 5e8103f007a412e48d4dc85dc22ab4b2ba68717e Mon Sep 17 00:00:00 2001 From: Jimmy Cuadra Date: Tue, 23 Jul 2019 09:58:36 -0700 Subject: [PATCH 02/27] Run cargo-audit on CI. --- .travis.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.travis.yml b/.travis.yml index a9eb5899..b55f94c6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,12 @@ language: "rust" +cache: "cargo" before_script: - "rustup component add rustfmt" - "rustup component add clippy" + - "cargo install --force cargo-audit" + - "cargo generate-lockfile" script: + - "cargo audit" - "cargo fmt --all -- --check" - "cargo clippy --all-targets --all-features -- -D warnings" - "cargo build --verbose" From f6abbf426253afac405daa6187b875f23b4cd98f Mon Sep 17 00:00:00 2001 From: Jimmy Cuadra Date: Sat, 3 Aug 2019 14:02:08 -0700 Subject: [PATCH 03/27] Only build PRs and the master branch on CI. --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index b55f94c6..deccdfd1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,6 +11,7 @@ script: - "cargo clippy --all-targets --all-features -- -D warnings" - "cargo build --verbose" - "cargo test --verbose" +if: "type != push OR (tag IS blank AND branch = master)" notifications: email: false irc: From f5c870bd5646f6d8438d10d65ec86444008561f4 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Fri, 1 Nov 2019 11:12:31 +0100 Subject: [PATCH 04/27] Re-run cargo fmt --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index e69de29b..8b137891 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -0,0 +1 @@ + From 2877fde4ad452f186094e76fe5302b8b7efd60a5 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Fri, 1 Nov 2019 11:13:45 +0100 Subject: [PATCH 05/27] Update travis config, README.md --- .travis.yml | 40 +++++++++++++++++++++++++++++++--------- README.md | 2 +- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/.travis.yml b/.travis.yml index deccdfd1..842713b9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,16 +1,38 @@ language: "rust" cache: "cargo" +rust: + - 1.34.2 + - stable + - beta + - nightly +jobs: + allow_failures: + - rust: nightly + fast_finish: true + before_script: - - "rustup component add rustfmt" - - "rustup component add clippy" - - "cargo install --force cargo-audit" - - "cargo generate-lockfile" + - rustup component add rustfmt + - | + if [ "$TRAVIS_RUST_VERSION" != "1.34.2" ]; then + rustup component add clippy + fi + - | + if [ "$TRAVIS_RUST_VERSION" == "stable" ]; then + cargo install --force cargo-audit + fi + - cargo generate-lockfile script: - - "cargo audit" - - "cargo fmt --all -- --check" - - "cargo clippy --all-targets --all-features -- -D warnings" - - "cargo build --verbose" - - "cargo test --verbose" + - | + if [ "$TRAVIS_RUST_VERSION" == "stable" ]; then + cargo audit + fi + - cargo fmt -- --check + - | + if [ "$TRAVIS_RUST_VERSION" != "1.34.2" ]; then + cargo clippy --all-targets --all-features -- -D warnings + fi + - cargo build --verbose + - cargo test --verbose if: "type != push OR (tag IS blank AND branch = master)" notifications: email: false diff --git a/README.md b/README.md index 1a84a6ec..e49dcaef 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ These types can be shared by client and server code. ## Minimum Rust version -ruma-federation-api requires Rust 1.34 or later. +ruma-federation-api requires Rust 1.34.2 or later. ## License From dbc8312730e03638045175d4c53bc2b86872b316 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 1 Apr 2020 21:27:10 +0200 Subject: [PATCH 06/27] Add PDU type for v3 rooms and above --- Cargo.toml | 7 ++++++- src/lib.rs | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index ca29e539..5cc90888 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -authors = ["Jimmy Cuadra "] +authors = ["Jonas Platte "] categories = ["api-bindings", "web-programming"] description = "Types for the endpoints in the Matrix server-server API." documentation = "https://docs.rs/ruma-federation-api" @@ -13,3 +13,8 @@ repository = "https://github.com/ruma/ruma-federation-api" version = "0.1.0" [dependencies] +js_int = "0.1.3" +ruma-events = "0.18.0" +ruma-identifiers = "0.14.1" +serde = { version = "1.0.105", features = ["derive"] } +serde_json = "1.0.50" diff --git a/src/lib.rs b/src/lib.rs index 8b137891..b2189a10 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,40 @@ +// TODO +//!#[warn(missing_docs)] +use std::collections::HashMap; + +use js_int::UInt; +use ruma_events::EventType; +use ruma_identifiers::{EventId, RoomId, UserId}; +use serde::{Deserialize, Serialize}; +use serde_json::Value as JsonValue; + +#[derive(Deserialize, Serialize)] +pub struct RoomV3Pdu { + pub room_id: RoomId, + pub sender: UserId, + pub origin: String, + pub origin_server_ts: UInt, + + // TODO: Replace with event content collection from ruma-events once that exists + #[serde(rename = "type")] + pub kind: EventType, + pub content: JsonValue, + + #[serde(skip_serializing_if = "Option::is_none")] + pub state_key: Option, + pub prev_events: Vec, + pub depth: UInt, + pub auth_events: Vec, + #[serde(skip_serializing_if = "Option::is_none")] + pub redacts: Option, + #[serde(default, skip_serializing_if = "serde_json::Map::is_empty")] + pub unsigned: serde_json::Map, + pub hashes: EventHash, + pub signatures: HashMap>, +} + +#[derive(Deserialize, Serialize)] +pub struct EventHash { + pub sha256: String, +} From cadada47662cdd4907dc6503149a447bb51d92c8 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 1 Apr 2020 21:27:39 +0200 Subject: [PATCH 07/27] Set version to 0.0.1 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 5cc90888..d348225d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ license = "MIT" name = "ruma-federation-api" readme = "README.md" repository = "https://github.com/ruma/ruma-federation-api" -version = "0.1.0" +version = "0.0.1" [dependencies] js_int = "0.1.3" From 604f32c5d1407962a082434f37013f89deca232b Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 1 Apr 2020 21:55:35 +0200 Subject: [PATCH 08/27] Fix accidental doc comment --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index b2189a10..8ecf4e17 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ // TODO -//!#[warn(missing_docs)] +// !#[warn(missing_docs)] use std::collections::HashMap; From de7045fa9d91810f4bec7248e33cd08a962a0628 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Thu, 2 Apr 2020 11:50:53 +0200 Subject: [PATCH 09/27] Add docs --- src/lib.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8ecf4e17..bd98af77 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ -// TODO -// !#[warn(missing_docs)] +//! (De)serializable types for the matrix server-server protocol. + +#![warn(missing_docs)] use std::collections::HashMap; @@ -9,32 +10,53 @@ use ruma_identifiers::{EventId, RoomId, UserId}; use serde::{Deserialize, Serialize}; use serde_json::Value as JsonValue; +/// A 'persistent data unit' (event) for room versions 3 and beyond. #[derive(Deserialize, Serialize)] pub struct RoomV3Pdu { + /// The room this event belongs to. pub room_id: RoomId, + /// The user id of the user who sent this event. pub sender: UserId, + /// The `server_name` of the homeserver that created this event. pub origin: String, + /// Timestamp (milliseconds since the UNIX epoch) on originating homeserver + /// of when this event was created. pub origin_server_ts: UInt, // TODO: Replace with event content collection from ruma-events once that exists + /// The event's type. #[serde(rename = "type")] pub kind: EventType, + /// The event's content. pub content: JsonValue, + /// A key that determines which piece of room state the event represents. #[serde(skip_serializing_if = "Option::is_none")] pub state_key: Option, + /// Event IDs for the most recent events in the room that the homeserver was + /// aware of when it created this event. pub prev_events: Vec, + /// The maximum depth of the `prev_events`, plus one. pub depth: UInt, + /// Event IDs for the authorization events that would allow this event to be + /// in the room. pub auth_events: Vec, + /// For redaction events, the ID of the event being redacted. #[serde(skip_serializing_if = "Option::is_none")] pub redacts: Option, + /// Additional data added by the origin server but not covered by the + /// signatures. #[serde(default, skip_serializing_if = "serde_json::Map::is_empty")] pub unsigned: serde_json::Map, + /// Content hashes of the PDU. pub hashes: EventHash, + /// Signatures for the PDU. pub signatures: HashMap>, } +/// Content hashes of a PDU. #[derive(Deserialize, Serialize)] pub struct EventHash { + /// The SHA-256 hash. pub sha256: String, } From 28ed87aba1922b646580e2a44cb9501eb0bf2e60 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Fri, 3 Apr 2020 19:24:53 +0200 Subject: [PATCH 10/27] CI: Disable IRC notifications --- .travis.yml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 842713b9..cc2b6e20 100644 --- a/.travis.yml +++ b/.travis.yml @@ -34,9 +34,3 @@ script: - cargo build --verbose - cargo test --verbose if: "type != push OR (tag IS blank AND branch = master)" -notifications: - email: false - irc: - channels: - - secure: "QCqLuvSHYRxTj28ze9lu5K4mgyAQBfBQA105GlVJLcG4x2rfkUXJR538qXuIt5IUBwXePgu8RzXRLjKd3Ev0bFnCDGzK2OTXIBg2517vuLmmhbJ97yVtLk6F0nfQ8IPMJq67EkfB5ecZYgNHmmzv1/+F4M7xU0BC4QwkpJparYTwJLrsC8B/pPQds45aZFmkfbe5Mh5nNcFlx7ueP/FQLrOxPWthgpKBZreS8PX7l/6uQYZdu0HmX8xP6HZk8GAdLj8oL8H2WcJ0PrmiQ7JdCJ+V50WgiB7beiqaRieag75XWmIxNpEF2s12dZD8FEUC2aDPyysGT/SUU8gAqsI6DUDwySeCEoad7dpr+CfmOu621X9Yw2ukOkQXH2EbHq/gKcr5GOaIteh0ZD56iZmzAxmo/4UOgJ1qaP14o936ry5+Vz4QP7Iphg9y0owVRB7BZPszN8PS9g70NDkd7Pvwe8V7OypckSHcIGRhAI0iKV8i4FhNNBJbKVcyjGUQpq2YsAQcBGB07JrkrfDfwkiuy2Eh6dzkv9U5VsMKSZtSLpKNAr/ZSGOn74iHVxlRrC4LKmGO2CqmPCW/7g2cBqH7cogWVJS+v/FRuec40ueKiarh8daacAmBggKuCsAqMJbhm3AF7Q8NWom+44Q+VO1upwTu9hT9laQq08HMXOT/uP8=" - use_notice: true From 4a69bbf0137edb806c12c0683aa83718f5bb62f1 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Fri, 3 Apr 2020 20:02:58 +0200 Subject: [PATCH 11/27] Bump MSRV --- .travis.yml | 6 +++--- README.md | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index cc2b6e20..7d848609 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: "rust" cache: "cargo" rust: - - 1.34.2 + - 1.36.0 - stable - beta - nightly @@ -13,7 +13,7 @@ jobs: before_script: - rustup component add rustfmt - | - if [ "$TRAVIS_RUST_VERSION" != "1.34.2" ]; then + if [ "$TRAVIS_RUST_VERSION" != "1.36.0" ]; then rustup component add clippy fi - | @@ -28,7 +28,7 @@ script: fi - cargo fmt -- --check - | - if [ "$TRAVIS_RUST_VERSION" != "1.34.2" ]; then + if [ "$TRAVIS_RUST_VERSION" != "1.36.0" ]; then cargo clippy --all-targets --all-features -- -D warnings fi - cargo build --verbose diff --git a/README.md b/README.md index e49dcaef..4b830201 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ These types can be shared by client and server code. ## Minimum Rust version -ruma-federation-api requires Rust 1.34.2 or later. +ruma-federation-api requires Rust 1.36.0 or later. ## License From 4845474294df95cb5168d3ab0aebb7bab4ff1284 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 15 Apr 2020 22:20:01 +0200 Subject: [PATCH 12/27] Bump dependencies --- Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d348225d..8d41de8c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,8 +13,8 @@ repository = "https://github.com/ruma/ruma-federation-api" version = "0.0.1" [dependencies] -js_int = "0.1.3" +js_int = "0.1.4" ruma-events = "0.18.0" ruma-identifiers = "0.14.1" -serde = { version = "1.0.105", features = ["derive"] } -serde_json = "1.0.50" +serde = { version = "1.0.106", features = ["derive"] } +serde_json = "1.0.51" From 1d6bf1979b34ce578b9596283e60b253dba96d00 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 15 Apr 2020 22:37:42 +0200 Subject: [PATCH 13/27] Add first endpoints --- Cargo.toml | 1 + src/lib.rs | 3 +++ src/unversioned.rs | 1 + src/unversioned/discover_homeserver.rs | 20 ++++++++++++++++++ src/v1.rs | 1 + src/v1/get_server_version.rs | 28 ++++++++++++++++++++++++++ 6 files changed, 54 insertions(+) create mode 100644 src/unversioned.rs create mode 100644 src/unversioned/discover_homeserver.rs create mode 100644 src/v1.rs create mode 100644 src/v1/get_server_version.rs diff --git a/Cargo.toml b/Cargo.toml index 8d41de8c..89501bb7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ version = "0.0.1" [dependencies] js_int = "0.1.4" +ruma-api = "0.15.0" ruma-events = "0.18.0" ruma-identifiers = "0.14.1" serde = { version = "1.0.106", features = ["derive"] } diff --git a/src/lib.rs b/src/lib.rs index bd98af77..be7c5164 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,6 +10,9 @@ use ruma_identifiers::{EventId, RoomId, UserId}; use serde::{Deserialize, Serialize}; use serde_json::Value as JsonValue; +mod unversioned; +mod v1; + /// A 'persistent data unit' (event) for room versions 3 and beyond. #[derive(Deserialize, Serialize)] pub struct RoomV3Pdu { diff --git a/src/unversioned.rs b/src/unversioned.rs new file mode 100644 index 00000000..13df6416 --- /dev/null +++ b/src/unversioned.rs @@ -0,0 +1 @@ +mod discover_homeserver; diff --git a/src/unversioned/discover_homeserver.rs b/src/unversioned/discover_homeserver.rs new file mode 100644 index 00000000..ed8f9f80 --- /dev/null +++ b/src/unversioned/discover_homeserver.rs @@ -0,0 +1,20 @@ +use ruma_api::ruma_api; + +ruma_api! { + metadata { + description: "Get discovery information about the domain.", + method: GET, + name: "discover_homeserver", + path: "/.well-known/matrix/server", + rate_limited: false, + requires_authentication: false, + } + + request {} + + response { + /// The server name to delegate server-server communciations to, with optional port. + #[serde(rename = "m.homeserver")] + pub homeserver: String, + } +} diff --git a/src/v1.rs b/src/v1.rs new file mode 100644 index 00000000..3f346b1e --- /dev/null +++ b/src/v1.rs @@ -0,0 +1 @@ +mod get_server_version; diff --git a/src/v1/get_server_version.rs b/src/v1/get_server_version.rs new file mode 100644 index 00000000..1fb9501b --- /dev/null +++ b/src/v1/get_server_version.rs @@ -0,0 +1,28 @@ +use ruma_api::ruma_api; +use serde::{Deserialize, Serialize}; + +ruma_api! { + metadata { + description: "Get the implementation name and version of this homeserver.", + method: GET, + name: "discover_homeserver", + path: "/.well-known/matrix/server", + rate_limited: false, + requires_authentication: false, + } + + request {} + + response { + /// Information about the homeserver implementation + server: Server, + } +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Server { + /// Arbitrary name that identify this implementation. + name: Option, + /// Version of this implementation. The version format depends on the implementation. + version: Option, +} From 32c161f81e319b17482cccfe81710e8e9314a7a3 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Thu, 16 Apr 2020 01:47:45 +0200 Subject: [PATCH 14/27] Bump MSRV --- .travis.yml | 6 +++--- README.md | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7d848609..891cecaa 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: "rust" cache: "cargo" rust: - - 1.36.0 + - 1.40.0 - stable - beta - nightly @@ -13,7 +13,7 @@ jobs: before_script: - rustup component add rustfmt - | - if [ "$TRAVIS_RUST_VERSION" != "1.36.0" ]; then + if [ "$TRAVIS_RUST_VERSION" != "1.40.0" ]; then rustup component add clippy fi - | @@ -28,7 +28,7 @@ script: fi - cargo fmt -- --check - | - if [ "$TRAVIS_RUST_VERSION" != "1.36.0" ]; then + if [ "$TRAVIS_RUST_VERSION" != "1.40.0" ]; then cargo clippy --all-targets --all-features -- -D warnings fi - cargo build --verbose diff --git a/README.md b/README.md index 4b830201..2040086a 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ These types can be shared by client and server code. ## Minimum Rust version -ruma-federation-api requires Rust 1.36.0 or later. +ruma-federation-api requires Rust 1.40.0 or later. ## License From 2285543d4d5c64803ef3dda1e8312552c48e500c Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sat, 18 Apr 2020 00:28:59 +0200 Subject: [PATCH 15/27] Update dependencies --- Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 89501bb7..47053ee0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,8 +14,8 @@ version = "0.0.1" [dependencies] js_int = "0.1.4" -ruma-api = "0.15.0" -ruma-events = "0.18.0" -ruma-identifiers = "0.14.1" +ruma-api = "0.16.0-rc.1" +ruma-events = "0.19.0-rc.1" +ruma-identifiers = "0.15.1" serde = { version = "1.0.106", features = ["derive"] } serde_json = "1.0.51" From 5448c650f0a583382152d0f43f2dcf720d495390 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sun, 19 Apr 2020 16:20:27 +0200 Subject: [PATCH 16/27] Replace HashMap with BTreeMap --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index be7c5164..ab8329b8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,7 @@ #![warn(missing_docs)] -use std::collections::HashMap; +use std::collections::BTreeMap; use js_int::UInt; use ruma_events::EventType; @@ -54,7 +54,7 @@ pub struct RoomV3Pdu { /// Content hashes of the PDU. pub hashes: EventHash, /// Signatures for the PDU. - pub signatures: HashMap>, + pub signatures: BTreeMap>, } /// Content hashes of a PDU. From ea4ca46cf452f1631be9ba23df63c53f48c96e65 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 22 Apr 2020 15:03:30 +0200 Subject: [PATCH 17/27] Update ruma-identifiers 0.16.0 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 47053ee0..bdb4b197 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,6 @@ version = "0.0.1" js_int = "0.1.4" ruma-api = "0.16.0-rc.1" ruma-events = "0.19.0-rc.1" -ruma-identifiers = "0.15.1" +ruma-identifiers = "0.16.0" serde = { version = "1.0.106", features = ["derive"] } serde_json = "1.0.51" From df634a2a02522e5b71c6b01af840cf343488f670 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 22 Apr 2020 15:09:28 +0200 Subject: [PATCH 18/27] Make unversioned, v1 modules public --- src/lib.rs | 4 ++-- src/unversioned.rs | 2 ++ src/v1.rs | 2 ++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ab8329b8..ccdc968e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,8 +10,8 @@ use ruma_identifiers::{EventId, RoomId, UserId}; use serde::{Deserialize, Serialize}; use serde_json::Value as JsonValue; -mod unversioned; -mod v1; +pub mod unversioned; +pub mod v1; /// A 'persistent data unit' (event) for room versions 3 and beyond. #[derive(Deserialize, Serialize)] diff --git a/src/unversioned.rs b/src/unversioned.rs index 13df6416..bc11c34a 100644 --- a/src/unversioned.rs +++ b/src/unversioned.rs @@ -1 +1,3 @@ +//! Endpoints that cannot change with new versions of the Matrix specification. + mod discover_homeserver; diff --git a/src/v1.rs b/src/v1.rs index 3f346b1e..d0381b80 100644 --- a/src/v1.rs +++ b/src/v1.rs @@ -1 +1,3 @@ +//! Endpoints for the r0.1.x versions of the federation API specification. + mod get_server_version; From 75a9065fc68de1d0fd66d756c1739c951e8d5f2d Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 22 Apr 2020 22:05:22 +0200 Subject: [PATCH 19/27] Update ruma-api, ruma-events --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index bdb4b197..a695911c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,8 +14,8 @@ version = "0.0.1" [dependencies] js_int = "0.1.4" -ruma-api = "0.16.0-rc.1" -ruma-events = "0.19.0-rc.1" +ruma-api = "0.16.0-rc.3" +ruma-events = "0.21.0-beta.1" ruma-identifiers = "0.16.0" serde = { version = "1.0.106", features = ["derive"] } serde_json = "1.0.51" From bf5ac6314b795389fb9b7d952842376629ec487e Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Fri, 24 Apr 2020 21:45:11 +0200 Subject: [PATCH 20/27] Switch CI from travis to builds.sr.ht --- .builds/beta.yml | 27 +++++++++++++++++++++++++++ .builds/msrv.yml | 16 ++++++++++++++++ .builds/nightly.yml | 32 ++++++++++++++++++++++++++++++++ .builds/stable.yml | 29 +++++++++++++++++++++++++++++ .travis.yml | 36 ------------------------------------ README.md | 2 -- 6 files changed, 104 insertions(+), 38 deletions(-) create mode 100644 .builds/beta.yml create mode 100644 .builds/msrv.yml create mode 100644 .builds/nightly.yml create mode 100644 .builds/stable.yml delete mode 100644 .travis.yml diff --git a/.builds/beta.yml b/.builds/beta.yml new file mode 100644 index 00000000..39aad8ea --- /dev/null +++ b/.builds/beta.yml @@ -0,0 +1,27 @@ +image: archlinux +packages: + - rustup +sources: + - https://github.com/ruma/ruma-federation-api +tasks: + - rustup: | + # We specify --profile minimal because we'd otherwise download docs + rustup toolchain install beta --profile minimal -c rustfmt -c clippy + rustup default beta + - test: | + cd ruma-federation-api + + # We don't want the build to stop on individual failure of independent + # tools, so capture tool exit codes and set the task exit code manually + set +e + + cargo fmt -- --check + fmt_exit=$? + + cargo clippy --all-targets --all-features -- -D warnings + clippy_exit=$? + + cargo test --verbose + test_exit=$? + + exit $(( $fmt_exit || $clippy_exit || $test_exit )) diff --git a/.builds/msrv.yml b/.builds/msrv.yml new file mode 100644 index 00000000..191fa3d7 --- /dev/null +++ b/.builds/msrv.yml @@ -0,0 +1,16 @@ +image: archlinux +packages: + - rustup +sources: + - https://github.com/ruma/ruma-federation-api +tasks: + - rustup: | + # We specify --profile minimal because we'd otherwise download docs + rustup toolchain install 1.40.0 --profile minimal + rustup default 1.40.0 + - test: | + cd ruma-federation-api + + # Only make sure the code builds with the MSRV. Tests can require later + # Rust versions, don't compile or run them. + cargo build --verbose diff --git a/.builds/nightly.yml b/.builds/nightly.yml new file mode 100644 index 00000000..76f68ffc --- /dev/null +++ b/.builds/nightly.yml @@ -0,0 +1,32 @@ +image: archlinux +packages: + - rustup +sources: + - https://github.com/ruma/ruma-federation-api +tasks: + - rustup: | + rustup toolchain install nightly --profile minimal + rustup default nightly + + # Try installing rustfmt & clippy for nightly, but don't fail the build + # if they are not available + rustup component add rustfmt || true + rustup component add clippy || true + - test: | + cd ruma-federation-api + + # We don't want the build to stop on individual failure of independent + # tools, so capture tool exit codes and set the task exit code manually + set +e + + if ( rustup component list | grep -q rustfmt ); then + cargo fmt -- --check + fi + fmt_exit=$? + + if ( rustup component list | grep -q clippy ); then + cargo clippy --all-targets --all-features -- -D warnings + fi + clippy_exit=$? + + exit $(( $fmt_exit || $clippy_exit )) diff --git a/.builds/stable.yml b/.builds/stable.yml new file mode 100644 index 00000000..c55b377a --- /dev/null +++ b/.builds/stable.yml @@ -0,0 +1,29 @@ +image: archlinux +packages: + - rustup +sources: + - https://github.com/ruma/ruma-federation-api +tasks: + - rustup: | + # We specify --profile minimal because we'd otherwise download docs + rustup toolchain install stable --profile minimal -c rustfmt -c clippy + rustup default stable + - test: | + cd ruma-federation-api + + # We don't want the build to stop on individual failure of independent + # tools, so capture tool exit codes and set the task exit code manually + set +e + + cargo fmt -- --check + fmt_exit=$? + + cargo clippy --all-targets --all-features -- -D warnings + clippy_exit=$? + + cargo test --verbose + test_exit=$? + + exit $(( $fmt_exit || $clippy_exit || $test_exit )) + # TODO: Add audit task once cargo-audit binary releases are available. + # See https://github.com/RustSec/cargo-audit/issues/66 diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 891cecaa..00000000 --- a/.travis.yml +++ /dev/null @@ -1,36 +0,0 @@ -language: "rust" -cache: "cargo" -rust: - - 1.40.0 - - stable - - beta - - nightly -jobs: - allow_failures: - - rust: nightly - fast_finish: true - -before_script: - - rustup component add rustfmt - - | - if [ "$TRAVIS_RUST_VERSION" != "1.40.0" ]; then - rustup component add clippy - fi - - | - if [ "$TRAVIS_RUST_VERSION" == "stable" ]; then - cargo install --force cargo-audit - fi - - cargo generate-lockfile -script: - - | - if [ "$TRAVIS_RUST_VERSION" == "stable" ]; then - cargo audit - fi - - cargo fmt -- --check - - | - if [ "$TRAVIS_RUST_VERSION" != "1.40.0" ]; then - cargo clippy --all-targets --all-features -- -D warnings - fi - - cargo build --verbose - - cargo test --verbose -if: "type != push OR (tag IS blank AND branch = master)" diff --git a/README.md b/README.md index 2040086a..f4ac57de 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,5 @@ # ruma-federation-api -[![Build Status](https://travis-ci.org/ruma/ruma-federation-api.svg?branch=master)](https://travis-ci.org/ruma/ruma-federation-api) - **ruma-federation-api** contains serializable types for the requests and responses for each endpoint in the [Matrix](https://matrix.org/) federation API specification. These types can be shared by client and server code. From 263f2ffc75be6542bd68161e446adf588505fb56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20K=C3=B6sters?= Date: Sun, 26 Apr 2020 16:22:08 +0200 Subject: [PATCH 21/27] Add get_public_rooms and get_server_keys endpoints --- Cargo.toml | 1 + src/lib.rs | 1 + src/unversioned.rs | 2 +- src/unversioned/discover_homeserver.rs | 2 + src/v1.rs | 3 +- src/v1/get_public_rooms.rs | 177 +++++++++++++++++++++++++ src/v1/get_server_version.rs | 14 +- src/v2.rs | 3 + src/v2/get_server_keys.rs | 54 ++++++++ 9 files changed, 251 insertions(+), 6 deletions(-) create mode 100644 src/v1/get_public_rooms.rs create mode 100644 src/v2.rs create mode 100644 src/v2/get_server_keys.rs diff --git a/Cargo.toml b/Cargo.toml index a695911c..99cfe4b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,5 +17,6 @@ js_int = "0.1.4" ruma-api = "0.16.0-rc.3" ruma-events = "0.21.0-beta.1" ruma-identifiers = "0.16.0" +ruma-serde = "0.1.0" serde = { version = "1.0.106", features = ["derive"] } serde_json = "1.0.51" diff --git a/src/lib.rs b/src/lib.rs index ccdc968e..9920920a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,7 @@ use serde_json::Value as JsonValue; pub mod unversioned; pub mod v1; +pub mod v2; /// A 'persistent data unit' (event) for room versions 3 and beyond. #[derive(Deserialize, Serialize)] diff --git a/src/unversioned.rs b/src/unversioned.rs index bc11c34a..5e98cd63 100644 --- a/src/unversioned.rs +++ b/src/unversioned.rs @@ -1,3 +1,3 @@ //! Endpoints that cannot change with new versions of the Matrix specification. -mod discover_homeserver; +pub mod discover_homeserver; diff --git a/src/unversioned/discover_homeserver.rs b/src/unversioned/discover_homeserver.rs index ed8f9f80..2d6f386d 100644 --- a/src/unversioned/discover_homeserver.rs +++ b/src/unversioned/discover_homeserver.rs @@ -1,3 +1,5 @@ +//! [GET /.well-known/matrix/server](https://matrix.org/docs/spec/server_server/r0.1.3#get-well-known-matrix-server) + use ruma_api::ruma_api; ruma_api! { diff --git a/src/v1.rs b/src/v1.rs index d0381b80..94961924 100644 --- a/src/v1.rs +++ b/src/v1.rs @@ -1,3 +1,4 @@ //! Endpoints for the r0.1.x versions of the federation API specification. -mod get_server_version; +pub mod get_public_rooms; +pub mod get_server_version; diff --git a/src/v1/get_public_rooms.rs b/src/v1/get_public_rooms.rs new file mode 100644 index 00000000..c989d008 --- /dev/null +++ b/src/v1/get_public_rooms.rs @@ -0,0 +1,177 @@ +//! [GET /_matrix/federation/v1/publicRooms](https://matrix.org/docs/spec/server_server/r0.1.3#get-matrix-federation-v1-publicrooms) + +use std::fmt; + +use js_int::UInt; +use ruma_api::ruma_api; +use ruma_identifiers::{RoomAliasId, RoomId}; +use serde::{ + de::{MapAccess, Visitor}, + ser::SerializeStruct, + Deserialize, Deserializer, Serialize, Serializer, +}; + +ruma_api! { + metadata { + description: "Gets all the public rooms for the homeserver.", + method: GET, + name: "get_public_rooms", + path: "/_matrix/federation/v1/publicRooms", + rate_limited: false, + requires_authentication: true, + } + + request { + /// The maximum number of rooms to return. Default is no limit. + #[serde(skip_serializing_if = "Option::is_none")] + #[ruma_api(query)] + pub limit: Option, + /// Pagination token from a previous request. + #[serde(skip_serializing_if = "Option::is_none")] + #[ruma_api(query)] + pub since: Option, + /// Network to fetch the public room lists from. + #[serde(flatten, skip_serializing_if = "ruma_serde::is_default")] + #[ruma_api(query)] + pub room_network: RoomNetwork, + } + + response { + /// A paginated chunk of public rooms. + pub chunk: Vec, + /// A pagination token for the response. + #[serde(skip_serializing_if = "Option::is_none")] + pub next_batch: Option, + /// A pagination token that allows fetching previous results. + #[serde(skip_serializing_if = "Option::is_none")] + pub prev_batch: Option, + /// An estimate on the total number of public rooms, if the server has an estimate. + pub total_room_count_estimate: Option, + } +} + +/// A chunk of a room list response, describing one room +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct PublicRoomsChunk { + /// Aliases of the room. + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub aliases: Vec, + /// The canonical alias of the room, if any. + #[serde(skip_serializing_if = "Option::is_none")] + pub canonical_alias: Option, + /// The name of the room, if any. + #[serde(skip_serializing_if = "Option::is_none")] + pub name: Option, + /// The number of members joined to the room. + pub num_joined_members: UInt, + /// The ID of the room. + pub room_id: RoomId, + /// The topic of the room, if any. + #[serde(skip_serializing_if = "Option::is_none")] + pub topic: Option, + /// Whether the room may be viewed by guest users without joining. + pub world_readable: bool, + /// Whether guest users may join the room and participate in it. + /// + /// If they can, they will be subject to ordinary power level rules like any other user. + pub guest_can_join: bool, + /// The URL for the room's avatar, if one is set. + #[serde(skip_serializing_if = "Option::is_none")] + pub avatar_url: Option, +} + +/// Information about which networks/protocols from application services on the +/// homeserver from which to request rooms. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum RoomNetwork { + /// Return rooms from the Matrix network. + Matrix, + /// Return rooms from all the networks/protocols the homeserver knows about. + All, + /// Return rooms from a specific third party network/protocol. + ThirdParty(String), +} + +impl Default for RoomNetwork { + fn default() -> Self { + RoomNetwork::Matrix + } +} + +impl Serialize for RoomNetwork { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + let mut state; + match self { + Self::Matrix => { + state = serializer.serialize_struct("RoomNetwork", 0)?; + } + Self::All => { + state = serializer.serialize_struct("RoomNetwork", 1)?; + state.serialize_field("include_all_networks", &true)?; + } + Self::ThirdParty(network) => { + state = serializer.serialize_struct("RoomNetwork", 1)?; + state.serialize_field("third_party_instance_id", network)?; + } + } + state.end() + } +} + +impl<'de> Deserialize<'de> for RoomNetwork { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + deserializer.deserialize_map(RoomNetworkVisitor) + } +} + +struct RoomNetworkVisitor; +impl<'de> Visitor<'de> for RoomNetworkVisitor { + type Value = RoomNetwork; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("Network selection") + } + + fn visit_map(self, mut access: M) -> Result + where + M: MapAccess<'de>, + { + let mut include_all_networks = false; + let mut third_party_instance_id = None; + while let Some((key, value)) = access.next_entry::()? { + match key.as_str() { + "include_all_networks" => { + include_all_networks = match value.as_bool() { + Some(b) => b, + _ => false, + } + } + "third_party_instance_id" => { + third_party_instance_id = value.as_str().map(|v| v.to_owned()) + } + _ => {} + }; + } + + if include_all_networks { + if third_party_instance_id.is_none() { + Ok(RoomNetwork::All) + } else { + Err(M::Error::custom( + "`include_all_networks = true` and `third_party_instance_id` are mutually exclusive.", + )) + } + } else { + Ok(match third_party_instance_id { + Some(network) => RoomNetwork::ThirdParty(network), + None => RoomNetwork::Matrix, + }) + } + } +} diff --git a/src/v1/get_server_version.rs b/src/v1/get_server_version.rs index 1fb9501b..ea8f2704 100644 --- a/src/v1/get_server_version.rs +++ b/src/v1/get_server_version.rs @@ -1,3 +1,5 @@ +//! [GET /_matrix/federation/v1/version](https://matrix.org/docs/spec/server_server/r0.1.3#get-matrix-federation-v1-version) + use ruma_api::ruma_api; use serde::{Deserialize, Serialize}; @@ -15,14 +17,18 @@ ruma_api! { response { /// Information about the homeserver implementation - server: Server, + #[serde(skip_serializing_if = "Option::is_none")] + pub server: Option, } } #[derive(Clone, Debug, Serialize, Deserialize)] +/// Arbitrary values that identify this implementation. pub struct Server { - /// Arbitrary name that identify this implementation. - name: Option, + /// Arbitrary name that identifies this implementation. + #[serde(skip_serializing_if = "Option::is_none")] + pub name: Option, /// Version of this implementation. The version format depends on the implementation. - version: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub version: Option, } diff --git a/src/v2.rs b/src/v2.rs new file mode 100644 index 00000000..e9fa4656 --- /dev/null +++ b/src/v2.rs @@ -0,0 +1,3 @@ +//! Endpoints for the r0.1.x versions of the federation API specification. + +pub mod get_server_keys; diff --git a/src/v2/get_server_keys.rs b/src/v2/get_server_keys.rs new file mode 100644 index 00000000..73160322 --- /dev/null +++ b/src/v2/get_server_keys.rs @@ -0,0 +1,54 @@ +//! [GET /_matrix/key/v2/server](https://matrix.org/docs/spec/server_server/r0.1.3#get-matrix-key-v2-server-keyid) + +use std::{collections::BTreeMap, time::SystemTime}; + +use ruma_api::ruma_api; +use serde::{Deserialize, Serialize}; + +ruma_api! { + metadata { + description: "Gets the homeserver's published signing keys.", + method: GET, + name: "get_server_keys", + path: "/_matrix/key/v2/server", + rate_limited: false, + requires_authentication: false, + } + + request {} + + response { + // Spec is wrong, all fields are required (see + // https://github.com/matrix-org/matrix-doc/issues/2508) + + /// DNS name of the homeserver. + pub server_name: String, + /// Public keys of the homeserver for verifying digital signatures. + pub verify_keys: BTreeMap, + /// Public keys that the homeserver used to use and when it stopped using them. + pub old_verify_keys: BTreeMap, + /// Digital signatures of this object signed using the verify_keys. + pub signatures: BTreeMap>, + /// Timestamp when the keys should be refreshed. This field MUST be ignored in room + /// versions 1, 2, 3, and 4. + #[serde(with = "ruma_serde::time::ms_since_unix_epoch")] + pub valid_until_ts: SystemTime, + } +} + +/// Public key of the homeserver for verifying digital signatures. +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct VerifyKey { + /// The Unpadded Base64 encoded key. + pub key: String, +} + +/// A key the server used to use, but stopped using. +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct OldVerifyKey { + /// Timestamp when this key expired. + #[serde(with = "ruma_serde::time::ms_since_unix_epoch")] + pub expired_ts: SystemTime, + /// The Unpadded Base64 encoded key. + pub key: String, +} From ccbf216f39bbbaa59131cc200eae5bd18aa1947c Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Thu, 30 Apr 2020 17:10:56 +0200 Subject: [PATCH 22/27] Replace serde_json::Map by BTreeMap --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9920920a..4e296b85 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,8 +50,8 @@ pub struct RoomV3Pdu { pub redacts: Option, /// Additional data added by the origin server but not covered by the /// signatures. - #[serde(default, skip_serializing_if = "serde_json::Map::is_empty")] - pub unsigned: serde_json::Map, + #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] + pub unsigned: BTreeMap, /// Content hashes of the PDU. pub hashes: EventHash, /// Signatures for the PDU. From 26307b68947ad21b914ffb5839da2ab02e351ff3 Mon Sep 17 00:00:00 2001 From: iinuwa Date: Fri, 22 May 2020 17:23:01 -0500 Subject: [PATCH 23/27] Add endpoints for joining rooms --- Cargo.toml | 1 + src/lib.rs | 5 +- src/serde.rs | 1 + src/serde/room_state.rs | 150 ++++++++++++++++++++++ src/v1.rs | 1 + src/v1/join.rs | 4 + src/v1/join/create_join_event.rs | 115 +++++++++++++++++ src/v1/join/create_join_event_template.rs | 39 ++++++ 8 files changed, 314 insertions(+), 2 deletions(-) create mode 100644 src/serde.rs create mode 100644 src/serde/room_state.rs create mode 100644 src/v1/join.rs create mode 100644 src/v1/join/create_join_event.rs create mode 100644 src/v1/join/create_join_event_template.rs diff --git a/Cargo.toml b/Cargo.toml index 99cfe4b0..adca0af2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ version = "0.0.1" [dependencies] js_int = "0.1.4" +matches = "0.1.8" ruma-api = "0.16.0-rc.3" ruma-events = "0.21.0-beta.1" ruma-identifiers = "0.16.0" diff --git a/src/lib.rs b/src/lib.rs index 4e296b85..4477bdcc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,12 +4,13 @@ use std::collections::BTreeMap; +use ::serde::{Deserialize, Serialize}; use js_int::UInt; use ruma_events::EventType; use ruma_identifiers::{EventId, RoomId, UserId}; -use serde::{Deserialize, Serialize}; use serde_json::Value as JsonValue; +mod serde; pub mod unversioned; pub mod v1; pub mod v2; @@ -59,7 +60,7 @@ pub struct RoomV3Pdu { } /// Content hashes of a PDU. -#[derive(Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct EventHash { /// The SHA-256 hash. pub sha256: String, diff --git a/src/serde.rs b/src/serde.rs new file mode 100644 index 00000000..2a8eebbd --- /dev/null +++ b/src/serde.rs @@ -0,0 +1 @@ +pub mod room_state; diff --git a/src/serde/room_state.rs b/src/serde/room_state.rs new file mode 100644 index 00000000..5f3fbb65 --- /dev/null +++ b/src/serde/room_state.rs @@ -0,0 +1,150 @@ +//! A module to deserialize a RoomState struct from incorrectly specified v1 +//! send_join endpoint. +//! +//! For more information, see this [GitHub issue](https://github.com/matrix-org/matrix-doc/issues/2541). + +use std::fmt; + +use serde::{ + de::{Deserializer, Error, IgnoredAny, SeqAccess, Visitor}, + ser::{SerializeSeq, Serializer}, +}; + +use crate::v1::join::create_join_event::RoomState; + +pub fn serialize(room_state: &RoomState, serializer: S) -> Result +where + S: Serializer, +{ + let mut seq = serializer.serialize_seq(Some(2))?; + seq.serialize_element(&200)?; + seq.serialize_element(room_state)?; + seq.end() +} + +pub fn deserialize<'de, D>(deserializer: D) -> Result +where + D: Deserializer<'de>, +{ + deserializer.deserialize_seq(RoomStateVisitor) +} + +struct RoomStateVisitor; + +impl<'de> Visitor<'de> for RoomStateVisitor { + type Value = RoomState; + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("Room State response wrapped in an array.") + } + + fn visit_seq(self, mut seq: A) -> Result + where + A: SeqAccess<'de>, + { + let expected = "a two-element list in the response"; + if seq.next_element::()?.is_none() { + return Err(A::Error::invalid_length(0, &expected)); + } + + let room_state = seq + .next_element()? + .ok_or_else(|| A::Error::invalid_length(1, &expected))?; + + while let Some(IgnoredAny) = seq.next_element()? { + // ignore extra elements + } + + Ok(room_state) + } +} + +#[cfg(test)] +mod tests { + use matches::assert_matches; + use serde_json::{json, to_value as to_json_value}; + + use super::{deserialize, serialize, RoomState}; + + #[test] + fn test_deserialize_response() { + let response = json!([ + 200, + { + "origin": "example.com", + "auth_chain": [], + "state": [] + } + ]); + + let parsed = deserialize(response).unwrap(); + + assert_matches!( + parsed, + RoomState { origin, auth_chain, state } + if origin == "example.com" + && auth_chain.is_empty() + && state.is_empty() + ); + } + + #[test] + fn test_serialize_response() { + let room_state = RoomState { + origin: "matrix.org".to_string(), + auth_chain: Vec::new(), + state: Vec::new(), + }; + + let serialized = serialize(&room_state, serde_json::value::Serializer).unwrap(); + let expected = to_json_value(&json!( + [ + 200, + { + "origin": "matrix.org", + "auth_chain": [], + "state": [] + } + ] + )) + .unwrap(); + + assert_eq!(serialized, expected); + } + + #[test] + fn test_too_short_array() { + let json = json!([200]); + let failed_room_state = deserialize(json); + assert_eq!( + failed_room_state.unwrap_err().to_string(), + "invalid length 1, expected a two-element list in the response" + ); + } + + #[test] + fn test_not_an_array() { + let json = json!({ + "origin": "matrix.org", + "auth_chain": [], + "state": [] + }); + let failed_room_state = deserialize(json); + + assert_eq!( + failed_room_state.unwrap_err().to_string(), + "invalid type: map, expected Room State response wrapped in an array.", + ) + } + + #[test] + fn test_too_long_array() { + let json = json!([200, {"origin": "", "auth_chain": [], "state": []}, 200]); + assert_matches!( + deserialize(json).unwrap(), + RoomState { origin, auth_chain, state } + if origin == "" + && auth_chain.is_empty() + && state.is_empty() + ); + } +} diff --git a/src/v1.rs b/src/v1.rs index 94961924..bfcc53a7 100644 --- a/src/v1.rs +++ b/src/v1.rs @@ -2,3 +2,4 @@ pub mod get_public_rooms; pub mod get_server_version; +pub mod join; diff --git a/src/v1/join.rs b/src/v1/join.rs new file mode 100644 index 00000000..64d7df09 --- /dev/null +++ b/src/v1/join.rs @@ -0,0 +1,4 @@ +//! Endpoints for joining rooms. + +pub mod create_join_event; +pub mod create_join_event_template; diff --git a/src/v1/join/create_join_event.rs b/src/v1/join/create_join_event.rs new file mode 100644 index 00000000..464ce4ca --- /dev/null +++ b/src/v1/join/create_join_event.rs @@ -0,0 +1,115 @@ +//! [PUT /_matrix/federation/v1/send_join/{roomId}/{eventId}](https://matrix.org/docs/spec/server_server/r0.1.3#put-matrix-federation-v1-send-join-roomid-eventid) + +use std::collections::BTreeMap; + +use js_int::UInt; +use ruma_api::ruma_api; +use ruma_events::{EventJson, EventType}; +use ruma_identifiers::{EventId, RoomId, UserId}; +use serde::{Deserialize, Serialize}; +use serde_json::Value as JsonValue; + +use crate::{EventHash, RoomV3Pdu}; + +ruma_api! { + metadata { + description: "Send a join event to a resident server.", + name: "create_join_event", + method: PUT, + path: "/_matrix/federation/v1/send_join/:room_id/:event_id", + rate_limited: false, + requires_authentication: true, + } + + request { + /// The room ID that is about to be joined. + #[ruma_api(path)] + pub room_id: RoomId, + /// The user ID the join event will be for. + #[ruma_api(path)] + pub event_id: EventId, + + /// The user id of the user who sent this event. + pub sender: UserId, + /// The `server_name` of the homeserver that created this event. + pub origin: String, + /// Timestamp (milliseconds since the UNIX epoch) on originating homeserver + /// of when this event was created. + pub origin_server_ts: UInt, + + // TODO: Replace with event content collection from ruma-events once that exists + /// The event's type. + #[serde(rename = "type")] + pub kind: EventType, + /// The event's content. + pub content: JsonValue, + + /// A key that determines which piece of room state the event represents. + #[serde(skip_serializing_if = "Option::is_none")] + pub state_key: Option, + /// Event IDs for the most recent events in the room that the homeserver was + /// aware of when it created this event. + pub prev_events: Vec, + /// The maximum depth of the `prev_events`, plus one. + pub depth: UInt, + /// Event IDs for the authorization events that would allow this event to be + /// in the room. + pub auth_events: Vec, + /// For redaction events, the ID of the event being redacted. + #[serde(skip_serializing_if = "Option::is_none")] + pub redacts: Option, + /// Additional data added by the origin server but not covered by the + /// signatures. + #[serde(default, skip_serializing_if = "serde_json::Map::is_empty")] + pub unsigned: serde_json::Map, + /// Content hashes of the PDU. + pub hashes: EventHash, + /// Signatures for the PDU. + pub signatures: BTreeMap>, + } + + response { + /// Full state of the room. + #[ruma_api(body)] + #[serde(with = "crate::serde::room_state")] + pub room_state: RoomState, + } +} + +impl Request { + /// Helper method to get event ID and PDU (with room ID) from the request + /// parameters. + pub fn into_id_and_v3_pdu(self) -> (EventId, RoomV3Pdu) { + ( + self.event_id, + RoomV3Pdu { + room_id: self.room_id, + sender: self.sender, + origin: self.origin, + origin_server_ts: self.origin_server_ts, + kind: self.kind, + content: self.content, + state_key: self.state_key, + prev_events: self.prev_events, + depth: self.depth, + auth_events: self.auth_events, + redacts: self.redacts, + unsigned: self.unsigned, + hashes: self.hashes, + signatures: self.signatures, + }, + ) + } +} + +/// Full state of the room. +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct RoomState { + /// The resident server's DNS name. + pub origin: String, + /// The full set of authorization events that make up the state of the room, + /// and their authorization events, recursively. + pub auth_chain: Vec>, + /// The room state. + pub state: Vec>, +} diff --git a/src/v1/join/create_join_event_template.rs b/src/v1/join/create_join_event_template.rs new file mode 100644 index 00000000..a25af1fd --- /dev/null +++ b/src/v1/join/create_join_event_template.rs @@ -0,0 +1,39 @@ +//! [GET /_matrix/federation/v1/make_join/{roomId}/{userId}](https://matrix.org/docs/spec/server_server/r0.1.3#get-matrix-federation-v1-make-join-roomid-userid) + +use js_int::UInt; +use ruma_api::ruma_api; +use ruma_events::EventJson; +use ruma_identifiers::{RoomId, UserId}; + +use crate::RoomV3Pdu; + +ruma_api! { + metadata { + description: "Send a request for a join event template to a resident server.", + name: "create_join_event_template", + method: GET, + path: "/_matrix/federation/v1/make_join/:room_id/:user_id", + rate_limited: false, + requires_authentication: true, + } + + request { + /// The room ID that is about to be joined. + #[ruma_api(path)] + pub room_id: RoomId, + /// The user ID the join event will be for. + #[ruma_api(path)] + pub user_id: UserId, + #[ruma_api(query)] + /// The room versions the sending server has support for. Defaults to [1]. + #[serde(skip_serializing_if = "Vec::is_empty")] + pub ver: Vec, + } + + response { + /// The version of the room where the server is trying to join. + pub room_version: Option, + /// An unsigned template event. + pub event: EventJson, + } +} From 250da9281aaf8c80af747bea3d5febaeaf143cfc Mon Sep 17 00:00:00 2001 From: Isaiah Inuwa Date: Fri, 22 May 2020 16:31:32 -0500 Subject: [PATCH 24/27] Restructure modules. --- CHANGELOG.md | 24 +++ CONTRIBUTING.md | 181 ++++++++++++++++++ README.md | 8 +- src/directory.rs | 3 + src/directory/get_public_rooms/mod.rs | 3 + .../get_public_rooms/v1.rs} | 0 src/discovery.rs | 5 + .../discover_homeserver.rs | 0 src/discovery/get_server_keys/mod.rs | 3 + .../get_server_keys/v2.rs} | 0 src/discovery/get_server_version/mod.rs | 3 + .../get_server_version/v1.rs} | 0 src/lib.rs | 9 +- src/{v1/join.rs => membership.rs} | 2 +- src/membership/create_join_event/mod.rs | 20 ++ .../create_join_event/v1.rs} | 20 +- .../create_join_event_template/mod.rs | 3 + .../create_join_event_template/v1.rs} | 0 src/serde.rs | 2 + src/serde/room_state.rs | 2 +- src/unversioned.rs | 3 - src/v1.rs | 5 - src/v2.rs | 3 - 23 files changed, 265 insertions(+), 34 deletions(-) create mode 100644 CHANGELOG.md create mode 100644 CONTRIBUTING.md create mode 100644 src/directory.rs create mode 100644 src/directory/get_public_rooms/mod.rs rename src/{v1/get_public_rooms.rs => directory/get_public_rooms/v1.rs} (100%) create mode 100644 src/discovery.rs rename src/{unversioned => discovery}/discover_homeserver.rs (100%) create mode 100644 src/discovery/get_server_keys/mod.rs rename src/{v2/get_server_keys.rs => discovery/get_server_keys/v2.rs} (100%) create mode 100644 src/discovery/get_server_version/mod.rs rename src/{v1/get_server_version.rs => discovery/get_server_version/v1.rs} (100%) rename src/{v1/join.rs => membership.rs} (65%) create mode 100644 src/membership/create_join_event/mod.rs rename src/{v1/join/create_join_event.rs => membership/create_join_event/v1.rs} (84%) create mode 100644 src/membership/create_join_event_template/mod.rs rename src/{v1/join/create_join_event_template.rs => membership/create_join_event_template/v1.rs} (100%) delete mode 100644 src/unversioned.rs delete mode 100644 src/v1.rs delete mode 100644 src/v2.rs diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000..a3598361 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,24 @@ +# [unreleased] + +Improvements: + +* Add endpoints: + ``` + directory::get_public_rooms::v1, + discovery::{ + discover_homeserver, + get_server_keys::v2, + get_server_version::v1 + }, + membership::{ + create_join_event::v1, + create_join_event_template::v1 + }, + version::get_server_version::v1 + ``` + +# 0.0.1 + +Improvements: + +* Provide `RoomV3Pdu` type for room versions 3 and above \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 00000000..bc7c736e --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,181 @@ +Welcome! Thanks for looking into contributing to our project! + +# Table of Contents + +- [Looking for Help?](#looking-for-help) + - [Documentation](#documentation) + - [Chat Rooms](#chat-rooms) +- [Reporting Issues](#reporting-issues) +- [Submitting Code](#submitting-code) + - [Coding Style](#coding-style) + - [Modifying Endpoints](#modifying-endpoints) + - [Submitting PRs](#submitting-prs) + - [Where do I start?](#where-do-i-start) +- [Testing](#testing) +- [Contact](#contact) + +# Looking for Help? + +Here is a list of helpful resources you can consult: + +## Documentation + +- [Matrix Federation API specification](https://matrix.org/docs/spec/server_server/latest) +- Documentation to other Ruma modules: + - [ruma-events](https://docs.rs/ruma-events/) + - [ruma-api](https://docs.rs/ruma-api/) + - [ruma-client](https://docs.rs/ruma-client/) + +## Chat Rooms + +- Ruma Matrix room: [#ruma:matrix.org](https://matrix.to/#/#ruma:matrix.org) +- Matrix Developer room: [#matrix-dev:matrix.org](https://matrix.to/#/#matrix-dev:matrix.org) +- Matrix Homeserver developers room: [#homeservers-dev:matrix.org](https://matrix.to/#/#homerservers-dev:matrix.org) + +# Reporting Issues + +If you find any bugs, inconsistencies or other problems, feel free to submit +a GitHub [issue](issues). + +If you have a quick question, it may be easier to leave a message on +[#ruma:matrix.org](https://matrix.to/#/#ruma:matrix.org). + +Also, if you have trouble getting on board, let us know so we can help future +contributors to the project overcome that hurdle too. + +# Submitting Code + +Ready to write some code? Great! Here are some guidelines to follow to +help you on your way: + +## Coding Style + +### Import Formatting + +Organize your imports into three groups separated by blank lines: + +1. `std` imports +1. External imports (from other crates) +1. Local imports (`self::`, `super::`, `crate::` and things like `LocalType::*`) + +For example, + +```rust +use std::collections::HashMap; + +use ruma_api::ruma_api; + +use super::MyType; +``` + +Also, group imports by module. For example, do this: + +```rust +use std::{ + collections::HashMap, + convert::TryFrom, + fmt::{Debug, Display, Error as FmtError, Formatter}, +}; +``` + +as opposed to: + +```rust +use std::collections::HashMap; +use std::convert::TryFrom; +use std::fmt::{Debug, Display, Error as FmtError, Formatter}; +``` + +### Code Formatting and Linting + +Use `rustfmt` to format your code and `clippy` to lint your code. Before +committing your changes, go ahead and run `cargo fmt` and `cargo clippy +--all-targets --all-features` on the repository to make sure that the +formatting and linting checks pass in CI. Note that `clippy` warnings are +reported as errors in CI builds, so make sure to handle those before +comitting as well. (To install the tools, run `rustup component add rustfmt +clippy`.) + +#### Git hooks +Tip: You may want to add these commands to your pre-commit git hook so you don't get bitten by CI. + +```sh +# ./git/hooks/pre-commit +cargo fmt && cargo clippy --all-targets --allfeatures +``` + +### Commit Messages + +Write commit messages using the imperative mood, as if completing the sentence: +"If applied, this commit will \_\_\_." For example, use "Fix some bug" instead +of "Fixed some bug" or "Add a feature" instead of "Added a feature". + +(Take a look at this +[blog post](https://www.freecodecamp.org/news/writing-good-commit-messages-a-practical-guide/) +for more information on writing good commit messages.) + +## Modifying Endpoints + +### Matrix Spec Version + +Use the latest r0.x.x documentation when adding or modifying code. We target +the latest minor version of the Matrix specification. (Note: We might +reconsider this when the Federation API hits r1.0.0.) + +### Endpoint Documentation Header + +Add a comment to the top of each endpoint file that includes the path +and a link to the documentation of the spec. You can use the latest +version at the time of the commit. For example: + +```rust +//! [GET /.well-known/matrix/server](https://matrix.org/docs/spec/server_server/r0.1.3#get-well-known-matrix-server) +``` + +### Naming Endpoints + +When adding new endpoints, select the module that fits the purpose of the +endpoint. When naming the endpoint itself, you can use the following +guidelines: +- The name should be a verb describing what the client is requesting, e.g. + `get_some_resource`. +- Endpoints which are basic CRUD operations should use the prefixes + `create`, `get`, `update`, and `delete`. +- The prefix `set` is preferred to create if the resource is a singleton. + In other words, when there's no distinction between `create` and `update`. +- Try to use names that are as descriptive as possible and distinct from + other endpoints in all other modules. (For example, instead of + `membership::create_event::v1`, use `membership::create_join_event::v1`). +- If you're not sure what to name it, pick any name and we can help you + with it. + +### Tracking Changes + +Add your changes to the [change log](CHANGELOG.md). If possible, try to +find and denote the version of the spec that included the change you are +making. + +## Submitting PRs + +Once you're ready to submit your code, create a pull request, and one of our +maintainers will review it. Once your PR has passed review, a maintainer will +merge the request and you're done! 🎉 + +## Where do I start? + +If this is your first contribution to the project, we recommend taking a look +at one of the [open issues][] we've marked for new contributors. + +It may be helpful to peruse some of the documentation for `ruma-events` and +`ruma-api` listed above for some context. + +[open issues]: https://github.com/ruma/ruma-federation-api/issues?q=is%3Aopen+is%3Aissue+label%3Aeffort%2Feasy + +# Testing + +Before committing, run `cargo check` to make sure that your changes can build, as well as running the formatting and linting tools [mentioned above](#code-formatting-and-linting). + +# Contact + +Thanks again for being a contributor! If you have any questions, join us at +[#ruma:matrix.org](https://matrix.to/#/#ruma:matrix.org). diff --git a/README.md b/README.md index f4ac57de..29e4dbc4 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,18 @@ # ruma-federation-api -**ruma-federation-api** contains serializable types for the requests and responses for each endpoint in the [Matrix](https://matrix.org/) federation API specification. +**ruma-federation-api** contains serializable types for the requests and responses for each endpoint in the [Matrix](https://matrix.org/) Federation API specification. These types can be shared by client and server code. ## Minimum Rust version ruma-federation-api requires Rust 1.40.0 or later. +## Documentation +[https://docs.rs/ruma-federation-api](https://docs.rs/ruma-federation-api) + +## Contributing +See [CONTRIBUTING.md](CONTRIBUTING.md). + ## License [MIT](http://opensource.org/licenses/MIT) diff --git a/src/directory.rs b/src/directory.rs new file mode 100644 index 00000000..d33d3254 --- /dev/null +++ b/src/directory.rs @@ -0,0 +1,3 @@ +//! Room directory endpoints. + +pub mod get_public_rooms; diff --git a/src/directory/get_public_rooms/mod.rs b/src/directory/get_public_rooms/mod.rs new file mode 100644 index 00000000..eb9b0dab --- /dev/null +++ b/src/directory/get_public_rooms/mod.rs @@ -0,0 +1,3 @@ +//! Endpoint to query a homeserver's public rooms. + +pub mod v1; diff --git a/src/v1/get_public_rooms.rs b/src/directory/get_public_rooms/v1.rs similarity index 100% rename from src/v1/get_public_rooms.rs rename to src/directory/get_public_rooms/v1.rs diff --git a/src/discovery.rs b/src/discovery.rs new file mode 100644 index 00000000..d09f4583 --- /dev/null +++ b/src/discovery.rs @@ -0,0 +1,5 @@ +//! Server discovery endpoints. + +pub mod discover_homeserver; +pub mod get_server_keys; +pub mod get_server_version; diff --git a/src/unversioned/discover_homeserver.rs b/src/discovery/discover_homeserver.rs similarity index 100% rename from src/unversioned/discover_homeserver.rs rename to src/discovery/discover_homeserver.rs diff --git a/src/discovery/get_server_keys/mod.rs b/src/discovery/get_server_keys/mod.rs new file mode 100644 index 00000000..4de7144b --- /dev/null +++ b/src/discovery/get_server_keys/mod.rs @@ -0,0 +1,3 @@ +//! Endpdoint for retrieving a server's published signing keys. + +pub mod v2; diff --git a/src/v2/get_server_keys.rs b/src/discovery/get_server_keys/v2.rs similarity index 100% rename from src/v2/get_server_keys.rs rename to src/discovery/get_server_keys/v2.rs diff --git a/src/discovery/get_server_version/mod.rs b/src/discovery/get_server_version/mod.rs new file mode 100644 index 00000000..35431f43 --- /dev/null +++ b/src/discovery/get_server_version/mod.rs @@ -0,0 +1,3 @@ +//! Endpoint to retrieve metadata about a server implementation. + +pub mod v1; diff --git a/src/v1/get_server_version.rs b/src/discovery/get_server_version/v1.rs similarity index 100% rename from src/v1/get_server_version.rs rename to src/discovery/get_server_version/v1.rs diff --git a/src/lib.rs b/src/lib.rs index 4477bdcc..8be02a60 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -//! (De)serializable types for the matrix server-server protocol. +//! (De)serializable types for the Matrix Federation API. #![warn(missing_docs)] @@ -11,9 +11,10 @@ use ruma_identifiers::{EventId, RoomId, UserId}; use serde_json::Value as JsonValue; mod serde; -pub mod unversioned; -pub mod v1; -pub mod v2; + +pub mod directory; +pub mod discovery; +pub mod membership; /// A 'persistent data unit' (event) for room versions 3 and beyond. #[derive(Deserialize, Serialize)] diff --git a/src/v1/join.rs b/src/membership.rs similarity index 65% rename from src/v1/join.rs rename to src/membership.rs index 64d7df09..6ca64dd8 100644 --- a/src/v1/join.rs +++ b/src/membership.rs @@ -1,4 +1,4 @@ -//! Endpoints for joining rooms. +//! Room membership endpoints. pub mod create_join_event; pub mod create_join_event_template; diff --git a/src/membership/create_join_event/mod.rs b/src/membership/create_join_event/mod.rs new file mode 100644 index 00000000..4c6d2730 --- /dev/null +++ b/src/membership/create_join_event/mod.rs @@ -0,0 +1,20 @@ +//! Endpoint to send join events to remote homeservers. + +pub mod v1; + +use ruma_events::EventJson; +use serde::{Deserialize, Serialize}; + +use crate::RoomV3Pdu; + +/// Full state of the room. +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct RoomState { + /// The resident server's DNS name. + pub origin: String, + /// The full set of authorization events that make up the state of the room, + /// and their authorization events, recursively. + pub auth_chain: Vec>, + /// The room state. + pub state: Vec>, +} diff --git a/src/v1/join/create_join_event.rs b/src/membership/create_join_event/v1.rs similarity index 84% rename from src/v1/join/create_join_event.rs rename to src/membership/create_join_event/v1.rs index 464ce4ca..3f5f2874 100644 --- a/src/v1/join/create_join_event.rs +++ b/src/membership/create_join_event/v1.rs @@ -4,11 +4,11 @@ use std::collections::BTreeMap; use js_int::UInt; use ruma_api::ruma_api; -use ruma_events::{EventJson, EventType}; +use ruma_events::EventType; use ruma_identifiers::{EventId, RoomId, UserId}; -use serde::{Deserialize, Serialize}; use serde_json::Value as JsonValue; +use super::RoomState; use crate::{EventHash, RoomV3Pdu}; ruma_api! { @@ -60,8 +60,8 @@ ruma_api! { pub redacts: Option, /// Additional data added by the origin server but not covered by the /// signatures. - #[serde(default, skip_serializing_if = "serde_json::Map::is_empty")] - pub unsigned: serde_json::Map, + #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] + pub unsigned: BTreeMap, /// Content hashes of the PDU. pub hashes: EventHash, /// Signatures for the PDU. @@ -101,15 +101,3 @@ impl Request { ) } } - -/// Full state of the room. -#[derive(Clone, Debug, Deserialize, Serialize)] -pub struct RoomState { - /// The resident server's DNS name. - pub origin: String, - /// The full set of authorization events that make up the state of the room, - /// and their authorization events, recursively. - pub auth_chain: Vec>, - /// The room state. - pub state: Vec>, -} diff --git a/src/membership/create_join_event_template/mod.rs b/src/membership/create_join_event_template/mod.rs new file mode 100644 index 00000000..1007e5ba --- /dev/null +++ b/src/membership/create_join_event_template/mod.rs @@ -0,0 +1,3 @@ +//! Endpoint to request a template for join events. + +pub mod v1; diff --git a/src/v1/join/create_join_event_template.rs b/src/membership/create_join_event_template/v1.rs similarity index 100% rename from src/v1/join/create_join_event_template.rs rename to src/membership/create_join_event_template/v1.rs diff --git a/src/serde.rs b/src/serde.rs index 2a8eebbd..3808d8c5 100644 --- a/src/serde.rs +++ b/src/serde.rs @@ -1 +1,3 @@ +//! Modules for custom serde de/-serialization implementations. + pub mod room_state; diff --git a/src/serde/room_state.rs b/src/serde/room_state.rs index 5f3fbb65..6df5933b 100644 --- a/src/serde/room_state.rs +++ b/src/serde/room_state.rs @@ -10,7 +10,7 @@ use serde::{ ser::{SerializeSeq, Serializer}, }; -use crate::v1::join::create_join_event::RoomState; +use crate::membership::create_join_event::RoomState; pub fn serialize(room_state: &RoomState, serializer: S) -> Result where diff --git a/src/unversioned.rs b/src/unversioned.rs deleted file mode 100644 index 5e98cd63..00000000 --- a/src/unversioned.rs +++ /dev/null @@ -1,3 +0,0 @@ -//! Endpoints that cannot change with new versions of the Matrix specification. - -pub mod discover_homeserver; diff --git a/src/v1.rs b/src/v1.rs deleted file mode 100644 index bfcc53a7..00000000 --- a/src/v1.rs +++ /dev/null @@ -1,5 +0,0 @@ -//! Endpoints for the r0.1.x versions of the federation API specification. - -pub mod get_public_rooms; -pub mod get_server_version; -pub mod join; diff --git a/src/v2.rs b/src/v2.rs deleted file mode 100644 index e9fa4656..00000000 --- a/src/v2.rs +++ /dev/null @@ -1,3 +0,0 @@ -//! Endpoints for the r0.1.x versions of the federation API specification. - -pub mod get_server_keys; From e4c7a81a7c82f1727e5490ed7f743bd8e2e60b61 Mon Sep 17 00:00:00 2001 From: Isaiah Inuwa Date: Sat, 23 May 2020 08:02:47 -0500 Subject: [PATCH 25/27] Use SystemTime for timestamp fields --- Cargo.toml | 2 +- src/lib.rs | 5 +++-- src/membership/create_join_event/v1.rs | 5 +++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index adca0af2..58d72b35 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,6 @@ matches = "0.1.8" ruma-api = "0.16.0-rc.3" ruma-events = "0.21.0-beta.1" ruma-identifiers = "0.16.0" -ruma-serde = "0.1.0" +ruma-serde = "0.2.1" serde = { version = "1.0.106", features = ["derive"] } serde_json = "1.0.51" diff --git a/src/lib.rs b/src/lib.rs index 8be02a60..ea235b73 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,7 @@ #![warn(missing_docs)] -use std::collections::BTreeMap; +use std::{collections::BTreeMap, time::SystemTime}; use ::serde::{Deserialize, Serialize}; use js_int::UInt; @@ -27,7 +27,8 @@ pub struct RoomV3Pdu { pub origin: String, /// Timestamp (milliseconds since the UNIX epoch) on originating homeserver /// of when this event was created. - pub origin_server_ts: UInt, + #[serde(with = "ruma_serde::time::ms_since_unix_epoch")] + pub origin_server_ts: SystemTime, // TODO: Replace with event content collection from ruma-events once that exists /// The event's type. diff --git a/src/membership/create_join_event/v1.rs b/src/membership/create_join_event/v1.rs index 3f5f2874..e4403f04 100644 --- a/src/membership/create_join_event/v1.rs +++ b/src/membership/create_join_event/v1.rs @@ -1,6 +1,6 @@ //! [PUT /_matrix/federation/v1/send_join/{roomId}/{eventId}](https://matrix.org/docs/spec/server_server/r0.1.3#put-matrix-federation-v1-send-join-roomid-eventid) -use std::collections::BTreeMap; +use std::{collections::BTreeMap, time::SystemTime}; use js_int::UInt; use ruma_api::ruma_api; @@ -35,7 +35,8 @@ ruma_api! { pub origin: String, /// Timestamp (milliseconds since the UNIX epoch) on originating homeserver /// of when this event was created. - pub origin_server_ts: UInt, + #[serde(with = "ruma_serde::time::ms_since_unix_epoch")] + pub origin_server_ts: SystemTime, // TODO: Replace with event content collection from ruma-events once that exists /// The event's type. From 4cf4aa6ef74b25ad8c14d99d7774129f023df163 Mon Sep 17 00:00:00 2001 From: Isaiah Inuwa Date: Sun, 24 May 2020 22:40:33 -0500 Subject: [PATCH 26/27] Add room information query endpoint. --- CHANGELOG.md | 1 + src/lib.rs | 1 + src/query.rs | 3 +++ src/query/get_room_information/mod.rs | 2 ++ src/query/get_room_information/v1.rs | 28 +++++++++++++++++++++++++++ 5 files changed, 35 insertions(+) create mode 100644 src/query.rs create mode 100644 src/query/get_room_information/mod.rs create mode 100644 src/query/get_room_information/v1.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index a3598361..bed78bb5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ Improvements: create_join_event::v1, create_join_event_template::v1 }, + query::get_room_information::v1, version::get_server_version::v1 ``` diff --git a/src/lib.rs b/src/lib.rs index ea235b73..ef6a1c66 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,6 +15,7 @@ mod serde; pub mod directory; pub mod discovery; pub mod membership; +pub mod query; /// A 'persistent data unit' (event) for room versions 3 and beyond. #[derive(Deserialize, Serialize)] diff --git a/src/query.rs b/src/query.rs new file mode 100644 index 00000000..7dc27cdb --- /dev/null +++ b/src/query.rs @@ -0,0 +1,3 @@ +//! Endpoints to retrieve information from a homeserver about a resource. + +pub mod get_room_information; diff --git a/src/query/get_room_information/mod.rs b/src/query/get_room_information/mod.rs new file mode 100644 index 00000000..bf4d11f2 --- /dev/null +++ b/src/query/get_room_information/mod.rs @@ -0,0 +1,2 @@ +//! Endpoint to query room information with a room alias. +pub mod v1; diff --git a/src/query/get_room_information/v1.rs b/src/query/get_room_information/v1.rs new file mode 100644 index 00000000..77c7219a --- /dev/null +++ b/src/query/get_room_information/v1.rs @@ -0,0 +1,28 @@ +//! [GET /_matrix/federation/v1/query/directory](https://matrix.org/docs/spec/server_server/r0.1.3#get-matrix-federation-v1-query-directory) + +use ruma_api::ruma_api; +use ruma_identifiers::RoomId; + +ruma_api! { + metadata { + description: "Get mapped room ID and resident homeservers for a given room alias.", + name: "get_room_information", + method: GET, + path: "/_matrix/federation/v1/query/directory", + rate_limited: false, + requires_authentication: true, + } + + request { + /// Room alias to query. + #[ruma_api(query)] + pub room_alias: String, + } + + response { + /// Room ID mapped to queried alias. + pub room_id: RoomId, + /// An array of server names that are likely to hold the given room. + pub servers: Vec, + } +} From 44a0f493d0ae119fb1175a5f61c2db52ef001fb7 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Thu, 4 Jun 2020 23:53:25 +0200 Subject: [PATCH 27/27] Update dependencies, bump version --- Cargo.toml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 58d72b35..6f93144b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,14 +10,14 @@ license = "MIT" name = "ruma-federation-api" readme = "README.md" repository = "https://github.com/ruma/ruma-federation-api" -version = "0.0.1" +version = "0.0.2" [dependencies] -js_int = "0.1.4" +js_int = "0.1.5" matches = "0.1.8" -ruma-api = "0.16.0-rc.3" -ruma-events = "0.21.0-beta.1" -ruma-identifiers = "0.16.0" -ruma-serde = "0.2.1" -serde = { version = "1.0.106", features = ["derive"] } -serde_json = "1.0.51" +ruma-api = "0.16.1" +ruma-events = "0.21.3" +ruma-identifiers = "0.16.2" +ruma-serde = "0.2.2" +serde = { version = "1.0.111", features = ["derive"] } +serde_json = "1.0.53"