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

Small sliding sync refactorings #2956

Merged
merged 3 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions crates/matrix-sdk-base/src/sliding_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,16 +435,20 @@ impl BaseClient {
state_events: &[AnySyncStateEvent],
room_info: &mut RoomInfo,
) {
for event in state_events {
let Some(meta) = self.session_meta() else {
return;
};

// Start from the last event; the first membership event we see in that order is
// the last in the regular order, so that's the only one we need to
// consider.
for event in state_events.iter().rev() {
if let AnySyncStateEvent::RoomMember(member) = &event {
// If this event updates the current user's membership, record that in the
// room_info.
if let Some(meta) = self.session_meta() {
if member.sender() == meta.user_id
&& member.state_key() == meta.user_id.as_str()
{
room_info.set_state(member.membership().into());
}
if member.sender() == meta.user_id && member.state_key() == meta.user_id.as_str() {
room_info.set_state(member.membership().into());
break;
}
}
}
Expand Down Expand Up @@ -479,8 +483,12 @@ impl BaseClient {
}

/// Find the most recent decrypted event and cache it in the supplied RoomInfo.
///
/// If any encrypted events are found after that one, store them in the RoomInfo
/// too so we can use them when we get the relevant keys.
///
/// It is the responsibility of the caller to update the `RoomInfo` instance
/// stored in the `Room`.
#[cfg(feature = "e2e-encryption")]
async fn cache_latest_events(
room: &Room,
Expand Down Expand Up @@ -553,10 +561,8 @@ async fn cache_latest_events(
sender_name_is_ambiguous,
));

// Store it in the return RoomInfo, and in the Room, to make sure they are
// consistent
// Store it in the return RoomInfo (it will be saved for us in the room later).
room_info.latest_event = Some(latest_event.clone());
room.set_latest_event(Some(latest_event));
// We don't need any of the older encrypted events because we have a new
// decrypted one.
room.latest_encrypted_events.write().unwrap().clear();
Expand Down Expand Up @@ -1168,9 +1174,11 @@ mod tests {

// The latest message is stored
assert_eq!(
ev_id(room_info.latest_event.map(|latest_event| latest_event.event().clone())),
ev_id(room_info.latest_event.as_ref().map(|latest_event| latest_event.event().clone())),
rawev_id(event2.clone())
);

room.update_summary(room_info);
assert_eq!(
ev_id(room.latest_event().map(|latest_event| latest_event.event().clone())),
rawev_id(event2)
Expand All @@ -1192,6 +1200,7 @@ mod tests {
let room = make_room();
let mut room_info = room.clone_info();
cache_latest_events(&room, &mut room_info, events, None, None).await;
room.update_summary(room_info);

// The latest message is stored
assert_eq!(
Expand All @@ -1218,6 +1227,7 @@ mod tests {
let room = make_room();
let mut room_info = room.clone_info();
cache_latest_events(&room, &mut room_info, events, None, None).await;
room.update_summary(room_info);

// The latest message is stored, ignoring the receipt
assert_eq!(
Expand Down Expand Up @@ -1270,6 +1280,7 @@ mod tests {
let room = make_room();
let mut room_info = room.clone_info();
cache_latest_events(&room, &mut room_info, events, None, None).await;
room.update_summary(room_info);

// The latest message is stored, ignoring encrypted and receipts
assert_eq!(
Expand Down Expand Up @@ -1310,13 +1321,16 @@ mod tests {
None,
)
.await;
room.update_summary(room_info);

// Sanity: room_info has 10 encrypted events inside it
assert_eq!(room.latest_encrypted_events.read().unwrap().len(), 10);

// When I ask to cache more encrypted events
let eventa = make_encrypted_event("$a");
let mut room_info = room.clone_info();
cache_latest_events(&room, &mut room_info, &[eventa], None, None).await;
room.update_summary(room_info);

// The oldest event is gone
assert!(!rawevs_ids(&room.latest_encrypted_events).contains(&"$0".to_owned()));
Expand All @@ -1338,11 +1352,13 @@ mod tests {
None,
)
.await;
room.update_summary(room_info.clone());

// When I ask to cache an unecnrypted event, and some more encrypted events
// When I ask to cache an unencrypted event, and some more encrypted events
let eventa = make_event("m.room.message", "$a");
let eventb = make_encrypted_event("$b");
cache_latest_events(&room, &mut room_info, &[eventa, eventb], None, None).await;
room.update_summary(room_info);

// The only encrypted events stored are the ones after the decrypted one
assert_eq!(rawevs_ids(&room.latest_encrypted_events), &["$b"]);
Expand All @@ -1355,6 +1371,7 @@ mod tests {
let room = make_room();
let mut room_info = room.clone_info();
cache_latest_events(&room, &mut room_info, events, None, None).await;
room.update_summary(room_info);
room.latest_event().map(|latest_event| latest_event.event().clone())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ async fn incremental_upload_of_keys_sliding_sync() -> Result<()> {
.await?;

let s = sliding.clone();
tokio::task::spawn(async move {
spawn(async move {
let stream = s.sync();
pin_mut!(stream);
while let Some(up) = stream.next().await {
Expand Down