refactor(crypto): Don't require event_type to return a static string

This commit is contained in:
Damir Jelić
2025-02-19 13:18:50 +01:00
parent 09513eaa5e
commit 8c1966a237
4 changed files with 19 additions and 12 deletions

View File

@@ -539,13 +539,15 @@ impl GossipMachine {
device: &Device,
content: SecretSendContent,
) -> OlmResult<Session> {
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(),
);

View File

@@ -425,18 +425,19 @@ impl Device {
session: InboundGroupSession,
message_index: Option<u32>,
) -> OlmResult<(Session, Raw<ToDeviceEncryptedEventContent>)> {
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<MaybeEncryptedRoomKey> {
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(),

View File

@@ -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(),
);

View File

@@ -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
}
}