diff --git a/src/api/client_server/sync.rs b/src/api/client_server/sync.rs index bcaad1d9..ef891621 100644 --- a/src/api/client_server/sync.rs +++ b/src/api/client_server/sync.rs @@ -571,7 +571,7 @@ async fn process_presence_updates( presence_updates: &mut HashMap, since: u64, syncing_user: &OwnedUserId, ) -> Result<()> { // Take presence updates - for (user_id, _, presence_event) in services().presence.presence_since(since) { + for (user_id, _, presence_bytes) in services().presence.presence_since(since) { if !services() .rooms .state_cache @@ -580,6 +580,8 @@ async fn process_presence_updates( continue; } + use crate::service::presence::Presence; + let presence_event = Presence::from_json_bytes_to_event(&presence_bytes, &user_id)?; match presence_updates.entry(user_id) { Entry::Vacant(slot) => { slot.insert(presence_event); diff --git a/src/database/key_value/presence.rs b/src/database/key_value/presence.rs index 153c5e0b..c123a339 100644 --- a/src/database/key_value/presence.rs +++ b/src/database/key_value/presence.rs @@ -92,16 +92,13 @@ impl service::presence::Data for KeyValueDatabase { Ok(()) } - fn presence_since<'a>(&'a self, since: u64) -> Box + 'a> { + fn presence_since<'a>(&'a self, since: u64) -> Box)> + 'a> { Box::new( self.presenceid_presence .iter() - .flat_map(|(key, presence_bytes)| -> Result<(OwnedUserId, u64, PresenceEvent)> { + .flat_map(|(key, presence_bytes)| -> Result<(OwnedUserId, u64, Vec)> { let (count, user_id) = presenceid_parse(&key)?; - let presence = Presence::from_json_bytes(&presence_bytes)?; - let presence_event = presence.to_presence_event(&user_id)?; - - Ok((user_id, count, presence_event)) + Ok((user_id, count, presence_bytes)) }) .filter(move |(_, count, _)| *count > since), ) diff --git a/src/service/presence/data.rs b/src/service/presence/data.rs index 649601a6..6f0f58f8 100644 --- a/src/service/presence/data.rs +++ b/src/service/presence/data.rs @@ -17,5 +17,5 @@ pub trait Data: Send + Sync { /// Returns the most recent presence updates that happened after the event /// with id `since`. - fn presence_since<'a>(&'a self, since: u64) -> Box + 'a>; + fn presence_since<'a>(&'a self, since: u64) -> Box)> + 'a>; } diff --git a/src/service/presence/mod.rs b/src/service/presence/mod.rs index 64a1682a..2be8c981 100644 --- a/src/service/presence/mod.rs +++ b/src/service/presence/mod.rs @@ -35,6 +35,11 @@ impl Presence { } } + pub fn from_json_bytes_to_event(bytes: &[u8], user_id: &UserId) -> Result { + let presence = Self::from_json_bytes(bytes)?; + presence.to_presence_event(user_id) + } + pub fn from_json_bytes(bytes: &[u8]) -> Result { serde_json::from_slice(bytes).map_err(|_| Error::bad_database("Invalid presence data in database")) } @@ -169,7 +174,7 @@ impl Service { /// Returns the most recent presence updates that happened after the event /// with id `since`. - pub fn presence_since(&self, since: u64) -> Box> { + pub fn presence_since(&self, since: u64) -> Box)>> { self.db.presence_since(since) } diff --git a/src/service/sending/mod.rs b/src/service/sending/mod.rs index fb9c34cd..a83555fa 100644 --- a/src/service/sending/mod.rs +++ b/src/service/sending/mod.rs @@ -28,7 +28,7 @@ use ruma::{ use tokio::sync::{Mutex, Semaphore}; use tracing::{error, warn}; -use crate::{services, utils::calculate_hash, Config, Error, PduEvent, Result}; +use crate::{service::presence::Presence, services, utils::calculate_hash, Config, Error, PduEvent, Result}; pub mod appservice; pub mod data; @@ -493,7 +493,7 @@ pub fn select_edus_presence( ) -> Result { // Look for presence updates for this server let mut presence_updates = Vec::new(); - for (user_id, count, presence_event) in services().presence.presence_since(since) { + for (user_id, count, presence_bytes) in services().presence.presence_since(since) { *max_edu_count = cmp::max(count, *max_edu_count); if user_id.server_name() != services().globals.server_name() { @@ -508,6 +508,7 @@ pub fn select_edus_presence( continue; } + let presence_event = Presence::from_json_bytes_to_event(&presence_bytes, &user_id)?; presence_updates.push(PresenceUpdate { user_id, presence: presence_event.content.presence,