ruma_events/key/verification/
done.rs

1//! Types for the [`m.key.verification.done`] event.
2//!
3//! [`m.key.verification.done`]: https://spec.matrix.org/latest/client-server-api/#mkeyverificationdone
4
5use ruma_common::OwnedTransactionId;
6use ruma_macros::EventContent;
7use serde::{Deserialize, Serialize};
8
9use crate::relation::Reference;
10
11/// The content of a to-device `m.m.key.verification.done` event.
12///
13/// Event signaling that the interactive key verification has successfully concluded.
14#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
15#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
16#[ruma_event(type = "m.key.verification.done", kind = ToDevice)]
17pub struct ToDeviceKeyVerificationDoneEventContent {
18    /// An opaque identifier for the verification process.
19    ///
20    /// Must be the same as the one used for the `m.key.verification.start` message.
21    pub transaction_id: OwnedTransactionId,
22}
23
24impl ToDeviceKeyVerificationDoneEventContent {
25    /// Creates a new `ToDeviceKeyVerificationDoneEventContent` with the given transaction ID.
26    pub fn new(transaction_id: OwnedTransactionId) -> Self {
27        Self { transaction_id }
28    }
29}
30
31/// The payload for a in-room `m.key.verification.done` event.
32///
33/// Event signaling that the interactive key verification has successfully concluded.
34#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
35#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
36#[ruma_event(type = "m.key.verification.done", kind = MessageLike)]
37pub struct KeyVerificationDoneEventContent {
38    /// Relation signaling which verification request this event is responding to.
39    #[serde(rename = "m.relates_to")]
40    pub relates_to: Reference,
41}
42
43impl KeyVerificationDoneEventContent {
44    /// Creates a new `KeyVerificationDoneEventContent` with the given reference.
45    pub fn new(relates_to: Reference) -> Self {
46        Self { relates_to }
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use ruma_common::{canonical_json::assert_to_canonical_json_eq, owned_event_id};
53    use serde_json::{from_value as from_json_value, json};
54
55    use super::KeyVerificationDoneEventContent;
56    use crate::relation::Reference;
57
58    #[test]
59    fn serialization() {
60        let event_id = owned_event_id!("$1598361704261elfgc:localhost");
61        let content = KeyVerificationDoneEventContent {
62            relates_to: Reference { event_id: event_id.clone() },
63        };
64
65        assert_to_canonical_json_eq!(
66            content,
67            json!({
68                "m.relates_to": {
69                    "rel_type": "m.reference",
70                    "event_id": event_id,
71                },
72            }),
73        );
74    }
75
76    #[test]
77    fn deserialization() {
78        let json_data = json!({
79            "m.relates_to": {
80                "rel_type": "m.reference",
81                "event_id": "$1598361704261elfgc:localhost",
82            }
83        });
84
85        let content = from_json_value::<KeyVerificationDoneEventContent>(json_data).unwrap();
86        assert_eq!(content.relates_to.event_id, "$1598361704261elfgc:localhost");
87    }
88}