From 8c1966a237d34ce126f2b4261032a5aecdbaa2a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Wed, 19 Feb 2025 13:18:50 +0100 Subject: [PATCH] refactor(crypto): Don't require `event_type` to return a static string --- crates/matrix-sdk-crypto/src/gossiping/machine.rs | 12 ++++++++---- crates/matrix-sdk-crypto/src/identities/device.rs | 13 +++++++------ .../src/session_manager/sessions.rs | 4 +++- crates/matrix-sdk-crypto/src/types/events/mod.rs | 2 +- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/crates/matrix-sdk-crypto/src/gossiping/machine.rs b/crates/matrix-sdk-crypto/src/gossiping/machine.rs index 0bb1204e8..a589bef0d 100644 --- a/crates/matrix-sdk-crypto/src/gossiping/machine.rs +++ b/crates/matrix-sdk-crypto/src/gossiping/machine.rs @@ -539,13 +539,15 @@ impl GossipMachine { device: &Device, content: SecretSendContent, ) -> OlmResult { - let event_type = content.event_type(); - let (used_session, content) = device.encrypt(event_type, content).await?; + let event_type = content.event_type().to_owned(); + let (used_session, content) = device.encrypt(&event_type, content).await?; + + let encrypted_event_type = content.event_type().to_owned(); let request = ToDeviceRequest::new( device.user_id(), device.device_id().to_owned(), - content.event_type(), + &encrypted_event_type, content.cast(), ); @@ -568,10 +570,12 @@ impl GossipMachine { let (used_session, content) = device.encrypt_room_key_for_forwarding(session.clone(), message_index).await?; + let event_type = content.event_type().to_owned(); + let request = ToDeviceRequest::new( device.user_id(), device.device_id().to_owned(), - content.event_type(), + &event_type, content.cast(), ); diff --git a/crates/matrix-sdk-crypto/src/identities/device.rs b/crates/matrix-sdk-crypto/src/identities/device.rs index a2ffc20f3..80f4d9fbe 100644 --- a/crates/matrix-sdk-crypto/src/identities/device.rs +++ b/crates/matrix-sdk-crypto/src/identities/device.rs @@ -425,18 +425,19 @@ impl Device { session: InboundGroupSession, message_index: Option, ) -> OlmResult<(Session, Raw)> { - let (event_type, content) = { + let content: ForwardedRoomKeyContent = { let export = if let Some(index) = message_index { session.export_at_index(index).await } else { session.export().await }; - let content: ForwardedRoomKeyContent = export.try_into()?; - (content.event_type(), content) + export.try_into()? }; - self.encrypt(event_type, content).await + let event_type = content.event_type().to_owned(); + + self.encrypt(&event_type, content).await } /// Encrypt an event for this device. @@ -832,9 +833,9 @@ impl DeviceData { ) -> OlmResult { let content = session.as_content().await; let message_index = session.message_index().await; - let event_type = content.event_type(); + let event_type = content.event_type().to_owned(); - match self.encrypt(store, event_type, content).await { + match self.encrypt(store, &event_type, content).await { Ok((session, encrypted)) => Ok(MaybeEncryptedRoomKey::Encrypted { share_info: ShareInfo::new_shared( session.sender_key().to_owned(), diff --git a/crates/matrix-sdk-crypto/src/session_manager/sessions.rs b/crates/matrix-sdk-crypto/src/session_manager/sessions.rs index e7bdc5ba0..94eb006fb 100644 --- a/crates/matrix-sdk-crypto/src/session_manager/sessions.rs +++ b/crates/matrix-sdk-crypto/src/session_manager/sessions.rs @@ -153,10 +153,12 @@ impl SessionManager { let (_, content) = device.encrypt("m.dummy", ToDeviceDummyEventContent::new()).await?; + let event_type = content.event_type().to_owned(); + let request = ToDeviceRequest::new( device.user_id(), device.device_id().to_owned(), - content.event_type(), + &event_type, content.cast(), ); diff --git a/crates/matrix-sdk-crypto/src/types/events/mod.rs b/crates/matrix-sdk-crypto/src/types/events/mod.rs index 8c766554e..a4fe78ec6 100644 --- a/crates/matrix-sdk-crypto/src/types/events/mod.rs +++ b/crates/matrix-sdk-crypto/src/types/events/mod.rs @@ -42,7 +42,7 @@ pub trait EventType { /// /// **Note**: This should never be implemented manually, this takes the /// event type from the constant. - fn event_type(&self) -> &'static str { + fn event_type(&self) -> &str { Self::EVENT_TYPE } }