ruma_events/key/verification/
ready.rs

1//! Types for the [`m.key.verification.ready`] event.
2//!
3//! [`m.key.verification.ready`]: https://spec.matrix.org/latest/client-server-api/#mkeyverificationready
4
5use ruma_common::{OwnedDeviceId, OwnedTransactionId};
6use ruma_macros::EventContent;
7use serde::{Deserialize, Serialize};
8
9use super::VerificationMethod;
10use crate::relation::Reference;
11
12/// The content of a to-device `m.m.key.verification.ready` event.
13///
14/// Response to a previously sent `m.key.verification.request` message.
15#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
16#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
17#[ruma_event(type = "m.key.verification.ready", kind = ToDevice)]
18pub struct ToDeviceKeyVerificationReadyEventContent {
19    /// The device ID which is initiating the request.
20    pub from_device: OwnedDeviceId,
21
22    /// The verification methods supported by the sender.
23    pub methods: Vec<VerificationMethod>,
24
25    /// An opaque identifier for the verification process.
26    ///
27    /// Must be unique with respect to the devices involved. Must be the same as the
28    /// `transaction_id` given in the `m.key.verification.request` from a
29    /// request.
30    pub transaction_id: OwnedTransactionId,
31}
32
33impl ToDeviceKeyVerificationReadyEventContent {
34    /// Creates a new `ToDeviceKeyVerificationReadyEventContent` with the given device ID,
35    /// verification methods and transaction ID.
36    pub fn new(
37        from_device: OwnedDeviceId,
38        methods: Vec<VerificationMethod>,
39        transaction_id: OwnedTransactionId,
40    ) -> Self {
41        Self { from_device, methods, transaction_id }
42    }
43}
44
45/// The content of an in-room `m.m.key.verification.ready` event.
46///
47/// Response to a previously sent `m.key.verification.request` message.
48#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
49#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
50#[ruma_event(type = "m.key.verification.ready", kind = MessageLike)]
51pub struct KeyVerificationReadyEventContent {
52    /// The device ID which is initiating the request.
53    pub from_device: OwnedDeviceId,
54
55    /// The verification methods supported by the sender.
56    pub methods: Vec<VerificationMethod>,
57
58    /// Relation signaling which verification request this event is responding
59    /// to.
60    #[serde(rename = "m.relates_to")]
61    pub relates_to: Reference,
62}
63
64impl KeyVerificationReadyEventContent {
65    /// Creates a new `KeyVerificationReadyEventContent` with the given device ID, methods and
66    /// reference.
67    pub fn new(
68        from_device: OwnedDeviceId,
69        methods: Vec<VerificationMethod>,
70        relates_to: Reference,
71    ) -> Self {
72        Self { from_device, methods, relates_to }
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use ruma_common::{owned_event_id, OwnedDeviceId};
79    use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
80
81    use super::{KeyVerificationReadyEventContent, ToDeviceKeyVerificationReadyEventContent};
82    use crate::{key::verification::VerificationMethod, relation::Reference};
83
84    #[test]
85    fn serialization() {
86        let event_id = owned_event_id!("$1598361704261elfgc:localhost");
87        let device: OwnedDeviceId = "123".into();
88
89        let json_data = json!({
90            "from_device": device,
91            "methods": ["m.sas.v1"],
92            "m.relates_to": {
93                "rel_type": "m.reference",
94                "event_id": event_id,
95            }
96        });
97
98        let content = KeyVerificationReadyEventContent {
99            from_device: device.clone(),
100            relates_to: Reference { event_id },
101            methods: vec![VerificationMethod::SasV1],
102        };
103
104        assert_eq!(to_json_value(&content).unwrap(), json_data);
105
106        let json_data = json!({
107            "from_device": device,
108            "methods": ["m.sas.v1"],
109            "transaction_id": "456",
110        });
111
112        let content = ToDeviceKeyVerificationReadyEventContent {
113            from_device: device,
114            transaction_id: "456".into(),
115            methods: vec![VerificationMethod::SasV1],
116        };
117
118        assert_eq!(to_json_value(&content).unwrap(), json_data);
119    }
120
121    #[test]
122    fn deserialization() {
123        let json_data = json!({
124            "from_device": "123",
125            "methods": ["m.sas.v1"],
126            "m.relates_to": {
127                "rel_type": "m.reference",
128                "event_id": "$1598361704261elfgc:localhost",
129            }
130        });
131
132        let content = from_json_value::<KeyVerificationReadyEventContent>(json_data).unwrap();
133        assert_eq!(content.from_device, "123");
134        assert_eq!(content.methods, vec![VerificationMethod::SasV1]);
135        assert_eq!(content.relates_to.event_id, "$1598361704261elfgc:localhost");
136
137        let json_data = json!({
138            "from_device": "123",
139            "methods": ["m.sas.v1"],
140            "transaction_id": "456",
141        });
142
143        let content =
144            from_json_value::<ToDeviceKeyVerificationReadyEventContent>(json_data).unwrap();
145        assert_eq!(content.from_device, "123");
146        assert_eq!(content.methods, vec![VerificationMethod::SasV1]);
147        assert_eq!(content.transaction_id, "456");
148    }
149}