From 8134dd9151212317a4a2e916d9d4afd8863ebf5a Mon Sep 17 00:00:00 2001 From: K900 Date: Wed, 3 Apr 2024 16:57:12 +0300 Subject: [PATCH] Reduce number of separate sources of truth for presence disabled-ness Instead of checking if we should update every time we want to update, call the updater every time and decide internally. --- src/api/client_server/profile.rs | 20 ++++++++------------ src/api/client_server/sync.rs | 8 +++----- src/database/key_value/presence.rs | 14 ++++++++++---- src/database/mod.rs | 13 ++++++++----- 4 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/api/client_server/profile.rs b/src/api/client_server/profile.rs index 846b6144..88059f94 100644 --- a/src/api/client_server/profile.rs +++ b/src/api/client_server/profile.rs @@ -85,12 +85,10 @@ pub async fn set_displayname_route( .await; } - if services().globals.allow_local_presence() { - // Presence update - services() - .presence - .ping_presence(sender_user, PresenceState::Online)?; - } + // Presence update + services() + .presence + .ping_presence(sender_user, PresenceState::Online)?; Ok(set_display_name::v3::Response {}) } @@ -224,12 +222,10 @@ pub async fn set_avatar_url_route(body: Ruma) -> Re .await; } - if services().globals.allow_local_presence() { - // Presence update - services() - .presence - .ping_presence(sender_user, PresenceState::Online)?; - } + // Presence update + services() + .presence + .ping_presence(sender_user, PresenceState::Online)?; Ok(set_avatar_url::v3::Response {}) } diff --git a/src/api/client_server/sync.rs b/src/api/client_server/sync.rs index fa24aed4..a8d1f7f4 100644 --- a/src/api/client_server/sync.rs +++ b/src/api/client_server/sync.rs @@ -171,11 +171,9 @@ async fn sync_helper( // bool = caching allowed ) -> Result<(sync_events::v3::Response, bool), Error> { // Presence update - if services().globals.allow_local_presence() { - services() - .presence - .ping_presence(&sender_user, body.set_presence)?; - } + services() + .presence + .ping_presence(&sender_user, body.set_presence)?; // Setup watchers, so if there's no response, we can wait for them let watcher = services().globals.watch(&sender_user, &sender_device); diff --git a/src/database/key_value/presence.rs b/src/database/key_value/presence.rs index e26765f0..23f5c43c 100644 --- a/src/database/key_value/presence.rs +++ b/src/database/key_value/presence.rs @@ -24,6 +24,10 @@ impl service::presence::Data for KeyValueDatabase { } fn ping_presence(&self, user_id: &UserId, new_state: PresenceState) -> Result<()> { + let Some(ref tx) = *self.presence_timer_sender else { + return Ok(()); + }; + let now = utils::millis_since_unix_epoch(); let mut state_changed = false; @@ -74,8 +78,7 @@ impl service::presence::Data for KeyValueDatabase { _ => services().globals.config.presence_offline_timeout_s, }; - self.presence_timer_sender - .send((user_id.to_owned(), Duration::from_secs(timeout))) + tx.send((user_id.to_owned(), Duration::from_secs(timeout))) .map_err(|e| { error!("Failed to add presence timer: {}", e); Error::bad_database("Failed to add presence timer") @@ -86,6 +89,10 @@ impl service::presence::Data for KeyValueDatabase { &self, room_id: &RoomId, user_id: &UserId, presence_state: PresenceState, currently_active: Option, last_active_ago: Option, status_msg: Option, ) -> Result<()> { + let Some(ref tx) = *self.presence_timer_sender else { + return Ok(()); + }; + let now = utils::millis_since_unix_epoch(); let last_active_ts = match last_active_ago { Some(last_active_ago) => now.saturating_sub(last_active_ago.into()), @@ -107,8 +114,7 @@ impl service::presence::Data for KeyValueDatabase { _ => services().globals.config.presence_offline_timeout_s, }; - self.presence_timer_sender - .send((user_id.to_owned(), Duration::from_secs(timeout))) + tx.send((user_id.to_owned(), Duration::from_secs(timeout))) .map_err(|e| { error!("Failed to add presence timer: {}", e); Error::bad_database("Failed to add presence timer") diff --git a/src/database/mod.rs b/src/database/mod.rs index cf0a1d15..564f90a6 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -185,7 +185,7 @@ pub struct KeyValueDatabase { pub(super) our_real_users_cache: RwLock>>>, pub(super) appservice_in_room_cache: RwLock>>, pub(super) lasttimelinecount_cache: Mutex>, - pub(super) presence_timer_sender: Arc>, + pub(super) presence_timer_sender: Arc>>, } #[derive(Deserialize)] @@ -275,7 +275,13 @@ impl KeyValueDatabase { }, }; - let (presence_sender, presence_receiver) = mpsc::unbounded_channel(); + let presence_sender = if services().globals.allow_local_presence() { + let (presence_sender, presence_receiver) = mpsc::unbounded_channel(); + Self::start_presence_handler(presence_receiver).await; + Some(presence_sender) + } else { + None + }; let db_raw = Box::new(Self { db: builder.clone(), @@ -1059,9 +1065,6 @@ impl KeyValueDatabase { if services().globals.allow_check_for_updates() { Self::start_check_for_updates_task().await; } - if services().globals.allow_local_presence() { - Self::start_presence_handler(presence_receiver).await; - } Ok(()) }