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 std::collections::BTreeSet;
1617use matrix_sdk_crypto::OlmMachine;
18use ruma::{OwnedUserId, RoomId};
1920use super::super::Context;
21use crate::{store::BaseStateStore, EncryptionState, Result, RoomMemberships};
2223/// Update tracked users, if the room is encrypted.
24pub async fn update(
25 _context: &mut Context,
26 olm_machine: Option<&OlmMachine>,
27 room_encryption_state: EncryptionState,
28 user_ids_to_track: &BTreeSet<OwnedUserId>,
29) -> Result<()> {
30if room_encryption_state.is_encrypted() {
31if let Some(olm) = olm_machine {
32if !user_ids_to_track.is_empty() {
33 olm.update_tracked_users(user_ids_to_track.iter().map(AsRef::as_ref)).await?
34}
35 }
36 }
3738Ok(())
39}
4041/// Update tracked users, if the room is encrypted, or if the room has become
42/// encrypted.
43pub async fn update_or_set_if_room_is_newly_encrypted(
44 _context: &mut Context,
45 olm_machine: Option<&OlmMachine>,
46 user_ids_to_track: &BTreeSet<OwnedUserId>,
47 new_room_encryption_state: EncryptionState,
48 previous_room_encryption_state: EncryptionState,
49 room_id: &RoomId,
50 state_store: &BaseStateStore,
51) -> Result<()> {
52if new_room_encryption_state.is_encrypted() {
53if let Some(olm) = olm_machine {
54if !previous_room_encryption_state.is_encrypted() {
55// The room turned on encryption in this sync, we need
56 // to also get all the existing users and mark them for
57 // tracking.
58let user_ids = state_store.get_user_ids(room_id, RoomMemberships::ACTIVE).await?;
59 olm.update_tracked_users(user_ids.iter().map(AsRef::as_ref)).await?
60}
6162if !user_ids_to_track.is_empty() {
63 olm.update_tracked_users(user_ids_to_track.iter().map(AsRef::as_ref)).await?;
64 }
65 }
66 }
6768Ok(())
69}