From e75fe7b75ac8dea2b39f83b3142c183872d8971a Mon Sep 17 00:00:00 2001 From: Lambda Date: Sun, 10 Aug 2025 11:41:24 +0000 Subject: [PATCH] MSC3575: factor out TodoRoom --- src/api/client_server/sync/msc3575.rs | 92 +++++++++++++++------------ 1 file changed, 52 insertions(+), 40 deletions(-) diff --git a/src/api/client_server/sync/msc3575.rs b/src/api/client_server/sync/msc3575.rs index e1ede5de..7d748e42 100644 --- a/src/api/client_server/sync/msc3575.rs +++ b/src/api/client_server/sync/msc3575.rs @@ -19,7 +19,7 @@ use ruma::{ room::member::{MembershipState, RoomMemberEventContent}, StateEventType, TimelineEventType, }, - uint, JsOption, UInt, UserId, + uint, JsOption, OwnedRoomId, RoomId, UInt, UserId, }; use tracing::{debug, error}; @@ -29,6 +29,38 @@ use crate::{ services, Ar, Error, Ra, Result, }; +struct TodoRoom { + required_state_request: BTreeSet<(StateEventType, String)>, + timeline_limit: u64, + roomsince: u64, +} +impl TodoRoom { + fn update( + &mut self, + required_state: Vec<(StateEventType, String)>, + timeline_limit: UInt, + known_rooms: Option<&BTreeMap>, + room_id: &RoomId, + ) { + self.required_state_request.extend(required_state); + self.timeline_limit = + self.timeline_limit.max(u64::from(timeline_limit).min(100)); + // 0 means unknown because it got out of date + self.roomsince = self.roomsince.min( + known_rooms.and_then(|k| k.get(room_id)).copied().unwrap_or(0), + ); + } +} +impl Default for TodoRoom { + fn default() -> Self { + Self { + required_state_request: BTreeSet::new(), + timeline_limit: 0, + roomsince: u64::MAX, + } + } +} + #[allow(clippy::too_many_lines)] pub(crate) async fn sync_events_v4_route( body: Ar, @@ -296,7 +328,7 @@ pub(crate) async fn sync_events_v4_route( let mut lists = BTreeMap::new(); // and required state - let mut todo_rooms = BTreeMap::new(); + let mut todo_rooms: BTreeMap = BTreeMap::new(); for (list_id, list) in body.lists { if list.filters.and_then(|f| f.is_invite).unwrap_or(false) { @@ -322,27 +354,11 @@ pub(crate) async fn sync_events_v4_route( new_known_rooms.extend(room_ids.iter().cloned()); for room_id in &room_ids { - let todo_room = todo_rooms.entry(room_id.clone()).or_insert(( - BTreeSet::new(), - 0, - u64::MAX, - )); - let limit = list - .room_details - .timeline_limit - .map_or(10, u64::from) - .min(100); - todo_room - .0 - .extend(list.room_details.required_state.iter().cloned()); - todo_room.1 = todo_room.1.max(limit); - // 0 means unknown because it got out of date - todo_room.2 = todo_room.2.min( - known_rooms - .get(&list_id) - .and_then(|k| k.get(room_id)) - .copied() - .unwrap_or(0), + todo_rooms.entry(room_id.clone()).or_default().update( + list.room_details.required_state.clone(), + list.room_details.timeline_limit.unwrap_or(uint!(10)), + known_rooms.get(&list_id), + room_id, ); } ops.push(sync_events::v4::SyncOp { @@ -380,21 +396,11 @@ pub(crate) async fn sync_events_v4_route( if !services().rooms.metadata.exists(room_id)? { continue; } - let todo_room = todo_rooms.entry(room_id.clone()).or_insert(( - BTreeSet::new(), - 0, - u64::MAX, - )); - let limit = room.timeline_limit.map_or(10, u64::from).min(100); - todo_room.0.extend(room.required_state.iter().cloned()); - todo_room.1 = todo_room.1.max(limit); - // 0 means unknown because it got out of date - todo_room.2 = todo_room.2.min( - known_rooms - .get("subscriptions") - .and_then(|k| k.get(room_id)) - .copied() - .unwrap_or(0), + todo_rooms.entry(room_id.clone()).or_default().update( + room.required_state.clone(), + room.timeline_limit.unwrap_or(uint!(10)), + known_rooms.get("subscriptions"), + room_id, ); known_subscription_rooms.insert(room_id.clone()); } @@ -425,8 +431,14 @@ pub(crate) async fn sync_events_v4_route( } let mut rooms = BTreeMap::new(); - for (room_id, (required_state_request, timeline_limit, roomsince)) in - &todo_rooms + for ( + room_id, + TodoRoom { + required_state_request, + timeline_limit, + roomsince, + }, + ) in &todo_rooms { let roomsincecount = PduCount::Normal(*roomsince);