Commit 44bc8680c1

44bc8680c1104443720bf6d6dec94c37cbed6ef4

parent: 55ad2d3402

Unregistered key

vaw <git@nlih.de> · 2026-11-11 15:17 UTC
committer: <noreply@github.com>

Implement redecryption of encrypted events (#855)

Layout: unified · split

src/main.rs +15
@@ -968,6 +968,7 @@ impl Application {
968 968
969 let store = self.store.clone(); 969 let store = self.store.clone();
970 970
971 tracing::info!("starting main loop");
971 while self.screen.tabs() != 0 { 972 while self.screen.tabs() != 0 {
972 let key = self.step().await?; 973 let key = self.step().await?;
973 974
@@ -1105,6 +1106,14 @@ async fn login_normal(
1105 println!("* Logging in for {}...", settings.user_id); 1106 println!("* Logging in for {}...", settings.user_id);
1106 login(worker, settings).await?; 1107 login(worker, settings).await?;
1107 println!("* Syncing..."); 1108 println!("* Syncing...");
1109
1110 // Only subscribe after the encryption session has been restored.
1111 worker
1112 .client
1113 .event_cache()
1114 .subscribe()
1115 .expect("Failed to start event cache");
1116
1108 worker.spawn_sync()?; 1117 worker.spawn_sync()?;
1109 worker.setup_persistent_tokens(); 1118 worker.setup_persistent_tokens();
1110 worker::do_first_sync(&worker.client, store) 1119 worker::do_first_sync(&worker.client, store)
@@ -1123,10 +1132,12 @@ async fn run(
1123 settings.probe_enhanced_keys(); 1132 settings.probe_enhanced_keys();
1124 1133
1125 // Set up client state. 1134 // Set up client state.
1135 tracing::info!("setting up client");
1126 create_dir_all(settings.sqlite_dir.as_path())?; 1136 create_dir_all(settings.sqlite_dir.as_path())?;
1127 let client = worker::create_client(&settings).await; 1137 let client = worker::create_client(&settings).await;
1128 1138
1129 // Set up the async worker thread and global store. 1139 // Set up the async worker thread and global store.
1140 tracing::debug!("setting up worker");
1130 let worker = ClientWorker::spawn(client.clone(), settings.clone()).await; 1141 let worker = ClientWorker::spawn(client.clone(), settings.clone()).await;
1131 let store = ChatStore::new(worker.clone(), settings.clone())?; 1142 let store = ChatStore::new(worker.clone(), settings.clone())?;
1132 let mut store = Store::new(store); 1143 let mut store = Store::new(store);
@@ -1137,9 +1148,11 @@ async fn run(
1137 store.registers.set_default_register(r.clone()); 1148 store.registers.set_default_register(r.clone());
1138 } 1149 }
1139 1150
1151 tracing::debug!("initializing worker");
1140 let store = Arc::new(AsyncMutex::new(store)); 1152 let store = Arc::new(AsyncMutex::new(store));
1141 worker.init(store.clone()); 1153 worker.init(store.clone());
1142 1154
1155 tracing::info!("logging in");
1143 let res = login_normal(&worker, &settings, &store).await; 1156 let res = login_normal(&worker, &settings, &store).await;
1144 1157
1145 match res { 1158 match res {
@@ -1157,6 +1170,7 @@ async fn run(
1157 Ok(()) => (), 1170 Ok(()) => (),
1158 } 1171 }
1159 1172
1173 tracing::debug!("setting up tty");
1160 // Set up the terminal for drawing, and cleanup properly on panics. 1174 // Set up the terminal for drawing, and cleanup properly on panics.
1161 setup_tty(&settings)?; 1175 setup_tty(&settings)?;
1162 1176
@@ -1170,6 +1184,7 @@ async fn run(
1170 })); 1184 }));
1171 1185
1172 // And finally, start running the terminal UI. 1186 // And finally, start running the terminal UI.
1187 tracing::debug!("creating application");
1173 let mut application = Application::new(settings, store, initial_room) 1188 let mut application = Application::new(settings, store, initial_room)
1174 .await 1189 .await
1175 .inspect_err(|_| restore_tty(enable_enhanced_keys, enable_mouse))?; 1190 .inspect_err(|_| restore_tty(enable_enhanced_keys, enable_mouse))?;
src/worker.rs +21 −3
@@ -3,6 +3,7 @@
3//! The worker thread handles asynchronous work, and can receive messages from the main thread that 3//! The worker thread handles asynchronous work, and can receive messages from the main thread that
4//! block on a reply from the async worker. 4//! block on a reply from the async worker.
5 5
6use std::collections::BTreeSet;
6use std::fmt::{Debug, Formatter}; 7use std::fmt::{Debug, Formatter};
7use std::sync::mpsc::{Receiver, SyncSender, sync_channel}; 8use std::sync::mpsc::{Receiver, SyncSender, sync_channel};
8 9
@@ -15,6 +16,7 @@ use matrix_sdk::config::{RequestConfig, SyncSettings};
15use matrix_sdk::deserialized_responses::RawSyncOrStrippedState; 16use matrix_sdk::deserialized_responses::RawSyncOrStrippedState;
16use matrix_sdk::deserialized_responses::{TimelineEvent, TimelineEventKind}; 17use matrix_sdk::deserialized_responses::{TimelineEvent, TimelineEventKind};
17use matrix_sdk::encryption::{BackupDownloadStrategy, EncryptionSettings}; 18use matrix_sdk::encryption::{BackupDownloadStrategy, EncryptionSettings};
19use matrix_sdk::event_cache::DecryptionRetryRequest;
18use matrix_sdk::event_handler::Ctx; 20use matrix_sdk::event_handler::Ctx;
19use matrix_sdk::notification_settings::RoomNotificationMode; 21use matrix_sdk::notification_settings::RoomNotificationMode;
20use matrix_sdk::room::RoomMember; 22use matrix_sdk::room::RoomMember;
@@ -382,6 +384,7 @@ async fn get_receipts_for_timeline_events(
382 events: Vec<TimelineEvent>, 384 events: Vec<TimelineEvent>,
383) -> Vec<MessageAndReceipts> { 385) -> Vec<MessageAndReceipts> {
384 let mut msgs = vec![]; 386 let mut msgs = vec![];
387 let mut redecryption_requests = BTreeSet::new();
385 388
386 for ev in events.into_iter() { 389 for ev in events.into_iter() {
387 let event_id = ev.event_id().map(ToOwned::to_owned); 390 let event_id = ev.event_id().map(ToOwned::to_owned);
@@ -401,9 +404,13 @@ async fn get_receipts_for_timeline_events(
401 } 404 }
402 }, 405 },
403 TimelineEventKind::UnableToDecrypt { event, utd_info, .. } => { 406 TimelineEventKind::UnableToDecrypt { event, utd_info, .. } => {
407 if let Some(session_id) = &utd_info.session_id {
408 redecryption_requests.insert(session_id.to_string());
409 }
410
404 let event = match event.deserialize() { 411 let event = match event.deserialize() {
405 Ok(event) => { 412 Ok(event) => {
406 tracing::debug!( 413 tracing::trace!(
407 ?utd_info, 414 ?utd_info,
408 room_id = room.room_id().as_str(), 415 room_id = room.room_id().as_str(),
409 ?event_id, 416 ?event_id,
@@ -458,6 +465,19 @@ async fn get_receipts_for_timeline_events(
458 msgs.push((msg, thread, receipts)); 465 msgs.push((msg, thread, receipts));
459 } 466 }
460 467
468 if !redecryption_requests.is_empty() {
469 tracing::debug!(
470 room_id = room.room_id().as_str(),
471 "Requesting redecryption of {} events",
472 redecryption_requests.len()
473 );
474 room.client().event_cache().request_decryption(DecryptionRetryRequest {
475 room_id: room.room_id().to_owned(),
476 utd_session_ids: redecryption_requests,
477 refresh_info_session_ids: Default::default(),
478 });
479 }
480
461 msgs 481 msgs
462} 482}
463 483
@@ -1371,8 +1391,6 @@ pub async fn create_client(settings: &ApplicationSettings) -> Client {
1371 1391
1372 let client = res.expect("Failed to instantiate client"); 1392 let client = res.expect("Failed to instantiate client");
1373 1393
1374 client.event_cache().subscribe().expect("Failed to start event cache");
1375
1376 client 1394 client
1377 .media() 1395 .media()
1378 .set_media_retention_policy(settings.tunables.cache_policy) 1396 .set_media_retention_policy(settings.tunables.cache_policy)