ruma_events/
room_key_request.rs

1//! Types for the [`m.room_key_request`] event.
2//!
3//! [`m.room_key_request`]: https://spec.matrix.org/latest/client-server-api/#mroom_key_request
4
5use ruma_common::{
6    serde::StringEnum, EventEncryptionAlgorithm, OwnedDeviceId, OwnedRoomId, OwnedTransactionId,
7};
8use ruma_macros::EventContent;
9use serde::{Deserialize, Serialize};
10
11use crate::PrivOwnedStr;
12
13/// The content of an `m.room_key_request` event.
14#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
15#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
16#[ruma_event(type = "m.room_key_request", kind = ToDevice)]
17pub struct ToDeviceRoomKeyRequestEventContent {
18    /// Whether this is a new key request or a cancellation of a previous request.
19    pub action: Action,
20
21    /// Information about the requested key.
22    ///
23    /// Required if action is `request`.
24    pub body: Option<RequestedKeyInfo>,
25
26    /// ID of the device requesting the key.
27    pub requesting_device_id: OwnedDeviceId,
28
29    /// A random string uniquely identifying the request for a key.
30    ///
31    /// If the key is requested multiple times, it should be reused. It should also reused
32    /// in order to cancel a request.
33    pub request_id: OwnedTransactionId,
34}
35
36impl ToDeviceRoomKeyRequestEventContent {
37    /// Creates a new `ToDeviceRoomKeyRequestEventContent` with the given action, boyd, device ID
38    /// and request ID.
39    pub fn new(
40        action: Action,
41        body: Option<RequestedKeyInfo>,
42        requesting_device_id: OwnedDeviceId,
43        request_id: OwnedTransactionId,
44    ) -> Self {
45        Self { action, body, requesting_device_id, request_id }
46    }
47}
48
49/// A new key request or a cancellation of a previous request.
50#[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))]
51#[derive(Clone, PartialEq, Eq, StringEnum)]
52#[ruma_enum(rename_all = "snake_case")]
53#[non_exhaustive]
54pub enum Action {
55    /// Request a key.
56    Request,
57
58    /// Cancel a request for a key.
59    #[ruma_enum(rename = "request_cancellation")]
60    CancelRequest,
61
62    #[doc(hidden)]
63    _Custom(PrivOwnedStr),
64}
65
66/// Information about a requested key.
67#[derive(Clone, Debug, Deserialize, Serialize)]
68#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
69pub struct RequestedKeyInfo {
70    /// The encryption algorithm the requested key in this event is to be used with.
71    pub algorithm: EventEncryptionAlgorithm,
72
73    /// The room where the key is used.
74    pub room_id: OwnedRoomId,
75
76    /// The Curve25519 key of the device which initiated the session originally.
77    #[deprecated = "this field still needs to be sent but should not be used when received"]
78    pub sender_key: String,
79
80    /// The ID of the session that the key is for.
81    pub session_id: String,
82}
83
84impl RequestedKeyInfo {
85    /// Creates a new `RequestedKeyInfo` with the given algorithm, room ID, sender key and session
86    /// ID.
87    pub fn new(
88        algorithm: EventEncryptionAlgorithm,
89        room_id: OwnedRoomId,
90        sender_key: String,
91        session_id: String,
92    ) -> Self {
93        #[allow(deprecated)]
94        Self { algorithm, room_id, sender_key, session_id }
95    }
96}