1
0
Fork 0
mirror of https://forgejo.ellis.link/continuwuation/continuwuity.git synced 2025-08-10 15:30:54 +00:00
continuwuity/src/service/rooms/edus/presence/mod.rs

123 lines
4.2 KiB
Rust
Raw Normal View History

mod data;
2022-10-05 18:36:12 +02:00
use std::{collections::HashMap, sync::Arc};
pub use data::Data;
2022-10-05 20:34:31 +02:00
use ruma::{events::presence::PresenceEvent, RoomId, UserId};
2022-09-07 13:25:51 +02:00
use crate::Result;
2022-10-05 12:45:54 +02:00
pub struct Service {
2022-10-05 18:36:12 +02:00
db: Arc<dyn Data>,
}
2022-10-05 12:45:54 +02:00
impl Service {
2020-07-28 15:58:50 +02:00
/// Adds a presence event which will be saved until a new event replaces it.
///
/// Note: This method takes a RoomId because presence updates are always bound to rooms to
/// make sure users outside these rooms can't see them.
pub fn update_presence(
&self,
user_id: &UserId,
room_id: &RoomId,
presence: PresenceEvent,
2020-07-28 15:58:50 +02:00
) -> Result<()> {
self.db.update_presence(user_id, room_id, presence)
2020-07-28 15:58:50 +02:00
}
/// Resets the presence timeout, so the user will stay in their current presence state.
pub fn ping_presence(&self, user_id: &UserId) -> Result<()> {
self.db.ping_presence(user_id)
2020-07-28 15:58:50 +02:00
}
2021-05-14 11:03:18 +02:00
pub fn get_last_presence_event(
&self,
user_id: &UserId,
room_id: &RoomId,
) -> Result<Option<PresenceEvent>> {
let last_update = match self.db.last_presence_update(user_id)? {
2021-05-14 11:03:18 +02:00
Some(last) => last,
None => return Ok(None),
};
self.db.get_presence_event(room_id, user_id, last_update)
2021-05-14 11:03:18 +02:00
}
/* TODO
2020-07-28 15:58:50 +02:00
/// Sets all users to offline who have been quiet for too long.
2021-09-01 15:21:02 +02:00
fn _presence_maintain(
2020-07-28 15:58:50 +02:00
&self,
rooms: &super::Rooms,
globals: &super::super::globals::Globals,
) -> Result<()> {
let current_timestamp = utils::millis_since_unix_epoch();
for (user_id_bytes, last_timestamp) in self
.userid_lastpresenceupdate
.iter()
.filter_map(|(k, bytes)| {
Some((
k,
utils::u64_from_bytes(&bytes)
.map_err(|_| {
Error::bad_database("Invalid timestamp in userid_lastpresenceupdate.")
})
.ok()?,
))
})
2021-04-11 21:35:17 +02:00
.take_while(|(_, timestamp)| current_timestamp.saturating_sub(*timestamp) > 5 * 60_000)
2020-07-29 17:37:26 +02:00
// 5 Minutes
2020-07-28 15:58:50 +02:00
{
// Send new presence events to set the user offline
let count = globals.next_count()?.to_be_bytes();
2021-11-26 20:36:40 +01:00
let user_id: Box<_> = utils::string_from_bytes(&user_id_bytes)
2020-07-28 15:58:50 +02:00
.map_err(|_| {
Error::bad_database("Invalid UserId bytes in userid_lastpresenceupdate.")
})?
.try_into()
.map_err(|_| Error::bad_database("Invalid UserId in userid_lastpresenceupdate."))?;
for room_id in rooms.rooms_joined(&user_id).filter_map(|r| r.ok()) {
let mut presence_id = room_id.as_bytes().to_vec();
2020-07-28 15:58:50 +02:00
presence_id.push(0xff);
presence_id.extend_from_slice(&count);
presence_id.push(0xff);
presence_id.extend_from_slice(&user_id_bytes);
self.presenceid_presence.insert(
2021-06-08 18:10:00 +02:00
&presence_id,
&serde_json::to_vec(&PresenceEvent {
2020-07-28 15:58:50 +02:00
content: PresenceEventContent {
avatar_url: None,
currently_active: None,
displayname: None,
last_active_ago: Some(
last_timestamp.try_into().expect("time is valid"),
),
presence: PresenceState::Offline,
status_msg: None,
},
2021-11-26 20:36:40 +01:00
sender: user_id.to_owned(),
2020-07-28 15:58:50 +02:00
})
.expect("PresenceEvent can be serialized"),
)?;
}
self.userid_lastpresenceupdate.insert(
user_id.as_bytes(),
&utils::millis_since_unix_epoch().to_be_bytes(),
)?;
2020-07-28 15:58:50 +02:00
}
Ok(())
}*/
2020-07-28 15:58:50 +02:00
/// Returns the most recent presence updates that happened after the event with id `since`.
#[tracing::instrument(skip(self, since, room_id))]
2020-07-28 15:58:50 +02:00
pub fn presence_since(
&self,
room_id: &RoomId,
since: u64,
2021-11-26 20:36:40 +01:00
) -> Result<HashMap<Box<UserId>, PresenceEvent>> {
self.db.presence_since(room_id, since)
2020-07-28 15:58:50 +02:00
}
}