2020-06-04 09:17:36 +00:00
|
|
|
use crate::{utils, Error, Result};
|
2020-06-05 16:19:26 +00:00
|
|
|
use ruma::{
|
2020-07-28 13:58:50 +00:00
|
|
|
events::{
|
|
|
|
presence::{PresenceEvent, PresenceEventContent},
|
|
|
|
AnyEvent as EduEvent, SyncEphemeralRoomEvent,
|
|
|
|
},
|
|
|
|
presence::PresenceState,
|
2020-12-04 23:16:17 +00:00
|
|
|
serde::Raw,
|
2021-03-04 11:29:08 +00:00
|
|
|
RoomId, UInt, UserId,
|
2020-06-05 16:19:26 +00:00
|
|
|
};
|
2020-07-28 13:58:50 +00:00
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
convert::{TryFrom, TryInto},
|
2020-10-06 18:43:35 +00:00
|
|
|
mem,
|
2020-07-28 13:58:50 +00:00
|
|
|
};
|
2020-05-03 15:25:31 +00:00
|
|
|
|
2020-09-15 14:13:54 +00:00
|
|
|
#[derive(Clone)]
|
2020-05-03 15:25:31 +00:00
|
|
|
pub struct RoomEdus {
|
2020-08-23 15:29:39 +00:00
|
|
|
pub(in super::super) readreceiptid_readreceipt: sled::Tree, // ReadReceiptId = RoomId + Count + UserId
|
|
|
|
pub(in super::super) roomuserid_privateread: sled::Tree, // RoomUserId = Room + User, PrivateRead = Count
|
|
|
|
pub(in super::super) roomuserid_lastprivatereadupdate: sled::Tree, // LastPrivateReadUpdate = Count
|
|
|
|
pub(in super::super) typingid_userid: sled::Tree, // TypingId = RoomId + TimeoutTime + Count
|
|
|
|
pub(in super::super) roomid_lasttypingupdate: sled::Tree, // LastRoomTypingUpdate = Count
|
2020-07-28 13:58:50 +00:00
|
|
|
pub(in super::super) presenceid_presence: sled::Tree, // PresenceId = RoomId + Count + UserId
|
|
|
|
pub(in super::super) userid_lastpresenceupdate: sled::Tree, // LastPresenceUpdate = Count
|
2020-05-03 15:25:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RoomEdus {
|
|
|
|
/// Adds an event which will be saved until a new event replaces it (e.g. read receipt).
|
2020-08-23 15:29:39 +00:00
|
|
|
pub fn readreceipt_update(
|
2020-05-03 15:25:31 +00:00
|
|
|
&self,
|
|
|
|
user_id: &UserId,
|
|
|
|
room_id: &RoomId,
|
|
|
|
event: EduEvent,
|
|
|
|
globals: &super::super::globals::Globals,
|
|
|
|
) -> Result<()> {
|
2021-04-05 19:44:21 +00:00
|
|
|
let mut prefix = room_id.as_bytes().to_vec();
|
2020-05-03 15:25:31 +00:00
|
|
|
prefix.push(0xff);
|
|
|
|
|
|
|
|
// Remove old entry
|
|
|
|
if let Some(old) = self
|
2020-08-23 15:29:39 +00:00
|
|
|
.readreceiptid_readreceipt
|
2020-05-03 15:25:31 +00:00
|
|
|
.scan_prefix(&prefix)
|
|
|
|
.keys()
|
|
|
|
.rev()
|
|
|
|
.filter_map(|r| r.ok())
|
|
|
|
.take_while(|key| key.starts_with(&prefix))
|
|
|
|
.find(|key| {
|
2020-06-09 13:13:17 +00:00
|
|
|
key.rsplit(|&b| b == 0xff)
|
|
|
|
.next()
|
|
|
|
.expect("rsplit always returns an element")
|
2021-04-05 19:44:21 +00:00
|
|
|
== user_id.as_bytes()
|
2020-05-03 15:25:31 +00:00
|
|
|
})
|
|
|
|
{
|
|
|
|
// This is the old room_latest
|
2020-08-23 15:29:39 +00:00
|
|
|
self.readreceiptid_readreceipt.remove(old)?;
|
2020-05-03 15:25:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut room_latest_id = prefix;
|
|
|
|
room_latest_id.extend_from_slice(&globals.next_count()?.to_be_bytes());
|
|
|
|
room_latest_id.push(0xff);
|
2021-04-05 19:44:21 +00:00
|
|
|
room_latest_id.extend_from_slice(&user_id.as_bytes());
|
2020-05-03 15:25:31 +00:00
|
|
|
|
2020-08-23 15:29:39 +00:00
|
|
|
self.readreceiptid_readreceipt.insert(
|
2020-06-09 13:13:17 +00:00
|
|
|
room_latest_id,
|
|
|
|
&*serde_json::to_string(&event).expect("EduEvent::to_string always works"),
|
|
|
|
)?;
|
2020-05-03 15:25:31 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an iterator over the most recent read_receipts in a room that happened after the event with id `since`.
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(self))]
|
2020-08-23 15:29:39 +00:00
|
|
|
pub fn readreceipts_since(
|
2020-05-03 15:25:31 +00:00
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
|
|
|
since: u64,
|
2020-07-26 13:41:28 +00:00
|
|
|
) -> Result<impl Iterator<Item = Result<Raw<ruma::events::AnySyncEphemeralRoomEvent>>>> {
|
2021-04-05 19:44:21 +00:00
|
|
|
let mut prefix = room_id.as_bytes().to_vec();
|
2020-05-03 15:25:31 +00:00
|
|
|
prefix.push(0xff);
|
|
|
|
|
|
|
|
let mut first_possible_edu = prefix.clone();
|
2020-06-04 20:36:48 +00:00
|
|
|
first_possible_edu.extend_from_slice(&(since + 1).to_be_bytes()); // +1 so we don't send the event at since
|
2020-05-03 15:25:31 +00:00
|
|
|
|
|
|
|
Ok(self
|
2020-08-23 15:29:39 +00:00
|
|
|
.readreceiptid_readreceipt
|
2020-05-03 15:25:31 +00:00
|
|
|
.range(&*first_possible_edu..)
|
|
|
|
.filter_map(|r| r.ok())
|
|
|
|
.take_while(move |(k, _)| k.starts_with(&prefix))
|
2020-06-09 13:13:17 +00:00
|
|
|
.map(|(_, v)| {
|
2020-06-11 08:03:08 +00:00
|
|
|
Ok(serde_json::from_slice(&v).map_err(|_| {
|
|
|
|
Error::bad_database("Read receipt in roomlatestid_roomlatest is invalid.")
|
|
|
|
})?)
|
2020-06-09 13:13:17 +00:00
|
|
|
}))
|
2020-05-03 15:25:31 +00:00
|
|
|
}
|
|
|
|
|
2020-08-23 15:29:39 +00:00
|
|
|
/// Sets a private read marker at `count`.
|
2020-08-25 11:24:38 +00:00
|
|
|
pub fn private_read_set(
|
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
|
|
|
user_id: &UserId,
|
|
|
|
count: u64,
|
|
|
|
globals: &super::super::globals::Globals,
|
|
|
|
) -> Result<()> {
|
2021-04-05 19:44:21 +00:00
|
|
|
let mut key = room_id.as_bytes().to_vec();
|
2020-08-23 15:29:39 +00:00
|
|
|
key.push(0xff);
|
2021-04-05 19:44:21 +00:00
|
|
|
key.extend_from_slice(&user_id.as_bytes());
|
2020-08-23 15:29:39 +00:00
|
|
|
|
|
|
|
self.roomuserid_privateread
|
|
|
|
.insert(&key, &count.to_be_bytes())?;
|
|
|
|
|
|
|
|
self.roomuserid_lastprivatereadupdate
|
|
|
|
.insert(&key, &globals.next_count()?.to_be_bytes())?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the private read marker.
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(self))]
|
2020-08-23 15:29:39 +00:00
|
|
|
pub fn private_read_get(&self, room_id: &RoomId, user_id: &UserId) -> Result<Option<u64>> {
|
2021-04-05 19:44:21 +00:00
|
|
|
let mut key = room_id.as_bytes().to_vec();
|
2020-08-23 15:29:39 +00:00
|
|
|
key.push(0xff);
|
2021-04-05 19:44:21 +00:00
|
|
|
key.extend_from_slice(&user_id.as_bytes());
|
2020-08-23 15:29:39 +00:00
|
|
|
|
|
|
|
self.roomuserid_privateread.get(key)?.map_or(Ok(None), |v| {
|
|
|
|
Ok(Some(utils::u64_from_bytes(&v).map_err(|_| {
|
|
|
|
Error::bad_database("Invalid private read marker bytes")
|
|
|
|
})?))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the count of the last typing update in this room.
|
|
|
|
pub fn last_privateread_update(&self, user_id: &UserId, room_id: &RoomId) -> Result<u64> {
|
2021-04-05 19:44:21 +00:00
|
|
|
let mut key = room_id.as_bytes().to_vec();
|
2020-08-23 15:29:39 +00:00
|
|
|
key.push(0xff);
|
2021-04-05 19:44:21 +00:00
|
|
|
key.extend_from_slice(&user_id.as_bytes());
|
2020-08-23 15:29:39 +00:00
|
|
|
|
|
|
|
Ok(self
|
|
|
|
.roomuserid_lastprivatereadupdate
|
|
|
|
.get(&key)?
|
|
|
|
.map_or(Ok::<_, Error>(None), |bytes| {
|
|
|
|
Ok(Some(utils::u64_from_bytes(&bytes).map_err(|_| {
|
|
|
|
Error::bad_database("Count in roomuserid_lastprivatereadupdate is invalid.")
|
|
|
|
})?))
|
|
|
|
})?
|
|
|
|
.unwrap_or(0))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets a user as typing until the timeout timestamp is reached or roomtyping_remove is
|
2020-06-04 09:17:36 +00:00
|
|
|
/// called.
|
2020-08-23 15:29:39 +00:00
|
|
|
pub fn typing_add(
|
2020-05-03 15:25:31 +00:00
|
|
|
&self,
|
2020-06-04 09:17:36 +00:00
|
|
|
user_id: &UserId,
|
2020-05-03 15:25:31 +00:00
|
|
|
room_id: &RoomId,
|
|
|
|
timeout: u64,
|
|
|
|
globals: &super::super::globals::Globals,
|
|
|
|
) -> Result<()> {
|
2021-04-05 19:44:21 +00:00
|
|
|
let mut prefix = room_id.as_bytes().to_vec();
|
2020-05-03 15:25:31 +00:00
|
|
|
prefix.push(0xff);
|
|
|
|
|
2020-06-04 09:17:36 +00:00
|
|
|
let count = globals.next_count()?.to_be_bytes();
|
|
|
|
|
2020-08-23 15:29:39 +00:00
|
|
|
let mut room_typing_id = prefix;
|
|
|
|
room_typing_id.extend_from_slice(&timeout.to_be_bytes());
|
|
|
|
room_typing_id.push(0xff);
|
|
|
|
room_typing_id.extend_from_slice(&count);
|
2020-06-04 09:17:36 +00:00
|
|
|
|
2020-08-23 15:29:39 +00:00
|
|
|
self.typingid_userid
|
2021-04-05 19:44:21 +00:00
|
|
|
.insert(&room_typing_id, &*user_id.as_bytes())?;
|
2020-06-04 09:17:36 +00:00
|
|
|
|
2020-08-23 15:29:39 +00:00
|
|
|
self.roomid_lasttypingupdate
|
2021-04-05 19:44:21 +00:00
|
|
|
.insert(&room_id.as_bytes(), &count)?;
|
2020-06-04 09:17:36 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Removes a user from typing before the timeout is reached.
|
2020-08-23 15:29:39 +00:00
|
|
|
pub fn typing_remove(
|
2020-06-04 09:17:36 +00:00
|
|
|
&self,
|
|
|
|
user_id: &UserId,
|
|
|
|
room_id: &RoomId,
|
|
|
|
globals: &super::super::globals::Globals,
|
|
|
|
) -> Result<()> {
|
2021-04-05 19:44:21 +00:00
|
|
|
let mut prefix = room_id.as_bytes().to_vec();
|
2020-06-04 09:17:36 +00:00
|
|
|
prefix.push(0xff);
|
|
|
|
|
|
|
|
let user_id = user_id.to_string();
|
|
|
|
|
|
|
|
let mut found_outdated = false;
|
|
|
|
|
2020-08-23 15:29:39 +00:00
|
|
|
// Maybe there are multiple ones from calling roomtyping_add multiple times
|
2020-05-03 15:25:31 +00:00
|
|
|
for outdated_edu in self
|
2020-08-23 15:29:39 +00:00
|
|
|
.typingid_userid
|
2020-05-03 15:25:31 +00:00
|
|
|
.scan_prefix(&prefix)
|
|
|
|
.filter_map(|r| r.ok())
|
2020-06-04 09:17:36 +00:00
|
|
|
.filter(|(_, v)| v == user_id.as_bytes())
|
2020-05-03 15:25:31 +00:00
|
|
|
{
|
2020-08-23 15:29:39 +00:00
|
|
|
self.typingid_userid.remove(outdated_edu.0)?;
|
2020-06-04 09:17:36 +00:00
|
|
|
found_outdated = true;
|
2020-05-03 15:25:31 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 09:17:36 +00:00
|
|
|
if found_outdated {
|
2021-04-05 19:44:21 +00:00
|
|
|
self.roomid_lasttypingupdate
|
|
|
|
.insert(&room_id.as_bytes(), &globals.next_count()?.to_be_bytes())?;
|
2020-06-04 09:17:36 +00:00
|
|
|
}
|
2020-05-03 15:25:31 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-06-04 09:17:36 +00:00
|
|
|
/// Makes sure that typing events with old timestamps get removed.
|
2020-08-23 15:29:39 +00:00
|
|
|
fn typings_maintain(
|
2020-06-04 09:17:36 +00:00
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
|
|
|
globals: &super::super::globals::Globals,
|
|
|
|
) -> Result<()> {
|
2021-04-05 19:44:21 +00:00
|
|
|
let mut prefix = room_id.as_bytes().to_vec();
|
2020-05-03 15:25:31 +00:00
|
|
|
prefix.push(0xff);
|
|
|
|
|
2020-06-04 09:17:36 +00:00
|
|
|
let current_timestamp = utils::millis_since_unix_epoch();
|
2020-05-03 15:25:31 +00:00
|
|
|
|
2020-06-04 09:17:36 +00:00
|
|
|
let mut found_outdated = false;
|
|
|
|
|
|
|
|
// Find all outdated edus before inserting a new one
|
2020-05-03 15:25:31 +00:00
|
|
|
for outdated_edu in self
|
2020-08-23 15:29:39 +00:00
|
|
|
.typingid_userid
|
2020-05-03 15:25:31 +00:00
|
|
|
.scan_prefix(&prefix)
|
2020-06-04 09:17:36 +00:00
|
|
|
.keys()
|
2020-06-09 13:13:17 +00:00
|
|
|
.map(|key| {
|
|
|
|
let key = key?;
|
|
|
|
Ok::<_, Error>((
|
|
|
|
key.clone(),
|
2020-10-06 18:43:35 +00:00
|
|
|
utils::u64_from_bytes(
|
|
|
|
&key.splitn(2, |&b| b == 0xff).nth(1).ok_or_else(|| {
|
|
|
|
Error::bad_database("RoomTyping has invalid timestamp or delimiters.")
|
|
|
|
})?[0..mem::size_of::<u64>()],
|
|
|
|
)
|
2020-08-23 15:29:39 +00:00
|
|
|
.map_err(|_| Error::bad_database("RoomTyping has invalid timestamp bytes."))?,
|
2020-06-09 13:13:17 +00:00
|
|
|
))
|
2020-06-04 09:17:36 +00:00
|
|
|
})
|
2020-06-09 13:13:17 +00:00
|
|
|
.filter_map(|r| r.ok())
|
|
|
|
.take_while(|&(_, timestamp)| timestamp < current_timestamp)
|
2020-05-03 15:25:31 +00:00
|
|
|
{
|
2020-06-04 09:17:36 +00:00
|
|
|
// This is an outdated edu (time > timestamp)
|
2020-08-23 15:29:39 +00:00
|
|
|
self.typingid_userid.remove(outdated_edu.0)?;
|
2020-06-04 09:17:36 +00:00
|
|
|
found_outdated = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if found_outdated {
|
2021-04-05 19:44:21 +00:00
|
|
|
self.roomid_lasttypingupdate
|
|
|
|
.insert(&room_id.as_bytes(), &globals.next_count()?.to_be_bytes())?;
|
2020-05-03 15:25:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-08-23 15:29:39 +00:00
|
|
|
/// Returns the count of the last typing update in this room.
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(self, globals))]
|
2020-08-23 15:29:39 +00:00
|
|
|
pub fn last_typing_update(
|
2020-05-03 15:25:31 +00:00
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
2020-06-04 09:17:36 +00:00
|
|
|
globals: &super::super::globals::Globals,
|
|
|
|
) -> Result<u64> {
|
2020-08-23 15:29:39 +00:00
|
|
|
self.typings_maintain(room_id, globals)?;
|
2020-06-04 09:17:36 +00:00
|
|
|
|
|
|
|
Ok(self
|
2020-08-23 15:29:39 +00:00
|
|
|
.roomid_lasttypingupdate
|
2021-04-05 19:44:21 +00:00
|
|
|
.get(&room_id.as_bytes())?
|
2020-06-09 13:13:17 +00:00
|
|
|
.map_or(Ok::<_, Error>(None), |bytes| {
|
2020-06-11 08:03:08 +00:00
|
|
|
Ok(Some(utils::u64_from_bytes(&bytes).map_err(|_| {
|
|
|
|
Error::bad_database("Count in roomid_lastroomactiveupdate is invalid.")
|
|
|
|
})?))
|
2020-06-09 13:13:17 +00:00
|
|
|
})?
|
2020-06-04 09:17:36 +00:00
|
|
|
.unwrap_or(0))
|
|
|
|
}
|
|
|
|
|
2020-08-23 15:29:39 +00:00
|
|
|
pub fn typings_all(
|
2020-07-21 18:04:39 +00:00
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
|
|
|
) -> Result<SyncEphemeralRoomEvent<ruma::events::typing::TypingEventContent>> {
|
2021-04-05 19:44:21 +00:00
|
|
|
let mut prefix = room_id.as_bytes().to_vec();
|
2020-05-03 15:25:31 +00:00
|
|
|
prefix.push(0xff);
|
|
|
|
|
2020-06-04 09:17:36 +00:00
|
|
|
let mut user_ids = Vec::new();
|
2020-05-03 15:25:31 +00:00
|
|
|
|
2020-06-04 09:17:36 +00:00
|
|
|
for user_id in self
|
2020-08-23 15:29:39 +00:00
|
|
|
.typingid_userid
|
2020-06-04 09:17:36 +00:00
|
|
|
.scan_prefix(prefix)
|
|
|
|
.values()
|
2020-06-09 13:13:17 +00:00
|
|
|
.map(|user_id| {
|
2020-06-11 08:03:08 +00:00
|
|
|
Ok::<_, Error>(
|
|
|
|
UserId::try_from(utils::string_from_bytes(&user_id?).map_err(|_| {
|
2020-08-23 15:29:39 +00:00
|
|
|
Error::bad_database("User ID in typingid_userid is invalid unicode.")
|
2020-06-11 08:03:08 +00:00
|
|
|
})?)
|
2020-08-23 15:29:39 +00:00
|
|
|
.map_err(|_| Error::bad_database("User ID in typingid_userid is invalid."))?,
|
2020-06-11 08:03:08 +00:00
|
|
|
)
|
2020-06-09 13:13:17 +00:00
|
|
|
})
|
2020-06-04 09:17:36 +00:00
|
|
|
{
|
|
|
|
user_ids.push(user_id?);
|
|
|
|
}
|
|
|
|
|
2020-07-21 18:04:39 +00:00
|
|
|
Ok(SyncEphemeralRoomEvent {
|
2020-06-05 16:19:26 +00:00
|
|
|
content: ruma::events::typing::TypingEventContent { user_ids },
|
2020-06-04 09:17:36 +00:00
|
|
|
})
|
2020-05-03 15:25:31 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 13:58:50 +00: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: ruma::events::presence::PresenceEvent,
|
|
|
|
globals: &super::super::globals::Globals,
|
|
|
|
) -> Result<()> {
|
|
|
|
// TODO: Remove old entry? Or maybe just wipe completely from time to time?
|
|
|
|
|
|
|
|
let count = globals.next_count()?.to_be_bytes();
|
|
|
|
|
2021-04-05 19:44:21 +00:00
|
|
|
let mut presence_id = room_id.as_bytes().to_vec();
|
2020-07-28 13:58:50 +00:00
|
|
|
presence_id.push(0xff);
|
|
|
|
presence_id.extend_from_slice(&count);
|
|
|
|
presence_id.push(0xff);
|
2021-04-05 19:44:21 +00:00
|
|
|
presence_id.extend_from_slice(&presence.sender.as_bytes());
|
2020-07-28 13:58:50 +00:00
|
|
|
|
|
|
|
self.presenceid_presence.insert(
|
|
|
|
presence_id,
|
|
|
|
&*serde_json::to_string(&presence).expect("PresenceEvent can be serialized"),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
self.userid_lastpresenceupdate.insert(
|
2021-04-05 19:44:21 +00:00
|
|
|
&user_id.as_bytes(),
|
2020-07-28 13:58:50 +00:00
|
|
|
&utils::millis_since_unix_epoch().to_be_bytes(),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Resets the presence timeout, so the user will stay in their current presence state.
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(self))]
|
2020-07-28 13:58:50 +00:00
|
|
|
pub fn ping_presence(&self, user_id: &UserId) -> Result<()> {
|
|
|
|
self.userid_lastpresenceupdate.insert(
|
2021-04-05 19:44:21 +00:00
|
|
|
&user_id.as_bytes(),
|
2020-07-28 13:58:50 +00:00
|
|
|
&utils::millis_since_unix_epoch().to_be_bytes(),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the timestamp of the last presence update of this user in millis since the unix epoch.
|
|
|
|
pub fn last_presence_update(&self, user_id: &UserId) -> Result<Option<u64>> {
|
|
|
|
self.userid_lastpresenceupdate
|
2021-04-05 19:44:21 +00:00
|
|
|
.get(&user_id.as_bytes())?
|
2020-07-28 13:58:50 +00:00
|
|
|
.map(|bytes| {
|
|
|
|
utils::u64_from_bytes(&bytes).map_err(|_| {
|
|
|
|
Error::bad_database("Invalid timestamp in userid_lastpresenceupdate.")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.transpose()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets all users to offline who have been quiet for too long.
|
|
|
|
pub fn presence_maintain(
|
|
|
|
&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(|r| r.ok())
|
|
|
|
.filter_map(|(k, bytes)| {
|
|
|
|
Some((
|
|
|
|
k,
|
|
|
|
utils::u64_from_bytes(&bytes)
|
|
|
|
.map_err(|_| {
|
|
|
|
Error::bad_database("Invalid timestamp in userid_lastpresenceupdate.")
|
|
|
|
})
|
|
|
|
.ok()?,
|
|
|
|
))
|
|
|
|
})
|
2020-07-29 15:37:26 +00:00
|
|
|
.take_while(|(_, timestamp)| current_timestamp - timestamp > 5 * 60_000)
|
|
|
|
// 5 Minutes
|
2020-07-28 13:58:50 +00:00
|
|
|
{
|
|
|
|
// Send new presence events to set the user offline
|
|
|
|
let count = globals.next_count()?.to_be_bytes();
|
|
|
|
let user_id = utils::string_from_bytes(&user_id_bytes)
|
|
|
|
.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()) {
|
2021-04-05 19:44:21 +00:00
|
|
|
let mut presence_id = room_id.as_bytes().to_vec();
|
2020-07-28 13:58:50 +00: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(
|
|
|
|
presence_id,
|
|
|
|
&*serde_json::to_string(&PresenceEvent {
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
sender: user_id.clone(),
|
|
|
|
})
|
|
|
|
.expect("PresenceEvent can be serialized"),
|
|
|
|
)?;
|
|
|
|
}
|
2021-01-10 16:12:16 +00:00
|
|
|
|
|
|
|
self.userid_lastpresenceupdate.insert(
|
2021-04-05 19:44:21 +00:00
|
|
|
&user_id.as_bytes(),
|
2021-01-10 16:12:16 +00:00
|
|
|
&utils::millis_since_unix_epoch().to_be_bytes(),
|
|
|
|
)?;
|
2020-07-28 13:58:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an iterator over the most recent presence updates that happened after the event with id `since`.
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(self, globals, rooms))]
|
2020-07-28 13:58:50 +00:00
|
|
|
pub fn presence_since(
|
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
|
|
|
since: u64,
|
|
|
|
rooms: &super::Rooms,
|
|
|
|
globals: &super::super::globals::Globals,
|
|
|
|
) -> Result<HashMap<UserId, PresenceEvent>> {
|
|
|
|
self.presence_maintain(rooms, globals)?;
|
|
|
|
|
2021-04-05 19:44:21 +00:00
|
|
|
let mut prefix = room_id.as_bytes().to_vec();
|
2020-07-28 13:58:50 +00:00
|
|
|
prefix.push(0xff);
|
|
|
|
|
|
|
|
let mut first_possible_edu = prefix.clone();
|
|
|
|
first_possible_edu.extend_from_slice(&(since + 1).to_be_bytes()); // +1 so we don't send the event at since
|
|
|
|
let mut hashmap = HashMap::new();
|
|
|
|
|
|
|
|
for (key, value) in self
|
|
|
|
.presenceid_presence
|
|
|
|
.range(&*first_possible_edu..)
|
|
|
|
.filter_map(|r| r.ok())
|
|
|
|
.take_while(|(key, _)| key.starts_with(&prefix))
|
|
|
|
{
|
|
|
|
let user_id = UserId::try_from(
|
|
|
|
utils::string_from_bytes(
|
|
|
|
key.rsplit(|&b| b == 0xff)
|
|
|
|
.next()
|
|
|
|
.expect("rsplit always returns an element"),
|
|
|
|
)
|
|
|
|
.map_err(|_| Error::bad_database("Invalid UserId bytes in presenceid_presence."))?,
|
|
|
|
)
|
|
|
|
.map_err(|_| Error::bad_database("Invalid UserId in presenceid_presence."))?;
|
|
|
|
|
|
|
|
let mut presence = serde_json::from_slice::<PresenceEvent>(&value)
|
|
|
|
.map_err(|_| Error::bad_database("Invalid presence event in db."))?;
|
|
|
|
|
|
|
|
let current_timestamp: UInt = utils::millis_since_unix_epoch()
|
|
|
|
.try_into()
|
|
|
|
.expect("time is valid");
|
|
|
|
|
|
|
|
if presence.content.presence == PresenceState::Online {
|
|
|
|
// Don't set last_active_ago when the user is online
|
|
|
|
presence.content.last_active_ago = None;
|
|
|
|
} else {
|
|
|
|
// Convert from timestamp to duration
|
|
|
|
presence.content.last_active_ago = presence
|
|
|
|
.content
|
|
|
|
.last_active_ago
|
|
|
|
.map(|timestamp| current_timestamp - timestamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
hashmap.insert(user_id, presence);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(hashmap)
|
|
|
|
}
|
2020-05-03 15:25:31 +00:00
|
|
|
}
|