2025-10-05 19:48:07 +00:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
|
|
|
|
|
use futures::{FutureExt, StreamExt, TryFutureExt};
|
|
|
|
|
use ruma::{
|
2025-10-06 01:12:01 +00:00
|
|
|
api::client::sync::sync_events::v5::response,
|
|
|
|
|
events::typing::{SyncTypingEvent, TypingEventContent},
|
|
|
|
|
serde::Raw,
|
2025-10-05 19:48:07 +00:00
|
|
|
};
|
|
|
|
|
use tuwunel_core::{
|
|
|
|
|
Result, debug_error,
|
|
|
|
|
utils::{IterStream, ReadyExt},
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-07 21:35:42 +00:00
|
|
|
use super::{Connection, SyncInfo, Window, extension_rooms_selector};
|
2025-10-05 19:48:07 +00:00
|
|
|
|
2025-10-22 07:05:03 +00:00
|
|
|
#[tracing::instrument(name = "typing", level = "trace", skip_all)]
|
2025-10-05 19:48:07 +00:00
|
|
|
pub(super) async fn collect(
|
|
|
|
|
sync_info: SyncInfo<'_>,
|
2025-10-07 21:35:42 +00:00
|
|
|
conn: &Connection,
|
|
|
|
|
window: &Window,
|
2025-10-05 19:48:07 +00:00
|
|
|
) -> Result<response::Typing> {
|
|
|
|
|
use response::Typing;
|
|
|
|
|
|
2025-10-07 21:35:42 +00:00
|
|
|
let SyncInfo { services, sender_user, .. } = sync_info;
|
2025-10-05 19:48:07 +00:00
|
|
|
|
2025-10-07 21:35:42 +00:00
|
|
|
let implicit = conn
|
2025-10-06 01:12:01 +00:00
|
|
|
.extensions
|
|
|
|
|
.typing
|
|
|
|
|
.lists
|
|
|
|
|
.as_deref()
|
|
|
|
|
.map(<[_]>::iter);
|
|
|
|
|
|
2025-10-07 21:35:42 +00:00
|
|
|
let explicit = conn
|
2025-10-06 01:12:01 +00:00
|
|
|
.extensions
|
|
|
|
|
.typing
|
|
|
|
|
.rooms
|
|
|
|
|
.as_deref()
|
|
|
|
|
.map(<[_]>::iter);
|
|
|
|
|
|
2025-10-07 21:35:42 +00:00
|
|
|
extension_rooms_selector(sync_info, conn, window, implicit, explicit)
|
2025-10-06 01:12:01 +00:00
|
|
|
.stream()
|
|
|
|
|
.filter_map(async |room_id| {
|
|
|
|
|
services
|
|
|
|
|
.typing
|
|
|
|
|
.typing_users_for_user(room_id, sender_user)
|
|
|
|
|
.inspect_err(|e| debug_error!(%room_id, "Failed to get typing events: {e}"))
|
|
|
|
|
.await
|
|
|
|
|
.ok()
|
|
|
|
|
.filter(|users| !users.is_empty())
|
|
|
|
|
.map(|users| (room_id, users))
|
|
|
|
|
})
|
|
|
|
|
.ready_filter_map(|(room_id, users)| {
|
|
|
|
|
let content = TypingEventContent::new(users);
|
|
|
|
|
let event = SyncTypingEvent { content };
|
|
|
|
|
let event = Raw::new(&event);
|
|
|
|
|
|
|
|
|
|
Some((room_id.to_owned(), event.ok()?))
|
|
|
|
|
})
|
|
|
|
|
.collect::<BTreeMap<_, _>>()
|
|
|
|
|
.map(|rooms| Typing { rooms })
|
|
|
|
|
.map(Ok)
|
|
|
|
|
.await
|
2025-10-05 19:48:07 +00:00
|
|
|
}
|