style(presence): use flat_map instead of matching Results in filter

This commit is contained in:
Jakub Kubík 2023-12-20 12:04:47 +01:00 committed by strawberry
parent e93b4aa08e
commit 69025d30f7
5 changed files with 9 additions and 16 deletions

View file

@ -537,14 +537,12 @@ async fn process_room_presence_updates(
since: u64, since: u64,
) -> Result<()> { ) -> Result<()> {
// Take presence updates from this room // Take presence updates from this room
for presence_data in services() for (user_id, _, presence_event) in services()
.rooms .rooms
.edus .edus
.presence .presence
.presence_since(room_id, since) .presence_since(room_id, since)
{ {
let (user_id, _, presence_event) = presence_data?;
match presence_updates.entry(user_id) { match presence_updates.entry(user_id) {
Entry::Vacant(slot) => { Entry::Vacant(slot) => {
slot.insert(presence_event); slot.insert(presence_event);

View file

@ -140,13 +140,13 @@ impl service::rooms::edus::presence::Data for KeyValueDatabase {
&'a self, &'a self,
room_id: &RoomId, room_id: &RoomId,
since: u64, since: u64,
) -> Box<dyn Iterator<Item = Result<(OwnedUserId, u64, PresenceEvent)>> + 'a> { ) -> Box<dyn Iterator<Item = (OwnedUserId, u64, PresenceEvent)> + 'a> {
let prefix = [room_id.as_bytes(), &[0xff]].concat(); let prefix = [room_id.as_bytes(), &[0xff]].concat();
Box::new( Box::new(
self.roomuserid_presence self.roomuserid_presence
.scan_prefix(prefix) .scan_prefix(prefix)
.map( .flat_map(
|(key, presence_bytes)| -> Result<(OwnedUserId, u64, PresenceEvent)> { |(key, presence_bytes)| -> Result<(OwnedUserId, u64, PresenceEvent)> {
let user_id = user_id_from_bytes( let user_id = user_id_from_bytes(
key.rsplit(|byte| *byte == 0xff).next().ok_or_else(|| { 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)) Ok((user_id, presence.last_count, presence_event))
}, },
) )
.filter(move |presence_data| match presence_data { .filter(move |(_, count, _)| *count > since),
Ok((_, count, _)) => *count > since,
Err(_) => false,
}),
) )
} }
} }

View file

@ -29,5 +29,5 @@ pub trait Data: Send + Sync {
&'a self, &'a self,
room_id: &RoomId, room_id: &RoomId,
since: u64, since: u64,
) -> Box<dyn Iterator<Item = Result<(OwnedUserId, u64, PresenceEvent)>> + 'a>; ) -> Box<dyn Iterator<Item = (OwnedUserId, u64, PresenceEvent)> + 'a>;
} }

View file

@ -122,11 +122,11 @@ impl Service {
} }
/// Returns the most recent presence updates that happened after the event with id `since`. /// Returns the most recent presence updates that happened after the event with id `since`.
pub fn presence_since<'a>( pub fn presence_since(
&'a self, &self,
room_id: &RoomId, room_id: &RoomId,
since: u64, since: u64,
) -> Box<dyn Iterator<Item = Result<(OwnedUserId, u64, PresenceEvent)>> + 'a> { ) -> Box<dyn Iterator<Item = (OwnedUserId, u64, PresenceEvent)>> {
self.db.presence_since(room_id, since) self.db.presence_since(room_id, since)
} }
} }

View file

@ -290,14 +290,12 @@ impl Service {
// Look for presence updates in this room // Look for presence updates in this room
let mut presence_updates = Vec::new(); let mut presence_updates = Vec::new();
for presence_data in services() for (user_id, count, presence_event) in services()
.rooms .rooms
.edus .edus
.presence .presence
.presence_since(&room_id, since) .presence_since(&room_id, since)
{ {
let (user_id, count, presence_event) = presence_data?;
if count > max_edu_count { if count > max_edu_count {
max_edu_count = count; max_edu_count = count;
} }