logo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//! `PUT /_matrix/client/*/rooms/{roomId}/typing/{userId}`

pub mod v3 {
    //! `/v3/` ([spec])
    //!
    //! [spec]: https://spec.matrix.org/v1.2/client-server-api/#put_matrixclientv3roomsroomidtypinguserid

    use std::time::Duration;

    use ruma_common::{api::ruma_api, RoomId, UserId};
    use serde::{de::Error, Deserialize, Deserializer, Serialize};

    ruma_api! {
        metadata: {
            method: PUT,
            r0_path: "/_matrix/client/r0/rooms/:room_id/typing/:user_id",
            stable_path: "/_matrix/client/v3/rooms/:room_id/typing/:user_id",
            name: "create_typing_event",
            description: "Send a typing event to a room.",
            authentication: AccessToken,
            rate_limited: true,
            added: 1.0,
        }

        request: {
            /// The user who has started to type.
            #[ruma_api(path)]
            pub user_id: &'a UserId,

            /// The room in which the user is typing.
            #[ruma_api(path)]
            pub room_id: &'a RoomId,

            /// Whether the user is typing within a length of time or not.
            #[serde(flatten)]
            pub state: Typing,
        }

        #[derive(Default)]
        response: {}

        error: crate::Error
    }

    impl<'a> Request<'a> {
        /// Creates a new `Request` with the given user ID, room ID and typing state.
        pub fn new(user_id: &'a UserId, room_id: &'a RoomId, state: Typing) -> Self {
            Self { user_id, room_id, state }
        }
    }

    impl Response {
        /// Creates an empty `Response`.
        pub fn new() -> Self {
            Self {}
        }
    }

    /// A mark for whether the user is typing within a length of time or not.
    #[derive(Clone, Copy, Debug, Serialize)]
    #[serde(into = "TypingInner")]
    #[allow(clippy::exhaustive_enums)]
    pub enum Typing {
        /// Not typing.
        No,

        /// Typing during the specified length of time.
        Yes(Duration),
    }

    #[derive(Deserialize, Serialize)]
    struct TypingInner {
        typing: bool,

        #[serde(
            with = "ruma_common::serde::duration::opt_ms",
            default,
            skip_serializing_if = "Option::is_none"
        )]
        timeout: Option<Duration>,
    }

    impl From<Typing> for TypingInner {
        fn from(typing: Typing) -> Self {
            match typing {
                Typing::No => Self { typing: false, timeout: None },
                Typing::Yes(time) => Self { typing: true, timeout: Some(time) },
            }
        }
    }

    impl<'de> Deserialize<'de> for Typing {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: Deserializer<'de>,
        {
            let inner = TypingInner::deserialize(deserializer)?;

            match (inner.typing, inner.timeout) {
                (false, _) => Ok(Self::No),
                (true, Some(time)) => Ok(Self::Yes(time)),
                _ => Err(D::Error::missing_field("timeout")),
            }
        }
    }
}