Skip to content

Commit 9d0c40c

Browse files
authored
feat(sdk): Clean up and test SlidingSyncRoom
feat(sdk): Clean up and test `SlidingSyncRoom`
2 parents 870d6d6 + f23fd18 commit 9d0c40c

File tree

5 files changed

+943
-227
lines changed

5 files changed

+943
-227
lines changed

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ pub struct SlidingSyncRoom {
159159
#[uniffi::export]
160160
impl SlidingSyncRoom {
161161
pub fn name(&self) -> Option<String> {
162-
self.inner.name().map(ToOwned::to_owned)
162+
self.inner.name()
163163
}
164164

165165
pub fn room_id(&self) -> String {
@@ -174,16 +174,12 @@ impl SlidingSyncRoom {
174174
self.inner.is_initial_response()
175175
}
176176

177-
pub fn is_loading_more(&self) -> bool {
178-
self.inner.is_loading_more()
179-
}
180-
181177
pub fn has_unread_notifications(&self) -> bool {
182178
self.inner.has_unread_notifications()
183179
}
184180

185181
pub fn unread_notifications(&self) -> Arc<UnreadNotificationsCount> {
186-
Arc::new(self.inner.unread_notifications().clone().into())
182+
Arc::new(self.inner.unread_notifications().into())
187183
}
188184

189185
pub fn full_room(&self) -> Option<Arc<Room>> {
@@ -217,7 +213,7 @@ impl SlidingSyncRoom {
217213
settings: Option<RoomSubscription>,
218214
) -> Result<SlidingSyncSubscribeResult, ClientError> {
219215
let (items, mut stoppable_spawn) = self.add_timeline_listener_inner(listener)?;
220-
let room_id = self.inner.room_id().clone();
216+
let room_id = self.inner.room_id().to_owned();
221217

222218
self.runner.subscribe(room_id.clone(), settings.map(Into::into))?;
223219

crates/matrix-sdk/src/sliding_sync/list/frozen.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ use imbl::Vector;
44
use ruma::OwnedRoomId;
55
use serde::{Deserialize, Serialize};
66

7-
use super::{FrozenSlidingSyncRoom, RoomListEntry, SlidingSyncList, SlidingSyncRoom};
7+
use super::{
8+
super::{FrozenSlidingSyncRoom, SlidingSyncRoom},
9+
RoomListEntry, SlidingSyncList,
10+
};
811

912
#[derive(Debug, Serialize, Deserialize)]
1013
pub struct FrozenSlidingSyncList {
@@ -72,7 +75,6 @@ mod tests {
7275
FrozenSlidingSyncRoom {
7376
room_id: room_id!("!foo:bar.org").to_owned(),
7477
inner: v4::SlidingSyncRoom::default(),
75-
prev_batch: Some("let it go!".to_owned()),
7678
timeline_queue: vector![TimelineEvent::new(
7779
Raw::new(&json!({
7880
"content": RoomMessageEventContent::text_plain("let it gooo!"),
@@ -100,7 +102,6 @@ mod tests {
100102
"!foo:bar.org": {
101103
"room_id": "!foo:bar.org",
102104
"inner": {},
103-
"prev_batch": "let it go!",
104105
"timeline": [
105106
{
106107
"event": {

crates/matrix-sdk/src/sliding_sync/list/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use serde::{Deserialize, Serialize};
2525
use tokio::sync::mpsc::Sender;
2626
use tracing::{instrument, warn};
2727

28-
use super::{Error, FrozenSlidingSyncRoom, SlidingSyncInternalMessage, SlidingSyncRoom};
28+
use super::{Error, SlidingSyncInternalMessage};
2929
use crate::Result;
3030

3131
/// The type used to express natural bounds (including but not limited to:

crates/matrix-sdk/src/sliding_sync/mod.rs

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -309,68 +309,76 @@ impl SlidingSync {
309309
}
310310

311311
let update_summary = {
312-
let mut rooms_map = self.inner.rooms.write().unwrap();
313-
314312
// Update the rooms.
315-
let mut updated_rooms = Vec::with_capacity(sliding_sync_response.rooms.len());
316-
317-
for (room_id, mut room_data) in sliding_sync_response.rooms.into_iter() {
318-
// `sync_response` contains the rooms with decrypted events if any, so look at
319-
// the timeline events here first if the room exists.
320-
// Otherwise, let's look at the timeline inside the `sliding_sync_response`.
321-
let timeline = if let Some(joined_room) = sync_response.rooms.join.remove(&room_id)
322-
{
323-
joined_room.timeline.events
324-
} else {
325-
room_data.timeline.drain(..).map(Into::into).collect()
326-
};
327-
328-
if let Some(mut room) = rooms_map.remove(&room_id) {
329-
// The room existed before, let's update it.
330-
331-
room.update(room_data, timeline);
332-
rooms_map.insert(room_id.clone(), room);
333-
} else {
334-
// First time we need this room, let's create it.
335-
336-
rooms_map.insert(
337-
room_id.clone(),
338-
SlidingSyncRoom::new(
339-
self.inner.client.clone(),
340-
room_id.clone(),
341-
room_data,
342-
timeline,
343-
),
344-
);
313+
let updated_rooms = {
314+
let mut rooms_map = self.inner.rooms.write().unwrap();
315+
316+
let mut updated_rooms = Vec::with_capacity(sliding_sync_response.rooms.len());
317+
318+
for (room_id, mut room_data) in sliding_sync_response.rooms.into_iter() {
319+
// `sync_response` contains the rooms with decrypted events if any, so look at
320+
// the timeline events here first if the room exists.
321+
// Otherwise, let's look at the timeline inside the `sliding_sync_response`.
322+
let timeline =
323+
if let Some(joined_room) = sync_response.rooms.join.remove(&room_id) {
324+
joined_room.timeline.events
325+
} else {
326+
room_data.timeline.drain(..).map(Into::into).collect()
327+
};
328+
329+
match rooms_map.get_mut(&room_id) {
330+
// The room existed before, let's update it.
331+
Some(room) => {
332+
room.update(room_data, timeline);
333+
}
334+
335+
// First time we need this room, let's create it.
336+
None => {
337+
rooms_map.insert(
338+
room_id.clone(),
339+
SlidingSyncRoom::new(
340+
self.inner.client.clone(),
341+
room_id.clone(),
342+
room_data,
343+
timeline,
344+
),
345+
);
346+
}
347+
}
348+
349+
updated_rooms.push(room_id);
345350
}
346351

347-
updated_rooms.push(room_id);
348-
}
352+
updated_rooms
353+
};
349354

350355
// Update the lists.
351-
let mut updated_lists = Vec::with_capacity(sliding_sync_response.lists.len());
356+
let updated_lists = {
357+
let mut updated_lists = Vec::with_capacity(sliding_sync_response.lists.len());
358+
let mut lists = self.inner.lists.write().unwrap();
352359

353-
let mut lists = self.inner.lists.write().unwrap();
360+
for (name, updates) in sliding_sync_response.lists {
361+
let Some(list) = lists.get_mut(&name) else {
362+
error!("Response for list `{name}` - unknown to us; skipping");
354363

355-
for (name, updates) in sliding_sync_response.lists {
356-
let Some(list) = lists.get_mut(&name) else {
357-
error!("Response for list `{name}` - unknown to us; skipping");
364+
continue;
365+
};
358366

359-
continue;
360-
};
367+
let maximum_number_of_rooms: u32 =
368+
updates.count.try_into().expect("failed to convert `count` to `u32`");
361369

362-
let maximum_number_of_rooms: u32 =
363-
updates.count.try_into().expect("failed to convert `count` to `u32`");
370+
if list.update(maximum_number_of_rooms, &updates.ops, &updated_rooms)? {
371+
updated_lists.push(name.clone());
372+
}
373+
}
364374

365-
if list.update(maximum_number_of_rooms, &updates.ops, &updated_rooms)? {
366-
updated_lists.push(name.clone());
375+
// Update the `to-device` next-batch if any.
376+
if let Some(to_device) = sliding_sync_response.extensions.to_device {
377+
self.update_to_device_since(to_device.next_batch);
367378
}
368-
}
369379

370-
// Update the `to-device` next-batch if any.
371-
if let Some(to_device) = sliding_sync_response.extensions.to_device {
372-
self.update_to_device_since(to_device.next_batch);
373-
}
380+
updated_lists
381+
};
374382

375383
UpdateSummary { lists: updated_lists, rooms: updated_rooms }
376384
};

0 commit comments

Comments
 (0)