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.
This commit is contained in:
Jonas Platte 2021-06-01 21:53:58 +02:00
parent 9b55e36ada
commit 13e3b45070
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
3 changed files with 84 additions and 79 deletions

View File

@ -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<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());
}
}

View File

@ -1,5 +1,9 @@
# [unreleased]
Improvements:
* Add `thirdparty::ThirdPartyIdentifier`
# 0.5.1
Improvements:

View File

@ -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<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 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());
}
}