Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sdk: notify the room list of a room info change due to late decryption #3018

Merged
merged 12 commits into from Feb 12, 2024

Conversation

timokoesters
Copy link
Contributor

This PR fixes element-hq/element-x-ios#1847 for delayed decryptions.
Wrong latest events can still occur when backfill is needed to find the events.

  • Public API changes documented in changelogs (optional)

@timokoesters timokoesters requested a review from a team as a code owner January 15, 2024 13:02
@timokoesters timokoesters requested review from Hywan and removed request for a team January 15, 2024 13:02
@timokoesters timokoesters changed the title sliding_sync: Trigger room list update when room info changes Fix latest_event for delayed decryptions Jan 15, 2024
@timokoesters timokoesters force-pushed the timo/fix-latest-events branch 2 times, most recently from 0852947 to 25b01fa Compare January 15, 2024 13:09
Copy link
Member

@bnjbvr bnjbvr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is on the right track, congratulations 🥳 Let's brainstorm together how to test this properly.

crates/matrix-sdk/src/sliding_sync/client.rs Outdated Show resolved Hide resolved
crates/matrix-sdk/src/sliding_sync/mod.rs Outdated Show resolved Hide resolved
crates/matrix-sdk-base/src/client.rs Outdated Show resolved Hide resolved
crates/matrix-sdk-base/src/store/mod.rs Outdated Show resolved Hide resolved
crates/matrix-sdk-ui/src/room_list_service/room_list.rs Outdated Show resolved Hide resolved
crates/matrix-sdk-ui/src/room_list_service/room_list.rs Outdated Show resolved Hide resolved
crates/matrix-sdk-ui/src/room_list_service/room_list.rs Outdated Show resolved Hide resolved
crates/matrix-sdk-ui/src/room_list_service/room_list.rs Outdated Show resolved Hide resolved
crates/matrix-sdk/src/client/mod.rs Outdated Show resolved Hide resolved
Hywan
Hywan previously requested changes Jan 18, 2024
crates/matrix-sdk-base/src/client.rs Outdated Show resolved Hide resolved
crates/matrix-sdk-base/src/client.rs Outdated Show resolved Hide resolved
crates/matrix-sdk-base/src/rooms/normal.rs Outdated Show resolved Hide resolved
@bnjbvr bnjbvr removed their request for review January 26, 2024 10:16
Copy link

codecov bot commented Jan 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (d84387d) 83.74% compared to head (aa17148) 83.85%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3018      +/-   ##
==========================================
+ Coverage   83.74%   83.85%   +0.10%     
==========================================
  Files         229      229              
  Lines       23647    23697      +50     
==========================================
+ Hits        19803    19870      +67     
+ Misses       3844     3827      -17     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bnjbvr bnjbvr requested review from bnjbvr and removed request for Hywan January 29, 2024 11:02
Copy link
Member

@bnjbvr bnjbvr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small logic bug, and then we need to improve a bit the code comments and documentation. And then we can merge it after the hopefully final round of review. Thanks, it's a bunch of work.

crates/matrix-sdk-base/src/rooms/normal.rs Outdated Show resolved Hide resolved
crates/matrix-sdk-base/src/rooms/normal.rs Outdated Show resolved Hide resolved
crates/matrix-sdk-base/src/sliding_sync.rs Show resolved Hide resolved
crates/matrix-sdk/src/client/mod.rs Outdated Show resolved Hide resolved
assert!(timeout(Duration::from_millis(100), stream.next()).await.is_err());

// Latest event is not set yet
assert!(matches!(alice_room.latest_event(), None));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert!(matches!(alice_room.latest_event(), None));
assert!(alice_room.latest_event().is_none());

@bnjbvr bnjbvr dismissed Hywan’s stale review January 29, 2024 15:47

I've taken over the review 👀

@bnjbvr bnjbvr mentioned this pull request Jan 30, 2024
10 tasks
Copy link
Member

@bnjbvr bnjbvr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay, let's address the last comments related to the test and merge this. Good job!

crates/matrix-sdk-base/src/sliding_sync.rs Show resolved Hide resolved
Comment on lines 631 to 632
r = r.set_body_bytes(bytes);
r
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should work:

Suggested change
r = r.set_body_bytes(bytes);
r
r.set_body_bytes(bytes)

Comment on lines 635 to 636
let r = wiremock::ResponseTemplate::new(504);
r
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let r = wiremock::ResponseTemplate::new(504);
r
wiremock::ResponseTemplate::new(504)

@@ -538,3 +551,354 @@ async fn test_room_notification_count() -> Result<()> {

Ok(())
}

/// Boolean that decides if to_device messages should be dropped.
static DROP_TODEVICE: StdMutex<bool> = StdMutex::new(true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of global state, could we store this bool in the CustomResponder struct?

Comment on lines 560 to 577
// Looks for a json payload containing "extensions" with a "to_device" part.
// This should only match the sliding sync response. In all other cases, it
// makes no changes.
let Ok(mut json) = serde_json::from_slice::<serde_json::Value>(response) else {
return;
};
let Some(extensions) = json.get_mut("extensions").and_then(|e| e.as_object_mut()) else {
return;
};
// Remove to_device field if it exists
let Some(to_device) = extensions.remove("to_device") else {
return;
};
if *DROP_TODEVICE.lock().unwrap() {
info!("Dropping to_device: {to_device}");
*response = serde_json::to_vec(&json).unwrap().into();
return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be easier to understand by checking the boolean first, and if it's false, return early.

entries.set_filter(new_filter_all());
pin_mut!(stream);

// Send a message, but the keys won't arrive
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Send a message, but the keys won't arrive
// Send a message, but the keys won't arrive because to-device events are stripped away from the server's response

alice_room.latest_event().unwrap();

// The stream has a single update
timeout(Duration::from_millis(100), stream.next()).await.unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This comment ^ still valid :P)

// Stream has the room again, but no second event
// TODO: Synapse sometimes sends the same event two times. This is the
// workaround:
assert!(timeout(Duration::from_millis(100), stream.next()).await.unwrap().unwrap().len() > 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use temporary variables here? It's not clear what's the final thing we're checking against...

@timokoesters timokoesters force-pushed the timo/fix-latest-events branch 2 times, most recently from cbb60f8 to e648a3e Compare February 12, 2024 09:26
Copy link
Member

@bnjbvr bnjbvr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, let's get this merged after a rebase 🥳 Thanks!

This fixes element-hq/element-x-ios#1847

Signed-off-by: Timo Kösters <timo@koesters.xyz>
Signed-off-by: Timo Kösters <timo@koesters.xyz>
timokoesters and others added 10 commits February 12, 2024 11:37
I have also changed the priorities so that manual updates are preferred.
This means that duplicate updates do not happen if the room was previously unknown.

Signed-off-by: Timo Kösters <timo@koesters.xyz>
Signed-off-by: Timo Kösters <timo@koesters.xyz>
Signed-off-by: Timo Kösters <timo@koesters.xyz>
Signed-off-by: Timo Kösters <timo@koesters.xyz>
Signed-off-by: Timo Kösters <timo@koesters.xyz>
…happens

Previously, there were duplicate updates.

Signed-off-by: Timo Kösters <timo@koesters.xyz>
Signed-off-by: Timo Kösters <timo@koesters.xyz>
Signed-off-by: Timo Kösters <timo@koesters.xyz>
Signed-off-by: Timo Kösters <timo@koesters.xyz>
@bnjbvr bnjbvr changed the title Fix latest_event for delayed decryptions sdk: notify the room list of a room info change due to late decryption Feb 12, 2024
@bnjbvr bnjbvr merged commit 5cb587a into main Feb 12, 2024
34 checks passed
@bnjbvr bnjbvr deleted the timo/fix-latest-events branch February 12, 2024 13:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Room list does not update when new event is sent
3 participants