2022-04-07 17:09:07 +02:00
|
|
|
use crate::{database::DatabaseGuard, utils, Error, Result, Ruma};
|
|
|
|
use ruma::api::client::{error::ErrorKind, typing::create_typing_event};
|
2020-07-30 18:14:47 +02: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.
|
2022-01-20 11:51:31 +01:00
|
|
|
pub async fn create_typing_event_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2022-04-06 21:31:29 +02:00
|
|
|
body: Ruma<create_typing_event::v3::IncomingRequest>,
|
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
|
|
|
|
2022-03-23 11:05:41 +01:00
|
|
|
if !db.rooms.is_joined(sender_user, &body.room_id)? {
|
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::Forbidden,
|
|
|
|
"You are not in this room.",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2020-09-08 17:32:03 +02:00
|
|
|
if let Typing::Yes(duration) = body.state {
|
2020-08-23 17:29:39 +02:00
|
|
|
db.rooms.edus.typing_add(
|
2021-09-13 19:45:56 +02:00
|
|
|
sender_user,
|
2020-07-30 18:14:47 +02:00
|
|
|
&body.room_id,
|
2020-09-08 17:32:03 +02:00
|
|
|
duration.as_millis() as u64 + utils::millis_since_unix_epoch(),
|
2020-07-30 18:14:47 +02:00
|
|
|
&db.globals,
|
|
|
|
)?;
|
|
|
|
} else {
|
|
|
|
db.rooms
|
|
|
|
.edus
|
2021-09-13 19:45:56 +02:00
|
|
|
.typing_remove(sender_user, &body.room_id, &db.globals)?;
|
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
|
|
|
}
|