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
45use ruma_common::{
6 serde::StringEnum, EventEncryptionAlgorithm, OwnedDeviceId, OwnedRoomId, OwnedTransactionId,
7};
8use ruma_macros::EventContent;
9use serde::{Deserialize, Serialize};
1011use crate::PrivOwnedStr;
1213/// 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.
19pub action: Action,
2021/// Information about the requested key.
22 ///
23 /// Required if action is `request`.
24pub body: Option<RequestedKeyInfo>,
2526/// ID of the device requesting the key.
27pub requesting_device_id: OwnedDeviceId,
2829/// 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.
33pub request_id: OwnedTransactionId,
34}
3536impl ToDeviceRoomKeyRequestEventContent {
37/// Creates a new `ToDeviceRoomKeyRequestEventContent` with the given action, boyd, device ID
38 /// and request ID.
39pub fn new(
40 action: Action,
41 body: Option<RequestedKeyInfo>,
42 requesting_device_id: OwnedDeviceId,
43 request_id: OwnedTransactionId,
44 ) -> Self {
45Self { action, body, requesting_device_id, request_id }
46 }
47}
4849/// 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.
56Request,
5758/// Cancel a request for a key.
59#[ruma_enum(rename = "request_cancellation")]
60CancelRequest,
6162#[doc(hidden)]
63_Custom(PrivOwnedStr),
64}
6566/// 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.
71pub algorithm: EventEncryptionAlgorithm,
7273/// The room where the key is used.
74pub room_id: OwnedRoomId,
7576/// 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"]
78pub sender_key: String,
7980/// The ID of the session that the key is for.
81pub session_id: String,
82}
8384impl RequestedKeyInfo {
85/// Creates a new `RequestedKeyInfo` with the given algorithm, room ID, sender key and session
86 /// ID.
87pub fn new(
88 algorithm: EventEncryptionAlgorithm,
89 room_id: OwnedRoomId,
90 sender_key: String,
91 session_id: String,
92 ) -> Self {
93#[allow(deprecated)]
94Self { algorithm, room_id, sender_key, session_id }
95 }
96}