Skip to content

Commit 24dcbf4

Browse files
committed
feat: Use struct for RoomKeyWithheldEntry, move to store.
1 parent 20246ba commit 24dcbf4

File tree

10 files changed

+44
-70
lines changed

10 files changed

+44
-70
lines changed

crates/matrix-sdk-crypto/src/machine/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,7 +2029,7 @@ impl OlmMachine {
20292029
.store
20302030
.get_withheld_info(room_id, content.session_id())
20312031
.await?
2032-
.map(|e| e.content().withheld_code());
2032+
.map(|e| e.content.withheld_code());
20332033

20342034
if withheld_code.is_some() {
20352035
// Partially withheld, report with a withheld code if we have one.
@@ -2145,7 +2145,7 @@ impl OlmMachine {
21452145
.store
21462146
.get_withheld_info(room_id, session_id)
21472147
.await?
2148-
.map(|e| e.content().withheld_code());
2148+
.map(|e| e.content.withheld_code());
21492149
Err(MegolmError::MissingRoomKey(withheld_code))
21502150
}
21512151
}

crates/matrix-sdk-crypto/src/machine/tests/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,7 @@ async fn test_withheld_unverified() {
10651065

10661066
assert_eq!(&withheld_received[0].room_id, room_id);
10671067
assert_matches!(
1068-
&withheld_received[0].withheld_event.content(),
1068+
&withheld_received[0].withheld_event.content,
10691069
RoomKeyWithheldContent::MegolmV1AesSha2(MegolmV1AesSha2WithheldContent::Unverified(
10701070
unverified_withheld_content
10711071
))

crates/matrix-sdk-crypto/src/store/integration_tests.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ macro_rules! cryptostore_integration_tests {
5353
store::{
5454
types::{
5555
BackupDecryptionKey, Changes, DehydratedDeviceKey, DeviceChanges,
56-
IdentityChanges, PendingChanges, StoredRoomKeyBundleData, RoomSettings,
56+
IdentityChanges, PendingChanges, StoredRoomKeyBundleData, RoomKeyWithheldEntry,
57+
RoomSettings
5758
},
5859
CryptoStore, GossipRequest,
5960
},
@@ -66,7 +67,6 @@ macro_rules! cryptostore_integration_tests {
6667
room_key_withheld::{
6768
CommonWithheldCodeContent, MegolmV1AesSha2WithheldContent,
6869
RoomKeyWithheldContent,
69-
RoomKeyWithheldEntry,
7070
},
7171
room_key_bundle::RoomKeyBundleContent,
7272
secret_send::SecretSendContent,
@@ -1193,16 +1193,16 @@ macro_rules! cryptostore_integration_tests {
11931193

11941194
assert_matches!(
11951195
is_withheld, Some(event)
1196-
if event.content().algorithm() == EventEncryptionAlgorithm::MegolmV1AesSha2 &&
1197-
event.content().withheld_code() == WithheldCode::Unverified
1196+
if event.content.algorithm() == EventEncryptionAlgorithm::MegolmV1AesSha2 &&
1197+
event.content.withheld_code() == WithheldCode::Unverified
11981198
);
11991199

12001200
let is_withheld = store.get_withheld_info(room_id, session_id_2).await.unwrap();
12011201

12021202
assert_matches!(
12031203
is_withheld, Some(event)
1204-
if event.content().algorithm() == EventEncryptionAlgorithm::MegolmV1AesSha2 &&
1205-
event.content().withheld_code() == WithheldCode::Blacklisted
1204+
if event.content.algorithm() == EventEncryptionAlgorithm::MegolmV1AesSha2 &&
1205+
event.content.withheld_code() == WithheldCode::Blacklisted
12061206
);
12071207

12081208
let other_room_id = room_id!("!nQRyiRFuyUhXeaQfiR:example.com");

crates/matrix-sdk-crypto/src/store/memorystore.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use crate::{
4545
OutboundGroupSession, PickledAccount, PickledInboundGroupSession, PickledSession,
4646
PrivateCrossSigningIdentity, SenderDataType, StaticAccountData,
4747
},
48-
types::events::room_key_withheld::RoomKeyWithheldEntry,
48+
store::types::RoomKeyWithheldEntry,
4949
};
5050

5151
fn encode_key_info(info: &SecretInfo) -> String {
@@ -1272,11 +1272,10 @@ mod integration_tests {
12721272
store::{
12731273
types::{
12741274
BackupKeys, Changes, DehydratedDeviceKey, PendingChanges, RoomKeyCounts,
1275-
RoomSettings, StoredRoomKeyBundleData, TrackedUser,
1275+
RoomKeyWithheldEntry, RoomSettings, StoredRoomKeyBundleData, TrackedUser,
12761276
},
12771277
CryptoStore,
12781278
},
1279-
types::events::room_key_withheld::RoomKeyWithheldEntry,
12801279
Account, DeviceData, GossipRequest, GossippedSecret, SecretInfo, Session, UserIdentityData,
12811280
};
12821281

crates/matrix-sdk-crypto/src/store/mod.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ use crate::{
7676
Account, ExportedRoomKey, InboundGroupSession, PrivateCrossSigningIdentity, SenderData,
7777
Session, StaticAccountData,
7878
},
79+
store::types::RoomKeyWithheldEntry,
7980
types::{
80-
events::room_key_withheld::{MegolmV1AesSha2WithheldContent, RoomKeyWithheldEntry},
81-
BackupSecrets, CrossSigningSecrets, MegolmBackupV1Curve25519AesSha2Secrets, RoomKeyExport,
82-
SecretsBundle,
81+
events::room_key_withheld::MegolmV1AesSha2WithheldContent, BackupSecrets,
82+
CrossSigningSecrets, MegolmBackupV1Curve25519AesSha2Secrets, RoomKeyExport, SecretsBundle,
8383
},
8484
verification::VerificationMachine,
8585
CrossSigningStatus, OwnUserIdentityData, RoomKeyImportResult,
@@ -1695,7 +1695,7 @@ impl Store {
16951695
{
16961696
changes.withheld_session_info.entry(c.room_id.to_owned()).or_default().insert(
16971697
c.session_id.to_owned(),
1698-
RoomKeyWithheldEntry::Bundle {
1698+
RoomKeyWithheldEntry {
16991699
sender: bundle_info.sender_user.clone(),
17001700
content: withheld.to_owned(),
17011701
},
@@ -1753,13 +1753,11 @@ mod tests {
17531753
use crate::{
17541754
machine::test_helpers::get_machine_pair,
17551755
olm::{InboundGroupSession, SenderData},
1756-
store::types::{DehydratedDeviceKey, StoredRoomKeyBundleData},
1756+
store::types::{DehydratedDeviceKey, RoomKeyWithheldEntry, StoredRoomKeyBundleData},
17571757
types::{
17581758
events::{
17591759
room_key_bundle::RoomKeyBundleContent,
1760-
room_key_withheld::{
1761-
MegolmV1AesSha2WithheldContent, RoomKeyWithheldContent, RoomKeyWithheldEntry,
1762-
},
1760+
room_key_withheld::{MegolmV1AesSha2WithheldContent, RoomKeyWithheldContent},
17631761
},
17641762
EventEncryptionAlgorithm,
17651763
},
@@ -2082,7 +2080,7 @@ mod tests {
20822080

20832081
assert_matches!(
20842082
bob.store().get_withheld_info(room_id, sessions[1].session_id()).await.unwrap(),
2085-
Some(RoomKeyWithheldEntry::Bundle {
2083+
Some(RoomKeyWithheldEntry {
20862084
content: RoomKeyWithheldContent::MegolmV1AesSha2(
20872085
MegolmV1AesSha2WithheldContent::Unauthorised(_)
20882086
),

crates/matrix-sdk-crypto/src/store/traits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use crate::{
3535
InboundGroupSession, OlmMessageHash, OutboundGroupSession, PrivateCrossSigningIdentity,
3636
SenderDataType, Session,
3737
},
38-
types::events::room_key_withheld::RoomKeyWithheldEntry,
38+
store::types::RoomKeyWithheldEntry,
3939
Account, DeviceData, GossipRequest, GossippedSecret, SecretInfo, UserIdentityData,
4040
};
4141

crates/matrix-sdk-crypto/src/store/types.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ use crate::{
3434
SenderData,
3535
},
3636
types::{
37-
events::{room_key_bundle::RoomKeyBundleContent, room_key_withheld::RoomKeyWithheldEntry},
37+
events::{
38+
room_key_bundle::RoomKeyBundleContent,
39+
room_key_withheld::{RoomKeyWithheldContent, RoomKeyWithheldEvent},
40+
},
3841
EventEncryptionAlgorithm,
3942
},
4043
Account, Device, DeviceData, GossippedSecret, Session, UserIdentity, UserIdentityData,
@@ -489,6 +492,22 @@ pub struct RoomKeyWithheldInfo {
489492
pub withheld_event: RoomKeyWithheldEntry,
490493
}
491494

495+
/// Represents an entry for a withheld room key event, which can be either a
496+
/// to-device event or a bundle entry.
497+
#[derive(Clone, Debug, Serialize, Deserialize)]
498+
pub struct RoomKeyWithheldEntry {
499+
/// The ID of the user who sent the bundle.
500+
pub sender: OwnedUserId,
501+
/// The contents of a single withheld entry in the bundle.
502+
pub content: RoomKeyWithheldContent,
503+
}
504+
505+
impl From<RoomKeyWithheldEvent> for RoomKeyWithheldEntry {
506+
fn from(value: RoomKeyWithheldEvent) -> Self {
507+
Self { sender: value.sender, content: value.content }
508+
}
509+
}
510+
492511
/// Information about a received historic room key bundle.
493512
///
494513
/// This struct contains information needed to uniquely identify a room key

crates/matrix-sdk-crypto/src/types/events/room_key_withheld.rs

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,46 +28,6 @@ use vodozemac::Curve25519PublicKey;
2828
use super::{EventType, ToDeviceEvent};
2929
use crate::types::{deserialize_curve_key, serialize_curve_key, EventEncryptionAlgorithm};
3030

31-
/// Represents an entry for a withheld room key event, which can be either a
32-
/// to-device event or a bundle entry.
33-
#[derive(Clone, Debug, Serialize, Deserialize)]
34-
#[serde(untagged)]
35-
pub enum RoomKeyWithheldEntry {
36-
/// A to-device event containing withheld room key information.
37-
ToDevice(RoomKeyWithheldEvent),
38-
/// Content held within a room key bundle.
39-
Bundle {
40-
/// The ID of the user who sent the bundle.
41-
sender: OwnedUserId,
42-
/// The contents of a single withheld entry in the bundle.
43-
content: RoomKeyWithheldContent,
44-
},
45-
}
46-
47-
impl RoomKeyWithheldEntry {
48-
/// Returns a reference to the underlying `RoomKeyWithheldContent`.
49-
pub fn content(&self) -> &RoomKeyWithheldContent {
50-
match self {
51-
RoomKeyWithheldEntry::ToDevice(ev) => &ev.content,
52-
RoomKeyWithheldEntry::Bundle { content, .. } => content,
53-
}
54-
}
55-
56-
/// Returns the sender's user ID, if available.
57-
pub fn sender(&self) -> &UserId {
58-
match self {
59-
RoomKeyWithheldEntry::ToDevice(ev) => &ev.sender,
60-
RoomKeyWithheldEntry::Bundle { sender, .. } => sender,
61-
}
62-
}
63-
}
64-
65-
impl From<RoomKeyWithheldEvent> for RoomKeyWithheldEntry {
66-
fn from(value: RoomKeyWithheldEvent) -> Self {
67-
Self::ToDevice(value)
68-
}
69-
}
70-
7131
/// The `m.room_key_request` to-device event.
7232
pub type RoomKeyWithheldEvent = ToDeviceEvent<RoomKeyWithheldContent>;
7333

crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,11 @@ use matrix_sdk_crypto::{
3030
},
3131
store::{
3232
types::{
33-
BackupKeys, Changes, DehydratedDeviceKey, PendingChanges, RoomKeyCounts, RoomSettings,
34-
StoredRoomKeyBundleData,
33+
BackupKeys, Changes, DehydratedDeviceKey, PendingChanges, RoomKeyCounts,
34+
RoomKeyWithheldEntry, RoomSettings, StoredRoomKeyBundleData,
3535
},
3636
CryptoStore, CryptoStoreError,
3737
},
38-
types::events::room_key_withheld::RoomKeyWithheldEntry,
3938
vodozemac::base64_encode,
4039
Account, DeviceData, GossipRequest, GossippedSecret, SecretInfo, TrackedUser, UserIdentityData,
4140
};

crates/matrix-sdk-sqlite/src/crypto_store.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@ use matrix_sdk_crypto::{
2828
},
2929
store::{
3030
types::{
31-
BackupKeys, Changes, DehydratedDeviceKey, PendingChanges, RoomKeyCounts, RoomSettings,
32-
StoredRoomKeyBundleData,
31+
BackupKeys, Changes, DehydratedDeviceKey, PendingChanges, RoomKeyCounts,
32+
RoomKeyWithheldEntry, RoomSettings, StoredRoomKeyBundleData,
3333
},
3434
CryptoStore,
3535
},
36-
types::events::room_key_withheld::RoomKeyWithheldEntry,
3736
Account, DeviceData, GossipRequest, GossippedSecret, SecretInfo, TrackedUser, UserIdentityData,
3837
};
3938
use matrix_sdk_store_encryption::StoreCipher;
@@ -1894,7 +1893,7 @@ mod tests {
18941893
.expect("This session should be withheld")
18951894
.unwrap();
18961895

1897-
assert_eq!(withheld_info.content().withheld_code(), WithheldCode::Unverified);
1896+
assert_eq!(withheld_info.content.withheld_code(), WithheldCode::Unverified);
18981897

18991898
let backup_keys = database.load_backup_keys().await.expect("backup key should be cached");
19001899
assert_eq!(backup_keys.backup_version.unwrap(), "6");

0 commit comments

Comments
 (0)