2022-06-25 16:12:23 +02:00
|
|
|
mod data;
|
|
|
|
|
pub use data::Data;
|
2022-09-07 13:25:51 +02:00
|
|
|
use ruma::{UserId, RoomId, events::SyncEphemeralRoomEvent};
|
2020-05-03 17:25:31 +02:00
|
|
|
|
2022-09-07 13:25:51 +02:00
|
|
|
use crate::Result;
|
2022-06-25 16:12:23 +02:00
|
|
|
|
2022-10-05 12:45:54 +02:00
|
|
|
pub struct Service {
|
|
|
|
|
db: Box<dyn Data>,
|
2020-05-03 17:25:31 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-05 12:45:54 +02:00
|
|
|
impl Service {
|
2020-08-23 17:29:39 +02:00
|
|
|
/// Sets a user as typing until the timeout timestamp is reached or roomtyping_remove is
|
2020-06-04 11:17:36 +02:00
|
|
|
/// called.
|
2022-07-10 14:37:34 +02:00
|
|
|
pub fn typing_add(&self, user_id: &UserId, room_id: &RoomId, timeout: u64) -> Result<()> {
|
2022-06-25 16:12:23 +02:00
|
|
|
self.db.typing_add(user_id, room_id, timeout)
|
2020-06-04 11:17:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Removes a user from typing before the timeout is reached.
|
2022-07-10 14:37:34 +02:00
|
|
|
pub fn typing_remove(&self, user_id: &UserId, room_id: &RoomId) -> Result<()> {
|
2022-06-25 16:12:23 +02:00
|
|
|
self.db.typing_remove(user_id, room_id)
|
2020-05-03 17:25:31 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-25 16:12:23 +02:00
|
|
|
/* TODO: Do this in background thread?
|
2020-06-04 11:17:36 +02:00
|
|
|
/// Makes sure that typing events with old timestamps get removed.
|
2020-08-23 17:29:39 +02:00
|
|
|
fn typings_maintain(
|
2020-06-04 11:17:36 +02:00
|
|
|
&self,
|
|
|
|
|
room_id: &RoomId,
|
|
|
|
|
globals: &super::super::globals::Globals,
|
|
|
|
|
) -> Result<()> {
|
2021-04-05 21:44:21 +02:00
|
|
|
let mut prefix = room_id.as_bytes().to_vec();
|
2020-05-03 17:25:31 +02:00
|
|
|
prefix.push(0xff);
|
|
|
|
|
|
2020-06-04 11:17:36 +02:00
|
|
|
let current_timestamp = utils::millis_since_unix_epoch();
|
2020-05-03 17:25:31 +02:00
|
|
|
|
2020-06-04 11:17:36 +02:00
|
|
|
let mut found_outdated = false;
|
|
|
|
|
|
|
|
|
|
// Find all outdated edus before inserting a new one
|
2020-05-03 17:25:31 +02:00
|
|
|
for outdated_edu in self
|
2020-08-23 17:29:39 +02:00
|
|
|
.typingid_userid
|
2021-06-08 18:10:00 +02:00
|
|
|
.scan_prefix(prefix)
|
|
|
|
|
.map(|(key, _)| {
|
2020-06-09 15:13:17 +02:00
|
|
|
Ok::<_, Error>((
|
|
|
|
|
key.clone(),
|
2020-10-06 20:43:35 +02: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 17:29:39 +02:00
|
|
|
.map_err(|_| Error::bad_database("RoomTyping has invalid timestamp bytes."))?,
|
2020-06-09 15:13:17 +02:00
|
|
|
))
|
2020-06-04 11:17:36 +02:00
|
|
|
})
|
2020-06-09 15:13:17 +02:00
|
|
|
.filter_map(|r| r.ok())
|
|
|
|
|
.take_while(|&(_, timestamp)| timestamp < current_timestamp)
|
2020-05-03 17:25:31 +02:00
|
|
|
{
|
2020-06-04 11:17:36 +02:00
|
|
|
// This is an outdated edu (time > timestamp)
|
2021-06-08 18:10:00 +02:00
|
|
|
self.typingid_userid.remove(&outdated_edu.0)?;
|
2020-06-04 11:17:36 +02:00
|
|
|
found_outdated = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if found_outdated {
|
2021-04-05 21:44:21 +02:00
|
|
|
self.roomid_lasttypingupdate
|
2021-09-13 19:45:56 +02:00
|
|
|
.insert(room_id.as_bytes(), &globals.next_count()?.to_be_bytes())?;
|
2020-05-03 17:25:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2022-06-25 16:12:23 +02:00
|
|
|
*/
|
2020-05-03 17:25:31 +02:00
|
|
|
|
2020-08-23 17:29:39 +02:00
|
|
|
/// Returns the count of the last typing update in this room.
|
2022-07-10 14:37:34 +02:00
|
|
|
pub fn last_typing_update(&self, room_id: &RoomId) -> Result<u64> {
|
2022-06-25 16:12:23 +02:00
|
|
|
self.db.last_typing_update(room_id)
|
2020-06-04 11:17:36 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-25 16:12:23 +02:00
|
|
|
/// Returns a new typing EDU.
|
2020-08-23 17:29:39 +02:00
|
|
|
pub fn typings_all(
|
2020-07-21 14:04:39 -04:00
|
|
|
&self,
|
|
|
|
|
room_id: &RoomId,
|
|
|
|
|
) -> Result<SyncEphemeralRoomEvent<ruma::events::typing::TypingEventContent>> {
|
2022-06-25 16:12:23 +02:00
|
|
|
let user_ids = self.db.typings_all(room_id)?;
|
2020-06-04 11:17:36 +02:00
|
|
|
|
2020-07-21 14:04:39 -04:00
|
|
|
Ok(SyncEphemeralRoomEvent {
|
2021-04-22 11:27:01 +02:00
|
|
|
content: ruma::events::typing::TypingEventContent {
|
|
|
|
|
user_ids: user_ids.into_iter().collect(),
|
|
|
|
|
},
|
2020-06-04 11:17:36 +02:00
|
|
|
})
|
2020-05-03 17:25:31 +02:00
|
|
|
}
|
|
|
|
|
}
|