diff --git a/crates/matrix-sdk-crypto/src/backups/mod.rs b/crates/matrix-sdk-crypto/src/backups/mod.rs index 372c7cb21..1568aa548 100644 --- a/crates/matrix-sdk-crypto/src/backups/mod.rs +++ b/crates/matrix-sdk-crypto/src/backups/mod.rs @@ -619,7 +619,15 @@ impl BackupMachine { } } - self.store.import_room_keys(decrypted_room_keys, true, progress_listener).await + // FIXME: This method is a bit flawed: we have no real idea which backup version + // these keys came from. For example, we might have reset the backup + // since the keys were downloaded. For now, let's assume they came from + // the "current" backup version. + let backup_version = self.backup_version().await; + + self.store + .import_room_keys(decrypted_room_keys, backup_version.as_deref(), progress_listener) + .await } } diff --git a/crates/matrix-sdk-crypto/src/machine.rs b/crates/matrix-sdk-crypto/src/machine.rs index 91bf21338..4479c1541 100644 --- a/crates/matrix-sdk-crypto/src/machine.rs +++ b/crates/matrix-sdk-crypto/src/machine.rs @@ -1823,7 +1823,11 @@ impl OlmMachine { from_backup: bool, progress_listener: impl Fn(usize, usize), ) -> StoreResult { - self.store().import_room_keys(exported_keys, from_backup, progress_listener).await + let backup_version = + if from_backup { self.backup_machine().backup_version().await } else { None }; + self.store() + .import_room_keys(exported_keys, backup_version.as_deref(), progress_listener) + .await } /// Get the status of the private cross signing keys. diff --git a/crates/matrix-sdk-crypto/src/store/mod.rs b/crates/matrix-sdk-crypto/src/store/mod.rs index bbc184719..0ec4e36c3 100644 --- a/crates/matrix-sdk-crypto/src/store/mod.rs +++ b/crates/matrix-sdk-crypto/src/store/mod.rs @@ -1630,10 +1630,22 @@ impl Store { self.inner.store.secrets_stream() } + /// Import the given room keys into the store. + /// + /// # Arguments + /// + /// * `exported_keys` - The keys to be imported. + /// * `from_backup_version` - If the keys came from key backup, the key + /// backup version. This will cause the keys to be marked as already + /// backed up, and therefore not requiring another backup. + /// * `progress_listener` - Callback which will be called after each key is + /// processed. Called with arguments `(processed, total)` where + /// `processed` is the number of keys processed so far, and `total` is the + /// total number of keys (i.e., `exported_keys.len()`). pub(crate) async fn import_room_keys( &self, exported_keys: Vec, - from_backup: bool, + from_backup_version: Option<&str>, progress_listener: impl Fn(usize, usize), ) -> Result { let mut sessions = Vec::new(); @@ -1664,7 +1676,7 @@ impl Store { // Only import the session if we didn't have this session or // if it's a better version of the same session. if new_session_better(&session, old_session).await { - if from_backup { + if from_backup_version.is_some() { session.mark_as_backed_up(); } @@ -1693,9 +1705,7 @@ impl Store { let imported_count = sessions.len(); - let changes = Changes { inbound_group_sessions: sessions, ..Default::default() }; - - self.save_changes(changes).await?; + self.inner.store.save_inbound_group_sessions(sessions, from_backup_version).await?; info!(total_count, imported_count, room_keys = ?keys, "Successfully imported room keys"); @@ -1733,7 +1743,7 @@ impl Store { exported_keys: Vec, progress_listener: impl Fn(usize, usize), ) -> Result { - self.import_room_keys(exported_keys, false, progress_listener).await + self.import_room_keys(exported_keys, None, progress_listener).await } pub(crate) fn crypto_store(&self) -> Arc {