1//! `/v4/` ([MSC])
2//!
3//! [MSC]: https://github.com/matrix-org/matrix-spec-proposals/pull/3983
45use std::{collections::BTreeMap, time::Duration};
67use 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;
1516const 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};
2425/// 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)]
35pub timeout: Option<Duration>,
3637/// The keys to be claimed.
38pub one_time_keys: BTreeMap<OwnedUserId, BTreeMap<OwnedDeviceId, Vec<OneTimeKeyAlgorithm>>>,
39}
4041/// 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")]
48pub failures: BTreeMap<String, JsonValue>,
4950/// One-time keys for the queried devices.
51pub one_time_keys: BTreeMap<OwnedUserId, OneTimeKeys>,
52}
5354impl Request {
55/// Creates a new `Request` with the given key claims and the recommended 10 second timeout.
56pub fn new(
57 one_time_keys: BTreeMap<OwnedUserId, BTreeMap<OwnedDeviceId, Vec<OneTimeKeyAlgorithm>>>,
58 ) -> Self {
59Self { timeout: Some(Duration::from_secs(10)), one_time_keys }
60 }
61}
6263impl Response {
64/// Creates a new `Response` with the given keys and no failures.
65pub fn new(one_time_keys: BTreeMap<OwnedUserId, OneTimeKeys>) -> Self {
66Self { failures: BTreeMap::new(), one_time_keys }
67 }
68}
6970/// The one-time keys for a given device.
71pub type OneTimeKeys = BTreeMap<OwnedDeviceId, BTreeMap<OwnedOneTimeKeyId, Raw<OneTimeKey>>>;