From 13e3b45070d1f1992c3097da28b06ef2f55ce465 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Tue, 1 Jun 2021 21:53:58 +0200 Subject: [PATCH] Move ThirdPartyIdentifier(Init) from client-api to common While it's not currently used in any other crates in this repo, synapse-admin-api does use it and it seems logical for it to be defined alongside the other thirdparty types in common. --- .../src/r0/contact/get_contacts.rs | 80 +------------------ crates/ruma-common/CHANGELOG.md | 4 + crates/ruma-common/src/thirdparty.rs | 79 ++++++++++++++++++ 3 files changed, 84 insertions(+), 79 deletions(-) diff --git a/crates/ruma-client-api/src/r0/contact/get_contacts.rs b/crates/ruma-client-api/src/r0/contact/get_contacts.rs index b567557d..ccf6880c 100644 --- a/crates/ruma-client-api/src/r0/contact/get_contacts.rs +++ b/crates/ruma-client-api/src/r0/contact/get_contacts.rs @@ -1,8 +1,7 @@ //! [GET /_matrix/client/r0/account/3pid](https://matrix.org/docs/spec/client_server/r0.6.0#get-matrix-client-r0-account-3pid) use ruma_api::ruma_api; -use ruma_common::{thirdparty::Medium, MilliSecondsSinceUnixEpoch}; -use serde::{Deserialize, Serialize}; +pub use ruma_common::thirdparty::{ThirdPartyIdentifier, ThirdPartyIdentifierInit}; ruma_api! { metadata: { @@ -40,80 +39,3 @@ impl Response { Self { threepids } } } - -/// An identifier external to Matrix. -/// -/// To create an instance of this type, first create a `ThirdPartyIdentifierInit` and convert it to -/// this type using `ThirdPartyIdentifier::Init` / `.into()`. -#[derive(Clone, Debug, Deserialize, Serialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -#[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. - pub validated_at: MilliSecondsSinceUnixEpoch, - - /// The time when the homeserver associated the third party identifier with the user. - pub added_at: MilliSecondsSinceUnixEpoch, -} - -/// Initial set of fields of `ThirdPartyIdentifier`. -/// -/// This struct will not be updated even if additional fields are added to `ThirdPartyIdentifier` -/// in a new (non-breaking) release of the Matrix specification. -#[derive(Debug)] -pub struct ThirdPartyIdentifierInit { - /// 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. - pub validated_at: MilliSecondsSinceUnixEpoch, - - /// The time when the homeserver associated the third party identifier with the user. - pub added_at: MilliSecondsSinceUnixEpoch, -} - -impl From 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()); - } -} diff --git a/crates/ruma-common/CHANGELOG.md b/crates/ruma-common/CHANGELOG.md index cdd21b36..03faf227 100644 --- a/crates/ruma-common/CHANGELOG.md +++ b/crates/ruma-common/CHANGELOG.md @@ -1,5 +1,9 @@ # [unreleased] +Improvements: + +* Add `thirdparty::ThirdPartyIdentifier` + # 0.5.1 Improvements: diff --git a/crates/ruma-common/src/thirdparty.rs b/crates/ruma-common/src/thirdparty.rs index 1453cd86..4eee803f 100644 --- a/crates/ruma-common/src/thirdparty.rs +++ b/crates/ruma-common/src/thirdparty.rs @@ -8,6 +8,8 @@ use ruma_identifiers::{RoomAliasId, UserId}; use ruma_serde::StringEnum; use serde::{Deserialize, Serialize}; +use crate::MilliSecondsSinceUnixEpoch; + /// Metadata about a third party protocol. /// /// To create an instance of this type, first create a `ProtocolInit` and convert it via @@ -196,3 +198,80 @@ pub enum Medium { #[doc(hidden)] _Custom(String), } + +/// An identifier external to Matrix. +/// +/// To create an instance of this type, first create a `ThirdPartyIdentifierInit` and convert it to +/// this type using `ThirdPartyIdentifier::Init` / `.into()`. +#[derive(Clone, Debug, Deserialize, Serialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +#[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. + pub validated_at: MilliSecondsSinceUnixEpoch, + + /// The time when the homeserver associated the third party identifier with the user. + pub added_at: MilliSecondsSinceUnixEpoch, +} + +/// Initial set of fields of `ThirdPartyIdentifier`. +/// +/// This struct will not be updated even if additional fields are added to `ThirdPartyIdentifier` +/// in a new (non-breaking) release of the Matrix specification. +#[derive(Debug)] +pub struct ThirdPartyIdentifierInit { + /// 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. + pub validated_at: MilliSecondsSinceUnixEpoch, + + /// The time when the homeserver associated the third party identifier with the user. + pub added_at: MilliSecondsSinceUnixEpoch, +} + +impl From 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 serde_json::{from_value as from_json_value, json, to_value as to_json_value}; + + use super::{Medium, ThirdPartyIdentifier}; + use crate::MilliSecondsSinceUnixEpoch; + + #[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()); + } +}