crypto: Add a progress listener for key imports

master
Damir Jelić 2021-04-13 12:47:22 +02:00
parent 53b1845cbe
commit 5132971558
3 changed files with 11 additions and 5 deletions

View File

@ -2330,7 +2330,7 @@ impl Client {
// TODO remove this unwrap. // TODO remove this unwrap.
let import = task.await.expect("Task join error").unwrap(); let import = task.await.expect("Task join error").unwrap();
Ok(olm.import_keys(import).await?) Ok(olm.import_keys(import, |_, _| {}).await?)
} }
} }

View File

@ -84,7 +84,7 @@ pub enum KeyExportError {
/// # block_on(async { /// # block_on(async {
/// # let export = Cursor::new("".to_owned()); /// # let export = Cursor::new("".to_owned());
/// let exported_keys = decrypt_key_export(export, "1234").unwrap(); /// let exported_keys = decrypt_key_export(export, "1234").unwrap();
/// machine.import_keys(exported_keys).await.unwrap(); /// machine.import_keys(exported_keys, |_, _| {}).await.unwrap();
/// # }); /// # });
/// ``` /// ```
pub fn decrypt_key_export( pub fn decrypt_key_export(
@ -316,7 +316,10 @@ mod test {
let decrypted = decrypt_key_export(Cursor::new(encrypted), "1234").unwrap(); let decrypted = decrypt_key_export(Cursor::new(encrypted), "1234").unwrap();
assert_eq!(export, decrypted); assert_eq!(export, decrypted);
assert_eq!(machine.import_keys(decrypted).await.unwrap(), (0, 1)); assert_eq!(
machine.import_keys(decrypted, |_, _| {}).await.unwrap(),
(0, 1)
);
} }
#[test] #[test]

View File

@ -1064,12 +1064,13 @@ impl OlmMachine {
/// # block_on(async { /// # block_on(async {
/// # let export = Cursor::new("".to_owned()); /// # let export = Cursor::new("".to_owned());
/// let exported_keys = decrypt_key_export(export, "1234").unwrap(); /// let exported_keys = decrypt_key_export(export, "1234").unwrap();
/// machine.import_keys(exported_keys).await.unwrap(); /// machine.import_keys(exported_keys, |_, _| {}).await.unwrap();
/// # }); /// # });
/// ``` /// ```
pub async fn import_keys( pub async fn import_keys(
&self, &self,
exported_keys: Vec<ExportedRoomKey>, exported_keys: Vec<ExportedRoomKey>,
progress_listener: impl Fn(usize, usize),
) -> StoreResult<(usize, usize)> { ) -> StoreResult<(usize, usize)> {
struct ShallowSessions { struct ShallowSessions {
inner: BTreeMap<Arc<RoomId>, u32>, inner: BTreeMap<Arc<RoomId>, u32>,
@ -1101,7 +1102,7 @@ impl OlmMachine {
let total_sessions = exported_keys.len(); let total_sessions = exported_keys.len();
for key in exported_keys.into_iter() { for (i, key) in exported_keys.into_iter().enumerate() {
let session = InboundGroupSession::from_export(key)?; let session = InboundGroupSession::from_export(key)?;
// Only import the session if we didn't have this session or if it's // Only import the session if we didn't have this session or if it's
@ -1110,6 +1111,8 @@ impl OlmMachine {
if !existing_sessions.has_better_session(&session) { if !existing_sessions.has_better_session(&session) {
sessions.push(session) sessions.push(session)
} }
progress_listener(i, total_sessions)
} }
let num_sessions = sessions.len(); let num_sessions = sessions.len();