ruwuma/src/r0/contact/get_contacts.rs
2020-04-17 23:54:13 +02:00

82 lines
2.5 KiB
Rust

//! [GET /_matrix/client/r0/account/3pid](https://matrix.org/docs/spec/client_server/r0.6.0#get-matrix-client-r0-account-3pid)
use std::time::SystemTime;
use ruma_api::ruma_api;
use serde::{Deserialize, Serialize};
use crate::r0::thirdparty::Medium;
ruma_api! {
metadata {
description: "Get a list of 3rd party contacts associated with the user's account.",
method: GET,
name: "get_contacts",
path: "/_matrix/client/r0/account/3pid",
rate_limited: false,
requires_authentication: true,
}
request {}
response {
/// A list of third party identifiers the homeserver has associated with the user's
/// account.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub threepids: Vec<ThirdPartyIdentifier>,
}
error: crate::Error
}
/// An identifier external to Matrix.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(test, derive(PartialEq))]
pub struct ThirdPartyIdentifier {
/// The third party identifier address.
pub address: String,
/// The medium of third party identifier.
pub medium: Medium,
/// The time when the identifier was validated by the identity server.
#[serde(with = "ruma_serde::time::ms_since_unix_epoch")]
pub validated_at: SystemTime,
/// The time when the homeserver associated the third party identifier with the user.
#[serde(with = "ruma_serde::time::ms_since_unix_epoch")]
pub added_at: SystemTime,
}
#[cfg(test)]
mod tests {
use std::time::{Duration, UNIX_EPOCH};
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
use super::{Medium, ThirdPartyIdentifier};
#[test]
fn third_party_identifier_serde() {
let third_party_id = ThirdPartyIdentifier {
address: "monkey@banana.island".into(),
medium: Medium::Email,
validated_at: UNIX_EPOCH + Duration::from_millis(1_535_176_800_000),
added_at: UNIX_EPOCH + Duration::from_millis(1_535_336_848_756),
};
let third_party_id_serialized = json!({
"medium": "email",
"address": "monkey@banana.island",
"validated_at": 1_535_176_800_000u64,
"added_at": 1_535_336_848_756u64
});
assert_eq!(
to_json_value(third_party_id.clone()).unwrap(),
third_party_id_serialized
);
assert_eq!(
third_party_id,
from_json_value(third_party_id_serialized).unwrap()
);
}
}