1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use ruma_api::ruma_api;
use ruma_common::{thirdparty::Medium, MilliSecondsSinceUnixEpoch};
use serde::{Deserialize, Serialize};
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,
authentication: AccessToken,
}
#[derive(Default)]
request: {}
response: {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub threepids: Vec<ThirdPartyIdentifier>,
}
error: crate::Error
}
impl Request {
pub fn new() -> Self {
Self
}
}
impl Response {
pub fn new(threepids: Vec<ThirdPartyIdentifier>) -> Self {
Self { threepids }
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[cfg_attr(test, derive(PartialEq))]
pub struct ThirdPartyIdentifier {
pub address: String,
pub medium: Medium,
pub validated_at: MilliSecondsSinceUnixEpoch,
pub added_at: MilliSecondsSinceUnixEpoch,
}
#[derive(Debug)]
pub struct ThirdPartyIdentifierInit {
pub address: String,
pub medium: Medium,
pub validated_at: MilliSecondsSinceUnixEpoch,
pub added_at: MilliSecondsSinceUnixEpoch,
}
impl From<ThirdPartyIdentifierInit> for ThirdPartyIdentifier {
fn from(init: ThirdPartyIdentifierInit) -> Self {
let ThirdPartyIdentifierInit { address, medium, validated_at, added_at } = init;
ThirdPartyIdentifier { address, medium, validated_at, added_at }
}
}
#[cfg(test)]
mod tests {
use std::convert::TryInto;
use ruma_common::MilliSecondsSinceUnixEpoch;
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: MilliSecondsSinceUnixEpoch(1_535_176_800_000_u64.try_into().unwrap()),
added_at: MilliSecondsSinceUnixEpoch(1_535_336_848_756_u64.try_into().unwrap()),
};
let third_party_id_serialized = json!({
"medium": "email",
"address": "monkey@banana.island",
"validated_at": 1_535_176_800_000_u64,
"added_at": 1_535_336_848_756_u64
});
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());
}
}