events: Add the to-device variant of the m.key.verification.ready event

This commit is contained in:
Damir Jelić 2021-05-12 15:03:14 +02:00 committed by Jonas Platte
parent cb489729b8
commit 166ee8b9d7
3 changed files with 64 additions and 4 deletions

View File

@ -107,6 +107,9 @@ event_enum! {
"m.room_key_request",
"m.forwarded_room_key",
"m.key.verification.request",
#[cfg(feature = "unstable-pre-spec")]
#[cfg_attr(docsrs, doc(cfg(feature = "unstable-pre-spec")))]
"m.key.verification.ready",
"m.key.verification.start",
"m.key.verification.cancel",
"m.key.verification.accept",

View File

@ -56,6 +56,10 @@ pub enum EventType {
#[ruma_enum(rename = "m.key.verification.mac")]
KeyVerificationMac,
/// m.key.verification.ready
#[ruma_enum(rename = "m.key.verification.ready")]
KeyVerificationReady,
/// m.key.verification.request
#[ruma_enum(rename = "m.key.verification.request")]
KeyVerificationRequest,
@ -229,6 +233,7 @@ mod tests {
serde_json_eq(EventType::KeyVerificationCancel, json!("m.key.verification.cancel"));
serde_json_eq(EventType::KeyVerificationKey, json!("m.key.verification.key"));
serde_json_eq(EventType::KeyVerificationMac, json!("m.key.verification.mac"));
serde_json_eq(EventType::KeyVerificationReady, json!("m.key.verification.ready"));
serde_json_eq(EventType::KeyVerificationRequest, json!("m.key.verification.request"));
serde_json_eq(EventType::KeyVerificationStart, json!("m.key.verification.start"));
serde_json_eq(EventType::IgnoredUserList, json!("m.ignored_user_list"));

View File

@ -1,6 +1,6 @@
//! Types for the *m.key.verification.ready* event.
use ruma_events_macros::MessageEventContent;
use ruma_events_macros::{EventContent, MessageEventContent};
use ruma_identifiers::DeviceIdBox;
use serde::{Deserialize, Serialize};
@ -10,7 +10,25 @@ use crate::MessageEvent;
/// Response to a previously sent *m.key.verification.request* message.
pub type ReadyEvent = MessageEvent<ReadyEventContent>;
/// The payload for `ReadyEvent`.
/// The payload for a to-device `m.key.verification.ready` event.
#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
#[ruma_event(type = "m.key.verification.ready")]
pub struct ReadyToDeviceEventContent {
/// The device ID which is initiating the request.
pub from_device: DeviceIdBox,
/// The verification methods supported by the sender.
pub methods: Vec<VerificationMethod>,
/// An opaque identifier for the verification process.
///
/// Must be unique with respect to the devices involved. Must be the same as the
/// `transaction_id` given in the *m.key.verification.request* from a
/// request.
pub transaction_id: String,
}
/// The payload for an in-room `m.key.verification.ready` event.
#[derive(Clone, Debug, Deserialize, Serialize, MessageEventContent)]
#[ruma_event(type = "m.key.verification.ready")]
pub struct ReadyEventContent {
@ -33,7 +51,7 @@ mod tests {
use ruma_serde::Raw;
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
use super::{ReadyEventContent, Relation, VerificationMethod};
use super::{ReadyEventContent, ReadyToDeviceEventContent, Relation, VerificationMethod};
#[test]
fn serialization() {
@ -50,12 +68,26 @@ mod tests {
});
let content = ReadyEventContent {
from_device: device,
from_device: device.clone(),
relation: Relation { event_id },
methods: vec![VerificationMethod::MSasV1],
};
assert_eq!(to_json_value(&content).unwrap(), json_data);
let json_data = json!({
"from_device": device,
"methods": ["m.sas.v1"],
"transaction_id": "456",
});
let content = ReadyToDeviceEventContent {
from_device: device,
transaction_id: "456".to_owned(),
methods: vec![VerificationMethod::MSasV1],
};
assert_eq!(to_json_value(&content).unwrap(), json_data);
}
#[test]
@ -87,5 +119,25 @@ mod tests {
&& methods == vec![VerificationMethod::MSasV1]
&& event_id == id
);
let json_data = json!({
"from_device": device,
"methods": ["m.sas.v1"],
"transaction_id": "456",
});
assert_matches!(
from_json_value::<Raw<ReadyToDeviceEventContent>>(json_data)
.unwrap()
.deserialize()
.unwrap(),
ReadyToDeviceEventContent {
from_device,
transaction_id,
methods,
} if from_device == device
&& methods == vec![VerificationMethod::MSasV1]
&& transaction_id == "456"
);
}
}