use std::{collections::BTreeMap, time::Duration};
use salvo::oapi::ToSchema;
use serde::{Deserialize, Serialize};
use crate::client::key::{SignedKeys, SignedKeysIter};
use crate::identifiers::*;
use crate::serde::{JsonValue, RawJsonValue};
use crate::{DeviceKeyAlgorithm, encryption::OneTimeKey};
impl<'a> IntoIterator for &'a SignedKeys {
type Item = (&'a str, &'a RawJsonValue);
type IntoIter = SignedKeysIter<'a>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
#[derive(ToSchema, Deserialize, Debug)]
pub struct ClaimKeysReqBody {
#[serde(
with = "crate::serde::duration::opt_ms",
default,
skip_serializing_if = "Option::is_none"
)]
pub timeout: Option<Duration>,
pub one_time_keys: BTreeMap<OwnedUserId, BTreeMap<OwnedDeviceId, DeviceKeyAlgorithm>>,
}
impl ClaimKeysReqBody {
pub fn new(one_time_keys: BTreeMap<OwnedUserId, BTreeMap<OwnedDeviceId, DeviceKeyAlgorithm>>) -> Self {
Self {
timeout: Some(Duration::from_secs(10)),
one_time_keys,
}
}
}
#[derive(ToSchema, Serialize, Debug)]
pub struct ClaimKeysResBody {
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
#[salvo(schema(value_type = Object, additional_properties = true))]
pub failures: BTreeMap<String, JsonValue>,
pub one_time_keys: BTreeMap<OwnedUserId, OneTimeKeys>,
}
impl ClaimKeysResBody {
pub fn new(one_time_keys: BTreeMap<OwnedUserId, OneTimeKeys>) -> Self {
Self {
failures: BTreeMap::new(),
one_time_keys,
}
}
}
pub type OneTimeKeys = BTreeMap<OwnedDeviceId, BTreeMap<OwnedDeviceKeyId, OneTimeKey>>;