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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright (c) Ankit Chaubey <ankitchaubey.dev@gmail.com>
//
// ferogram: async Telegram MTProto client in Rust
// https://github.com/ankit-chaubey/ferogram
//
// Licensed under either the MIT License or the Apache License 2.0.
// See the LICENSE-MIT or LICENSE-APACHE file in this repository:
// https://github.com/ankit-chaubey/ferogram
//
// Feel free to use, modify, and share this code.
// Please keep this notice when redistributing.
use crate::{Client, InvocationError, PeerRef};
use ferogram_tl_types as tl;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::Notify;
use tokio::task::JoinHandle;
// TypingGuard
/// Scoped typing indicator. Keeps the action alive by re-sending it every
/// ~4 seconds (Telegram drops the indicator after ~5 s).
///
/// Drop this guard to cancel the action immediately.
pub struct TypingGuard {
stop: Arc<Notify>,
task: Option<JoinHandle<()>>,
}
impl TypingGuard {
/// Send `action` to `peer` and keep repeating it until the guard is dropped.
pub async fn start(
client: &Client,
peer: impl Into<PeerRef>,
action: tl::enums::SendMessageAction,
) -> Result<Self, InvocationError> {
let peer = peer.into().resolve(client).await?;
Self::start_inner(client, peer, action, None, Duration::from_secs(4)).await
}
/// Send `action` to a **forum topic** thread in `peer` and keep it alive
/// until the guard is dropped.
///
/// `topic_id` is the `top_msg_id` of the forum topic thread.
pub async fn start_in_topic(
client: &Client,
peer: impl Into<PeerRef>,
action: tl::enums::SendMessageAction,
topic_id: i32,
) -> Result<Self, InvocationError> {
let peer = peer.into().resolve(client).await?;
Self::start_inner(client, peer, action, Some(topic_id), Duration::from_secs(4)).await
}
/// Internal helper shared by `start` and `start_in_topic`.
pub(crate) async fn start_inner(
client: &Client,
peer: tl::enums::Peer,
action: tl::enums::SendMessageAction,
topic_id: Option<i32>,
repeat_delay: Duration,
) -> Result<Self, InvocationError> {
// Send once immediately so the indicator appears without delay.
client
.send_chat_action_ex(peer.clone(), action.clone(), topic_id)
.await?;
let stop = Arc::new(Notify::new());
let stop2 = stop.clone();
let client = client.clone();
let task = tokio::spawn(async move {
loop {
tokio::select! {
_ = tokio::time::sleep(repeat_delay) => {
if let Err(e) = client.send_chat_action_ex(peer.clone(), action.clone(), topic_id).await {
tracing::warn!("[typing_guard] Failed to refresh typing action: {e}");
break;
}
}
_ = stop2.notified() => break,
}
}
// Cancel the action
let cancel = tl::enums::SendMessageAction::SendMessageCancelAction;
let _ = client
.send_chat_action_ex(peer.clone(), cancel, topic_id)
.await;
});
Ok(Self {
stop,
task: Some(task),
})
}
/// Cancel the typing indicator immediately without waiting for the drop.
pub fn cancel(&mut self) {
self.stop.notify_one();
}
}
impl Drop for TypingGuard {
fn drop(&mut self) {
self.stop.notify_one();
if let Some(t) = self.task.take() {
t.abort();
}
}
}
// Client extension
impl Client {
/// Start a scoped typing indicator that auto-cancels when dropped.
///
/// A convenience wrapper around [`TypingGuard::start`].
pub async fn typing(&self, peer: impl Into<PeerRef>) -> Result<TypingGuard, InvocationError> {
TypingGuard::start(
self,
peer,
tl::enums::SendMessageAction::SendMessageTypingAction,
)
.await
}
/// Start a scoped typing indicator in a **forum topic** thread.
///
/// `topic_id` is the `top_msg_id` of the forum topic.
pub async fn typing_in_topic(
&self,
peer: impl Into<PeerRef>,
topic_id: i32,
) -> Result<TypingGuard, InvocationError> {
let peer = peer.into().resolve(self).await?;
TypingGuard::start_inner(
self,
peer,
tl::enums::SendMessageAction::SendMessageTypingAction,
Some(topic_id),
std::time::Duration::from_secs(4),
)
.await
}
/// Start a scoped "uploading document" action that auto-cancels when dropped.
pub async fn uploading_document(
&self,
peer: impl Into<PeerRef>,
) -> Result<TypingGuard, InvocationError> {
TypingGuard::start(
self,
peer,
tl::enums::SendMessageAction::SendMessageUploadDocumentAction(
tl::types::SendMessageUploadDocumentAction { progress: 0 },
),
)
.await
}
/// Start a scoped "recording video" action that auto-cancels when dropped.
pub async fn recording_video(
&self,
peer: impl Into<PeerRef>,
) -> Result<TypingGuard, InvocationError> {
TypingGuard::start(
self,
peer,
tl::enums::SendMessageAction::SendMessageRecordVideoAction,
)
.await
}
/// Send a chat action with optional forum topic support (internal helper).
pub(crate) async fn send_chat_action_ex(
&self,
peer: tl::enums::Peer,
action: tl::enums::SendMessageAction,
topic_id: Option<i32>,
) -> Result<(), InvocationError> {
let input_peer = self.inner.peer_cache.read().await.peer_to_input(&peer)?;
let req = tl::functions::messages::SetTyping {
peer: input_peer,
top_msg_id: topic_id,
action,
};
self.rpc_write(&req).await
}
}