feat(presence): add configuration option to disable presence

This commit is contained in:
Jakub Kubík 2022-12-07 18:26:48 +01:00
parent a6d5bfe35f
commit 1f698718a0
No known key found for this signature in database
GPG key ID: D3A0D5D60F3A173F
3 changed files with 40 additions and 1 deletions

View file

@ -76,6 +76,9 @@ pub struct Config {
pub emergency_password: Option<String>, pub emergency_password: Option<String>,
#[serde(default = "true_fn")]
pub allow_presence: bool,
#[serde(default = "default_presence_idle_timeout")] #[serde(default = "default_presence_idle_timeout")]
pub presence_idle_timeout: u64, pub presence_idle_timeout: u64,
#[serde(default = "default_presence_offline_timeout")] #[serde(default = "default_presence_offline_timeout")]

View file

@ -286,6 +286,10 @@ impl Service {
&self.config.emergency_password &self.config.emergency_password
} }
pub fn allow_presence(&self) -> bool {
self.config.allow_presence
}
pub fn presence_idle_timeout(&self) -> u64 { pub fn presence_idle_timeout(&self) -> u64 {
self.config.presence_idle_timeout self.config.presence_idle_timeout
} }

View file

@ -4,7 +4,7 @@ pub use data::Data;
use ruma::{events::presence::PresenceEvent, OwnedUserId, RoomId, UserId}; use ruma::{events::presence::PresenceEvent, OwnedUserId, RoomId, UserId};
use tokio::sync::mpsc; use tokio::sync::mpsc;
use crate::{Error, Result}; use crate::{Error, Result, services};
pub struct Service { pub struct Service {
pub db: &'static dyn Data, pub db: &'static dyn Data,
@ -36,6 +36,10 @@ impl Service {
update_timestamp: bool, update_timestamp: bool,
spawn_timer: bool, spawn_timer: bool,
) -> Result<()> { ) -> Result<()> {
if !services().globals.allow_presence() {
return Ok(())
}
if spawn_timer { if spawn_timer {
self.spawn_timer(user_id)?; self.spawn_timer(user_id)?;
} }
@ -55,6 +59,10 @@ impl Service {
presence: PresenceEvent, presence: PresenceEvent,
spawn_timer: bool, spawn_timer: bool,
) -> Result<()> { ) -> Result<()> {
if !services().globals.allow_presence() {
return Ok(())
}
if spawn_timer { if spawn_timer {
self.spawn_timer(user_id)?; 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. /// Returns the timestamp of when the presence was last updated for the specified user.
pub fn last_presence_update(&self, user_id: &UserId) -> Result<Option<(u64, u64)>> { pub fn last_presence_update(&self, user_id: &UserId) -> Result<Option<(u64, u64)>> {
if !services().globals.allow_presence() {
return Ok(None)
}
self.db.last_presence_update(user_id) self.db.last_presence_update(user_id)
} }
@ -73,6 +85,10 @@ impl Service {
user_id: &UserId, user_id: &UserId,
room_id: &RoomId, room_id: &RoomId,
) -> Result<Option<PresenceEvent>> { ) -> Result<Option<PresenceEvent>> {
if !services().globals.allow_presence() {
return Ok(None)
}
let last_update = match self.db.last_presence_update(user_id)? { let last_update = match self.db.last_presence_update(user_id)? {
Some(last) => last.1, Some(last) => last.1,
None => return Ok(None), None => return Ok(None),
@ -88,6 +104,10 @@ impl Service {
room_id: &RoomId, room_id: &RoomId,
since: u64, since: u64,
) -> Result<Box<dyn Iterator<Item = (OwnedUserId, PresenceEvent)>>> { ) -> Result<Box<dyn Iterator<Item = (OwnedUserId, PresenceEvent)>>> {
if !services().globals.allow_presence() {
return Ok(Box::new(std::iter::empty()))
}
self.db.presence_since(room_id, since) self.db.presence_since(room_id, since)
} }
@ -96,15 +116,27 @@ impl Service {
&self, &self,
timer_receiver: mpsc::UnboundedReceiver<OwnedUserId>, timer_receiver: mpsc::UnboundedReceiver<OwnedUserId>,
) -> Result<()> { ) -> Result<()> {
if !services().globals.allow_presence() {
return Ok(())
}
self.db.presence_maintain(timer_receiver) self.db.presence_maintain(timer_receiver)
} }
fn presence_cleanup(&self) -> Result<()> { fn presence_cleanup(&self) -> Result<()> {
if !services().globals.allow_presence() {
return Ok(())
}
self.db.presence_cleanup() self.db.presence_cleanup()
} }
/// Spawns a timer for the user used by the maintenance task /// Spawns a timer for the user used by the maintenance task
fn spawn_timer(&self, user_id: &UserId) -> Result<()> { fn spawn_timer(&self, user_id: &UserId) -> Result<()> {
if !services().globals.allow_presence() {
return Ok(())
}
self.timer_sender self.timer_sender
.send(user_id.into()) .send(user_id.into())
.map_err(|_| Error::bad_database("Sender errored out"))?; .map_err(|_| Error::bad_database("Sender errored out"))?;