Skip to main content

ruma_client_api/discovery/
get_supported_versions.rs

1//! `GET /_matrix/client/versions` ([spec])
2//!
3//! Get the versions of the client-server API supported by this homeserver.
4//!
5//! [spec]: https://spec.matrix.org/v1.18/client-server-api/#get_matrixclientversions
6
7use std::collections::BTreeMap;
8
9use ruma_common::{
10    api::{SupportedVersions, auth_scheme::AccessTokenOptional, request, response},
11    metadata,
12};
13#[cfg(feature = "unstable-msc4383")]
14use serde::{Deserialize, Serialize};
15
16metadata! {
17    method: GET,
18    rate_limited: false,
19    authentication: AccessTokenOptional,
20    path: "/_matrix/client/versions",
21}
22
23/// Request type for the `api_versions` endpoint.
24#[request]
25#[derive(Default)]
26pub struct Request {}
27
28/// Response type for the `api_versions` endpoint.
29#[response]
30pub struct Response {
31    /// A list of Matrix client API protocol versions supported by the homeserver.
32    pub versions: Vec<String>,
33
34    /// Experimental features supported by the server.
35    ///
36    /// Servers can enable some unstable features only for some users, so this
37    /// list might differ when an access token is provided.
38    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
39    pub unstable_features: BTreeMap<String, bool>,
40
41    /// Information about the homeserver implementation, with the same shape as
42    /// the object returned by `GET /_matrix/federation/v1/version`.
43    ///
44    /// This uses the unstable prefix defined in [MSC4383].
45    ///
46    /// [MSC4383]: https://github.com/matrix-org/matrix-spec-proposals/pull/4383
47    #[cfg(feature = "unstable-msc4383")]
48    #[serde(rename = "net.zemos.msc4383.server", default, skip_serializing_if = "Option::is_none")]
49    pub server: Option<Server>,
50}
51
52/// Identifying information about the homeserver implementation.
53///
54/// This uses the unstable prefix defined in [MSC4383].
55///
56/// [MSC4383]: https://github.com/matrix-org/matrix-spec-proposals/pull/4383
57#[cfg(feature = "unstable-msc4383")]
58#[derive(Clone, Debug, Deserialize, Serialize)]
59#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
60pub struct Server {
61    /// Arbitrary name that identifies this implementation.
62    pub name: String,
63
64    /// Version of this implementation.
65    ///
66    /// The version format depends on the implementation.
67    pub version: String,
68}
69
70#[cfg(feature = "unstable-msc4383")]
71impl Server {
72    /// Creates a `Server` with the given implementation `name` and `version`.
73    pub fn new(name: String, version: String) -> Self {
74        Self { name, version }
75    }
76}
77
78impl Request {
79    /// Creates an empty `Request`.
80    pub fn new() -> Self {
81        Self {}
82    }
83}
84
85impl Response {
86    /// Creates a new `Response` with the given `versions`.
87    pub fn new(versions: Vec<String>) -> Self {
88        Self {
89            versions,
90            unstable_features: BTreeMap::new(),
91            #[cfg(feature = "unstable-msc4383")]
92            server: None,
93        }
94    }
95
96    /// Convert this `Response` into a [`SupportedVersions`] that can be used with
97    /// `OutgoingRequest::try_into_http_request()`.
98    ///
99    /// Matrix versions that can't be parsed to a `MatrixVersion`, and features with the boolean
100    /// value set to `false` are discarded.
101    pub fn as_supported_versions(&self) -> SupportedVersions {
102        SupportedVersions::from_parts(&self.versions, &self.unstable_features)
103    }
104}