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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// 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.
#[allow(unused_imports)]
use super::{chat_to_peer, updates_entities};
use crate::*;
#[allow(unused_imports)]
use crate::{
InputMessage, InvocationError, PeerRef,
dialog::{Dialog, DialogIter, MessageIter},
inline_iter, media, participants, search, update,
};
#[allow(unused_imports)]
use ferogram_tl_types::{Cursor, Deserializable};
impl Client {
/// Resolve any peer reference to a [`tl::enums::Peer`].
///
/// Accepts everything [`PeerRef`] accepts:
///
/// - `&str` / `String`: `"@username"`, `"me"`, `"self"`, numeric string,
/// `t.me/` URL, invite link, E.164 phone
/// - `i64` / `i32`: Bot-API encoded numeric ID
/// - [`tl::enums::Peer`]: returned as-is (zero cost)
/// - [`tl::enums::InputPeer`]: hash cached, then stripped to `Peer`
///
/// Resolution is cache-first; an RPC is only made on a genuine cache miss.
pub async fn resolve<P: Into<PeerRef>>(
&self,
peer: P,
) -> Result<tl::enums::Peer, InvocationError> {
peer.into().resolve(self).await
}
/// `contacts.resolveUsername` RPC; called only on cache miss.
pub(crate) async fn resolve_username_rpc(
&self,
username: &str,
) -> Result<tl::enums::Peer, InvocationError> {
let req = tl::functions::contacts::ResolveUsername {
username: username.to_string(),
referer: None,
};
let body: Vec<u8> = self.rpc_call_raw(&req).await?;
let mut cur = Cursor::from_slice(&body);
let tl::enums::contacts::ResolvedPeer::ResolvedPeer(resolved) =
tl::enums::contacts::ResolvedPeer::deserialize(&mut cur)?;
self.cache_users_slice(&resolved.users).await;
self.cache_chats_slice(&resolved.chats).await;
Ok(resolved.peer)
}
/// RPC fallback for `PeerRef::Id` when the peer is not in the cache.
///
/// - `Peer::User` → `users.getUsers` with `access_hash = 0` (works for
/// contacts and recently-interacted users; may return `UserEmpty` for
/// strangers; falls back to `messages.getPeerDialogs` in that case).
/// - `Peer::Channel` → `channels.getChannels` with `access_hash = 0`.
/// This works only for public channels; returns `ChannelEmpty` otherwise.
/// - `Peer::Chat` → basic groups never need a hash; return immediately.
///
/// On success the resolved entity is inserted into the peer cache so
/// subsequent lookups are free.
pub(crate) async fn fetch_by_id_rpc(
&self,
peer: tl::enums::Peer,
) -> Result<tl::enums::Peer, InvocationError> {
match &peer {
tl::enums::Peer::Chat(_) => {
// Basic groups need no access_hash; always resolvable from ID.
Ok(peer)
}
tl::enums::Peer::User(u) => {
let req = tl::functions::users::GetUsers {
id: vec![tl::enums::InputUser::InputUser(tl::types::InputUser {
user_id: u.user_id,
access_hash: 0,
})],
};
let body: Vec<u8> = self.rpc_call_raw(&req).await?;
let mut cur = Cursor::from_slice(&body);
let users = Vec::<tl::enums::User>::deserialize(&mut cur)?;
// Filter out UserEmpty responses
let valid: Vec<_> = users
.into_iter()
.filter(|u| matches!(u, tl::enums::User::User(_)))
.collect();
if !valid.is_empty() {
self.cache_users_slice(&valid).await;
return Ok(peer);
}
// Fallback: messages.getPeerDialogs (finds peers you've interacted with)
let cache_read: tokio::sync::RwLockReadGuard<'_, crate::PeerCache> =
self.inner.peer_cache.read().await;
if cache_read.users.contains_key(&match &peer {
tl::enums::Peer::User(u) => u.user_id,
_ => unreachable!(),
}) {
drop(cache_read);
return Ok(peer);
}
drop(cache_read);
let uid = match &peer {
tl::enums::Peer::User(u) => u.user_id,
_ => unreachable!(),
};
let req2 = tl::functions::messages::GetPeerDialogs {
peers: vec![tl::enums::InputDialogPeer::InputDialogPeer(
tl::types::InputDialogPeer {
peer: tl::enums::InputPeer::User(tl::types::InputPeerUser {
user_id: uid,
access_hash: 0,
}),
},
)],
};
let body2 = self.rpc_call_raw(&req2).await;
match body2 {
Ok(b) => {
let mut cur2 = Cursor::from_slice(&b);
if let Ok(tl::enums::messages::PeerDialogs::PeerDialogs(pd)) =
tl::enums::messages::PeerDialogs::deserialize(&mut cur2)
{
self.cache_users_and_chats(&pd.users, &pd.chats).await;
}
Ok(peer)
}
Err(e) => Err(e),
}
}
tl::enums::Peer::Channel(c) => {
let req = tl::functions::channels::GetChannels {
id: vec![tl::enums::InputChannel::InputChannel(
tl::types::InputChannel {
channel_id: c.channel_id,
access_hash: 0,
},
)],
};
let body: Vec<u8> = self.rpc_call_raw(&req).await?;
let mut cur = Cursor::from_slice(&body);
let chats = tl::enums::messages::Chats::deserialize(&mut cur)?;
let chats_vec = match chats {
tl::enums::messages::Chats::Chats(c) => c.chats,
tl::enums::messages::Chats::Slice(c) => c.chats,
};
let non_empty: Vec<_> = chats_vec
.into_iter()
.filter(|ch| !matches!(ch, tl::enums::Chat::Empty(_)))
.collect();
if !non_empty.is_empty() {
self.cache_chats_slice(&non_empty).await;
return Ok(peer);
}
// Fallback: getPeerDialogs
let cid = c.channel_id;
let req2 = tl::functions::messages::GetPeerDialogs {
peers: vec![tl::enums::InputDialogPeer::InputDialogPeer(
tl::types::InputDialogPeer {
peer: tl::enums::InputPeer::Channel(tl::types::InputPeerChannel {
channel_id: cid,
access_hash: 0,
}),
},
)],
};
let body2 = self.rpc_call_raw(&req2).await;
match body2 {
Ok(b) => {
let mut cur2 = Cursor::from_slice(&b);
if let Ok(tl::enums::messages::PeerDialogs::PeerDialogs(pd)) =
tl::enums::messages::PeerDialogs::deserialize(&mut cur2)
{
self.cache_users_and_chats(&pd.users, &pd.chats).await;
}
Ok(peer)
}
Err(e) => Err(e),
}
}
}
}
/// `contacts.importContacts` RPC for phone-based resolution.
///
/// Imports the phone as a temporary contact, caches the returned user, and
/// returns the resolved Peer.
pub(crate) async fn resolve_phone_rpc(
&self,
phone: &str,
) -> Result<tl::enums::Peer, InvocationError> {
let req = tl::functions::contacts::ImportContacts {
contacts: vec![tl::enums::InputContact::InputPhoneContact(
tl::types::InputPhoneContact {
client_id: 0,
phone: phone.to_string(),
first_name: String::new(),
last_name: String::new(),
note: None,
},
)],
};
let body: Vec<u8> = self.rpc_call_raw(&req).await?;
let mut cur = Cursor::from_slice(&body);
let tl::enums::contacts::ImportedContacts::ImportedContacts(result) =
tl::enums::contacts::ImportedContacts::deserialize(&mut cur)?;
self.cache_users_slice(&result.users).await;
// Check if the phone is now in the cache reverse index
{
let cache: tokio::sync::RwLockReadGuard<'_, PeerCache> =
self.inner.peer_cache.read().await;
if let Some(&uid) = cache.phone_to_user.get(phone) {
return Ok(tl::enums::Peer::User(tl::types::PeerUser { user_id: uid }));
}
}
// Fall back: first imported contact's user_id
result
.imported
.first()
.map(|imp| match imp {
tl::enums::ImportedContact::ImportedContact(c) => {
Ok(tl::enums::Peer::User(tl::types::PeerUser {
user_id: c.user_id,
}))
}
})
.unwrap_or_else(|| {
Err(InvocationError::Deserialize(format!(
"phone {phone} not found on Telegram"
)))
})
}
/// `messages.checkChatInvite`; resolves an invite hash to a Peer.
///
/// Succeeds only when you are already a member (`chatInviteAlready` or
/// `chatInvitePeek`). Use [`Client::join_by_invite`] to join first.
pub(crate) async fn resolve_invite_hash_rpc(
&self,
hash: &str,
) -> Result<tl::enums::Peer, InvocationError> {
let req = tl::functions::messages::CheckChatInvite {
hash: hash.to_string(),
};
let body: Vec<u8> = self.rpc_call_raw(&req).await?;
let mut cur = Cursor::from_slice(&body);
let invite = tl::enums::ChatInvite::deserialize(&mut cur)?;
match invite {
tl::enums::ChatInvite::Already(a) => {
let peer = chat_to_peer(&a.chat);
self.cache_chats_slice(&[a.chat]).await;
peer.ok_or_else(|| {
InvocationError::Deserialize(
"chatInviteAlready: unrecognised chat variant".into(),
)
})
}
tl::enums::ChatInvite::Peek(p) => {
let peer = chat_to_peer(&p.chat);
self.cache_chats_slice(&[p.chat]).await;
peer.ok_or_else(|| {
InvocationError::Deserialize("chatInvitePeek: unrecognised chat variant".into())
})
}
tl::enums::ChatInvite::ChatInvite(_) => Err(InvocationError::Deserialize(
"not a member of this chat yet; call client.join_by_invite() first".into(),
)),
}
}
/// Join a chat by invite link and return its `InputPeer`.
///
/// Calls `messages.importChatInvite`, caches all returned entities, and
/// returns the `InputPeer` of the joined chat.
/// Join a chat or channel via an invite link.
pub async fn join_link(&self, link: &str) -> Result<tl::enums::InputPeer, InvocationError> {
let hash = PeerRef::parse_invite_hash(link)
.ok_or_else(|| InvocationError::Deserialize(format!("invalid invite link: {link}")))?;
let req = tl::functions::messages::ImportChatInvite {
hash: hash.to_string(),
};
let body: Vec<u8> = self.rpc_call_raw(&req).await?;
let mut cur = Cursor::from_slice(&body);
let updates = tl::enums::Updates::deserialize(&mut cur)?;
// Extract users and chats embedded in the Updates object
let (users, chats) = updates_entities(&updates);
self.cache_users_and_chats(&users, &chats).await;
// Return the InputPeer of the first chat from the updates
let cache: tokio::sync::RwLockReadGuard<'_, PeerCache> = self.inner.peer_cache.read().await;
for chat in &chats {
match chat {
tl::enums::Chat::Channel(c) if !c.min => {
if let Some(&(hash, _)) = cache.channels.get(&c.id) {
return Ok(tl::enums::InputPeer::Channel(tl::types::InputPeerChannel {
channel_id: c.id,
access_hash: hash,
}));
}
}
tl::enums::Chat::Chat(c) => {
return Ok(tl::enums::InputPeer::Chat(tl::types::InputPeerChat {
chat_id: c.id,
}));
}
_ => {}
}
}
Err(InvocationError::Deserialize(
"importChatInvite: no chat returned".into(),
))
}
/// Peek at an invite link without joining.
///
/// Returns the title and participant count of the chat the link points to.
pub async fn check_invite(&self, link: &str) -> Result<tl::enums::ChatInvite, InvocationError> {
let hash = PeerRef::parse_invite_hash(link)
.ok_or_else(|| InvocationError::Deserialize(format!("invalid invite link: {link}")))?;
let req = tl::functions::messages::CheckChatInvite {
hash: hash.to_string(),
};
let body: Vec<u8> = self.rpc_call_raw(&req).await?;
let mut cur = Cursor::from_slice(&body);
Ok(tl::enums::ChatInvite::deserialize(&mut cur)?)
}
}