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;