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
//! [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()
        );
    }
}