Skip to content

Commit

Permalink
base: move the initial filling of the display name cache into the syn…
Browse files Browse the repository at this point in the history
…c methods

It's not perfect, but it's honest work.
  • Loading branch information
bnjbvr committed Jun 10, 2024
1 parent cc18b31 commit 51a599b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
7 changes: 7 additions & 0 deletions crates/matrix-sdk-base/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,13 @@ impl BaseClient {
self.apply_changes(&changes, false);
}

// Now that all the rooms information have been saved, update the display name
// cache (which relies on information stored in the database). This will
// live in memory, until the next sync which will saves the room info to
// disk; we do this to avoid saving that would be redundant with the
// above. Oh well.
new_rooms.update_in_memory_caches(&self.store).await;

info!("Processed a sync response in {:?}", now.elapsed());

let response = SyncResponse {
Expand Down
7 changes: 7 additions & 0 deletions crates/matrix-sdk-base/src/sliding_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,13 @@ impl BaseClient {
self.apply_changes(&changes, false);
trace!("applied changes");

// Now that all the rooms information have been saved, update the display name
// cache (which relies on information stored in the database). This will
// live in memory, until the next sync which will saves the room info to
// disk; we do this to avoid saving that would be redundant with the
// above. Oh well.
new_rooms.update_in_memory_caches(&self.store).await;

Ok(SyncResponse {
rooms: new_rooms,
notifications,
Expand Down
18 changes: 18 additions & 0 deletions crates/matrix-sdk-base/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use serde::{Deserialize, Serialize};
use crate::{
debug::{DebugInvitedRoom, DebugListOfRawEvents, DebugListOfRawEventsNoId},
deserialized_responses::{AmbiguityChange, RawAnySyncOrStrippedTimelineEvent},
store::Store,
};

/// Generalized representation of a `/sync` response.
Expand Down Expand Up @@ -78,6 +79,23 @@ pub struct RoomUpdates {
pub invite: BTreeMap<OwnedRoomId, InvitedRoomUpdate>,
}

impl RoomUpdates {
/// Update the caches for the rooms that received updates.
///
/// This will only fill the in-memory caches, not save the info on disk.
pub(crate) async fn update_in_memory_caches(&self, store: &Store) {
for room in self
.leave
.keys()
.chain(self.join.keys())
.chain(self.invite.keys())
.filter_map(|room_id| store.get_room(room_id))
{
let _ = room.compute_display_name().await;
}
}
}

#[cfg(not(tarpaulin_include))]
impl fmt::Debug for RoomUpdates {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down
13 changes: 0 additions & 13 deletions crates/matrix-sdk/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,6 @@ impl Client {
) -> Result<()> {
let BaseSyncResponse { rooms, presence, account_data, to_device, notifications } = response;

{
// Recompute the computed display name for all the rooms which had an update.
for room in rooms
.leave
.keys()
.chain(rooms.join.keys())
.chain(rooms.invite.keys())
.filter_map(|room_id| self.get_room(room_id))
{
let _ = room.compute_display_name().await;
}
}

let now = Instant::now();
self.handle_sync_events(HandlerKind::GlobalAccountData, None, account_data).await?;
self.handle_sync_events(HandlerKind::Presence, None, presence).await?;
Expand Down

0 comments on commit 51a599b

Please sign in to comment.