Skip to content

Commit f565a3a

Browse files
committed
Missing stores
1 parent 86853de commit f565a3a

File tree

3 files changed

+89
-23
lines changed

3 files changed

+89
-23
lines changed

bindings/matrix-sdk-crypto-ffi/src/lib.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ mod test {
701701
use tempfile::tempdir;
702702

703703
use super::MigrationData;
704-
use crate::{migrate, OlmMachine, RoomSettings, EventEncryptionAlgorithm};
704+
use crate::{migrate, EventEncryptionAlgorithm, OlmMachine, RoomSettings};
705705

706706
#[test]
707707
fn android_migration() -> Result<()> {
@@ -824,13 +824,25 @@ mod test {
824824
assert!(backup_keys.is_some());
825825

826826
let settings1 = machine.get_room_settings("!AZkqtjvtwPAuyNOXEt:matrix.org")?;
827-
assert_eq!(Some(RoomSettings { algorithm: EventEncryptionAlgorithm::OlmV1Curve25519AesSha2, only_allow_trusted_devices: true }), settings1);
827+
assert_eq!(
828+
Some(RoomSettings {
829+
algorithm: EventEncryptionAlgorithm::OlmV1Curve25519AesSha2,
830+
only_allow_trusted_devices: true
831+
}),
832+
settings1
833+
);
828834

829835
let settings2 = machine.get_room_settings("!CWLUCoEWXSFyTCOtfL:matrix.org")?;
830-
assert_eq!(Some(RoomSettings { algorithm: EventEncryptionAlgorithm::MegolmV1AesSha2, only_allow_trusted_devices: false }), settings2);
836+
assert_eq!(
837+
Some(RoomSettings {
838+
algorithm: EventEncryptionAlgorithm::MegolmV1AesSha2,
839+
only_allow_trusted_devices: false
840+
}),
841+
settings2
842+
);
831843

832844
let settings3 = machine.get_room_settings("!XYZ:matrix.org")?;
833-
assert!(settings3.is_none());
845+
assert!(settings3.is_none());
834846

835847
Ok(())
836848
}

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

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ use matrix_sdk_crypto::{
3636
use matrix_sdk_store_encryption::StoreCipher;
3737
use ruma::{DeviceId, OwnedDeviceId, RoomId, TransactionId, UserId};
3838
use serde::{de::DeserializeOwned, Serialize};
39-
use tracing::warn;
4039
use wasm_bindgen::JsValue;
4140
use web_sys::IdbKeyRange;
4241

@@ -63,6 +62,8 @@ mod KEYS {
6362
pub const SECRET_REQUESTS_BY_INFO: &str = "secret_requests_by_info";
6463
pub const KEY_REQUEST: &str = "key_request";
6564

65+
pub const ROOM_SETTINGS: &str = "room_settings";
66+
6667
// KEYS
6768
pub const STORE_CIPHER: &str = "store_cipher";
6869
pub const ACCOUNT: &str = "account";
@@ -168,6 +169,8 @@ impl IndexeddbCryptoStore {
168169
db.create_object_store(KEYS::SECRET_REQUESTS_BY_INFO)?;
169170

170171
db.create_object_store(KEYS::BACKUP_KEYS)?;
172+
173+
db.create_object_store(KEYS::ROOM_SETTINGS)?;
171174
} else if old_version < 1.1 {
172175
// We changed how we store inbound group sessions, the key used to
173176
// be a trippled of `(room_id, sender_key, session_id)` now it's a
@@ -369,6 +372,7 @@ impl_crypto_store! {
369372
(!changes.inbound_group_sessions.is_empty(), KEYS::INBOUND_GROUP_SESSIONS),
370373
(!changes.outbound_group_sessions.is_empty(), KEYS::OUTBOUND_GROUP_SESSIONS),
371374
(!changes.message_hashes.is_empty(), KEYS::OLM_HASHES),
375+
(!changes.room_settings.is_empty(), KEYS::ROOM_SETTINGS),
372376
]
373377
.iter()
374378
.filter_map(|(id, key)| if *id { Some(*key) } else { None })
@@ -477,6 +481,7 @@ impl_crypto_store! {
477481
let identity_changes = changes.identities;
478482
let olm_hashes = changes.message_hashes;
479483
let key_requests = changes.key_requests;
484+
let room_settings_changes = changes.room_settings;
480485

481486
if !device_changes.new.is_empty() || !device_changes.changed.is_empty() {
482487
let device_store = tx.object_store(KEYS::DEVICES)?;
@@ -541,6 +546,16 @@ impl_crypto_store! {
541546
}
542547
}
543548

549+
if !room_settings_changes.is_empty() {
550+
let settings_store = tx.object_store(KEYS::ROOM_SETTINGS)?;
551+
552+
for (room_id, settings) in &room_settings_changes {
553+
let key = self.encode_key(KEYS::ROOM_SETTINGS, room_id);
554+
let value = self.serialize_value(&settings)?;
555+
settings_store.put_key_val(&key, &value)?;
556+
}
557+
}
558+
544559
tx.await.into_result()?;
545560

546561
// all good, let's update our caches:indexeddb
@@ -954,18 +969,35 @@ impl_crypto_store! {
954969
Ok(key)
955970
}
956971

957-
async fn get_room_settings(&self, _room_id: &RoomId) -> Result<Option<RoomSettings>> {
958-
warn!("Method not implemented");
959-
Ok(None)
972+
async fn get_room_settings(&self, room_id: &RoomId) -> Result<Option<RoomSettings>> {
973+
let key = self.encode_key(KEYS::ROOM_SETTINGS, room_id);
974+
Ok(self
975+
.inner
976+
.transaction_on_one_with_mode(KEYS::ROOM_SETTINGS, IdbTransactionMode::Readonly)?
977+
.object_store(KEYS::ROOM_SETTINGS)?
978+
.get(&key)?
979+
.await?
980+
.map(|v| self.deserialize_value(v))
981+
.transpose()?)
960982
}
961983

962-
async fn get_custom_value(&self, _key: &str) -> Result<Option<Vec<u8>>> {
963-
warn!("Method not implemented");
964-
Ok(None)
984+
async fn get_custom_value(&self, key: &str) -> Result<Option<Vec<u8>>> {
985+
Ok(self
986+
.inner
987+
.transaction_on_one_with_mode(KEYS::CORE, IdbTransactionMode::Readonly)?
988+
.object_store(KEYS::CORE)?
989+
.get(&JsValue::from_str(key))?
990+
.await?
991+
.map(|v| self.deserialize_value(v))
992+
.transpose()?)
965993
}
966994

967-
async fn set_custom_value(&self, _key: &str, _value: Vec<u8>) -> Result<()> {
968-
warn!("Method not implemented");
995+
async fn set_custom_value(&self, key: &str, value: Vec<u8>) -> Result<()> {
996+
self
997+
.inner
998+
.transaction_on_one_with_mode(KEYS::CORE, IdbTransactionMode::Readwrite)?
999+
.object_store(KEYS::CORE)?
1000+
.put_key_val(&JsValue::from_str(key), &self.serialize_value(&value)?)?;
9691001
Ok(())
9701002
}
9711003
}

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

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ use sled::{
4141
transaction::{ConflictableTransactionError, TransactionError},
4242
Batch, Config, Db, IVec, Transactional, Tree,
4343
};
44-
use tracing::{debug, warn};
44+
use tracing::debug;
4545

4646
use super::OpenStoreError;
4747
use crate::encode_key::{EncodeKey, ENCODE_SEPARATOR};
4848

49-
const DATABASE_VERSION: u8 = 6;
49+
const DATABASE_VERSION: u8 = 7;
5050

5151
// Table names that are used to derive a separate key for each tree. This ensure
5252
// that user ids encoded for different trees won't end up as the same byte
@@ -58,6 +58,7 @@ const INBOUND_GROUP_TABLE_NAME: &str = "crypto-store-inbound-group-sessions";
5858
const OUTBOUND_GROUP_TABLE_NAME: &str = "crypto-store-outbound-group-sessions";
5959
const SECRET_REQUEST_BY_INFO_TABLE: &str = "crypto-store-secret-request-by-info";
6060
const TRACKED_USERS_TABLE: &str = "crypto-store-secret-tracked-users";
61+
const ROOM_SETTINGS_TABLE: &str = "crypto-store-secret-room-settings";
6162

6263
impl EncodeKey for InboundGroupSession {
6364
fn encode(&self) -> Vec<u8> {
@@ -185,6 +186,8 @@ pub struct SledCryptoStore {
185186
identities: Tree,
186187

187188
tracked_users: Tree,
189+
190+
room_settings: Tree,
188191
}
189192

190193
impl std::fmt::Debug for SledCryptoStore {
@@ -386,6 +389,8 @@ impl SledCryptoStore {
386389
let unsent_secret_requests = db.open_tree("unsent_secret_requests")?;
387390
let secret_requests_by_info = db.open_tree("secret_requests_by_info")?;
388391

392+
let room_settings = db.open_tree("room_settings")?;
393+
389394
let session_cache = SessionStore::new();
390395

391396
let database = Self {
@@ -406,6 +411,7 @@ impl SledCryptoStore {
406411
tracked_users,
407412
olm_hashes,
408413
identities,
414+
room_settings,
409415
};
410416

411417
database.upgrade().await?;
@@ -499,6 +505,7 @@ impl SledCryptoStore {
499505
let olm_hashes = changes.message_hashes;
500506
let key_requests = changes.key_requests;
501507
let backup_version = changes.backup_version;
508+
let room_settings_changes = changes.room_settings;
502509

503510
let ret: Result<(), TransactionError<CryptoStoreError>> = (
504511
&self.account,
@@ -512,6 +519,7 @@ impl SledCryptoStore {
512519
&self.outgoing_secret_requests,
513520
&self.unsent_secret_requests,
514521
&self.secret_requests_by_info,
522+
&self.room_settings,
515523
)
516524
.transaction(
517525
|(
@@ -526,6 +534,7 @@ impl SledCryptoStore {
526534
outgoing_secret_requests,
527535
unsent_secret_requests,
528536
secret_requests_by_info,
537+
room_settings,
529538
)| {
530539
if let Some(a) = &account_pickle {
531540
account.insert(
@@ -635,6 +644,15 @@ impl SledCryptoStore {
635644
}
636645
}
637646

647+
for (room_id, settings) in &room_settings_changes {
648+
let key = self.encode_key(ROOM_SETTINGS_TABLE, room_id);
649+
room_settings.insert(
650+
key.as_slice(),
651+
self.serialize_value(&settings)
652+
.map_err(ConflictableTransactionError::Abort)?,
653+
)?;
654+
}
655+
638656
Ok(())
639657
},
640658
);
@@ -1011,18 +1029,22 @@ impl CryptoStore for SledCryptoStore {
10111029
Ok(key)
10121030
}
10131031

1014-
async fn get_room_settings(&self, _room_id: &RoomId) -> Result<Option<RoomSettings>> {
1015-
warn!("Method not implemented");
1016-
Ok(None)
1032+
async fn get_room_settings(&self, room_id: &RoomId) -> Result<Option<RoomSettings>> {
1033+
let key = self.encode_key(ROOM_SETTINGS_TABLE, room_id);
1034+
self.room_settings
1035+
.get(key)
1036+
.map_err(CryptoStoreError::backend)?
1037+
.map(|p| self.deserialize_value(&p))
1038+
.transpose()
10171039
}
10181040

1019-
async fn get_custom_value(&self, _key: &str) -> Result<Option<Vec<u8>>> {
1020-
warn!("Method not implemented");
1021-
Ok(None)
1041+
async fn get_custom_value(&self, key: &str) -> Result<Option<Vec<u8>>> {
1042+
let value = self.inner.get(key).map_err(CryptoStoreError::backend)?.map(|v| v.to_vec());
1043+
Ok(value)
10221044
}
10231045

1024-
async fn set_custom_value(&self, _key: &str, _value: Vec<u8>) -> Result<()> {
1025-
warn!("Method not implemented");
1046+
async fn set_custom_value(&self, key: &str, value: Vec<u8>) -> Result<()> {
1047+
self.inner.insert(key, value).map_err(CryptoStoreError::backend)?;
10261048
Ok(())
10271049
}
10281050
}

0 commit comments

Comments
 (0)