2022-04-07 17:09:07 +02:00
|
|
|
use ruma::api::client::{error::ErrorKind, typing::create_typing_event};
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2022-10-05 20:34:31 +02:00
|
|
|
use crate::{services, utils, Error, Result, 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(
|
2022-12-14 13:09:10 +01:00
|
|
|
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;
|
|
|
|
|
2020-10-18 20:33:12 +02:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2024-03-25 17:05:11 -04:00
|
|
|
if !services()
|
|
|
|
.rooms
|
|
|
|
.state_cache
|
|
|
|
.is_joined(sender_user, &body.room_id)?
|
|
|
|
{
|
2024-04-03 15:59:03 -04:00
|
|
|
return Err(Error::BadRequest(ErrorKind::forbidden(), "You are not in this room."));
|
2022-03-23 11:05:41 +01:00
|
|
|
}
|
|
|
|
|
2020-09-08 17:32:03 +02:00
|
|
|
if let Typing::Yes(duration) = body.state {
|
2024-03-29 18:35:02 -07:00
|
|
|
let duration = utils::clamp(
|
2024-05-03 21:42:47 -04:00
|
|
|
duration.as_millis().try_into().unwrap_or(u64::MAX),
|
|
|
|
services()
|
|
|
|
.globals
|
|
|
|
.config
|
|
|
|
.typing_client_timeout_min_s
|
|
|
|
.checked_mul(1000)
|
|
|
|
.unwrap(),
|
|
|
|
services()
|
|
|
|
.globals
|
|
|
|
.config
|
|
|
|
.typing_client_timeout_max_s
|
|
|
|
.checked_mul(1000)
|
|
|
|
.unwrap(),
|
2024-03-29 18:35:02 -07:00
|
|
|
);
|
2024-03-08 10:05:48 -05:00
|
|
|
services()
|
|
|
|
.rooms
|
|
|
|
.typing
|
2024-05-03 21:42:47 -04:00
|
|
|
.typing_add(
|
|
|
|
sender_user,
|
|
|
|
&body.room_id,
|
|
|
|
utils::millis_since_unix_epoch()
|
|
|
|
.checked_add(duration)
|
|
|
|
.expect("user typing timeout should not get this high"),
|
|
|
|
)
|
2024-03-08 10:05:48 -05:00
|
|
|
.await?;
|
2020-07-30 18:14:47 +02:00
|
|
|
} else {
|
2024-03-25 17:05:11 -04:00
|
|
|
services()
|
|
|
|
.rooms
|
|
|
|
.typing
|
|
|
|
.typing_remove(sender_user, &body.room_id)
|
|
|
|
.await?;
|
2020-07-30 18:14:47 +02:00
|
|
|
}
|
|
|
|
|
2022-02-18 15:33:14 +01:00
|
|
|
Ok(create_typing_event::v3::Response {})
|
2020-07-30 18:14:47 +02:00
|
|
|
}
|