conduwuit/src/service/rooms/edus/typing/mod.rs

50 lines
1.5 KiB
Rust
Raw Normal View History

mod data;
2022-10-08 13:04:55 +02:00
pub use data::Data;
2022-10-05 20:34:31 +02:00
use ruma::{events::SyncEphemeralRoomEvent, 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-08 13:02:52 +02:00
pub db: &'static dyn Data,
}
2022-10-05 12:45:54 +02:00
impl Service {
/// Sets a user as typing until the timeout timestamp is reached or roomtyping_remove is
/// called.
pub fn typing_add(&self, user_id: &UserId, room_id: &RoomId, timeout: u64) -> Result<()> {
self.db.typing_add(user_id, room_id, timeout)
}
/// Removes a user from typing before the timeout is reached.
pub fn typing_remove(&self, user_id: &UserId, room_id: &RoomId) -> Result<()> {
self.db.typing_remove(user_id, room_id)
}
/// 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)
}
/// Returns the count of the last typing update in this room.
pub fn last_typing_update(&self, room_id: &RoomId) -> Result<u64> {
2022-10-30 21:22:32 +01:00
self.typings_maintain(room_id)?;
self.db.last_typing_update(room_id)
}
/// Returns a new typing EDU.
pub fn typings_all(
&self,
room_id: &RoomId,
) -> Result<SyncEphemeralRoomEvent<ruma::events::typing::TypingEventContent>> {
let user_ids = self.db.typings_all(room_id)?;
Ok(SyncEphemeralRoomEvent {
content: ruma::events::typing::TypingEventContent {
user_ids: user_ids.into_iter().collect(),
},
})
}
}