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::{OwnedDeviceId, canonical_json::assert_to_canonical_json_eq, owned_event_id};
79    use serde_json::{from_value as from_json_value, json};
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 content = KeyVerificationReadyEventContent {
90            from_device: device.clone(),
91            relates_to: Reference { event_id: event_id.clone() },
92            methods: vec![VerificationMethod::SasV1],
93        };
94
95        assert_to_canonical_json_eq!(
96            content,
97            json!({
98                "from_device": device,
99                "methods": ["m.sas.v1"],
100                "m.relates_to": {
101                    "rel_type": "m.reference",
102                    "event_id": event_id,
103                },
104            }),
105        );
106
107        let content = ToDeviceKeyVerificationReadyEventContent {
108            from_device: device.clone(),
109            transaction_id: "456".into(),
110            methods: vec![VerificationMethod::SasV1],
111        };
112
113        assert_to_canonical_json_eq!(
114            content,
115            json!({
116                "from_device": device,
117                "methods": ["m.sas.v1"],
118                "transaction_id": "456",
119            }),
120        );
121    }
122
123    #[test]
124    fn deserialization() {
125        let json_data = json!({
126            "from_device": "123",
127            "methods": ["m.sas.v1"],
128            "m.relates_to": {
129                "rel_type": "m.reference",
130                "event_id": "$1598361704261elfgc:localhost",
131            }
132        });
133
134        let content = from_json_value::<KeyVerificationReadyEventContent>(json_data).unwrap();
135        assert_eq!(content.from_device, "123");
136        assert_eq!(content.methods, vec![VerificationMethod::SasV1]);
137        assert_eq!(content.relates_to.event_id, "$1598361704261elfgc:localhost");
138
139        let json_data = json!({
140            "from_device": "123",
141            "methods": ["m.sas.v1"],
142            "transaction_id": "456",
143        });
144
145        let content =
146            from_json_value::<ToDeviceKeyVerificationReadyEventContent>(json_data).unwrap();
147        assert_eq!(content.from_device, "123");
148        assert_eq!(content.methods, vec![VerificationMethod::SasV1]);
149        assert_eq!(content.transaction_id, "456");
150    }
151}