Skip to content
Snippets Groups Projects
typing.rs 1.15 KiB
Newer Older
  • Learn to ignore specific revisions
  • Timo Kösters's avatar
    Timo Kösters committed
    use crate::{services, utils, Error, Result, Ruma};
    
    Timo Kösters's avatar
    Timo Kösters committed
    use ruma::api::client::{error::ErrorKind, typing::create_typing_event};
    
    /// # `PUT /_matrix/client/r0/rooms/{roomId}/typing/{userId}`
    ///
    /// Sets the typing state of the sender user.
    
    Jonas Platte's avatar
    Jonas Platte committed
    pub async fn create_typing_event_route(
    
    Jonas Platte's avatar
    Jonas Platte committed
        body: Ruma<create_typing_event::v3::Request>,
    
    Jonathan de Jong's avatar
    Jonathan de Jong committed
    ) -> Result<create_typing_event::v3::Response> {
        use create_typing_event::v3::Typing;
    
    
        let sender_user = body.sender_user.as_ref().expect("user is authenticated");
    
    Timo Kösters's avatar
    Timo Kösters committed
        if !services()
            .rooms
            .state_cache
            .is_joined(sender_user, &body.room_id)?
        {
    
            return Err(Error::BadRequest(
                ErrorKind::Forbidden,
                "You are not in this room.",
            ));
        }
    
    
    Timo Kösters's avatar
    Timo Kösters committed
        if let Typing::Yes(duration) = body.state {
    
    Timo Kösters's avatar
    Timo Kösters committed
            services().rooms.edus.typing.typing_add(
    
                &body.room_id,
    
    Timo Kösters's avatar
    Timo Kösters committed
                duration.as_millis() as u64 + utils::millis_since_unix_epoch(),
    
    Timo Kösters's avatar
    Timo Kösters committed
            services()
                .rooms
                .edus
                .typing
    
                .typing_remove(sender_user, &body.room_id)?;
    
    Jonathan de Jong's avatar
    Jonathan de Jong committed
        Ok(create_typing_event::v3::Response {})