lazy-construct presence; avoids useless db queries in sender and syncer.

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-04-16 02:00:54 -07:00 committed by June
parent 8b003e6be2
commit 33cc3d56c1
5 changed files with 16 additions and 11 deletions

View file

@ -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<dyn Iterator<Item = (OwnedUserId, u64, PresenceEvent)> + 'a>;
fn presence_since<'a>(&'a self, since: u64) -> Box<dyn Iterator<Item = (OwnedUserId, u64, Vec<u8>)> + 'a>;
}

View file

@ -35,6 +35,11 @@ impl Presence {
}
}
pub fn from_json_bytes_to_event(bytes: &[u8], user_id: &UserId) -> Result<PresenceEvent> {
let presence = Self::from_json_bytes(bytes)?;
presence.to_presence_event(user_id)
}
pub fn from_json_bytes(bytes: &[u8]) -> Result<Self> {
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<dyn Iterator<Item = (OwnedUserId, u64, PresenceEvent)>> {
pub fn presence_since(&self, since: u64) -> Box<dyn Iterator<Item = (OwnedUserId, u64, Vec<u8>)>> {
self.db.presence_since(since)
}

View file

@ -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<bool> {
// 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,