@@ -62,7 +62,7 @@ impl RestrictionReason {
62
62
/// [@BotFather]: https://t.me/BotFather
63
63
#[ derive( Clone ) ]
64
64
pub struct User {
65
- pub raw : tl:: types :: User ,
65
+ pub raw : tl:: enums :: User ,
66
66
}
67
67
68
68
impl fmt:: Debug for User {
@@ -74,73 +74,30 @@ impl fmt::Debug for User {
74
74
// TODO: photo
75
75
impl User {
76
76
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 ,
126
84
}
127
85
}
128
86
129
87
/// Return the user presence status (also known as "last seen").
130
88
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 ( ) )
134
91
. unwrap_or ( & grammers_tl_types:: enums:: UserStatus :: Empty )
135
92
}
136
93
137
94
/// Return the unique identifier for this user.
138
95
pub fn id ( & self ) -> i64 {
139
- self . raw . id
96
+ self . raw . id ( )
140
97
}
141
98
142
99
pub ( crate ) fn access_hash ( & self ) -> Option < i64 > {
143
- self . raw . access_hash
100
+ self . user ( ) . and_then ( |u| u . access_hash )
144
101
}
145
102
146
103
/// Pack this user into a smaller representation that can be loaded later.
@@ -161,15 +118,16 @@ impl User {
161
118
/// The name will be `None` if the account was deleted. It may also be `None` if you received
162
119
/// it previously.
163
120
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 ( ) )
165
122
}
166
123
167
124
/// Return the last name of this user, if any.
168
125
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
+ } )
173
131
}
174
132
175
133
/// Return the full name of this user.
@@ -196,7 +154,7 @@ impl User {
196
154
/// Outside of the application, people may link to this user with one of Telegram's URLs, such
197
155
/// as https://t.me/username.
198
156
pub fn username ( & self ) -> Option < & str > {
199
- self . raw . username . as_deref ( )
157
+ self . user ( ) . and_then ( |u| u . username . as_deref ( ) )
200
158
}
201
159
202
160
/// Return collectible usernames of this chat, if any.
@@ -206,9 +164,8 @@ impl User {
206
164
/// Outside of the application, people may link to this user with one of its username, such
207
165
/// as https://t.me/username.
208
166
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 ( ) )
212
169
. map_or ( Vec :: new ( ) , |usernames| {
213
170
usernames
214
171
. iter ( )
@@ -222,12 +179,12 @@ impl User {
222
179
/// Return the phone number of this user, if they are not a bot and their privacy settings
223
180
/// allow you to see it.
224
181
pub fn phone ( & self ) -> Option < & str > {
225
- self . raw . phone . as_deref ( )
182
+ self . user ( ) . and_then ( |u| u . phone . as_deref ( ) )
226
183
}
227
184
228
185
/// Return the photo of this user, if any.
229
186
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 ( ) ) {
231
188
Some ( maybe_photo) => match maybe_photo {
232
189
tl:: enums:: UserProfilePhoto :: Empty => None ,
233
190
tl:: enums:: UserProfilePhoto :: Photo ( photo) => Some ( photo) ,
@@ -239,32 +196,32 @@ impl User {
239
196
/// Does this user represent the account that's currently logged in?
240
197
pub fn is_self ( & self ) -> bool {
241
198
// 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 )
243
200
}
244
201
245
202
/// Is this user in your account's contact list?
246
203
pub fn contact ( & self ) -> bool {
247
- self . raw . contact
204
+ self . user ( ) . map ( |u| u . contact ) . unwrap_or ( false )
248
205
}
249
206
250
207
/// Is this user a mutual contact?
251
208
///
252
209
/// Contacts are mutual if both the user of the current account and this user have eachother
253
210
/// in their respective contact list.
254
211
pub fn mutual_contact ( & self ) -> bool {
255
- self . raw . mutual_contact
212
+ self . user ( ) . map ( |u| u . mutual_contact ) . unwrap_or ( false )
256
213
}
257
214
258
215
/// Has the account of this user been deleted?
259
216
pub fn deleted ( & self ) -> bool {
260
- self . raw . deleted
217
+ self . user ( ) . map ( |u| u . deleted ) . unwrap_or ( false )
261
218
}
262
219
263
220
/// Is the current account a bot?
264
221
///
265
222
/// Bot accounts are those created by [@BotFather](https://t.me/BotFather).
266
223
pub fn is_bot ( & self ) -> bool {
267
- self . raw . bot
224
+ self . user ( ) . map ( |u| u . bot ) . unwrap_or ( false )
268
225
}
269
226
270
227
/// If the current user is a bot, does it have [privacy mode] enabled?
@@ -275,45 +232,45 @@ impl User {
275
232
///
276
233
/// [privacy mode]: https://core.telegram.org/bots#privacy-mode
277
234
pub fn bot_privacy ( & self ) -> bool {
278
- ! self . raw . bot_chat_history
235
+ self . user ( ) . map ( |u| !u . bot_chat_history ) . unwrap_or ( false )
279
236
}
280
237
281
238
/// If the current user is a bot, can it be added to groups?
282
239
pub fn bot_supports_chats ( self ) -> bool {
283
- self . raw . bot_nochats
240
+ self . user ( ) . map ( |u| u . bot_nochats ) . unwrap_or ( false )
284
241
}
285
242
286
243
/// Has the account of this user been verified?
287
244
///
288
245
/// Verified accounts, such as [@BotFather](https://t.me/BotFather), have a special icon next
289
246
/// to their names in official applications (commonly a blue starred checkmark).
290
247
pub fn verified ( & self ) -> bool {
291
- self . raw . verified
248
+ self . user ( ) . map ( |u| u . verified ) . unwrap_or ( false )
292
249
}
293
250
294
251
/// Does this user have restrictions applied to their account?
295
252
pub fn restricted ( & self ) -> bool {
296
- self . raw . restricted
253
+ self . user ( ) . map ( |u| u . restricted ) . unwrap_or ( false )
297
254
}
298
255
299
256
/// If the current user is a bot, does it want geolocation information on inline queries?
300
257
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 )
302
259
}
303
260
304
261
/// Is this user an official member of the support team?
305
262
pub fn support ( & self ) -> bool {
306
- self . raw . support
263
+ self . user ( ) . map ( |u| u . support ) . unwrap_or ( false )
307
264
}
308
265
309
266
/// Has this user been flagged for trying to scam other people?
310
267
pub fn scam ( & self ) -> bool {
311
- self . raw . scam
268
+ self . user ( ) . map ( |u| u . scam ) . unwrap_or ( false )
312
269
}
313
270
314
271
/// The reason(s) why this user is restricted, could be empty.
315
272
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 ( ) ) {
317
274
reasons. iter ( ) . map ( RestrictionReason :: from_raw) . collect ( )
318
275
} else {
319
276
Vec :: new ( )
@@ -323,12 +280,13 @@ impl User {
323
280
/// Return the placeholder for inline queries if the current user is a bot and has said
324
281
/// placeholder configured.
325
282
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 ( ) )
327
285
}
328
286
329
287
/// Language code of the user, if any.
330
288
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 ( ) )
332
290
}
333
291
}
334
292
0 commit comments