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
// Copyright (c) Ankit Chaubey <ankitchaubey.dev@gmail.com>
// SPDX-License-Identifier: MIT OR Apache-2.0
//
// ferogram: async Telegram MTProto client in Rust
// https://github.com/ankit-chaubey/ferogram
//
// If you use or modify this code, keep this notice at the top of your file
// and include the LICENSE-MIT or LICENSE-APACHE file from this repository:
// https://github.com/ankit-chaubey/ferogram
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_ex(client, peer, action, None, Duration::from_secs(4)).await
}
/// Like [`start`](Self::start) but also accepts a forum **topic id**
/// (`top_msg_id`) and a custom **repeat delay**.
///
/// # Arguments
/// * `topic_id` : `Some(msg_id)` for a forum topic thread; `None` for
/// the main chat.
/// * `repeat_delay`: How often to re-send the action to keep it alive.
/// Telegram drops the indicator after ~5 s; ≤ 4 s is
/// recommended.
pub async fn start_ex(
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_ex(
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
}
}