Skip to content

Commit c4c79ff

Browse files
committed
Store original User enum
1 parent daf8189 commit c4c79ff

File tree

1 file changed

+39
-81
lines changed
  • lib/grammers-client/src/types/chat

1 file changed

+39
-81
lines changed

lib/grammers-client/src/types/chat/user.rs

+39-81
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl RestrictionReason {
6262
/// [@BotFather]: https://t.me/BotFather
6363
#[derive(Clone)]
6464
pub struct User {
65-
pub raw: tl::types::User,
65+
pub raw: tl::enums::User,
6666
}
6767

6868
impl fmt::Debug for User {
@@ -74,73 +74,30 @@ impl fmt::Debug for User {
7474
// TODO: photo
7575
impl User {
7676
pub fn from_raw(user: tl::enums::User) -> Self {
77-
Self {
78-
raw: match user {
79-
tl::enums::User::Empty(empty) => tl::types::User {
80-
is_self: false,
81-
contact: false,
82-
mutual_contact: false,
83-
deleted: false,
84-
bot: false,
85-
bot_chat_history: false,
86-
bot_nochats: false,
87-
verified: false,
88-
restricted: false,
89-
min: false,
90-
bot_inline_geo: false,
91-
support: false,
92-
scam: false,
93-
apply_min_photo: false,
94-
fake: false,
95-
bot_attach_menu: false,
96-
premium: false,
97-
attach_menu_enabled: false,
98-
bot_can_edit: false,
99-
close_friend: false,
100-
stories_hidden: false,
101-
stories_unavailable: true,
102-
contact_require_premium: false,
103-
bot_business: false,
104-
bot_has_main_app: false,
105-
id: empty.id,
106-
access_hash: None,
107-
first_name: None,
108-
last_name: None,
109-
username: None,
110-
phone: None,
111-
photo: None,
112-
status: None,
113-
bot_info_version: None,
114-
restriction_reason: None,
115-
bot_inline_placeholder: None,
116-
lang_code: None,
117-
emoji_status: None,
118-
usernames: None,
119-
stories_max_id: None,
120-
color: None,
121-
profile_color: None,
122-
bot_active_users: None,
123-
},
124-
tl::enums::User::User(user) => user,
125-
},
77+
Self { raw: user }
78+
}
79+
80+
fn user(&self) -> Option<&tl::types::User> {
81+
match &self.raw {
82+
tl::enums::User::User(u) => Some(u),
83+
tl::enums::User::Empty(_) => None,
12684
}
12785
}
12886

12987
/// Return the user presence status (also known as "last seen").
13088
pub fn status(&self) -> &grammers_tl_types::enums::UserStatus {
131-
self.raw
132-
.status
133-
.as_ref()
89+
self.user()
90+
.and_then(|u| u.status.as_ref())
13491
.unwrap_or(&grammers_tl_types::enums::UserStatus::Empty)
13592
}
13693

13794
/// Return the unique identifier for this user.
13895
pub fn id(&self) -> i64 {
139-
self.raw.id
96+
self.raw.id()
14097
}
14198

14299
pub(crate) fn access_hash(&self) -> Option<i64> {
143-
self.raw.access_hash
100+
self.user().and_then(|u| u.access_hash)
144101
}
145102

146103
/// Pack this user into a smaller representation that can be loaded later.
@@ -161,15 +118,16 @@ impl User {
161118
/// The name will be `None` if the account was deleted. It may also be `None` if you received
162119
/// it previously.
163120
pub fn first_name(&self) -> Option<&str> {
164-
self.raw.first_name.as_deref()
121+
self.user().and_then(|u| u.first_name.as_deref())
165122
}
166123

167124
/// Return the last name of this user, if any.
168125
pub fn last_name(&self) -> Option<&str> {
169-
self.raw
170-
.last_name
171-
.as_deref()
172-
.and_then(|name| if name.is_empty() { None } else { Some(name) })
126+
self.user().and_then(|u| {
127+
u.last_name
128+
.as_deref()
129+
.and_then(|name| if name.is_empty() { None } else { Some(name) })
130+
})
173131
}
174132

175133
/// Return the full name of this user.
@@ -196,7 +154,7 @@ impl User {
196154
/// Outside of the application, people may link to this user with one of Telegram's URLs, such
197155
/// as https://t.me/username.
198156
pub fn username(&self) -> Option<&str> {
199-
self.raw.username.as_deref()
157+
self.user().and_then(|u| u.username.as_deref())
200158
}
201159

202160
/// Return collectible usernames of this chat, if any.
@@ -206,9 +164,8 @@ impl User {
206164
/// Outside of the application, people may link to this user with one of its username, such
207165
/// as https://t.me/username.
208166
pub fn usernames(&self) -> Vec<&str> {
209-
self.raw
210-
.usernames
211-
.as_deref()
167+
self.user()
168+
.and_then(|u| u.usernames.as_deref())
212169
.map_or(Vec::new(), |usernames| {
213170
usernames
214171
.iter()
@@ -222,12 +179,12 @@ impl User {
222179
/// Return the phone number of this user, if they are not a bot and their privacy settings
223180
/// allow you to see it.
224181
pub fn phone(&self) -> Option<&str> {
225-
self.raw.phone.as_deref()
182+
self.user().and_then(|u| u.phone.as_deref())
226183
}
227184

228185
/// Return the photo of this user, if any.
229186
pub fn photo(&self) -> Option<&tl::types::UserProfilePhoto> {
230-
match self.raw.photo.as_ref() {
187+
match self.user().and_then(|u| u.photo.as_ref()) {
231188
Some(maybe_photo) => match maybe_photo {
232189
tl::enums::UserProfilePhoto::Empty => None,
233190
tl::enums::UserProfilePhoto::Photo(photo) => Some(photo),
@@ -239,32 +196,32 @@ impl User {
239196
/// Does this user represent the account that's currently logged in?
240197
pub fn is_self(&self) -> bool {
241198
// TODO if is_self is false, check in chat cache if id == ourself
242-
self.raw.is_self
199+
self.user().map(|u| u.is_self).unwrap_or(false)
243200
}
244201

245202
/// Is this user in your account's contact list?
246203
pub fn contact(&self) -> bool {
247-
self.raw.contact
204+
self.user().map(|u| u.contact).unwrap_or(false)
248205
}
249206

250207
/// Is this user a mutual contact?
251208
///
252209
/// Contacts are mutual if both the user of the current account and this user have eachother
253210
/// in their respective contact list.
254211
pub fn mutual_contact(&self) -> bool {
255-
self.raw.mutual_contact
212+
self.user().map(|u| u.mutual_contact).unwrap_or(false)
256213
}
257214

258215
/// Has the account of this user been deleted?
259216
pub fn deleted(&self) -> bool {
260-
self.raw.deleted
217+
self.user().map(|u| u.deleted).unwrap_or(false)
261218
}
262219

263220
/// Is the current account a bot?
264221
///
265222
/// Bot accounts are those created by [@BotFather](https://t.me/BotFather).
266223
pub fn is_bot(&self) -> bool {
267-
self.raw.bot
224+
self.user().map(|u| u.bot).unwrap_or(false)
268225
}
269226

270227
/// If the current user is a bot, does it have [privacy mode] enabled?
@@ -275,45 +232,45 @@ impl User {
275232
///
276233
/// [privacy mode]: https://core.telegram.org/bots#privacy-mode
277234
pub fn bot_privacy(&self) -> bool {
278-
!self.raw.bot_chat_history
235+
self.user().map(|u| !u.bot_chat_history).unwrap_or(false)
279236
}
280237

281238
/// If the current user is a bot, can it be added to groups?
282239
pub fn bot_supports_chats(self) -> bool {
283-
self.raw.bot_nochats
240+
self.user().map(|u| u.bot_nochats).unwrap_or(false)
284241
}
285242

286243
/// Has the account of this user been verified?
287244
///
288245
/// Verified accounts, such as [@BotFather](https://t.me/BotFather), have a special icon next
289246
/// to their names in official applications (commonly a blue starred checkmark).
290247
pub fn verified(&self) -> bool {
291-
self.raw.verified
248+
self.user().map(|u| u.verified).unwrap_or(false)
292249
}
293250

294251
/// Does this user have restrictions applied to their account?
295252
pub fn restricted(&self) -> bool {
296-
self.raw.restricted
253+
self.user().map(|u| u.restricted).unwrap_or(false)
297254
}
298255

299256
/// If the current user is a bot, does it want geolocation information on inline queries?
300257
pub fn bot_inline_geo(&self) -> bool {
301-
self.raw.bot_inline_geo
258+
self.user().map(|u| u.bot_inline_geo).unwrap_or(false)
302259
}
303260

304261
/// Is this user an official member of the support team?
305262
pub fn support(&self) -> bool {
306-
self.raw.support
263+
self.user().map(|u| u.support).unwrap_or(false)
307264
}
308265

309266
/// Has this user been flagged for trying to scam other people?
310267
pub fn scam(&self) -> bool {
311-
self.raw.scam
268+
self.user().map(|u| u.scam).unwrap_or(false)
312269
}
313270

314271
/// The reason(s) why this user is restricted, could be empty.
315272
pub fn restriction_reason(&self) -> Vec<RestrictionReason> {
316-
if let Some(reasons) = &self.raw.restriction_reason {
273+
if let Some(reasons) = self.user().and_then(|u| u.restriction_reason.as_ref()) {
317274
reasons.iter().map(RestrictionReason::from_raw).collect()
318275
} else {
319276
Vec::new()
@@ -323,12 +280,13 @@ impl User {
323280
/// Return the placeholder for inline queries if the current user is a bot and has said
324281
/// placeholder configured.
325282
pub fn bot_inline_placeholder(&self) -> Option<&str> {
326-
self.raw.bot_inline_placeholder.as_deref()
283+
self.user()
284+
.and_then(|u| u.bot_inline_placeholder.as_deref())
327285
}
328286

329287
/// Language code of the user, if any.
330288
pub fn lang_code(&self) -> Option<&str> {
331-
self.raw.lang_code.as_deref()
289+
self.user().and_then(|u| u.lang_code.as_deref())
332290
}
333291
}
334292

0 commit comments

Comments
 (0)