Skip to content

Commit 255d3b7

Browse files
authored
Merge pull request #1154 from matrix-org/stefan/sessionVerificationFixes
Sliding Sync FFI and DevEx fixes
2 parents 6ad7d29 + 0cb34f7 commit 255d3b7

File tree

6 files changed

+186
-98
lines changed

6 files changed

+186
-98
lines changed

bindings/matrix-sdk-ffi/src/client.rs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use matrix_sdk::{
1313
session::get_login_types,
1414
sync::sync_events::v3::Filter,
1515
},
16-
events::room::MediaSource,
16+
events::{room::MediaSource, AnyToDeviceEvent},
1717
serde::Raw,
1818
TransactionId, UInt,
1919
},
@@ -49,11 +49,29 @@ pub struct Client {
4949

5050
impl Client {
5151
pub fn new(client: MatrixClient, state: ClientState) -> Self {
52+
let session_verification_controller: Arc<
53+
matrix_sdk::locks::RwLock<Option<SessionVerificationController>>,
54+
> = Default::default();
55+
let ctrl = session_verification_controller.clone();
56+
57+
client.add_event_handler(move |ev: AnyToDeviceEvent| {
58+
let ctrl = ctrl.clone();
59+
async move {
60+
if let Some(session_verification_controller) = &*ctrl.clone().read().await {
61+
session_verification_controller.process_to_device_message(ev).await;
62+
} else {
63+
tracing::warn!(
64+
"received to-device message, but verification controller isn't ready"
65+
);
66+
}
67+
}
68+
});
69+
5270
Client {
5371
client,
5472
state: Arc::new(RwLock::new(state)),
5573
delegate: Arc::new(RwLock::new(None)),
56-
session_verification_controller: Arc::new(matrix_sdk::locks::RwLock::new(None)),
74+
session_verification_controller,
5775
}
5876
}
5977

@@ -234,7 +252,6 @@ impl Client {
234252
{
235253
return Ok(Arc::new(session_verification_controller.clone()));
236254
}
237-
238255
let user_id = self.client.user_id().context("Failed retrieving current user_id")?;
239256
let user_identity = self
240257
.client
@@ -263,19 +280,20 @@ impl Client {
263280
}
264281

265282
/// Process a sync error and return loop control accordingly
266-
fn process_sync_error(&self, sync_error: Error) -> LoopCtrl {
267-
let mut control = LoopCtrl::Continue;
283+
pub(crate) fn process_sync_error(&self, sync_error: Error) -> LoopCtrl {
268284
if let Some(RumaApiError::ClientApi(error)) = sync_error.as_ruma_api_error() {
269285
if let ErrorKind::UnknownToken { soft_logout } = error.kind {
270286
self.state.write().unwrap().is_soft_logout = soft_logout;
271287
if let Some(delegate) = &*self.delegate.read().unwrap() {
272288
delegate.did_update_restore_token();
273289
delegate.did_receive_auth_error(soft_logout);
274290
}
275-
control = LoopCtrl::Break
291+
return LoopCtrl::Break;
276292
}
277293
}
278-
control
294+
295+
tracing::warn!("Ignoring sync error: {:?}", sync_error);
296+
LoopCtrl::Continue
279297
}
280298
}
281299

@@ -315,7 +333,6 @@ impl Client {
315333
let client = self.client.clone();
316334
let state = self.state.clone();
317335
let delegate = self.delegate.clone();
318-
let session_verification_controller = self.session_verification_controller.clone();
319336
let local_self = self.clone();
320337
RUNTIME.spawn(async move {
321338
let mut filter = FilterDefinition::default();
@@ -337,7 +354,7 @@ impl Client {
337354

338355
client
339356
.sync_with_result_callback(sync_settings, |result| async {
340-
Ok(if let Ok(sync_response) = result {
357+
Ok(if result.is_ok() {
341358
if !state.read().unwrap().has_first_synced {
342359
state.write().unwrap().has_first_synced = true;
343360
}
@@ -353,14 +370,6 @@ impl Client {
353370
delegate.did_receive_sync_update()
354371
}
355372

356-
if let Some(session_verification_controller) =
357-
&*session_verification_controller.read().await
358-
{
359-
session_verification_controller
360-
.process_to_device_messages(sync_response.to_device_events)
361-
.await;
362-
}
363-
364373
LoopCtrl::Continue
365374
} else {
366375
local_self.process_sync_error(result.err().unwrap())

bindings/matrix-sdk-ffi/src/session_verification.rs

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -109,64 +109,66 @@ impl SessionVerificationController {
109109
})
110110
}
111111

112-
pub async fn process_to_device_messages(&self, to_device_events: Vec<Raw<AnyToDeviceEvent>>) {
113-
let sas_verification = self.sas_verification.clone();
114-
115-
for event in to_device_events.into_iter().filter_map(|e| e.deserialize().ok()) {
116-
match event {
117-
AnyToDeviceEvent::KeyVerificationReady(event) => {
118-
if !self.is_transaction_id_valid(event.content.transaction_id.to_string()) {
119-
return;
120-
}
121-
self.start_sas_verification().await;
112+
pub async fn process_to_device_message(&self, event: AnyToDeviceEvent) {
113+
match event {
114+
AnyToDeviceEvent::KeyVerificationReady(event) => {
115+
if !self.is_transaction_id_valid(event.content.transaction_id.to_string()) {
116+
return;
117+
}
118+
self.start_sas_verification().await;
119+
}
120+
AnyToDeviceEvent::KeyVerificationCancel(event) => {
121+
if !self.is_transaction_id_valid(event.content.transaction_id.to_string()) {
122+
return;
122123
}
123-
AnyToDeviceEvent::KeyVerificationCancel(event) => {
124-
if !self.is_transaction_id_valid(event.content.transaction_id.to_string()) {
125-
return;
126-
}
127124

128-
if let Some(delegate) = &*self.delegate.read().unwrap() {
129-
delegate.did_cancel()
130-
}
125+
if let Some(delegate) = &*self.delegate.read().unwrap() {
126+
delegate.did_cancel()
127+
}
128+
}
129+
AnyToDeviceEvent::KeyVerificationKey(event) => {
130+
if !self.is_transaction_id_valid(event.content.transaction_id.to_string()) {
131+
return;
131132
}
132-
AnyToDeviceEvent::KeyVerificationKey(event) => {
133-
if !self.is_transaction_id_valid(event.content.transaction_id.to_string()) {
134-
return;
135-
}
136133

137-
if let Some(sas_verification) = &*sas_verification.read().unwrap() {
138-
if let Some(emojis) = sas_verification.emoji() {
139-
if let Some(delegate) = &*self.delegate.read().unwrap() {
140-
let emojis = emojis
141-
.iter()
142-
.map(|e| {
143-
Arc::new(SessionVerificationEmoji {
144-
symbol: e.symbol.to_owned(),
145-
description: e.description.to_owned(),
146-
})
134+
if let Some(sas_verification) = &*self.sas_verification.read().unwrap() {
135+
if let Some(emojis) = sas_verification.emoji() {
136+
if let Some(delegate) = &*self.delegate.read().unwrap() {
137+
let emojis = emojis
138+
.iter()
139+
.map(|e| {
140+
Arc::new(SessionVerificationEmoji {
141+
symbol: e.symbol.to_owned(),
142+
description: e.description.to_owned(),
147143
})
148-
.collect::<Vec<_>>();
144+
})
145+
.collect::<Vec<_>>();
149146

150-
delegate.did_receive_verification_data(emojis);
151-
}
152-
} else if let Some(delegate) = &*self.delegate.read().unwrap() {
153-
delegate.did_fail()
147+
delegate.did_receive_verification_data(emojis);
154148
}
155149
} else if let Some(delegate) = &*self.delegate.read().unwrap() {
156150
delegate.did_fail()
157151
}
152+
} else if let Some(delegate) = &*self.delegate.read().unwrap() {
153+
delegate.did_fail()
154+
}
155+
}
156+
AnyToDeviceEvent::KeyVerificationDone(event) => {
157+
if !self.is_transaction_id_valid(event.content.transaction_id.to_string()) {
158+
return;
158159
}
159-
AnyToDeviceEvent::KeyVerificationDone(event) => {
160-
if !self.is_transaction_id_valid(event.content.transaction_id.to_string()) {
161-
return;
162-
}
163160

164-
if let Some(delegate) = &*self.delegate.read().unwrap() {
165-
delegate.did_finish()
166-
}
161+
if let Some(delegate) = &*self.delegate.read().unwrap() {
162+
delegate.did_finish()
167163
}
168-
_ => (),
169164
}
165+
_ => (),
166+
}
167+
}
168+
169+
pub async fn process_to_device_messages(&self, to_device_events: Vec<Raw<AnyToDeviceEvent>>) {
170+
for event in to_device_events.into_iter().filter_map(|e| e.deserialize().ok()) {
171+
self.process_to_device_message(event).await;
170172
}
171173
}
172174

bindings/matrix-sdk-ffi/src/sliding_sync.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use matrix_sdk::ruma::{
1717
assign, IdParseError, OwnedRoomId,
1818
};
1919
pub use matrix_sdk::{
20-
Client as MatrixClient, RoomListEntry as MatrixRoomEntry,
20+
Client as MatrixClient, LoopCtrl, RoomListEntry as MatrixRoomEntry,
2121
SlidingSyncBuilder as MatrixSlidingSyncBuilder, SlidingSyncMode, SlidingSyncState,
2222
};
2323
use tokio::task::JoinHandle;
@@ -521,6 +521,7 @@ impl SlidingSync {
521521
let inner_spawn = spawn.clone();
522522
{
523523
let mut sync_handle = self.sync_handle.write().unwrap();
524+
let client = self.client.clone();
524525

525526
if let Some(handle) = sync_handle.take() {
526527
handle.abort();
@@ -533,9 +534,11 @@ impl SlidingSync {
533534
let update = match stream.next().await {
534535
Some(Ok(u)) => u,
535536
Some(Err(e)) => {
536-
// FIXME: send this over the FFI
537-
tracing::warn!("Sliding Sync failure: {:?}", e);
538-
continue;
537+
if client.process_sync_error(e) == LoopCtrl::Break {
538+
break;
539+
} else {
540+
continue;
541+
}
539542
}
540543
None => {
541544
tracing::debug!("No update from loop, cancelled");

crates/matrix-sdk/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ experimental-timeline = ["ruma/unstable-msc2676", "ruma/unstable-msc2677"]
4747

4848
sliding-sync = [
4949
"matrix-sdk-base/sliding-sync",
50-
"anyhow",
5150
"dep:derive_builder",
5251
]
5352

crates/matrix-sdk/src/error.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ pub enum Error {
193193
#[error(transparent)]
194194
ImageError(#[from] ImageError),
195195

196+
/// An error occurred within sliding-sync
197+
#[cfg(feature = "sliding-sync")]
198+
#[error(transparent)]
199+
SlidingSync(#[from] crate::sliding_sync::Error),
200+
196201
/// An other error was raised
197202
/// this might happen because encryption was enabled on the base-crate
198203
/// but not here and that raised.

0 commit comments

Comments
 (0)