1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//! Types for the *m.room_key_request* event.

use ruma_events_macros::ruma_event;
use ruma_identifiers::{DeviceId, RoomId};
use serde::{Deserialize, Serialize};

use super::Algorithm;

ruma_event! {
    /// This event type is used to request keys for end-to-end encryption.
    ///
    /// It is sent as an unencrypted to-device event.
    RoomKeyRequestEvent {
        kind: Event,
        event_type: "m.room_key_request",
        content: {
            /// Whether this is a new key request or a cancellation of a previous request.
            pub action: Action,

            /// Information about the requested key.
            ///
            /// Required when action is `request`.
            pub body: Option<RequestedKeyInfo>,

            /// ID of the device requesting the key.
            pub requesting_device_id: DeviceId,

            /// A random string uniquely identifying the request for a key.
            ///
            /// If the key is requested multiple times, it should be reused. It should also reused
            /// in order to cancel a request.
            pub request_id: String,
        },
    }
}

/// A new key request or a cancellation of a previous request.
#[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)]
pub enum Action {
    /// Request a key.
    #[serde(rename = "request")]
    Request,

    /// Cancel a request for a key.
    #[serde(rename = "request_cancellation")]
    CancelRequest,
}

impl_enum! {
    Action {
        Request => "request",
        CancelRequest => "cancel_request",
    }
}

/// Information about a requested key.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct RequestedKeyInfo {
    /// The encryption algorithm the requested key in this event is to be used with.
    pub algorithm: Algorithm,

    /// The room where the key is used.
    pub room_id: RoomId,

    /// The Curve25519 key of the device which initiated the session originally.
    pub sender_key: String,

    /// The ID of the session that the key is for.
    pub session_id: String,
}