diff --git a/Cargo.toml b/Cargo.toml index 034231d04..83d70b227 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -272,6 +272,9 @@ version = "0.5.2" [workspace.dependencies.libloading] version = "0.9.0" +[workspace.dependencies.wildmatch] +version = "2.7.0" + # Validating urls in config, was already a transitive dependency [workspace.dependencies.url] version = "2.5.8" diff --git a/src/api/Cargo.toml b/src/api/Cargo.toml index ac70cb37d..689902717 100644 --- a/src/api/Cargo.toml +++ b/src/api/Cargo.toml @@ -87,7 +87,7 @@ serde.workspace = true sha1.workspace = true tokio.workspace = true tracing.workspace = true -wildmatch = "2.7.0" +wildmatch.workspace = true [lints] workspace = true diff --git a/src/api/client/sync/v3/joined.rs b/src/api/client/sync/v3/joined.rs index 7eee0d1d5..3a2828e69 100644 --- a/src/api/client/sync/v3/joined.rs +++ b/src/api/client/sync/v3/joined.rs @@ -64,7 +64,7 @@ pub(super) async fn load_joined_room( services: &Services, sync_context: SyncContext<'_>, ref room_id: OwnedRoomId, -) -> Result<(JoinedRoom, DeviceListUpdates)> { +) -> Result { /* Building a sync response involves many steps which all depend on each other. To parallelize the process as much as possible, each step is divided into its own function, @@ -80,7 +80,6 @@ pub(super) async fn load_joined_room( sticky, summary, notification_counts, - device_list_updates, }, ) = try_join3( build_account_data(services, sync_context, room_id), @@ -117,7 +116,7 @@ pub(super) async fn load_joined_room( unread_thread_notifications: BTreeMap::new(), }); - Ok((joined_room, device_list_updates)) + Ok(joined_room) } /// Collect changes to the syncing user's account data events. @@ -263,7 +262,6 @@ struct StateAndTimeline { sticky: Sticky, summary: Option, notification_counts: Option, - device_list_updates: DeviceListUpdates, } async fn build_sticky_events( @@ -377,24 +375,14 @@ async fn build_state_and_timeline( debug_warn!("timeline for newly joined room is empty"); } - let (summary, device_list_updates) = try_join( - build_room_summary( - services, - sync_context, - room_id, - shortstatehashes, - &timeline, - &state_events, - joined_since_last_sync, - ), - build_device_list_updates( - services, - sync_context, - room_id, - shortstatehashes, - &state_events, - joined_since_last_sync, - ), + let summary = build_room_summary( + services, + sync_context, + room_id, + shortstatehashes, + &timeline, + &state_events, + joined_since_last_sync, ) .await?; @@ -430,7 +418,6 @@ async fn build_state_and_timeline( sticky, summary, notification_counts, - device_list_updates, }) } @@ -854,35 +841,34 @@ async fn build_heroes( .await } -/// Collect updates to users' device lists for E2EE. -#[tracing::instrument(level = "debug", skip_all)] -async fn build_device_list_updates( +/// Collect updates to users' device lists for E2EE in a joined room. +#[tracing::instrument(level = "debug", skip_all, fields(%room_id))] +pub(super) async fn load_device_list_updates( services: &Services, - SyncContext { + sync_context: SyncContext<'_>, + room_id: &RoomId, +) -> Result { + let SyncContext { syncing_user, last_sync_end_count, current_count, .. - }: SyncContext<'_>, - room_id: &RoomId, - ShortStateHashes { current_shortstatehash, .. }: ShortStateHashes, - state_events: &Vec, - joined_since_last_sync: bool, -) -> Result { - let is_encrypted_room = services - .rooms - .state_accessor - .state_get(current_shortstatehash, &StateEventType::RoomEncryption, "") - .is_ok(); + } = sync_context; + + let mut device_list_updates = DeviceListUpdates::new(); // initial syncs don't include device updates, and rooms which aren't // encrypted don't affect them, so return early in either of those cases - if last_sync_end_count.is_none() || !(is_encrypted_room.await) { - return Ok(DeviceListUpdates::new()); + if last_sync_end_count.is_none() + || !services + .rooms + .state_accessor + .is_encrypted_room(room_id) + .await + { + return Ok(device_list_updates); } - let mut device_list_updates = DeviceListUpdates::new(); - // add users with changed keys to the `changed` list services .users @@ -893,19 +879,56 @@ async fn build_device_list_updates( }) .await; - // add users who now share encrypted rooms to `changed` and - // users who no longer share encrypted rooms to `left` - for state_event in state_events { - if state_event.kind == RoomMember { - let Some(content): Option = state_event.get_content().ok() + let shortstatehashes = fetch_shortstatehashes(services, sync_context, room_id).await?; + let ShortStateHashes { + current_shortstatehash, + last_sync_end_shortstatehash, + } = shortstatehashes; + + if check_joined_since_last_sync(services, shortstatehashes, sync_context).await? { + // the syncing user now shares an encrypted room with every member + services + .rooms + .state_cache + .room_members(room_id) + .ready_for_each(|user_id| { + device_list_updates.changed.insert(user_id); + }) + .await; + } else if let Some(last_sync_end_shortstatehash) = last_sync_end_shortstatehash + && last_sync_end_shortstatehash != current_shortstatehash + { + // add users who now share encrypted rooms to `changed` and + // users who no longer share encrypted rooms to `left` + let state_added = services + .rooms + .state_accessor + .state_added((last_sync_end_shortstatehash, current_shortstatehash)) + .await?; + + for (shortstatekey, _) in state_added { + let Ok((StateEventType::RoomMember, state_key)) = services + .rooms + .short + .get_statekey_from_short(shortstatekey) + .await else { continue; }; - let Some(user_id): Option = state_event - .state_key - .as_ref() - .and_then(|key| key.parse().ok()) + let Ok(user_id) = OwnedUserId::try_from(state_key.as_str()) else { + continue; + }; + + let Ok(content): Result = services + .rooms + .state_accessor + .state_get_content( + current_shortstatehash, + &StateEventType::RoomMember, + user_id.as_str(), + ) + .await else { continue; }; @@ -921,7 +944,7 @@ async fn build_device_list_updates( | Leave if !shares_encrypted_room => { device_list_updates.left.insert(user_id); }, - | Join if joined_since_last_sync || shares_encrypted_room => { + | Join if shares_encrypted_room => { device_list_updates.changed.insert(user_id); }, | _ => (), diff --git a/src/api/client/sync/v3/mod.rs b/src/api/client/sync/v3/mod.rs index 1363a1e0e..a2ef6ae00 100644 --- a/src/api/client/sync/v3/mod.rs +++ b/src/api/client/sync/v3/mod.rs @@ -46,7 +46,10 @@ use crate::{ Ruma, client::{ is_ignored_invite, - sync::v3::{joined::load_joined_room, left::load_left_room}, + sync::v3::{ + joined::{load_device_list_updates, load_joined_room}, + left::load_left_room, + }, }, client_ip::ClientIp, }; @@ -268,47 +271,45 @@ pub(crate) async fn build_sync_events( use_state_after: body.use_state_after, }; - let unfiltered = Box::new(FilterDefinition::default()); - let unfiltered_context = SyncContext { filter: &unfiltered, ..context }; - let joined_rooms = services .rooms .state_cache .rooms_joined(syncing_user) - // a filtered out room still contributes its device list updates, which - // an initial sync does not have - .ready_filter(|room_id| { - last_sync_end_count.is_some() || room_selected(&filter.room, room_id) - }) + .ready_filter(|room_id| room_selected(&filter.room, room_id)) .broad_filter_map(|room_id| async { - let selected = room_selected(&filter.room, &room_id); - // the client is not sent this room, so its lazy loading state must - // not change - let context = if selected { context } else { unfiltered_context }; let joined_room = load_joined_room(services, context, room_id.clone()).await; match joined_room { - | Ok((room, updates)) => Some((room_id, selected.then_some(room), updates)), + | Ok(room) => Some((room_id, room)), | Err(err) => { error!(?err, %room_id, "error loading joined room"); None }, } }) - .ready_fold( - (BTreeMap::new(), DeviceListUpdates::new()), - |(mut joined_rooms, mut all_updates), (room_id, joined_room, updates)| { - all_updates.merge(updates); + .ready_fold(BTreeMap::new(), |mut joined_rooms, (room_id, joined_room)| { + if !joined_room.is_empty() { + joined_rooms.insert(room_id, joined_room); + } - if let Some(joined_room) = joined_room - && !joined_room.is_empty() - { - joined_rooms.insert(room_id, joined_room); - } + joined_rooms + }); - (joined_rooms, all_updates) - }, - ); + // a filtered out room still contributes its device list updates + let device_list_updates = services + .rooms + .state_cache + .rooms_joined(syncing_user) + .broad_filter_map(|room_id| async move { + load_device_list_updates(services, context, &room_id) + .await + .inspect_err(|err| error!(?err, %room_id, "error loading device list updates")) + .ok() + }) + .ready_fold(DeviceListUpdates::new(), |mut all_updates, updates| { + all_updates.merge(updates); + all_updates + }); let left_rooms = services .rooms @@ -429,7 +430,8 @@ pub(crate) async fn build_sync_events( .list_unused_fallback_key_types(syncing_user, syncing_device); let ( - (joined_rooms, mut device_list_updates), + joined_rooms, + mut device_list_updates, left_rooms, invited_rooms, knocked_rooms, @@ -442,6 +444,7 @@ pub(crate) async fn build_sync_events( ) = async { futures::join!( joined_rooms, + device_list_updates, left_rooms, invited_rooms, knocked_rooms, diff --git a/src/ruminuwuity/Cargo.toml b/src/ruminuwuity/Cargo.toml index 19818776a..d523ce8a6 100644 --- a/src/ruminuwuity/Cargo.toml +++ b/src/ruminuwuity/Cargo.toml @@ -25,7 +25,7 @@ http.workspace = true ruma.workspace = true serde = { workspace = true } serde_json = { workspace = true } -wildmatch = "2.7.0" +wildmatch.workspace = true [dev-dependencies]