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/latest/client-server-api/#get_matrixclientversions
6
7use std::collections::{BTreeMap, BTreeSet};
8
9use ruma_common::{
10    api::{request, response, MatrixVersion, Metadata},
11    metadata,
12};
13
14const METADATA: Metadata = metadata! {
15    method: GET,
16    rate_limited: false,
17    authentication: AccessTokenOptional,
18    history: {
19        1.0 => "/_matrix/client/versions",
20    }
21};
22
23/// Request type for the `api_versions` endpoint.
24#[request(error = crate::Error)]
25#[derive(Default)]
26pub struct Request {}
27
28/// Response type for the `api_versions` endpoint.
29#[response(error = crate::Error)]
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
42impl Request {
43    /// Creates an empty `Request`.
44    pub fn new() -> Self {
45        Self {}
46    }
47}
48
49impl Response {
50    /// Creates a new `Response` with the given `versions`.
51    pub fn new(versions: Vec<String>) -> Self {
52        Self { versions, unstable_features: BTreeMap::new() }
53    }
54
55    /// Extracts known Matrix versions from this response.
56    ///
57    /// Matrix versions that Ruma cannot parse, or does not know about, are discarded.
58    ///
59    /// The versions returned will be sorted from oldest to latest. Use [`.find()`][Iterator::find]
60    /// or [`.rfind()`][DoubleEndedIterator::rfind] to look for a minimum or maximum version to use
61    /// given some constraint.
62    pub fn known_versions(&self) -> BTreeSet<MatrixVersion> {
63        self.versions
64            .iter()
65            // Parse, discard unknown versions
66            .flat_map(|s| s.parse::<MatrixVersion>())
67            // Collect to BTreeSet
68            .collect()
69    }
70}
71
72#[cfg(test)]
73mod tests {
74    use std::collections::BTreeSet;
75
76    use ruma_common::api::MatrixVersion;
77
78    use super::Response;
79
80    #[test]
81    fn known_versions() {
82        let none = Response::new(vec![]);
83        assert_eq!(none.known_versions(), BTreeSet::new());
84
85        let single_known = Response::new(vec!["r0.6.0".to_owned()]);
86        assert_eq!(single_known.known_versions(), BTreeSet::from([MatrixVersion::V1_0]));
87
88        let single_unknown = Response::new(vec!["v0.0".to_owned()]);
89        assert_eq!(single_unknown.known_versions(), BTreeSet::new());
90    }
91
92    #[test]
93    fn known_versions_order() {
94        let sorted = Response::new(vec![
95            "r0.0.1".to_owned(),
96            "r0.5.0".to_owned(),
97            "r0.6.0".to_owned(),
98            "r0.6.1".to_owned(),
99            "v1.1".to_owned(),
100            "v1.2".to_owned(),
101        ]);
102        assert_eq!(
103            sorted.known_versions(),
104            BTreeSet::from([MatrixVersion::V1_0, MatrixVersion::V1_1, MatrixVersion::V1_2])
105        );
106
107        let sorted_reverse = Response::new(vec![
108            "v1.2".to_owned(),
109            "v1.1".to_owned(),
110            "r0.6.1".to_owned(),
111            "r0.6.0".to_owned(),
112            "r0.5.0".to_owned(),
113            "r0.0.1".to_owned(),
114        ]);
115        assert_eq!(
116            sorted_reverse.known_versions(),
117            BTreeSet::from([MatrixVersion::V1_0, MatrixVersion::V1_1, MatrixVersion::V1_2])
118        );
119
120        let random_order = Response::new(vec![
121            "v1.1".to_owned(),
122            "r0.6.1".to_owned(),
123            "r0.5.0".to_owned(),
124            "r0.6.0".to_owned(),
125            "r0.0.1".to_owned(),
126            "v1.2".to_owned(),
127        ]);
128        assert_eq!(
129            random_order.known_versions(),
130            BTreeSet::from([MatrixVersion::V1_0, MatrixVersion::V1_1, MatrixVersion::V1_2])
131        );
132    }
133}