identity-service-api: Add /versions endpoint (MSC2320)

This commit is contained in:
Jonathan de Jong 2022-03-14 10:06:35 +01:00 committed by GitHub
parent a0e05c0a31
commit a54cf06b9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 73 additions and 1 deletions

View File

@ -0,0 +1,4 @@
//! Various endpoints related to identity server status and metadata discovery.
pub mod get_server_status;
pub mod get_supported_versions;

View File

@ -0,0 +1,68 @@
//! `GET /_matrix/identity/versions` ([spec])
//!
//! [spec]: https://spec.matrix.org/v1.2/identity-service-api/#get_matrixidentityversions
//!
//! Note: This endpoint was only implemented in/after 1.1, so a 404 could indicate the server only
//! supports 1.0 endpoints. Please use [`server_status`](crate::discovery::server_status) to
//! double-check.
//!
//! Note: This endpoint does not contain an unstable variant for 1.0.
use std::collections::BTreeMap;
use ruma_common::api::{ruma_api, MatrixVersion};
ruma_api! {
metadata: {
description: "Get the versions of the identity service API supported by this endpoint.",
method: GET,
name: "versions",
stable_path: "/_matrix/identity/versions",
rate_limited: false,
authentication: None,
added: 1.1,
}
#[derive(Default)]
request: {}
response: {
/// A list of Matrix client API protocol versions supported by the endpoint.
pub versions: Vec<String>,
}
}
impl Request {
/// Creates an empty `Request`.
pub fn new() -> Self {
Self {}
}
}
impl Response {
/// Creates a new `Response` with the given `versions`.
pub fn new(versions: Vec<String>) -> Self {
Self { versions }
}
/// Extracts known Matrix versions from this response.
///
/// Matrix versions that Ruma cannot parse, or does not know about, are discarded.
///
/// The versions returned will be sorted from oldest to latest. Use [`.find()`][Iterator::find]
/// or [`.rfind()`][DoubleEndedIterator::rfind] to look for a minimum or maximum version to use
/// given some constraint.
pub fn known_versions(&self) -> impl Iterator<Item = MatrixVersion> + DoubleEndedIterator {
self.versions
.iter()
// Parse, discard unknown versions
.flat_map(|s| s.parse::<MatrixVersion>())
// Map to key-value pairs where the key is the major-minor representation
// (which can be used as a BTreeMap unlike MatrixVersion itself)
.map(|v| (v.into_parts(), v))
// Collect to BTreeMap
.collect::<BTreeMap<_, _>>()
// Return an iterator over just the values (`MatrixVersion`s)
.into_values()
}
}

View File

@ -11,10 +11,10 @@ use std::fmt;
pub mod association;
pub mod authentication;
pub mod discovery;
pub mod invitation;
pub mod keys;
pub mod lookup;
pub mod status;
pub mod tos;
// Wrapper around `Box<str>` that cannot be used in a meaningful way outside of