telexide_fork/model/
chat.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3
4use super::{raw::RawChat, utils::unix_date_formatting, User};
5
6/// A private chat object, also known as a DM, between the bot and an user
7#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
8pub struct PrivateChat {
9    /// Unique identifier for this chat
10    pub id: i64,
11    /// Username if available
12    pub username: Option<String>,
13    /// First name of the other party
14    pub first_name: Option<String>,
15    /// Bio of the other party in a private chat. Returned only in [`get_chat`].
16    ///
17    /// [`get_chat`]: ../../api/trait.API.html#method.get_chat
18    pub bio: Option<String>,
19    /// Last name of the other party
20    pub last_name: Option<String>,
21    /// Chat photo. Returned only in [`get_chat`].
22    ///
23    /// [`get_chat`]: ../../api/trait.API.html#method.get_chat
24    pub photo: Option<ChatPhoto>,
25}
26
27/// A Group chat object
28#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
29pub struct GroupChat {
30    pub id: i64,
31    /// Title
32    pub title: String,
33    /// Username if available
34    pub username: Option<String>,
35    /// Chat photo. Returned only in [`get_chat`].
36    ///
37    /// [`get_chat`]: ../../api/trait.API.html#method.get_chat
38    pub photo: Option<ChatPhoto>,
39    /// Description. Returned only in [`get_chat`].
40    ///
41    /// [`get_chat`]: ../../api/trait.API.html#method.get_chat
42    pub description: Option<String>,
43    /// Chat invite link
44    pub invite_link: Option<String>,
45    /// Pinned message. Returned only in [`get_chat`].
46    ///
47    /// [`get_chat`]: ../../api/trait.API.html#method.get_chat
48    pub pinned_message: Option<Box<super::Message>>,
49    /// Default chat member permissions. Returned only in [`get_chat`].
50    ///
51    /// [`get_chat`]: ../../api/trait.API.html#method.get_chat
52    pub permissions: Option<super::ChatPermissions>,
53}
54
55/// A supergroup object (a group with more than 200 members)
56#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
57pub struct SuperGroupChat {
58    pub id: i64,
59    /// Title
60    pub title: String,
61    /// Username if available
62    pub username: Option<String>,
63    /// Chat photo. Returned only in [`get_chat`].
64    ///
65    /// [`get_chat`]: ../../api/trait.API.html#method.get_chat
66    pub photo: Option<ChatPhoto>,
67    /// Description. Returned only in [`get_chat`].
68    ///
69    /// [`get_chat`]: ../../api/trait.API.html#method.get_chat
70    pub description: Option<String>,
71    /// Chat invite link
72    pub invite_link: Option<String>,
73    /// Pinned message. Returned only in [`get_chat`].
74    ///
75    /// [`get_chat`]: ../../api/trait.API.html#method.get_chat
76    pub pinned_message: Option<Box<super::Message>>,
77    /// Default chat member permissions. Returned only in [`get_chat`].
78    ///
79    /// [`get_chat`]: ../../api/trait.API.html#method.get_chat
80    pub permissions: Option<super::ChatPermissions>,
81    /// The minimum allowed delay between consecutive messages sent by each
82    /// unprivileged user. Returned only in [`get_chat`].
83    ///
84    /// [`get_chat`]: ../../api/trait.API.html#method.get_chat
85    pub slow_mode_delay: Option<usize>,
86    /// Name of group sticker set. Returned only in [`get_chat`].
87    ///
88    /// [`get_chat`]: ../../api/trait.API.html#method.get_chat
89    pub sticker_set_name: Option<String>,
90    /// True, if the bot can change the group sticker set. Returned only in
91    /// [`get_chat`].
92    ///
93    /// [`get_chat`]: ../../api/trait.API.html#method.get_chat
94    pub can_set_sticker_set: Option<bool>,
95    /// Unique identifier for the linked chat, i.e. the discussion group
96    /// identifier for a channel and vice versa; for supergroups and channel
97    /// chats. Returned only in [`get_chat`].
98    ///
99    /// [`get_chat`]: ../../api/trait.API.html#method.get_chat
100    pub linked_chat_id: Option<i64>,
101    /// For supergroups, the location to which the supergroup is connected.
102    /// Returned only in [`get_chat`].
103    ///
104    /// [`get_chat`]: ../../api/trait.API.html#method.get_chat
105    pub location: Option<ChatLocation>,
106}
107
108/// A Channel object
109#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
110pub struct ChannelChat {
111    pub id: i64,
112    /// Title
113    pub title: String,
114    /// Username if available
115    pub username: Option<String>,
116    /// Chat photo. Returned only in [`get_chat`].
117    ///
118    /// [`get_chat`]: ../../api/trait.API.html#method.get_chat
119    pub photo: Option<ChatPhoto>,
120    /// Description. Returned only in [`get_chat`].
121    ///
122    /// [`get_chat`]: ../../api/trait.API.html#method.get_chat
123    pub description: Option<String>,
124    /// Chat invite link
125    pub invite_link: Option<String>,
126    /// Pinned message. Returned only in [`get_chat`].
127    ///
128    /// [`get_chat`]: ../../api/trait.API.html#method.get_chat
129    pub pinned_message: Option<Box<super::Message>>,
130    /// Unique identifier for the linked chat, i.e. the discussion group
131    /// identifier for a channel and vice versa; for supergroups and channel
132    /// chats. Returned only in [`get_chat`].
133    ///
134    /// [`get_chat`]: ../../api/trait.API.html#method.get_chat
135    pub linked_chat_id: Option<i64>,
136}
137
138/// This object represents a chat. It can be a private, group, supergroup or
139/// channel chat
140#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
141#[serde(tag = "type")]
142pub enum Chat {
143    #[serde(rename = "private")]
144    Private(PrivateChat),
145    #[serde(rename = "group")]
146    Group(GroupChat),
147    #[serde(rename = "supergroup")]
148    SuperGroup(SuperGroupChat),
149    #[serde(rename = "channel")]
150    Channel(ChannelChat),
151}
152
153/// Represents a location to which a chat is connected.
154#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
155pub struct ChatLocation {
156    /// The location to which the supergroup is connected. Can't be a live
157    /// location.
158    pub location: super::Location,
159    /// Location address; 1-64 characters, as defined by the chat owner
160    pub address: String,
161}
162
163/// Describes actions that a non-administrator user is allowed to take in a
164/// chat.
165#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
166pub struct ChatPermissions {
167    /// True, if the user is allowed to send text messages, contacts, locations
168    /// and venues.
169    #[serde(default)]
170    pub can_send_messages: bool,
171    /// True, if the user is allowed to send audios, documents, photos, videos,
172    /// video notes and voice notes, implies can_send_messages to be true.
173    #[serde(default)]
174    pub can_send_media_messages: bool,
175    /// True, if the user is allowed to send polls, implies can_send_messages to
176    /// be true.
177    #[serde(default)]
178    pub can_send_polls: bool,
179    /// True, if the user is allowed to send animations, games, stickers and use
180    /// inline bots, implies can_send_media_messages to be true.
181    #[serde(default)]
182    pub can_send_other_messages: bool,
183    /// True, if the user is allowed to add web page previews to their messages,
184    /// implies can_send_media_messages to be true.
185    #[serde(default)]
186    pub can_add_web_page_previews: bool,
187    /// True, if the user is allowed to change the chat title, photo and other
188    /// settings. Ignored in public supergroups.
189    #[serde(default)]
190    pub can_change_info: bool,
191    /// True, if the user is allowed to invite new users to the chat.
192    #[serde(default)]
193    pub can_invite_users: bool,
194    /// True, if the user is allowed to pin messages. Ignored in public
195    /// supergroups.
196    #[serde(default)]
197    pub can_pin_messages: bool,
198}
199
200/// This object represents a chat photo.
201#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
202pub struct ChatPhoto {
203    /// File identifier of small (160x160) chat photo.
204    /// This file_id can be used only for photo download and only for as long as
205    /// the photo is not changed.
206    pub small_file_id: String,
207    /// Unique file identifier of small (160x160) chat photo, which is supposed
208    /// to be the same over time and for different bots. Can't be used to
209    /// download or reuse the file.
210    pub small_file_unique_id: String,
211    /// File identifier of big (640x640) chat photo.
212    /// This file_id can be used only for photo download and only for as long as
213    /// the photo is not changed.
214    pub big_file_id: String,
215    /// Unique file identifier of big (640x640) chat photo, which is supposed to
216    /// be the same over time and for different bots. Can't be used to
217    /// download or reuse the file.
218    pub big_file_unique_id: String,
219}
220
221impl Chat {
222    /// Gets the id of the chat
223    pub fn get_id(&self) -> i64 {
224        match self {
225            Chat::Private(c) => c.id,
226            Chat::Channel(c) => c.id,
227            Chat::Group(c) => c.id,
228            Chat::SuperGroup(c) => c.id,
229        }
230    }
231}
232
233impl From<RawChat> for Chat {
234    fn from(raw: RawChat) -> Chat {
235        match raw.chat_type {
236            ChatType::Channel => Chat::Channel(ChannelChat {
237                id: raw.id,
238                title: raw.title.unwrap_or_default(),
239                username: raw.username,
240                photo: raw.photo,
241                description: raw.description,
242                pinned_message: raw.pinned_message.map(|m| Box::new((*m).into())),
243                invite_link: raw.invite_link,
244                linked_chat_id: raw.linked_chat_id,
245            }),
246            ChatType::Private => Chat::Private(PrivateChat {
247                id: raw.id,
248                first_name: raw.first_name,
249                last_name: raw.last_name,
250                username: raw.username,
251                photo: raw.photo,
252                bio: raw.bio,
253            }),
254            ChatType::Group => Chat::Group(GroupChat {
255                id: raw.id,
256                title: raw.title.unwrap_or_default(),
257                username: raw.username,
258                photo: raw.photo,
259                description: raw.description,
260                pinned_message: raw.pinned_message.map(|m| Box::new((*m).into())),
261                invite_link: raw.invite_link,
262                permissions: raw.permissions,
263            }),
264            ChatType::SuperGroup => Chat::SuperGroup(SuperGroupChat {
265                id: raw.id,
266                title: raw.title.unwrap_or_default(),
267                username: raw.username,
268                photo: raw.photo,
269                description: raw.description,
270                pinned_message: raw.pinned_message.map(|m| Box::new((*m).into())),
271                invite_link: raw.invite_link,
272                permissions: raw.permissions,
273                sticker_set_name: raw.sticker_set_name,
274                can_set_sticker_set: raw.can_set_sticker_set,
275                slow_mode_delay: raw.slow_mode_delay,
276                linked_chat_id: raw.linked_chat_id,
277                location: raw.location,
278            }),
279            ChatType::Sender => unreachable!(),
280        }
281    }
282}
283
284impl From<Chat> for RawChat {
285    fn from(chat: Chat) -> RawChat {
286        match chat {
287            Chat::Private(c) => RawChat {
288                chat_type: ChatType::Private,
289                first_name: c.first_name,
290                last_name: c.last_name,
291                id: c.id,
292                username: c.username,
293                photo: c.photo,
294                bio: c.bio,
295                title: None,
296                description: None,
297                pinned_message: None,
298                invite_link: None,
299                permissions: None,
300                sticker_set_name: None,
301                can_set_sticker_set: None,
302                slow_mode_delay: None,
303                linked_chat_id: None,
304                location: None,
305            },
306            Chat::Group(c) => RawChat {
307                chat_type: ChatType::Group,
308                id: c.id,
309                title: Some(c.title),
310                photo: c.photo,
311                description: c.description,
312                pinned_message: c.pinned_message.map(|m| Box::new((*m).into())),
313                invite_link: c.invite_link,
314                permissions: c.permissions,
315                username: None,
316                sticker_set_name: None,
317                can_set_sticker_set: None,
318                slow_mode_delay: None,
319                first_name: None,
320                last_name: None,
321                bio: None,
322                linked_chat_id: None,
323                location: None,
324            },
325            Chat::SuperGroup(c) => RawChat {
326                chat_type: ChatType::SuperGroup,
327                id: c.id,
328                title: Some(c.title),
329                username: c.username,
330                photo: c.photo,
331                description: c.description,
332                pinned_message: c.pinned_message.map(|m| Box::new((*m).into())),
333                invite_link: c.invite_link,
334                permissions: c.permissions,
335                sticker_set_name: c.sticker_set_name,
336                can_set_sticker_set: c.can_set_sticker_set,
337                slow_mode_delay: c.slow_mode_delay,
338                linked_chat_id: c.linked_chat_id,
339                location: c.location,
340                bio: None,
341                first_name: None,
342                last_name: None,
343            },
344            Chat::Channel(c) => RawChat {
345                chat_type: ChatType::Channel,
346                id: c.id,
347                title: Some(c.title),
348                username: c.username,
349                photo: c.photo,
350                description: c.description,
351                pinned_message: c.pinned_message.map(|m| Box::new((*m).into())),
352                invite_link: c.invite_link,
353                linked_chat_id: c.linked_chat_id,
354                permissions: None,
355                sticker_set_name: None,
356                can_set_sticker_set: None,
357                slow_mode_delay: None,
358                first_name: None,
359                last_name: None,
360                bio: None,
361                location: None,
362            },
363        }
364    }
365}
366
367/// This object contains information about one member of a chat.
368#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
369#[serde(tag = "status")]
370pub enum ChatMember {
371    #[serde(rename = "creator")]
372    Creator(CreatorMemberStatus),
373    #[serde(rename = "administrator")]
374    Administrator(AdministratorMemberStatus),
375    #[serde(rename = "member")]
376    Member(MemberMemberStatus),
377    #[serde(rename = "restricted")]
378    Restricted(RestrictedMemberStatus),
379    #[serde(rename = "left")]
380    Left(LeftMemberStatus),
381    #[serde(rename = "kicked")]
382    Kicked(KickedMemberStatus),
383}
384
385/// Represents a [`ChatMember`] who is the creator of the [`Chat`].
386#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
387pub struct CreatorMemberStatus {
388    /// Information about the user
389    pub user: User,
390    /// Custom title for this user
391    pub custom_title: Option<String>,
392    /// Owner and administrators only. True, if the user's presence in the chat
393    /// is hidden
394    #[serde(default)]
395    pub is_anonymous: bool,
396}
397
398/// Represents a [`ChatMember`] who is an Admin of the [`Chat`].
399#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
400pub struct AdministratorMemberStatus {
401    /// Information about the user
402    pub user: User,
403    /// True, if the bot is allowed to edit administrator privileges of that
404    /// user
405    #[serde(default)]
406    pub can_be_edited: bool,
407    /// Owner and administrators only. True, if the user's presence in the chat
408    /// is hidden
409    #[serde(default)]
410    pub is_anonymous: bool,
411    /// True, if the administrator can access the chat event log, chat
412    /// statistics, message statistics in channels, see channel members, see
413    /// anonymous administrators in supergroups and ignore slow mode.
414    /// Implied by any other administrator privilege
415    #[serde(default)]
416    pub can_manage_chat: bool,
417    /// True, if the administrator can post in the channel; channels only
418    /// If the administrator can delete messages of other users
419    #[serde(default)]
420    pub can_delete_messages: bool,
421    /// True, if the administrator can manage voice chats
422    #[serde(default)]
423    pub can_manage_voice_chats: bool,
424    /// True, if the administrator can restrict, ban or unban chat members
425    #[serde(default)]
426    pub can_restrict_members: bool,
427    /// True, if the administrator can add new administrators with a subset of their
428    /// own privileges or demote administrators that he has promoted, directly
429    /// or indirectly (promoted by administrators that were appointed by the user)
430    #[serde(default)]
431    pub can_promote_members: bool,
432    /// True, if the user is allowed to change the chat title, photo and other
433    /// settings
434    pub can_change_info: bool,
435    /// True, if the user is allowed to invite new users to the chat
436    #[serde(default)]
437    pub can_invite_users: bool,
438    #[serde(skip_serializing_if = "Option::is_none")]
439    pub can_post_messages: Option<bool>,
440    /// If the administrator can edit messages of other users and can pin
441    /// messages, channels only
442    #[serde(skip_serializing_if = "Option::is_none")]
443    pub can_edit_messages: Option<bool>,
444    /// If the administrator can add new administrators with a subset of his own
445    /// privileges or demote administrators that he has promoted, directly
446    /// or indirectly (promoted by administrators that were appointed by
447    /// him)
448    #[serde(skip_serializing_if = "Option::is_none")]
449    pub can_pin_messages: Option<bool>,
450    /// Custom title for this user
451    pub custom_title: Option<String>,
452}
453
454/// Represents a [`ChatMember`] who is a normal member of the [`Chat`] without
455/// any special powers.
456#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
457pub struct MemberMemberStatus {
458    /// Information about the user
459    pub user: User,
460}
461
462/// Represents a restricted [`ChatMember`] of a [`Chat`].
463#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
464pub struct RestrictedMemberStatus {
465    /// Information about the user
466    pub user: User,
467    /// Date when restrictions will be lifted for this user; unix time
468    #[serde(with = "unix_date_formatting::optional")]
469    pub until_date: Option<DateTime<Utc>>,
470    #[serde(default)]
471    /// True, if the user is allowed to change the chat title, photo and other
472    /// settings
473    pub can_change_info: bool,
474    /// True, if the user is allowed to invite new users to the chat
475    #[serde(default)]
476    pub can_invite_users: bool,
477    ///  True, if the user is allowed to pin messages; groups and supergroups
478    /// only
479    #[serde(default)]
480    pub can_pin_messages: bool,
481    /// True, if the user is a member of the chat at the moment of the request
482    #[serde(default)]
483    pub is_member: bool,
484    /// True, if the user is allowed to send text messages, contacts, locations
485    /// and venues
486    #[serde(default)]
487    pub can_send_messages: bool,
488    /// True, if the user is allowed to send audios, documents, photos, videos,
489    /// video notes and voice notes
490    #[serde(default)]
491    pub can_send_media_messages: bool,
492    /// True, if the user is allowed to send polls
493    #[serde(default)]
494    pub can_send_polls: bool,
495    /// True, if the user is allowed to send animations, games, stickers and use
496    /// inline bots
497    #[serde(default)]
498    pub can_send_other_messages: bool,
499    /// True, if the user is allowed to add web page previews to their messages
500    #[serde(default)]
501    pub can_add_web_page_previews: bool,
502}
503
504/// Represents a [`ChatMember`] who left the [`Chat`].
505#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
506pub struct LeftMemberStatus {
507    /// Information about the user
508    pub user: User,
509}
510
511/// Represents a [`ChatMember`] who has been kicked from the [`Chat`].
512#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
513pub struct KickedMemberStatus {
514    /// Information about the user
515    pub user: User,
516    /// Date when restrictions will be lifted for this user; unix time
517    #[serde(with = "unix_date_formatting::optional")]
518    pub until_date: Option<DateTime<Utc>>,
519}
520
521impl ChatMember {
522    /// Retrieves the underlying [`User`] of the [`ChatMember`].
523    pub fn get_user(&self) -> &User {
524        match self {
525            ChatMember::Administrator(m) => &m.user,
526            ChatMember::Creator(m) => &m.user,
527            ChatMember::Kicked(m) => &m.user,
528            ChatMember::Left(m) => &m.user,
529            ChatMember::Member(m) => &m.user,
530            ChatMember::Restricted(m) => &m.user,
531        }
532    }
533}
534
535/// Represents an invite link for a chat.
536#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
537pub struct ChatInviteLink {
538    /// The invite link. If the link was created by another chat administrator,
539    /// then the second part of the link will be replaced with “…”.
540    pub invite_link: String,
541    /// Creator of the link
542    pub creator: User,
543    /// If the link is primary
544    pub is_primary: bool,
545    /// If the link is revoked
546    pub is_revoked: bool,
547    /// When the link will expire or has been expired
548    #[serde(with = "unix_date_formatting::optional")]
549    pub expire_date: Option<DateTime<Utc>>,
550    /// Maximum number of users that can be members of the chat simultaneously
551    /// after joining the chat via this invite link; 1-99999
552    #[serde(default)]
553    pub member_limit: Option<i32>,
554}
555
556/// Represents changes in the status of a chat member.
557#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
558pub struct ChatMemberUpdated {
559    /// Chat the user belongs to
560    pub chat: Chat,
561    /// Performer of the action, which resulted in the change
562    pub from: User,
563    /// Date the change was done
564    #[serde(with = "unix_date_formatting")]
565    pub date: DateTime<Utc>,
566    /// Previous information about the chat member
567    pub old_chat_member: ChatMember,
568    /// New information about the chat member
569    pub new_chat_member: ChatMember,
570    /// Chat invite link, which was used by the user to join the chat; for
571    /// joining by invite link events only.
572    pub invite_link: Option<ChatInviteLink>,
573}
574
575/// The type of chat
576#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
577pub enum ChatType {
578    #[serde(rename = "private")]
579    Private,
580    #[serde(rename = "group")]
581    Group,
582    #[serde(rename = "supergroup")]
583    SuperGroup,
584    #[serde(rename = "channel")]
585    Channel,
586    #[serde(rename = "sender")]
587    Sender,
588}