From 5314221e89cfd7ab013a63614fb94016458a1edd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Kub=C3=ADk?= Date: Wed, 20 Dec 2023 12:04:47 +0100 Subject: [PATCH] style(presence): use flat_map instead of matching Results in filter --- src/api/client_server/sync.rs | 4 +--- src/database/key_value/rooms/edus/presence.rs | 9 +++------ src/service/rooms/edus/presence/data.rs | 2 +- src/service/rooms/edus/presence/mod.rs | 6 +++--- src/service/sending/mod.rs | 4 +--- 5 files changed, 9 insertions(+), 16 deletions(-) diff --git a/src/api/client_server/sync.rs b/src/api/client_server/sync.rs index ac8dcd5d..b9878c6c 100644 --- a/src/api/client_server/sync.rs +++ b/src/api/client_server/sync.rs @@ -577,14 +577,12 @@ async fn process_room_presence_updates( since: u64, ) -> Result<()> { // Take presence updates from this room - for presence_data in services() + for (user_id, _, presence_event) in services() .rooms .edus .presence .presence_since(room_id, since) { - let (user_id, _, presence_event) = presence_data?; - match presence_updates.entry(user_id) { Entry::Vacant(slot) => { slot.insert(presence_event); diff --git a/src/database/key_value/rooms/edus/presence.rs b/src/database/key_value/rooms/edus/presence.rs index d70117e0..544f1537 100644 --- a/src/database/key_value/rooms/edus/presence.rs +++ b/src/database/key_value/rooms/edus/presence.rs @@ -140,13 +140,13 @@ impl service::rooms::edus::presence::Data for KeyValueDatabase { &'a self, room_id: &RoomId, since: u64, - ) -> Box> + 'a> { + ) -> Box + 'a> { let prefix = [room_id.as_bytes(), &[0xff]].concat(); Box::new( self.roomuserid_presence .scan_prefix(prefix) - .map( + .flat_map( |(key, presence_bytes)| -> Result<(OwnedUserId, u64, PresenceEvent)> { let user_id = user_id_from_bytes( key.rsplit(|byte| *byte == 0xff).next().ok_or_else(|| { @@ -160,10 +160,7 @@ impl service::rooms::edus::presence::Data for KeyValueDatabase { Ok((user_id, presence.last_count, presence_event)) }, ) - .filter(move |presence_data| match presence_data { - Ok((_, count, _)) => *count > since, - Err(_) => false, - }), + .filter(move |(_, count, _)| *count > since), ) } } diff --git a/src/service/rooms/edus/presence/data.rs b/src/service/rooms/edus/presence/data.rs index cfac45aa..c13ac6f4 100644 --- a/src/service/rooms/edus/presence/data.rs +++ b/src/service/rooms/edus/presence/data.rs @@ -29,5 +29,5 @@ pub trait Data: Send + Sync { &'a self, room_id: &RoomId, since: u64, - ) -> Box> + 'a>; + ) -> Box + 'a>; } diff --git a/src/service/rooms/edus/presence/mod.rs b/src/service/rooms/edus/presence/mod.rs index 6a09e08e..fb929c45 100644 --- a/src/service/rooms/edus/presence/mod.rs +++ b/src/service/rooms/edus/presence/mod.rs @@ -122,11 +122,11 @@ impl Service { } /// Returns the most recent presence updates that happened after the event with id `since`. - pub fn presence_since<'a>( - &'a self, + pub fn presence_since( + &self, room_id: &RoomId, since: u64, - ) -> Box> + 'a> { + ) -> Box> { self.db.presence_since(room_id, since) } } diff --git a/src/service/sending/mod.rs b/src/service/sending/mod.rs index 670463a7..9cc80a7f 100644 --- a/src/service/sending/mod.rs +++ b/src/service/sending/mod.rs @@ -290,14 +290,12 @@ impl Service { // Look for presence updates in this room let mut presence_updates = Vec::new(); - for presence_data in services() + for (user_id, count, presence_event) in services() .rooms .edus .presence .presence_since(&room_id, since) { - let (user_id, count, presence_event) = presence_data?; - if count > max_edu_count { max_edu_count = count; }