ruma_client_api/keys/claim_keys/
v4.rs

1//! `/v4/` ([MSC])
2//!
3//! [MSC]: https://github.com/matrix-org/matrix-spec-proposals/pull/3983
4
5use std::{collections::BTreeMap, time::Duration};
6
7use ruma_common::{
8    api::{request, response, Metadata},
9    encryption::OneTimeKey,
10    metadata,
11    serde::Raw,
12    OneTimeKeyAlgorithm, OwnedDeviceId, OwnedOneTimeKeyId, OwnedUserId,
13};
14use serde_json::Value as JsonValue;
15
16const METADATA: Metadata = metadata! {
17    method: POST,
18    rate_limited: false,
19    authentication: AccessToken,
20    history: {
21        unstable => "/_matrix/client/unstable/org.matrix.msc3983/keys/claim",
22    }
23};
24
25/// Request type for the `claim_keys` endpoint.
26#[request(error = crate::Error)]
27pub struct Request {
28    /// The time (in milliseconds) to wait when downloading keys from remote servers.
29    /// 10 seconds is the recommended default.
30    #[serde(
31        with = "ruma_common::serde::duration::opt_ms",
32        default,
33        skip_serializing_if = "Option::is_none"
34    )]
35    pub timeout: Option<Duration>,
36
37    /// The keys to be claimed.
38    pub one_time_keys: BTreeMap<OwnedUserId, BTreeMap<OwnedDeviceId, Vec<OneTimeKeyAlgorithm>>>,
39}
40
41/// Response type for the `claim_keys` endpoint.
42#[response(error = crate::Error)]
43pub struct Response {
44    /// If any remote homeservers could not be reached, they are recorded here.
45    ///
46    /// The names of the properties are the names of the unreachable servers.
47    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
48    pub failures: BTreeMap<String, JsonValue>,
49
50    /// One-time keys for the queried devices.
51    pub one_time_keys: BTreeMap<OwnedUserId, OneTimeKeys>,
52}
53
54impl Request {
55    /// Creates a new `Request` with the given key claims and the recommended 10 second timeout.
56    pub fn new(
57        one_time_keys: BTreeMap<OwnedUserId, BTreeMap<OwnedDeviceId, Vec<OneTimeKeyAlgorithm>>>,
58    ) -> Self {
59        Self { timeout: Some(Duration::from_secs(10)), one_time_keys }
60    }
61}
62
63impl Response {
64    /// Creates a new `Response` with the given keys and no failures.
65    pub fn new(one_time_keys: BTreeMap<OwnedUserId, OneTimeKeys>) -> Self {
66        Self { failures: BTreeMap::new(), one_time_keys }
67    }
68}
69
70/// The one-time keys for a given device.
71pub type OneTimeKeys = BTreeMap<OwnedDeviceId, BTreeMap<OwnedOneTimeKeyId, Raw<OneTimeKey>>>;