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
|
|
|
|
2024-03-05 19:48:54 -05:00
|
|
|
use crate::{services, utils, Error, Result, Ruma};
|
|
|
|
|
2021-08-31 19:14:37 +02:00
|
|
|
/// # `PUT /_matrix/client/r0/rooms/{roomId}/typing/{userId}`
|
|
|
|
///
|
|
|
|
/// Sets the typing state of the sender user.
|
2022-01-20 11:51:31 +01:00
|
|
|
pub async fn create_typing_event_route(
|
2024-03-05 19:48:54 -05:00
|
|
|
body: Ruma<create_typing_event::v3::Request>,
|
2022-02-18 15:33:14 +01:00
|
|
|
) -> Result<create_typing_event::v3::Response> {
|
2024-03-05 19:48:54 -05:00
|
|
|
use create_typing_event::v3::Typing;
|
2022-02-18 15:33:14 +01:00
|
|
|
|
2024-03-05 19:48:54 -05:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2024-03-05 19:48:54 -05:00
|
|
|
if !services().rooms.state_cache.is_joined(sender_user, &body.room_id)? {
|
|
|
|
return Err(Error::BadRequest(ErrorKind::Forbidden, "You are not in this room."));
|
|
|
|
}
|
2022-03-23 11:05:41 +01:00
|
|
|
|
2024-03-05 19:48:54 -05:00
|
|
|
if let Typing::Yes(duration) = body.state {
|
|
|
|
services().rooms.edus.typing.typing_add(
|
|
|
|
sender_user,
|
|
|
|
&body.room_id,
|
|
|
|
duration.as_millis() as u64 + utils::millis_since_unix_epoch(),
|
|
|
|
)?;
|
|
|
|
} else {
|
|
|
|
services().rooms.edus.typing.typing_remove(sender_user, &body.room_id)?;
|
|
|
|
}
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2024-03-05 19:48:54 -05:00
|
|
|
Ok(create_typing_event::v3::Response {})
|
2020-07-30 18:14:47 +02:00
|
|
|
}
|