2022-06-25 16:12:23 +02:00
|
|
|
mod data;
|
2022-10-08 13:04:55 +02:00
|
|
|
|
2022-06-25 16:12:23 +02:00
|
|
|
pub use data::Data;
|
2022-10-05 20:34:31 +02:00
|
|
|
use ruma::{events::SyncEphemeralRoomEvent, RoomId, UserId};
|
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 {
|
2022-10-08 13:02:52 +02:00
|
|
|
pub db: &'static 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
|
|
|
}
|
|
|
|
|
|
2020-06-04 11:17:36 +02:00
|
|
|
/// Makes sure that typing events with old timestamps get removed.
|
2022-10-30 21:23:10 +01:00
|
|
|
fn typings_maintain(&self, room_id: &RoomId) -> Result<()> {
|
2022-10-30 21:22:32 +01:00
|
|
|
self.db.typings_maintain(room_id)
|
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-10-30 21:22:32 +01:00
|
|
|
self.typings_maintain(room_id)?;
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|