Skip to content

Commit

Permalink
crypto: Create a SenderDataType enum
Browse files Browse the repository at this point in the history
  • Loading branch information
andybalaam authored and richvdh committed Aug 16, 2024
1 parent ebc94bc commit 2797256
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
11 changes: 9 additions & 2 deletions crates/matrix-sdk-crypto/src/olm/group_sessions/inbound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ use vodozemac::{
};

use super::{
BackedUpRoomKey, ExportedRoomKey, OutboundGroupSession, SenderData, SessionCreationError,
SessionKey,
BackedUpRoomKey, ExportedRoomKey, OutboundGroupSession, SenderData, SenderDataType,
SessionCreationError, SessionKey,
};
use crate::{
error::{EventError, MegolmResult},
Expand Down Expand Up @@ -477,6 +477,13 @@ impl InboundGroupSession {
pub(crate) fn mark_as_imported(&mut self) {
self.imported = true;
}

/// Return the [`SenderDataType`] of our [`SenderData`]. This is used during
/// serialization, to allow us to store the type in a separate queryable
/// column/property.
pub fn sender_data_type(&self) -> SenderDataType {
self.sender_data.to_type()
}
}

#[cfg(not(tarpaulin_include))]
Expand Down
2 changes: 1 addition & 1 deletion crates/matrix-sdk-crypto/src/olm/group_sessions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub(crate) use outbound::ShareState;
pub use outbound::{
EncryptionSettings, OutboundGroupSession, PickledOutboundGroupSession, ShareInfo,
};
pub use sender_data::SenderData;
pub use sender_data::{SenderData, SenderDataType};
pub(crate) use sender_data_finder::SenderDataFinder;
use thiserror::Error;
pub use vodozemac::megolm::{ExportedSessionKey, SessionKey};
Expand Down
22 changes: 22 additions & 0 deletions crates/matrix-sdk-crypto/src/olm/group_sessions/sender_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,15 @@ impl SenderData {
SenderData::SenderKnown { master_key_verified: true, .. } => 3,
}
}

/// Return our type: `UnknownDevice`, `DeviceInfo`, or `SenderKnown`.
pub fn to_type(&self) -> SenderDataType {
match self {
Self::UnknownDevice { .. } => SenderDataType::UnknownDevice,
Self::DeviceInfo { .. } => SenderDataType::DeviceInfo,
Self::SenderKnown { .. } => SenderDataType::SenderKnown,
}
}
}

/// Used when deserialising and the sender_data property is missing.
Expand All @@ -169,6 +178,19 @@ impl Default for SenderData {
}
}

/// Used when serializing [`crate::olm::group_sessions::InboundGroupSession`]s.
/// We want just the type of the session's [`SenderData`] to be queryable, so we
/// store the type as a separate column/property in the database.
#[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)]
pub enum SenderDataType {
/// The [`SenderData`] is of type `UnknownDevice`.
UnknownDevice = 1,
/// The [`SenderData`] is of type `DeviceInfo`.
DeviceInfo = 2,
/// The [`SenderData`] is of type `SenderKnown`.
SenderKnown = 3,
}

#[cfg(test)]
mod tests {
use std::{cmp::Ordering, collections::BTreeMap};
Expand Down
2 changes: 1 addition & 1 deletion crates/matrix-sdk-crypto/src/olm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub(crate) use account::{OlmDecryptionInfo, SessionType};
pub use group_sessions::{
BackedUpRoomKey, EncryptionSettings, ExportedRoomKey, InboundGroupSession,
OutboundGroupSession, PickledInboundGroupSession, PickledOutboundGroupSession, SenderData,
SessionCreationError, SessionExportError, SessionKey, ShareInfo,
SenderDataType, SessionCreationError, SessionExportError, SessionKey, ShareInfo,
};
pub(crate) use group_sessions::{SenderDataFinder, ShareState};
pub use session::{PickledSession, Session};
Expand Down

0 comments on commit 2797256

Please sign in to comment.