From 1f698718a04613319a19299840c28d3a09e75f23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Kub=C3=ADk?= Date: Wed, 7 Dec 2022 18:26:48 +0100 Subject: [PATCH] feat(presence): add configuration option to disable presence --- src/config/mod.rs | 3 +++ src/service/globals/mod.rs | 4 +++ src/service/rooms/edus/presence/mod.rs | 34 +++++++++++++++++++++++++- 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/config/mod.rs b/src/config/mod.rs index 43bd0179..025969e3 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -76,6 +76,9 @@ pub struct Config { pub emergency_password: Option, + #[serde(default = "true_fn")] + pub allow_presence: bool, + #[serde(default = "default_presence_idle_timeout")] pub presence_idle_timeout: u64, #[serde(default = "default_presence_offline_timeout")] diff --git a/src/service/globals/mod.rs b/src/service/globals/mod.rs index aa9e832d..50c465ce 100644 --- a/src/service/globals/mod.rs +++ b/src/service/globals/mod.rs @@ -286,6 +286,10 @@ impl Service { &self.config.emergency_password } + pub fn allow_presence(&self) -> bool { + self.config.allow_presence + } + pub fn presence_idle_timeout(&self) -> u64 { self.config.presence_idle_timeout } diff --git a/src/service/rooms/edus/presence/mod.rs b/src/service/rooms/edus/presence/mod.rs index f0e9833d..b78bf46e 100644 --- a/src/service/rooms/edus/presence/mod.rs +++ b/src/service/rooms/edus/presence/mod.rs @@ -4,7 +4,7 @@ pub use data::Data; use ruma::{events::presence::PresenceEvent, OwnedUserId, RoomId, UserId}; use tokio::sync::mpsc; -use crate::{Error, Result}; +use crate::{Error, Result, services}; pub struct Service { pub db: &'static dyn Data, @@ -36,6 +36,10 @@ impl Service { update_timestamp: bool, spawn_timer: bool, ) -> Result<()> { + if !services().globals.allow_presence() { + return Ok(()) + } + if spawn_timer { self.spawn_timer(user_id)?; } @@ -55,6 +59,10 @@ impl Service { presence: PresenceEvent, spawn_timer: bool, ) -> Result<()> { + if !services().globals.allow_presence() { + return Ok(()) + } + if spawn_timer { self.spawn_timer(user_id)?; } @@ -64,6 +72,10 @@ impl Service { /// Returns the timestamp of when the presence was last updated for the specified user. pub fn last_presence_update(&self, user_id: &UserId) -> Result> { + if !services().globals.allow_presence() { + return Ok(None) + } + self.db.last_presence_update(user_id) } @@ -73,6 +85,10 @@ impl Service { user_id: &UserId, room_id: &RoomId, ) -> Result> { + if !services().globals.allow_presence() { + return Ok(None) + } + let last_update = match self.db.last_presence_update(user_id)? { Some(last) => last.1, None => return Ok(None), @@ -88,6 +104,10 @@ impl Service { room_id: &RoomId, since: u64, ) -> Result>> { + if !services().globals.allow_presence() { + return Ok(Box::new(std::iter::empty())) + } + self.db.presence_since(room_id, since) } @@ -96,15 +116,27 @@ impl Service { &self, timer_receiver: mpsc::UnboundedReceiver, ) -> Result<()> { + if !services().globals.allow_presence() { + return Ok(()) + } + self.db.presence_maintain(timer_receiver) } fn presence_cleanup(&self) -> Result<()> { + if !services().globals.allow_presence() { + return Ok(()) + } + self.db.presence_cleanup() } /// Spawns a timer for the user used by the maintenance task fn spawn_timer(&self, user_id: &UserId) -> Result<()> { + if !services().globals.allow_presence() { + return Ok(()) + } + self.timer_sender .send(user_id.into()) .map_err(|_| Error::bad_database("Sender errored out"))?;