2024-07-16 08:05:25 +00:00
|
|
|
use axum::extract::State;
|
2025-04-04 03:30:13 +00:00
|
|
|
use conduwuit::{Err, Result, utils, utils::math::Tried};
|
2024-12-15 02:53:32 -05:00
|
|
|
use ruma::api::client::typing::create_typing_event;
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2025-04-04 03:30:13 +00:00
|
|
|
use crate::Ruma;
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2021-08-31 19:14:37 +02:00
|
|
|
/// # `PUT /_matrix/client/r0/rooms/{roomId}/typing/{userId}`
|
|
|
|
///
|
|
|
|
/// Sets the typing state of the sender user.
|
2024-04-22 23:48:57 -04:00
|
|
|
pub(crate) async fn create_typing_event_route(
|
2024-12-15 00:05:47 -05:00
|
|
|
State(services): State<crate::State>,
|
|
|
|
body: Ruma<create_typing_event::v3::Request>,
|
2022-02-18 15:33:14 +01:00
|
|
|
) -> Result<create_typing_event::v3::Response> {
|
|
|
|
use create_typing_event::v3::Typing;
|
2024-12-15 02:53:32 -05:00
|
|
|
let sender_user = body.sender_user();
|
2022-02-18 15:33:14 +01:00
|
|
|
|
2024-12-15 02:53:32 -05:00
|
|
|
if sender_user != body.user_id && body.appservice_info.is_none() {
|
|
|
|
return Err!(Request(Forbidden("You cannot update typing status of other users.")));
|
|
|
|
}
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2024-07-16 08:05:25 +00:00
|
|
|
if !services
|
2024-03-25 17:05:11 -04:00
|
|
|
.rooms
|
|
|
|
.state_cache
|
2024-08-08 17:18:30 +00:00
|
|
|
.is_joined(sender_user, &body.room_id)
|
|
|
|
.await
|
2024-03-25 17:05:11 -04:00
|
|
|
{
|
2024-12-15 02:53:32 -05:00
|
|
|
return Err!(Request(Forbidden("You are not in this room.")));
|
2022-03-23 11:05:41 +01:00
|
|
|
}
|
2025-06-28 22:53:25 +01:00
|
|
|
if !services.users.is_suspended(sender_user).await? {
|
|
|
|
match body.state {
|
|
|
|
| Typing::Yes(duration) => {
|
|
|
|
let duration = utils::clamp(
|
|
|
|
duration.as_millis().try_into().unwrap_or(u64::MAX),
|
|
|
|
services
|
|
|
|
.server
|
|
|
|
.config
|
|
|
|
.typing_client_timeout_min_s
|
|
|
|
.try_mul(1000)?,
|
|
|
|
services
|
|
|
|
.server
|
|
|
|
.config
|
|
|
|
.typing_client_timeout_max_s
|
|
|
|
.try_mul(1000)?,
|
|
|
|
);
|
2025-02-23 01:17:45 -05:00
|
|
|
services
|
2025-06-28 22:53:25 +01:00
|
|
|
.rooms
|
|
|
|
.typing
|
|
|
|
.typing_add(
|
|
|
|
sender_user,
|
|
|
|
&body.room_id,
|
|
|
|
utils::millis_since_unix_epoch()
|
|
|
|
.checked_add(duration)
|
|
|
|
.expect("user typing timeout should not get this high"),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
},
|
|
|
|
| _ => {
|
2025-02-23 01:17:45 -05:00
|
|
|
services
|
2025-06-28 22:53:25 +01:00
|
|
|
.rooms
|
|
|
|
.typing
|
|
|
|
.typing_remove(sender_user, &body.room_id)
|
|
|
|
.await?;
|
|
|
|
},
|
|
|
|
}
|
2020-07-30 18:14:47 +02:00
|
|
|
}
|
|
|
|
|
2024-12-15 02:53:32 -05:00
|
|
|
// ping presence
|
2025-03-31 02:28:01 +00:00
|
|
|
if services.config.allow_local_presence {
|
2024-12-15 02:53:32 -05:00
|
|
|
services
|
|
|
|
.presence
|
|
|
|
.ping_presence(&body.user_id, &ruma::presence::PresenceState::Online)
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
|
2022-02-18 15:33:14 +01:00
|
|
|
Ok(create_typing_event::v3::Response {})
|
2020-07-30 18:14:47 +02:00
|
|
|
}
|