Skip to content
Snippets Groups Projects
Commit 69025d30 authored by Jakub Kubík's avatar Jakub Kubík Committed by 🥺
Browse files

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

parent e93b4aa0
No related branches found
No related tags found
No related merge requests found
...@@ -537,14 +537,12 @@ async fn process_room_presence_updates( ...@@ -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);
......
...@@ -140,13 +140,13 @@ fn presence_since<'a>( ...@@ -140,13 +140,13 @@ fn presence_since<'a>(
&'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 @@ fn presence_since<'a>( ...@@ -160,10 +160,7 @@ fn presence_since<'a>(
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,
}),
) )
} }
} }
......
...@@ -29,5 +29,5 @@ fn presence_since<'a>( ...@@ -29,5 +29,5 @@ fn presence_since<'a>(
&'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>;
} }
...@@ -122,11 +122,11 @@ pub fn remove_presence(&self, user_id: &UserId) -> Result<()> { ...@@ -122,11 +122,11 @@ pub fn remove_presence(&self, user_id: &UserId) -> Result<()> {
} }
/// 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)
} }
} }
......
...@@ -290,14 +290,12 @@ pub fn select_edus(&self, server_name: &ServerName) -> Result<(Vec<Vec<u8>>, u64 ...@@ -290,14 +290,12 @@ pub fn select_edus(&self, server_name: &ServerName) -> Result<(Vec<Vec<u8>>, u64
// 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;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment