tg_flows/types/
chat_member.rs

1use std::ops::Deref;
2
3use serde::{Deserialize, Serialize};
4
5use crate::types::{UntilDate, User};
6
7/// This object contains information about one member of the chat.
8///
9/// [The official docs](https://core.telegram.org/bots/api#chatmember).
10#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
11pub struct ChatMember {
12    /// Information about the user.
13    pub user: User,
14
15    /// The member's status in the chat.
16    #[serde(flatten)]
17    pub kind: ChatMemberKind,
18}
19
20#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
21#[serde(rename_all = "snake_case")]
22#[serde(tag = "status")]
23pub enum ChatMemberKind {
24    #[serde(rename = "creator")]
25    Owner(Owner),
26    Administrator(Administrator),
27    Member,
28    Restricted(Restricted),
29    Left,
30    #[serde(rename = "kicked")]
31    Banned(Banned),
32}
33
34/// Owner of the group. This struct is part of the [`ChatMemberKind`] enum.
35#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
36pub struct Owner {
37    /// Custom title for this user.
38    pub custom_title: Option<String>,
39
40    /// True, if the user's presence in the chat is hidden
41    pub is_anonymous: bool,
42}
43
44/// Administrator of the group. This struct is part of the [`ChatMemberKind`]
45/// enum.
46#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
47pub struct Administrator {
48    /// Custom title for this user.
49    pub custom_title: Option<String>,
50
51    /// `true` if the user's presence in the chat is hidden
52    pub is_anonymous: bool,
53
54    /// `true` if the bot is allowed to edit administrator privileges of that
55    /// user.
56    pub can_be_edited: bool,
57
58    /// `true` if the administrator can access the chat event log, chat
59    /// statistics, message statistics in channels, see channel members, see
60    /// anonymous administrators in supergroups and ignore slow mode. Implied by
61    /// any other administrator privilege
62    pub can_manage_chat: bool,
63
64    /// `true` if the administrator can change the chat title, photo and other
65    /// settings.
66    pub can_change_info: bool,
67
68    /// `true` if the administrator can post in the channel, channels only.
69    #[serde(default)]
70    pub can_post_messages: bool,
71
72    /// `true` if the administrator can edit messages of other users and can pin
73    /// messages, channels only.
74    #[serde(default)]
75    pub can_edit_messages: bool,
76
77    /// `true` if the administrator can delete messages of other users.
78    pub can_delete_messages: bool,
79
80    /// `true` if the administrator can manage video chats.
81    pub can_manage_video_chats: bool,
82
83    /// `true` if the administrator can invite new users to the chat.
84    pub can_invite_users: bool,
85
86    /// `true` if the administrator can restrict, ban or unban chat members.
87    pub can_restrict_members: bool,
88
89    /// `true` if the administrator can pin messages, supergroups only.
90    #[serde(default)]
91    pub can_pin_messages: bool,
92
93    /// `true`, if the user is allowed to create, rename, close, and reopen
94    /// forum topics; supergroups only
95    #[serde(default)]
96    pub can_manage_topics: bool,
97
98    /// `true` if the administrator can add new administrators with a subset of
99    /// his own privileges or demote administrators that he has promoted,
100    /// directly or indirectly (promoted by administrators that were appointed
101    /// by the user).
102    pub can_promote_members: bool,
103}
104
105/// User, restricted in the group. This struct is part of the [`ChatMemberKind`]
106/// enum.
107#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
108pub struct Restricted {
109    /// Date when restrictions will be lifted for this user.
110    pub until_date: UntilDate,
111
112    /// `true` if the user is a member of the chat at the moment of the request.
113    pub is_member: bool,
114
115    /// `true` if the user can send text messages, contacts, locations and
116    /// venues.
117    pub can_send_messages: bool,
118
119    /// `true` if the user is allowed to send audios, documents, photos, videos,
120    /// video notes and voice notes.
121    pub can_send_media_messages: bool,
122
123    /// `true` if the user is allowed to send animations, games, stickers and
124    /// use inline bots.
125    pub can_send_other_messages: bool,
126
127    /// `true` if the user is allowed to add web page previews to their
128    /// messages.
129    pub can_add_web_page_previews: bool,
130
131    /// `true` if the user is allowed to change the chat title, photo
132    /// and other settings.
133    pub can_change_info: bool,
134
135    /// `true` if the user is allowed to invite new users to the chat.
136    pub can_invite_users: bool,
137
138    /// `true` if the user is allowed to pin messages.
139    pub can_pin_messages: bool,
140
141    /// `true`, if the user is allowed to create, rename, close, and reopen
142    /// forum topics
143    pub can_manage_topics: bool,
144
145    /// `true` if the user is allowed to send polls.
146    pub can_send_polls: bool,
147}
148
149/// User that was banned in the chat and can't return to it or view chat
150/// messages. This struct is part of the [`ChatMemberKind`] enum.
151#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
152pub struct Banned {
153    /// Date when restrictions will be lifted for this user.
154    pub until_date: UntilDate,
155}
156
157/// This allows calling [`ChatMemberKind`]'s methods directly on [`ChatMember`].
158///
159/// ```no_run
160/// use teloxide_core::types::ChatMember;
161///
162/// let member: ChatMember = todo!();
163///
164/// let _ = member.status();
165/// let _ = member.kind.status();
166/// ```
167impl Deref for ChatMember {
168    type Target = ChatMemberKind;
169
170    fn deref(&self) -> &Self::Target {
171        &self.kind
172    }
173}
174
175/// Simple methods for checking a user status.
176impl ChatMemberKind {
177    /// Returns chat member status.
178    #[must_use]
179    pub fn status(&self) -> ChatMemberStatus {
180        match self {
181            ChatMemberKind::Owner(_) => ChatMemberStatus::Owner,
182            ChatMemberKind::Administrator(_) => ChatMemberStatus::Administrator,
183            ChatMemberKind::Member => ChatMemberStatus::Member,
184            ChatMemberKind::Restricted(_) => ChatMemberStatus::Restricted,
185            ChatMemberKind::Left => ChatMemberStatus::Left,
186            ChatMemberKind::Banned(_) => ChatMemberStatus::Banned,
187        }
188    }
189
190    /// Returns `true` if the user is the [owner] of the given chat.
191    ///
192    /// [owner]: ChatMemberKind::Owner
193    #[must_use]
194    pub fn is_owner(&self) -> bool {
195        matches!(self, Self::Owner { .. })
196    }
197
198    /// Returns `true` if the user is an [administrator] of the given chat.
199    ///
200    /// [administrator]: ChatMemberKind::Administrator
201    ///
202    /// **Note**: this function **doesn't** return `true` if the user is the
203    /// owner of the given chat. See also: [`is_privileged`].
204    ///
205    /// [`is_privileged`]: ChatMemberKind::is_privileged
206    #[must_use]
207    pub fn is_administrator(&self) -> bool {
208        matches!(self, Self::Administrator { .. })
209    }
210
211    /// Returns `true` if the user is a common [member] of the given chat.
212    ///
213    /// ⚠️ Don't confuse this with [`is_present`]. This method merely checks
214    /// for [`ChatMemberKind::Member`] variant which is not enough to determine
215    /// if the user is joinned to the chat. Use [`is_present`] for that instead.
216    ///
217    /// [member]: ChatMemberKind::Member
218    /// [`is_present`]: ChatMemberKind::is_present
219    #[must_use]
220    pub fn is_member(&self) -> bool {
221        matches!(self, Self::Member { .. })
222    }
223
224    /// Returns `true` if the user is [restricted] in the given chat.
225    ///
226    /// [restricted]: ChatMemberKind::Restricted
227    #[must_use]
228    pub fn is_restricted(&self) -> bool {
229        matches!(self, Self::Restricted { .. })
230    }
231
232    /// Returns `true` if the user [left] the given chat.
233    ///
234    /// ⚠️ Don't confuse this with [`is_present`]. This method merely checks
235    /// for [`ChatMemberKind::Left`] variant which is not enough to determine
236    /// if the user is joinned to the chat. Use [`is_present`] for that instead.
237    ///
238    /// [left]: ChatMemberKind::Left
239    /// [`is_present`]: ChatMemberKind::is_present
240    #[must_use]
241    pub fn is_left(&self) -> bool {
242        matches!(self, Self::Left { .. })
243    }
244
245    /// Returns `true` if the user is [banned] in the given chat.
246    ///
247    /// [banned]: ChatMemberKind::Banned
248    #[must_use]
249    pub fn is_banned(&self) -> bool {
250        matches!(self, Self::Banned { .. })
251    }
252
253    /// Returns `true` if the user is [kicked] from the given chat.
254    ///
255    /// [kicked]: ChatMemberKind::Banned
256    #[deprecated = "use `is_banned` instead"]
257    #[must_use]
258    pub fn is_kicked(&self) -> bool {
259        self.is_banned()
260    }
261
262    /// Returns `true` if the user is the [creator] (owner) of the given chat.
263    ///
264    /// [creator]: ChatMemberKind::Owner
265    #[deprecated = "use `is_owner` instead"]
266    #[must_use]
267    pub fn is_creator(&self) -> bool {
268        self.is_owner()
269    }
270}
271
272/// Compound methods for checking a user status.
273impl ChatMemberKind {
274    /// Returns `true` if the user is privileged in the given chat. i.e. if the
275    /// user is either the [owner] or an [administrator] in the given chat.
276    ///
277    /// [owner]: ChatMemberKind::Owner
278    /// [administrator]: ChatMemberKind::Administrator
279    #[must_use]
280    pub fn is_privileged(&self) -> bool {
281        self.is_administrator() || self.is_owner()
282    }
283
284    /// Returns `true` if the user is currently present in the chat. i.e. if the
285    /// user **hasn't** [left] or been [banned]. It also returns `false` if the
286    /// user left the chat, but was [restricted].
287    ///
288    /// [left]: ChatMemberKind::Left
289    /// [banned]: ChatMemberKind::Banned
290    /// [restricted]: ChatMemberKind::Restricted
291    #[must_use]
292    pub fn is_present(&self) -> bool {
293        let is_restricted_non_member =
294            matches!(self, Self::Restricted(Restricted { is_member: false, .. }));
295
296        !(self.is_left() || self.is_banned() || is_restricted_non_member)
297    }
298}
299
300impl ChatMemberKind {
301    /// Getter for [`Administrator::custom_title`] and [`Owner::custom_title`]
302    /// fields.
303    #[must_use]
304    pub fn custom_title(&self) -> Option<&str> {
305        match &self {
306            Self::Administrator(Administrator { custom_title, .. })
307            | Self::Owner(Owner { custom_title, .. }) => custom_title.as_deref(),
308            Self::Member | Self::Restricted(_) | Self::Left | Self::Banned(_) => None,
309        }
310    }
311
312    /// Returns `true` if the user's presence in the chat is hidden.
313    ///
314    /// I.e. returns `true` if the user is the owner of the chat or an
315    /// administrator in the chat and has the [`can_manage_chat`] privilege.
316    /// Returns `false` otherwise.
317    ///
318    /// [`can_manage_chat`]: Administrator::can_manage_chat
319    #[must_use]
320    pub fn is_anonymous(&self) -> bool {
321        match self {
322            Self::Owner(Owner { is_anonymous, .. })
323            | Self::Administrator(Administrator { is_anonymous, .. }) => *is_anonymous,
324            Self::Member | Self::Restricted(_) | Self::Left | Self::Banned(_) => false,
325        }
326    }
327
328    /// Getter for [`Restricted::until_date`] and [`Banned::until_date`] fields.
329    #[must_use]
330    pub fn until_date(&self) -> Option<UntilDate> {
331        match &self {
332            Self::Owner(_) | Self::Administrator(_) | Self::Member | Self::Left => None,
333            Self::Restricted(Restricted { until_date, .. })
334            | Self::Banned(Banned { until_date, .. }) => Some(*until_date),
335        }
336    }
337}
338
339/// Methods for checking admin privileges.
340impl ChatMemberKind {
341    /// Returns `true` if the user is an administrator in the given chat and the
342    /// bot is allowed to edit administrator privileges of that user.
343    #[must_use]
344    pub fn can_be_edited(&self) -> bool {
345        match self {
346            Self::Administrator(Administrator { can_be_edited, .. }) => *can_be_edited,
347            // Owner can't ever be edited by any bot.
348            Self::Owner(_) | Self::Member | Self::Restricted(_) | Self::Left | Self::Banned(_) => {
349                false
350            }
351        }
352    }
353
354    /// Returns `true` if the user can access the chat event log, chat
355    /// statistics, message statistics in channels, see channel members, see
356    /// anonymous administrators in supergroups and ignore slow mode. Implied by
357    /// any other administrator privilege.
358    ///
359    /// I.e. returns `true` if the user
360    /// - is the owner of the chat
361    /// - is an administrator in the given chat and has [`can_manage_chat`]
362    ///   privilege.
363    /// Returns `false` otherwise.
364    ///
365    /// [`can_manage_chat`]: Administrator::can_manage_chat
366    #[must_use]
367    pub fn can_manage_chat(&self) -> bool {
368        match self {
369            Self::Owner(_) => true,
370            Self::Administrator(Administrator { can_manage_chat, .. }) => *can_manage_chat,
371            Self::Member | Self::Restricted(_) | Self::Left | Self::Banned(_) => false,
372        }
373    }
374
375    /// Returns `true` if the user can change the chat title, photo and other
376    /// settings.
377    ///
378    /// I.e. returns `true` if the user
379    /// - is the owner of the chat
380    /// - is an administrator in the given chat and has the
381    ///   [`Administrator::can_change_info`] privilege.
382    /// - is restricted, but does have [`Restricted::can_change_info`] privilege
383    /// Returns `false` otherwise.
384    #[deprecated(
385        since = "0.9.0",
386        note = "Match manually and use `can_change_info` field directly. Details: https://github.com/teloxide/teloxide/issues/781"
387    )]
388    #[must_use]
389    pub fn can_change_info(&self) -> bool {
390        match self {
391            Self::Owner(_) => true,
392            Self::Administrator(Administrator { can_change_info, .. })
393            | Self::Restricted(Restricted { can_change_info, .. }) => *can_change_info,
394            Self::Member | Self::Left | Self::Banned(_) => false,
395        }
396    }
397
398    /// Returns `true` if the user can post in the channel, channels only.
399    ///
400    /// I.e. returns `true` if the user
401    /// - is the owner of the chat (even if the chat is not a channel)
402    /// - is an administrator in the given chat and has [`can_post_messages`]
403    ///   privilege.
404    /// Returns `false` otherwise.
405    ///
406    /// [`can_post_messages`]: Administrator::can_post_messages
407    #[must_use]
408    pub fn can_post_messages(&self) -> bool {
409        match self {
410            Self::Owner(_) => true,
411            Self::Administrator(Administrator { can_post_messages, .. }) => *can_post_messages,
412            Self::Member | Self::Restricted(_) | Self::Left | Self::Banned(_) => false,
413        }
414    }
415
416    /// Returns `true` if the user can edit messages of other users and can pin
417    /// messages, channels only.
418    ///
419    /// I.e. returns `true` if the user
420    /// - is the owner of the chat (even if the chat is not a channel)
421    /// - is an administrator in the given chat and has the
422    ///   [`can_edit_messages`] privilege.
423    /// Returns `false` otherwise.
424    ///
425    /// [`can_edit_messages`]: Administrator::can_edit_messages
426    #[must_use]
427    pub fn can_edit_messages(&self) -> bool {
428        match self {
429            Self::Owner(_) => true,
430            Self::Administrator(Administrator { can_edit_messages, .. }) => *can_edit_messages,
431            Self::Member | Self::Restricted(_) | Self::Left | Self::Banned(_) => false,
432        }
433    }
434
435    /// Returns `true` if the user can delete messages of other users.
436    ///
437    /// I.e. returns `true` if the user
438    /// - is the owner of the chat
439    /// - is an administrator in the given chat and has the
440    ///   [`can_delete_messages`] privilege.
441    /// Returns `false` otherwise.
442    ///
443    /// [`can_delete_messages`]: Administrator::can_delete_messages
444    #[must_use]
445    pub fn can_delete_messages(&self) -> bool {
446        match self {
447            Self::Owner(_) => true,
448            Self::Administrator(Administrator { can_delete_messages, .. }) => *can_delete_messages,
449            Self::Member | Self::Restricted(_) | Self::Left | Self::Banned(_) => false,
450        }
451    }
452
453    /// Returns `true` if the user can manage video chats.
454    ///
455    /// I.e. returns `true` if the user
456    /// - is the owner of the chat
457    /// - is an administrator in the given chat and has the
458    ///   [`can_manage_video_chats`] privilege.
459    /// Returns `false` otherwise.
460    ///
461    /// [`can_manage_video_chats`]: Administrator::can_manage_video_chats
462    #[must_use]
463    pub fn can_manage_video_chats(&self) -> bool {
464        match self {
465            Self::Owner(_) => true,
466            Self::Administrator(Administrator { can_manage_video_chats, .. }) => {
467                *can_manage_video_chats
468            }
469            Self::Member | Self::Restricted(_) | Self::Left | Self::Banned(_) => false,
470        }
471    }
472
473    #[deprecated(since = "0.6.0", note = "renamed to `can_manage_video_chats`")]
474    #[must_use]
475    pub fn can_manage_voice_chats(&self) -> bool {
476        self.can_manage_video_chats()
477    }
478
479    /// Returns `true` if the user can can invite new users to the chat.
480    ///
481    /// I.e. returns `true` if the user
482    /// - is the owner of the chat
483    /// - is an administrator in the given chat and has the
484    ///   [`Administrator::can_invite_users`] privilege.
485    /// - is restricted, but does have [`Restricted::can_invite_users`]
486    ///   privilege
487    /// Returns `false` otherwise.
488    #[deprecated(
489        since = "0.9.0",
490        note = "Match manually and use `can_invite_users` field directly. Details: https://github.com/teloxide/teloxide/issues/781"
491    )]
492    #[must_use]
493    pub fn can_invite_users(&self) -> bool {
494        match &self {
495            Self::Owner(_) => true,
496            Self::Administrator(Administrator { can_invite_users, .. })
497            | Self::Restricted(Restricted { can_invite_users, .. }) => *can_invite_users,
498            Self::Member | Self::Left | Self::Banned(_) => false,
499        }
500    }
501
502    /// Returns `true` if the user can restrict, ban or unban chat members.
503    ///
504    /// I.e. returns `true` if the user
505    /// - is the owner of the chat
506    /// - is an administrator in the given chat and has the
507    ///   [`can_restrict_members`] privilege.
508    /// Returns `false` otherwise.
509    ///
510    /// [`can_restrict_members`]: Administrator::can_restrict_members
511    #[must_use]
512    pub fn can_restrict_members(&self) -> bool {
513        match self {
514            Self::Owner(_) => true,
515            Self::Administrator(Administrator { can_restrict_members, .. }) => {
516                *can_restrict_members
517            }
518            Self::Member | Self::Restricted(_) | Self::Left | Self::Banned(_) => false,
519        }
520    }
521
522    /// Returns `true` if the user can pin messages, supergroups only.
523    ///
524    /// I.e. returns `true` if the user
525    /// - is the owner of the chat (even if the chat is not a supergroup)
526    /// - is an administrator in the given chat and has the
527    ///   [`Administrator::can_pin_messages`] privilege.
528    /// - is restricted, but does have [`Restricted::can_pin_messages`]
529    ///   privilege
530    /// Returns `false` otherwise.
531    #[deprecated(
532        since = "0.9.0",
533        note = "Match manually and use `can_pin_messages` field directly. Details: https://github.com/teloxide/teloxide/issues/781"
534    )]
535    #[must_use]
536    pub fn can_pin_messages(&self) -> bool {
537        match self {
538            Self::Owner(_) => true,
539            Self::Administrator(Administrator { can_pin_messages, .. })
540            | Self::Restricted(Restricted { can_pin_messages, .. }) => *can_pin_messages,
541            Self::Member | Self::Left | Self::Banned(_) => false,
542        }
543    }
544
545    /// Returns `true` if the user is allowed to manage topics.
546    ///
547    /// I.e. returns `true` if the user
548    /// - is the owner of the chat (even if the chat is not a supergroup)
549    /// - is an administrator in the given chat and has the
550    ///   [`Administrator::can_manage_topics`] privilege.
551    /// - is restricted, but does have [`Restricted::can_manage_topics`]
552    ///   privilege
553    /// Returns `false` otherwise.
554    #[deprecated(
555        since = "0.9.0",
556        note = "Match manually and use `can_manage_topics` field directly. Details: https://github.com/teloxide/teloxide/issues/781"
557    )]
558    #[must_use]
559    pub fn can_manage_topics(&self) -> bool {
560        match self {
561            ChatMemberKind::Owner(_) => true,
562            ChatMemberKind::Administrator(Administrator { can_manage_topics, .. })
563            | ChatMemberKind::Restricted(Restricted { can_manage_topics, .. }) => {
564                *can_manage_topics
565            }
566            ChatMemberKind::Member | ChatMemberKind::Left | ChatMemberKind::Banned(_) => false,
567        }
568    }
569
570    /// Returns `true` if the user can add new administrators with a subset of
571    /// his own privileges or demote administrators that he has promoted,
572    /// directly or indirectly (promoted by administrators that were appointed
573    /// by the user).
574    ///
575    /// I.e. returns `true` if the user
576    /// - is the owner of the chat (even if the chat is not a channel)
577    /// - is an administrator in the given chat and has the
578    ///   [`can_promote_members`] privilege.
579    /// Returns `false` otherwise.
580    ///
581    /// [`can_promote_members`]: Administrator::can_promote_members
582    #[must_use]
583    pub fn can_promote_members(&self) -> bool {
584        match self {
585            Self::Owner(_) => true,
586            Self::Administrator(Administrator { can_promote_members, .. }) => *can_promote_members,
587            Self::Member | Self::Restricted(_) | Self::Left | Self::Banned(_) => false,
588        }
589    }
590}
591
592/// Methods for checking member rights.
593impl ChatMemberKind {
594    /// Returns `true` if the user can send text messages, contacts, locations
595    /// and venues.
596    ///
597    /// I.e. returns **`false`** if the user
598    /// - has left or has been banned in the chat
599    /// - is restricted and doesn't have the [`can_send_messages`] right
600    /// Returns `true` otherwise.
601    ///
602    /// [`can_send_messages`]: Restricted::can_send_messages
603    #[deprecated(
604        since = "0.9.0",
605        note = "Match manually and use `can_send_messages` field directly. Details: https://github.com/teloxide/teloxide/issues/781"
606    )]
607    #[must_use]
608    pub fn can_send_messages(&self) -> bool {
609        match &self {
610            Self::Restricted(Restricted { can_send_messages, .. }) => *can_send_messages,
611            Self::Owner(_) | Self::Administrator(_) | Self::Member => true,
612            Self::Left | Self::Banned(_) => false,
613        }
614    }
615
616    /// Returns `true` if the user is allowed to send audios, documents, photos,
617    /// videos, video notes and voice notes.
618    ///
619    /// I.e. returns **`false`** if the user
620    /// - has left or has been banned in the chat
621    /// - is restricted and doesn't have the [`can_send_media_messages`] right
622    /// Returns `true` otherwise.
623    ///
624    /// [`can_send_media_messages`]: Restricted::can_send_media_messages
625    #[deprecated(
626        since = "0.9.0",
627        note = "Match manually and use `can_send_media_messages` field directly. Details: https://github.com/teloxide/teloxide/issues/781"
628    )]
629    #[must_use]
630    pub fn can_send_media_messages(&self) -> bool {
631        match &self {
632            Self::Restricted(Restricted { can_send_media_messages, .. }) => {
633                *can_send_media_messages
634            }
635            Self::Owner(_) | Self::Administrator(_) | Self::Member => true,
636            Self::Left | Self::Banned(_) => false,
637        }
638    }
639
640    /// Returns `true` if the user is allowed to send animations, games,
641    /// stickers and use inline bots.
642    ///
643    /// I.e. returns **`false`** if the user
644    /// - has left or has been banned from the chat
645    /// - is restricted and doesn't have the [`can_send_media_messages`] right
646    /// Returns `true` otherwise.
647    ///
648    /// [`can_send_media_messages`]: Restricted::can_send_media_messages
649    #[deprecated(
650        since = "0.9.0",
651        note = "Match manually and use `can_send_other_messages` field directly. Details: https://github.com/teloxide/teloxide/issues/781"
652    )]
653    #[must_use]
654    pub fn can_send_other_messages(&self) -> bool {
655        match &self {
656            Self::Restricted(Restricted { can_send_other_messages, .. }) => {
657                *can_send_other_messages
658            }
659            Self::Owner(_) | Self::Administrator(_) | Self::Member => true,
660            Self::Left | Self::Banned(_) => false,
661        }
662    }
663
664    /// Returns `true` if the user is allowed to add web page previews to their
665    /// messages.
666    ///
667    /// I.e. returns **`false`** if the user
668    /// - has left or has been banned from the chat
669    /// - is restricted and doesn't have the [`can_send_media_messages`] right
670    /// Returns `true` otherwise.
671    ///
672    /// [`can_send_media_messages`]: Restricted::can_send_media_messages
673    #[deprecated(
674        since = "0.9.0",
675        note = "Match manually and use `can_add_web_page_previews` field directly. Details: https://github.com/teloxide/teloxide/issues/781"
676    )]
677    #[must_use]
678    pub fn can_add_web_page_previews(&self) -> bool {
679        match &self {
680            Self::Restricted(Restricted { can_add_web_page_previews, .. }) => {
681                *can_add_web_page_previews
682            }
683            Self::Owner(_) | Self::Administrator(_) | Self::Member => true,
684            Self::Left | Self::Banned(_) => false,
685        }
686    }
687
688    /// Returns `true` if the user is allowed to send polls.
689    ///
690    /// I.e. returns **`false`** if the user
691    /// - has left or has been banned from the chat
692    /// - is restricted and doesn't have the [`can_send_polls`] right
693    /// Returns `true` otherwise.
694    ///
695    /// [`can_send_polls`]: Restricted::can_send_polls
696    #[deprecated(
697        since = "0.9.0",
698        note = "Match manually and use `can_send_polls` field directly. Details: https://github.com/teloxide/teloxide/issues/781"
699    )]
700    #[must_use]
701    pub fn can_send_polls(&self) -> bool {
702        match &self {
703            Self::Restricted(Restricted { can_send_polls, .. }) => *can_send_polls,
704            Self::Owner(_) | Self::Administrator(_) | Self::Member => true,
705            Self::Left | Self::Banned(_) => false,
706        }
707    }
708}
709
710#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
711#[serde(rename_all = "snake_case")]
712pub enum ChatMemberStatus {
713    #[serde(rename = "creator")]
714    Owner,
715    Administrator,
716    Member,
717    Restricted,
718    Left,
719    #[serde(rename = "kicked")]
720    Banned,
721}
722
723/// Simple methods for checking a user status.
724impl ChatMemberStatus {
725    /// Returns `true` if the user is the [owner] of the given chat.
726    ///
727    /// [owner]: ChatMemberKind::Owner
728    #[must_use]
729    pub fn is_owner(&self) -> bool {
730        matches!(self, Self::Owner { .. })
731    }
732
733    /// Returns `true` if the user is an [administrator] of the given chat.
734    ///
735    /// [administrator]: ChatMemberKind::Administrator
736    ///
737    /// **Note**: this function **doesn't** return `true` if the user is the
738    /// owner of the given chat. See also: [`is_privileged`].
739    ///
740    /// [`is_privileged`]: ChatMemberKind::is_privileged
741    #[must_use]
742    pub fn is_administrator(&self) -> bool {
743        matches!(self, Self::Administrator { .. })
744    }
745
746    /// Returns `true` if the user is a common [member] of the given chat.
747    ///
748    /// ⚠️ Don't confuse this with [`is_present`]. This method merely checks
749    /// for [`ChatMemberStatus::Member`] variant which is not enough to
750    /// determine if the user is joinned to the chat. Use [`is_present`] for
751    /// that instead.
752    ///
753    /// [member]: ChatMemberKind::Member
754    /// [`is_present`]: ChatMemberKind::is_present
755    #[must_use]
756    pub fn is_member(&self) -> bool {
757        matches!(self, Self::Member { .. })
758    }
759
760    /// Returns `true` if the user is [restricted] in the given chat.
761    ///
762    /// [restricted]: ChatMemberKind::Restricted
763    #[must_use]
764    pub fn is_restricted(&self) -> bool {
765        matches!(self, Self::Restricted { .. })
766    }
767
768    /// Returns `true` if the user [left] the given chat.
769    ///
770    /// ⚠️ Don't confuse this with [`is_present`]. This method merely checks
771    /// for [`ChatMemberStatus::Left`] variant which is not enough to determine
772    /// if the user is joinned to the chat. Use [`is_present`] for that instead.
773    ///
774    /// [left]: ChatMemberKind::Left
775    /// [`is_present`]: ChatMemberKind::is_present
776    #[must_use]
777    pub fn is_left(&self) -> bool {
778        matches!(self, Self::Left { .. })
779    }
780
781    /// Returns `true` if the user is [banned] in the given chat.
782    ///
783    /// [banned]: ChatMemberKind::Banned
784    #[must_use]
785    pub fn is_banned(&self) -> bool {
786        matches!(self, Self::Banned { .. })
787    }
788}
789
790/// Compound methods for checking a user status.
791impl ChatMemberStatus {
792    /// Returns `true` if the user is privileged in the given chat. i.e. if the
793    /// user is either the [owner] or an [administrator] in the given chat.
794    ///
795    /// [owner]: ChatMemberKind::Owner
796    /// [administrator]: ChatMemberKind::Administrator
797    #[must_use]
798    pub fn is_privileged(&self) -> bool {
799        self.is_administrator() || self.is_owner()
800    }
801
802    /// Returns `true` if the user is currently present in the chat. i.e. if the
803    /// user **hasn't** [left] or been [banned].
804    ///
805    /// [left]: ChatMemberKind::Left
806    /// [banned]: ChatMemberKind::Banned
807    #[must_use]
808    #[deprecated(
809        since = "0.9.0",
810        note = "Use `ChatMemberKind::is_present` method instead. Details: https://github.com/teloxide/teloxide/issues/781"
811    )]
812    pub fn is_present(&self) -> bool {
813        !(self.is_left() || self.is_banned())
814    }
815}
816
817#[cfg(test)]
818mod tests {
819    use crate::types::UserId;
820
821    use super::*;
822
823    #[test]
824    fn deserialize() {
825        let json = r#"{
826            "user":{
827                "id":1029940401,
828                "is_bot":false,
829                "first_name":"First",
830                "last_name":"Last",
831                "username":"fl",
832                "language_code":"en"
833            },
834            "status":"administrator",
835            "is_anonymous": false,
836            "can_be_edited": false,
837            "can_manage_chat": true,
838            "can_change_info": true,
839            "can_delete_messages": true,
840            "can_manage_video_chats": true,
841            "can_invite_users": true,
842            "can_restrict_members": true,
843            "can_pin_messages": true,
844            "can_promote_members": true
845        }"#;
846        let expected = ChatMember {
847            user: User {
848                id: UserId(1029940401),
849                is_bot: false,
850                first_name: "First".to_string(),
851                last_name: Some("Last".to_string()),
852                username: Some("fl".to_string()),
853                language_code: Some("en".to_string()),
854                is_premium: false,
855                added_to_attachment_menu: false,
856            },
857            kind: ChatMemberKind::Administrator(Administrator {
858                custom_title: None,
859                is_anonymous: false,
860                can_be_edited: false,
861                can_manage_chat: true,
862                can_change_info: true,
863                can_post_messages: false,
864                can_edit_messages: false,
865                can_delete_messages: true,
866                can_manage_video_chats: true,
867                can_invite_users: true,
868                can_restrict_members: true,
869                can_pin_messages: true,
870                can_promote_members: true,
871                can_manage_topics: false,
872            }),
873        };
874        let actual = serde_json::from_str::<ChatMember>(json).unwrap();
875        assert_eq!(actual, expected)
876    }
877}