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.
1415use eyeball::SharedObservable;
16use ruma::{
17 events::{ignored_user_list::IgnoredUserListEvent, GlobalAccountDataEventType},
18 serde::Raw,
19};
20use tracing::{error, instrument, trace};
2122use super::Context;
23use crate::{
24 store::{BaseStateStore, StateStoreExt as _},
25Result,
26};
2728/// 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)]
31pub async fn save_only(context: Context, state_store: &BaseStateStore) -> Result<()> {
32 save_changes(&context, state_store, None).await?;
33 broadcast_room_info_notable_updates(&context, state_store);
3435Ok(())
36}
3738/// Save the [`StateChanges`] from the [`Context`] inside the
39/// [`BaseStateStore`], and apply them on the in-memory rooms.
40#[instrument(skip_all)]
41pub async fn save_and_apply(
42 context: Context,
43 state_store: &BaseStateStore,
44 ignore_user_list_changes: &SharedObservable<Vec<String>>,
45 sync_token: Option<String>,
46) -> Result<()> {
47trace!("ready to submit changes to store");
4849let previous_ignored_user_list =
50 state_store.get_account_data_event_static().await.ok().flatten();
5152 save_changes(&context, state_store, sync_token).await?;
53 apply_changes(&context, ignore_user_list_changes, previous_ignored_user_list);
54 broadcast_room_info_notable_updates(&context, state_store);
5556trace!("applied changes");
5758Ok(())
59}
6061async fn save_changes(
62 context: &Context,
63 state_store: &BaseStateStore,
64 sync_token: Option<String>,
65) -> Result<()> {
66 state_store.save_changes(&context.state_changes).await?;
6768if let Some(sync_token) = sync_token {
69*state_store.sync_token.write().await = Some(sync_token);
70 }
7172Ok(())
73}
7475fn apply_changes(
76 context: &Context,
77 ignore_user_list_changes: &SharedObservable<Vec<String>>,
78 previous_ignored_user_list: Option<Raw<IgnoredUserListEvent>>,
79) {
80if let Some(event) =
81 context.state_changes.account_data.get(&GlobalAccountDataEventType::IgnoredUserList)
82 {
83match event.deserialize_as::<IgnoredUserListEvent>() {
84Ok(event) => {
85let user_ids: Vec<String> =
86 event.content.ignored_users.keys().map(|id| id.to_string()).collect();
8788// Try to only trigger the observable if the ignored user list has changed,
89 // from the previous time we've seen it. If we couldn't load the previous event
90 // for any reason, always trigger.
91if let Some(prev_user_ids) =
92 previous_ignored_user_list.and_then(|raw| raw.deserialize().ok()).map(|event| {
93 event
94 .content
95 .ignored_users
96 .keys()
97 .map(|id| id.to_string())
98 .collect::<Vec<_>>()
99 })
100 {
101if user_ids != prev_user_ids {
102 ignore_user_list_changes.set(user_ids);
103 }
104 } else {
105 ignore_user_list_changes.set(user_ids);
106 }
107 }
108109Err(error) => {
110error!("Failed to deserialize ignored user list event: {error}")
111 }
112 }
113 }
114}
115116fn broadcast_room_info_notable_updates(context: &Context, state_store: &BaseStateStore) {
117for (room_id, room_info) in &context.state_changes.room_infos {
118if let Some(room) = state_store.room(room_id) {
119let room_info_notable_update_reasons =
120 context.room_info_notable_updates.get(room_id).copied().unwrap_or_default();
121122 room.set_room_info(room_info.clone(), room_info_notable_update_reasons)
123 }
124 }
125}