2024-05-27 03:17:20 +00:00
|
|
|
use std::{collections::BTreeMap, sync::Arc};
|
2024-03-08 10:05:48 -05:00
|
|
|
|
2024-09-28 21:44:38 -04:00
|
|
|
use futures::StreamExt;
|
2024-03-29 18:35:02 -07:00
|
|
|
use ruma::{
|
2025-02-23 01:17:45 -05:00
|
|
|
OwnedRoomId, OwnedUserId, RoomId, UserId,
|
2024-12-18 11:26:18 -05:00
|
|
|
api::federation::transactions::edu::{Edu, TypingContent},
|
|
|
|
|
events::SyncEphemeralRoomEvent,
|
2024-03-29 18:35:02 -07:00
|
|
|
};
|
2025-02-23 01:17:45 -05:00
|
|
|
use tokio::sync::{RwLock, broadcast};
|
2025-04-22 01:41:02 +00:00
|
|
|
use tuwunel_core::{
|
|
|
|
|
Result, Server, debug_info, trace,
|
|
|
|
|
utils::{self, IterStream},
|
|
|
|
|
};
|
2020-05-03 17:25:31 +02:00
|
|
|
|
2025-02-23 01:17:45 -05:00
|
|
|
use crate::{Dep, globals, sending, sending::EduBuf, users};
|
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-09-28 21:44:38 -04:00
|
|
|
users: Dep<users::Service>,
|
2024-07-18 06:37:47 +00:00
|
|
|
}
|
|
|
|
|
|
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-09-28 21:44:38 -04:00
|
|
|
users: args.depend::<users::Service>("users"),
|
2024-07-18 06:37:47 +00:00
|
|
|
},
|
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.
|
2025-07-08 12:08:13 +00: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
|
|
|
|
2025-04-22 04:42:26 +00:00
|
|
|
if self
|
|
|
|
|
.typing_update_sender
|
|
|
|
|
.send(room_id.to_owned())
|
|
|
|
|
.is_err()
|
|
|
|
|
{
|
2024-05-24 19:20:19 -04:00
|
|
|
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) {
|
2025-04-22 04:42:26 +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.
|
2025-07-08 12:08:13 +00: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
|
|
|
|
2025-04-22 04:42:26 +00:00
|
|
|
if self
|
|
|
|
|
.typing_update_sender
|
|
|
|
|
.send(room_id.to_owned())
|
|
|
|
|
.is_err()
|
|
|
|
|
{
|
2024-05-24 19:20:19 -04:00
|
|
|
trace!("receiver found what it was looking for and is no longer interested");
|
|
|
|
|
}
|
2024-12-18 11:26:18 -05:00
|
|
|
|
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) {
|
2025-04-22 04:42:26 +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.
|
2025-07-08 12:08:13 +00:00
|
|
|
async fn typings_maintain(&self, room_id: &RoomId) -> Result {
|
2024-03-08 10:05:48 -05:00
|
|
|
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-12-18 11:26:04 -05:00
|
|
|
if !removable.is_empty() {
|
|
|
|
|
let typing = &mut self.typing.write().await;
|
|
|
|
|
let room = typing.entry(room_id.to_owned()).or_default();
|
|
|
|
|
for user in &removable {
|
|
|
|
|
debug_info!("typing timeout {user:?} in {room_id:?}");
|
|
|
|
|
room.remove(user);
|
|
|
|
|
}
|
2024-08-08 17:18:30 +00:00
|
|
|
|
2024-12-18 11:26:04 -05:00
|
|
|
// update clients
|
|
|
|
|
self.last_typing_update
|
|
|
|
|
.write()
|
|
|
|
|
.await
|
|
|
|
|
.insert(room_id.to_owned(), self.services.globals.next_count()?);
|
2024-08-08 17:18:30 +00:00
|
|
|
|
2025-04-22 04:42:26 +00:00
|
|
|
if self
|
|
|
|
|
.typing_update_sender
|
|
|
|
|
.send(room_id.to_owned())
|
|
|
|
|
.is_err()
|
|
|
|
|
{
|
2024-12-18 11:26:04 -05:00
|
|
|
trace!("receiver found what it was looking for and is no longer interested");
|
|
|
|
|
}
|
2024-03-29 18:35:02 -07:00
|
|
|
|
2024-12-18 11:26:04 -05:00
|
|
|
// update federation
|
|
|
|
|
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(
|
2024-12-15 00:05:47 -05:00
|
|
|
&self,
|
|
|
|
|
room_id: &RoomId,
|
|
|
|
|
sender_user: &UserId,
|
2024-12-18 11:26:18 -05:00
|
|
|
) -> Result<SyncEphemeralRoomEvent<ruma::events::typing::TypingEventContent>> {
|
2024-09-28 21:44:38 -04:00
|
|
|
let room_typing_indicators = self.typing.read().await.get(room_id).cloned();
|
|
|
|
|
|
|
|
|
|
let Some(typing_indicators) = room_typing_indicators else {
|
2024-12-18 11:26:04 -05:00
|
|
|
return Ok(SyncEphemeralRoomEvent {
|
2024-12-18 11:26:18 -05:00
|
|
|
content: ruma::events::typing::TypingEventContent { user_ids: Vec::new() },
|
2024-12-18 11:26:04 -05:00
|
|
|
});
|
2024-09-28 21:44:38 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let user_ids: Vec<_> = typing_indicators
|
|
|
|
|
.into_keys()
|
|
|
|
|
.stream()
|
2025-07-08 11:08:58 +00:00
|
|
|
.filter_map(async |typing_user_id| {
|
|
|
|
|
self.services
|
2024-09-28 21:44:38 -04:00
|
|
|
.users
|
|
|
|
|
.user_is_ignored(&typing_user_id, sender_user)
|
2025-07-08 11:08:58 +00:00
|
|
|
.await
|
|
|
|
|
.eq(&false)
|
2024-09-28 21:44:38 -04:00
|
|
|
.then_some(typing_user_id)
|
|
|
|
|
})
|
|
|
|
|
.collect()
|
|
|
|
|
.await;
|
|
|
|
|
|
2024-12-18 11:26:18 -05:00
|
|
|
Ok(SyncEphemeralRoomEvent {
|
|
|
|
|
content: ruma::events::typing::TypingEventContent { user_ids },
|
|
|
|
|
})
|
2020-05-03 17:25:31 +02:00
|
|
|
}
|
2024-03-29 18:35:02 -07:00
|
|
|
|
2025-07-08 12:08:13 +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(());
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-26 21:47:52 +00:00
|
|
|
let content = TypingContent::new(room_id.to_owned(), user_id.to_owned(), typing);
|
|
|
|
|
let edu = Edu::Typing(content);
|
2024-03-29 18:35:02 -07:00
|
|
|
|
2025-01-26 21:47:52 +00:00
|
|
|
let mut buf = EduBuf::new();
|
|
|
|
|
serde_json::to_writer(&mut buf, &edu).expect("Serialized Edu::Typing");
|
|
|
|
|
|
2025-04-22 04:42:26 +00:00
|
|
|
self.services
|
|
|
|
|
.sending
|
|
|
|
|
.send_edu_room(room_id, buf)
|
|
|
|
|
.await?;
|
2024-12-18 11:26:04 -05:00
|
|
|
|
|
|
|
|
Ok(())
|
2024-03-29 18:35:02 -07:00
|
|
|
}
|
2020-05-03 17:25:31 +02:00
|
|
|
}
|