2024-05-27 03:17:20 +00:00
|
|
|
use std::{collections::BTreeMap, sync::Arc};
|
2024-03-08 10:05:48 -05:00
|
|
|
|
2024-07-18 06:37:47 +00:00
|
|
|
use conduit::{debug_info, trace, utils, Result, Server};
|
2024-03-29 18:35:02 -07:00
|
|
|
use ruma::{
|
|
|
|
|
api::federation::transactions::edu::{Edu, TypingContent},
|
|
|
|
|
events::SyncEphemeralRoomEvent,
|
|
|
|
|
OwnedRoomId, OwnedUserId, RoomId, UserId,
|
|
|
|
|
};
|
2024-03-22 19:47:56 -04:00
|
|
|
use tokio::sync::{broadcast, RwLock};
|
2020-05-03 17:25:31 +02:00
|
|
|
|
2024-07-22 07:43:51 +00:00
|
|
|
use crate::{globals, sending, Dep};
|
2022-06-25 16:12:23 +02:00
|
|
|
|
2024-05-09 15:59:08 -07:00
|
|
|
pub struct Service {
|
2024-07-18 06:37:47 +00:00
|
|
|
server: Arc<Server>,
|
|
|
|
|
services: Services,
|
|
|
|
|
/// u64 is unix timestamp of timeout
|
|
|
|
|
pub typing: RwLock<BTreeMap<OwnedRoomId, BTreeMap<OwnedUserId, u64>>>,
|
|
|
|
|
/// timestamp of the last change to typing users
|
|
|
|
|
pub last_typing_update: RwLock<BTreeMap<OwnedRoomId, u64>>,
|
2024-05-09 15:59:08 -07:00
|
|
|
pub typing_update_sender: broadcast::Sender<OwnedRoomId>,
|
2020-05-03 17:25:31 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-18 06:37:47 +00:00
|
|
|
struct Services {
|
|
|
|
|
globals: Dep<globals::Service>,
|
|
|
|
|
sending: Dep<sending::Service>,
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-04 03:26:19 +00:00
|
|
|
impl crate::Service for Service {
|
2024-07-18 06:37:47 +00:00
|
|
|
fn build(args: crate::Args<'_>) -> Result<Arc<Self>> {
|
2024-07-04 03:26:19 +00:00
|
|
|
Ok(Arc::new(Self {
|
2024-07-18 06:37:47 +00:00
|
|
|
server: args.server.clone(),
|
|
|
|
|
services: Services {
|
|
|
|
|
globals: args.depend::<globals::Service>("globals"),
|
|
|
|
|
sending: args.depend::<sending::Service>("sending"),
|
|
|
|
|
},
|
2024-05-27 03:17:20 +00:00
|
|
|
typing: RwLock::new(BTreeMap::new()),
|
|
|
|
|
last_typing_update: RwLock::new(BTreeMap::new()),
|
|
|
|
|
typing_update_sender: broadcast::channel(100).0,
|
2024-07-04 03:26:19 +00:00
|
|
|
}))
|
2024-05-27 03:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-04 03:26:19 +00:00
|
|
|
fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 called.
|
2024-05-09 15:59:08 -07:00
|
|
|
pub async fn typing_add(&self, user_id: &UserId, room_id: &RoomId, timeout: u64) -> Result<()> {
|
2024-08-08 17:18:30 +00:00
|
|
|
debug_info!("typing started {user_id:?} in {room_id:?} timeout:{timeout:?}");
|
2024-03-29 18:35:02 -07:00
|
|
|
// update clients
|
2024-03-25 17:05:11 -04:00
|
|
|
self.typing
|
|
|
|
|
.write()
|
|
|
|
|
.await
|
|
|
|
|
.entry(room_id.to_owned())
|
|
|
|
|
.or_default()
|
|
|
|
|
.insert(user_id.to_owned(), timeout);
|
2024-08-08 17:18:30 +00:00
|
|
|
|
2024-03-25 17:05:11 -04:00
|
|
|
self.last_typing_update
|
|
|
|
|
.write()
|
|
|
|
|
.await
|
2024-07-18 06:37:47 +00:00
|
|
|
.insert(room_id.to_owned(), self.services.globals.next_count()?);
|
2024-08-08 17:18:30 +00:00
|
|
|
|
2024-05-24 19:20:19 -04:00
|
|
|
if self.typing_update_sender.send(room_id.to_owned()).is_err() {
|
|
|
|
|
trace!("receiver found what it was looking for and is no longer interested");
|
|
|
|
|
}
|
2024-03-29 18:35:02 -07:00
|
|
|
|
|
|
|
|
// update federation
|
2024-07-22 07:43:51 +00:00
|
|
|
if self.services.globals.user_is_local(user_id) {
|
2024-08-08 17:18:30 +00:00
|
|
|
self.federation_send(room_id, user_id, true).await?;
|
2024-03-29 18:35:02 -07:00
|
|
|
}
|
|
|
|
|
|
2024-03-08 10:05:48 -05:00
|
|
|
Ok(())
|
2020-06-04 11:17:36 +02:00
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2020-06-04 11:17:36 +02:00
|
|
|
/// Removes a user from typing before the timeout is reached.
|
2024-05-09 15:59:08 -07:00
|
|
|
pub async fn typing_remove(&self, user_id: &UserId, room_id: &RoomId) -> Result<()> {
|
2024-08-08 17:18:30 +00:00
|
|
|
debug_info!("typing stopped {user_id:?} in {room_id:?}");
|
2024-03-29 18:35:02 -07:00
|
|
|
// update clients
|
2024-03-25 17:05:11 -04:00
|
|
|
self.typing
|
|
|
|
|
.write()
|
|
|
|
|
.await
|
|
|
|
|
.entry(room_id.to_owned())
|
|
|
|
|
.or_default()
|
|
|
|
|
.remove(user_id);
|
2024-08-08 17:18:30 +00:00
|
|
|
|
2024-03-25 17:05:11 -04:00
|
|
|
self.last_typing_update
|
|
|
|
|
.write()
|
|
|
|
|
.await
|
2024-07-18 06:37:47 +00:00
|
|
|
.insert(room_id.to_owned(), self.services.globals.next_count()?);
|
2024-08-08 17:18:30 +00:00
|
|
|
|
2024-05-24 19:20:19 -04:00
|
|
|
if self.typing_update_sender.send(room_id.to_owned()).is_err() {
|
|
|
|
|
trace!("receiver found what it was looking for and is no longer interested");
|
|
|
|
|
}
|
2024-03-29 18:35:02 -07:00
|
|
|
|
|
|
|
|
// update federation
|
2024-07-22 07:43:51 +00:00
|
|
|
if self.services.globals.user_is_local(user_id) {
|
2024-08-08 17:18:30 +00:00
|
|
|
self.federation_send(room_id, user_id, false).await?;
|
2024-03-29 18:35:02 -07:00
|
|
|
}
|
|
|
|
|
|
2024-03-22 19:47:56 -04:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
pub async fn wait_for_update(&self, room_id: &RoomId) {
|
2024-03-22 19:47:56 -04:00
|
|
|
let mut receiver = self.typing_update_sender.subscribe();
|
|
|
|
|
while let Ok(next) = receiver.recv().await {
|
|
|
|
|
if next == room_id {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-03 17:25:31 +02:00
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2020-06-04 11:17:36 +02:00
|
|
|
/// Makes sure that typing events with old timestamps get removed.
|
2024-03-08 10:05:48 -05:00
|
|
|
async fn typings_maintain(&self, room_id: &RoomId) -> Result<()> {
|
|
|
|
|
let current_timestamp = utils::millis_since_unix_epoch();
|
|
|
|
|
let mut removable = Vec::new();
|
2024-03-22 21:51:21 -04:00
|
|
|
|
2024-03-08 10:05:48 -05:00
|
|
|
{
|
|
|
|
|
let typing = self.typing.read().await;
|
|
|
|
|
let Some(room) = typing.get(room_id) else {
|
|
|
|
|
return Ok(());
|
|
|
|
|
};
|
2024-03-22 21:51:21 -04:00
|
|
|
|
2024-03-08 10:05:48 -05:00
|
|
|
for (user, timeout) in room {
|
|
|
|
|
if *timeout < current_timestamp {
|
|
|
|
|
removable.push(user.clone());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-03-08 10:05:48 -05:00
|
|
|
if !removable.is_empty() {
|
|
|
|
|
let typing = &mut self.typing.write().await;
|
|
|
|
|
let room = typing.entry(room_id.to_owned()).or_default();
|
2024-03-29 18:35:02 -07:00
|
|
|
for user in &removable {
|
2024-08-08 17:18:30 +00:00
|
|
|
debug_info!("typing timeout {user:?} in {room_id:?}");
|
2024-03-29 18:35:02 -07:00
|
|
|
room.remove(user);
|
2024-03-08 10:05:48 -05:00
|
|
|
}
|
2024-08-08 17:18:30 +00:00
|
|
|
|
2024-03-29 18:35:02 -07:00
|
|
|
// update clients
|
2024-03-25 17:05:11 -04:00
|
|
|
self.last_typing_update
|
|
|
|
|
.write()
|
|
|
|
|
.await
|
2024-07-18 06:37:47 +00:00
|
|
|
.insert(room_id.to_owned(), self.services.globals.next_count()?);
|
2024-08-08 17:18:30 +00:00
|
|
|
|
2024-05-24 19:20:19 -04:00
|
|
|
if self.typing_update_sender.send(room_id.to_owned()).is_err() {
|
|
|
|
|
trace!("receiver found what it was looking for and is no longer interested");
|
|
|
|
|
}
|
2024-03-29 18:35:02 -07:00
|
|
|
|
|
|
|
|
// update federation
|
2024-08-08 17:18:30 +00:00
|
|
|
for user in &removable {
|
|
|
|
|
if self.services.globals.user_is_local(user) {
|
|
|
|
|
self.federation_send(room_id, user, false).await?;
|
2024-03-29 18:35:02 -07:00
|
|
|
}
|
|
|
|
|
}
|
2024-03-08 10:05:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-03-08 10:05:48 -05:00
|
|
|
/// Returns the count of the last typing update in this room.
|
2024-05-09 15:59:08 -07:00
|
|
|
pub async fn last_typing_update(&self, room_id: &RoomId) -> Result<u64> {
|
2024-03-08 10:05:48 -05:00
|
|
|
self.typings_maintain(room_id).await?;
|
2024-03-25 17:05:11 -04:00
|
|
|
Ok(self
|
|
|
|
|
.last_typing_update
|
|
|
|
|
.read()
|
|
|
|
|
.await
|
|
|
|
|
.get(room_id)
|
|
|
|
|
.copied()
|
|
|
|
|
.unwrap_or(0))
|
2020-06-04 11:17:36 +02:00
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2022-06-25 16:12:23 +02:00
|
|
|
/// Returns a new typing EDU.
|
2024-05-09 15:59:08 -07:00
|
|
|
pub async fn typings_all(
|
2020-07-21 14:04:39 -04:00
|
|
|
&self, room_id: &RoomId,
|
|
|
|
|
) -> Result<SyncEphemeralRoomEvent<ruma::events::typing::TypingEventContent>> {
|
|
|
|
|
Ok(SyncEphemeralRoomEvent {
|
2021-04-22 11:27:01 +02:00
|
|
|
content: ruma::events::typing::TypingEventContent {
|
2024-03-08 10:05:48 -05:00
|
|
|
user_ids: self
|
|
|
|
|
.typing
|
|
|
|
|
.read()
|
|
|
|
|
.await
|
|
|
|
|
.get(room_id)
|
|
|
|
|
.map(|m| m.keys().cloned().collect())
|
|
|
|
|
.unwrap_or_default(),
|
2021-04-22 11:27:01 +02:00
|
|
|
},
|
2020-06-04 11:17:36 +02:00
|
|
|
})
|
2020-05-03 17:25:31 +02:00
|
|
|
}
|
2024-03-29 18:35:02 -07:00
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
async fn federation_send(&self, room_id: &RoomId, user_id: &UserId, typing: bool) -> Result<()> {
|
2024-07-22 07:43:51 +00:00
|
|
|
debug_assert!(
|
|
|
|
|
self.services.globals.user_is_local(user_id),
|
|
|
|
|
"tried to broadcast typing status of remote user",
|
|
|
|
|
);
|
|
|
|
|
|
2024-07-18 06:37:47 +00:00
|
|
|
if !self.server.config.allow_outgoing_typing {
|
2024-03-29 18:35:02 -07:00
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let edu = Edu::Typing(TypingContent::new(room_id.to_owned(), user_id.to_owned(), typing));
|
|
|
|
|
|
2024-07-18 06:37:47 +00:00
|
|
|
self.services
|
2024-03-29 18:35:02 -07:00
|
|
|
.sending
|
2024-08-08 17:18:30 +00:00
|
|
|
.send_edu_room(room_id, serde_json::to_vec(&edu).expect("Serialized Edu::Typing"))
|
|
|
|
|
.await?;
|
2024-03-29 18:35:02 -07:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2020-05-03 17:25:31 +02:00
|
|
|
}
|