Skip to content

Commit c1d885f

Browse files
Hywanstefanceriu
authored andcommitted
fix(base): Create the room::display_name response processor.
This patch creates the `room::display_name::update_for_rooms` response processor. It also creates the `changes::save_only` response processor. Finally, this patch uses both to compute the new display name for all rooms and save them, for sliding sync only.
1 parent 568e60b commit c1d885f

File tree

4 files changed

+58
-5
lines changed

4 files changed

+58
-5
lines changed

crates/matrix-sdk-base/src/response_processors/changes.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ use crate::{
2525
Result, StateChanges,
2626
};
2727

28+
/// Save the [`StateChanges`] from the [`Context`] inside the [`BaseStateStore`]
29+
/// only! The changes aren't applied on the in-memory rooms.
30+
#[instrument(skip_all)]
31+
pub async fn save_only(context: Context, state_store: &BaseStateStore) -> Result<()> {
32+
save_changes(&context.state_changes, state_store, None).await
33+
}
34+
2835
/// Save the [`StateChanges`] from the [`Context`] inside the
2936
/// [`BaseStateStore`], and apply them on the in-memory rooms.
3037
#[instrument(skip_all)]
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2025 The Matrix.org Foundation C.I.C.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use super::super::Context;
16+
use crate::{rooms::UpdatedRoomDisplayName, store::BaseStateStore, sync::RoomUpdates};
17+
18+
pub async fn update_for_rooms(
19+
context: &mut Context,
20+
room_updates: &RoomUpdates,
21+
state_store: &BaseStateStore,
22+
) {
23+
for room in room_updates
24+
.left
25+
.keys()
26+
.chain(room_updates.joined.keys())
27+
.chain(room_updates.invited.keys())
28+
.chain(room_updates.knocked.keys())
29+
.filter_map(|room_id| state_store.room(room_id))
30+
{
31+
// Compute the display name. If it's different, let's register the `RoomInfo` in
32+
// the `StateChanges`.
33+
if let Ok(UpdatedRoomDisplayName::New(_)) = room.compute_display_name().await {
34+
context.state_changes.room_infos.insert(room.room_id().to_owned(), room.clone_info());
35+
}
36+
}
37+
}

crates/matrix-sdk-base/src/response_processors/room/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use tokio::sync::broadcast::Sender;
1717

1818
use crate::{store::ambiguity_map::AmbiguityCache, RequestedRequiredStates, RoomInfoNotableUpdate};
1919

20+
pub mod display_name;
2021
pub mod msc4186;
2122
pub mod sync_v2;
2223

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ impl BaseClient {
254254

255255
context.state_changes.ambiguity_maps = ambiguity_cache.cache;
256256

257+
// Save the changes and apply them.
257258
processors::changes::save_and_apply(
258259
context,
259260
&self.state_store,
@@ -262,12 +263,19 @@ impl BaseClient {
262263
)
263264
.await?;
264265

266+
let mut context = processors::Context::default();
267+
265268
// Now that all the rooms information have been saved, update the display name
266-
// cache (which relies on information stored in the database). This will
267-
// live in memory, until the next sync which will saves the room info to
268-
// disk; we do this to avoid saving that would be redundant with the
269-
// above. Oh well.
270-
room_updates.update_in_memory_caches(&self.state_store).await;
269+
// of the updated rooms (which relies on information stored in the database).
270+
processors::room::display_name::update_for_rooms(
271+
&mut context,
272+
&room_updates,
273+
&self.state_store,
274+
)
275+
.await;
276+
277+
// Save the new display name updates if any.
278+
processors::changes::save_only(context, &self.state_store).await?;
271279

272280
Ok(SyncResponse {
273281
rooms: room_updates,

0 commit comments

Comments
 (0)