Skip to main content

rustigram_types/
chat_member.rs

1use crate::chat::ChatPermissions;
2use crate::user::User;
3use serde::{Deserialize, Serialize};
4
5/// Information about one member of a chat.
6#[derive(Debug, Clone, Serialize, Deserialize)]
7#[serde(tag = "status", rename_all = "snake_case")]
8pub enum ChatMember {
9    /// The chat owner.
10    #[serde(rename = "creator")]
11    Owner(ChatMemberOwner),
12    /// An administrator with custom privileges.
13    Administrator(ChatMemberAdministrator),
14    /// A regular chat member.
15    Member(ChatMemberMember),
16    /// A member with restricted permissions.
17    Restricted(ChatMemberRestricted),
18    /// A user who left the chat.
19    Left(ChatMemberLeft),
20    /// A banned (kicked) user.
21    #[serde(rename = "kicked")]
22    Banned(ChatMemberBanned),
23}
24
25impl ChatMember {
26    /// Returns the user from any variant.
27    #[must_use]
28    pub fn user(&self) -> &User {
29        match self {
30            Self::Owner(m) => &m.user,
31            Self::Administrator(m) => &m.user,
32            Self::Member(m) => &m.user,
33            Self::Restricted(m) => &m.user,
34            Self::Left(m) => &m.user,
35            Self::Banned(m) => &m.user,
36        }
37    }
38}
39
40/// Chat owner.
41#[derive(Debug, Clone, Serialize, Deserialize)]
42pub struct ChatMemberOwner {
43    /// The user.
44    pub user: User,
45    /// `true` if the user's presence in the chat is hidden.
46    pub is_anonymous: bool,
47    /// Custom title shown instead of "Owner".
48    #[serde(skip_serializing_if = "Option::is_none")]
49    pub custom_title: Option<String>,
50}
51
52/// Chat administrator.
53#[derive(Debug, Clone, Serialize, Deserialize)]
54pub struct ChatMemberAdministrator {
55    /// The user.
56    pub user: User,
57    /// `true` if the bot can edit this administrator's privileges.
58    pub can_be_edited: bool,
59    /// `true` if the admin's identity is hidden.
60    pub is_anonymous: bool,
61    /// Allows managing the chat.
62    pub can_manage_chat: bool,
63    /// Allows deleting messages of other users.
64    pub can_delete_messages: bool,
65    /// Allows managing video chats.
66    pub can_manage_video_chats: bool,
67    /// Allows restricting, banning, or unbanning members.
68    pub can_restrict_members: bool,
69    /// Allows the admin to add new administrators with a subset of their own privileges or demote administrators that they have promoted, directly or indirectly (promoted by administrators that were appointed by the user).
70    pub can_promote_members: bool,
71    /// Allows changing the chat title, photo, and other settings.
72    pub can_change_info: bool,
73    /// Allows inviting new users.
74    pub can_invite_users: bool,
75    #[serde(default)]
76    /// Allows posting messages in channels.
77    pub can_post_messages: bool,
78    #[serde(default)]
79    /// Allows editing messages in channels.
80    pub can_edit_messages: bool,
81    /// Allows pinning messages.
82    #[serde(default)]
83    pub can_pin_messages: bool,
84    /// Allows managing forum topics.
85    #[serde(default)]
86    pub can_manage_topics: bool,
87    /// Allows posting stories.
88    #[serde(default)]
89    pub can_post_stories: bool,
90    /// Allows editing stories.
91    #[serde(default)]
92    pub can_edit_stories: bool,
93    /// Allows deleting stories.
94    #[serde(default)]
95    pub can_delete_stories: bool,
96    /// Allows managing direct messages.
97    #[serde(default)]
98    pub can_manage_direct_messages: bool,
99    /// Allows managing tags.
100    #[serde(default)]
101    pub can_manage_tags: bool,
102    /// Custom title shown instead of "Administrator".
103    #[serde(skip_serializing_if = "Option::is_none")]
104    pub custom_title: Option<String>,
105}
106
107/// Regular chat member.
108#[derive(Debug, Clone, Serialize, Deserialize)]
109pub struct ChatMemberMember {
110    /// The user.
111    pub user: User,
112    /// Date when the membership expires, as a Unix timestamp. `0` means permanent.
113    #[serde(skip_serializing_if = "Option::is_none")]
114    pub until_date: Option<i64>,
115}
116
117/// Restricted chat member.
118#[derive(Debug, Clone, Serialize, Deserialize)]
119pub struct ChatMemberRestricted {
120    /// The user.
121    pub user: User,
122    /// `true` if the user is still a member of the chat.
123    pub is_member: bool,
124    /// The current permissions of the restricted user.
125    #[serde(flatten)]
126    pub permissions: ChatPermissions,
127    /// Unix timestamp when the restriction ends. `0` means permanent.
128    #[serde(skip_serializing_if = "Option::is_none")]
129    pub until_date: Option<i64>,
130}
131
132#[derive(Debug, Clone, Serialize, Deserialize)]
133/// A chat member who left or was removed from the chat.
134pub struct ChatMemberLeft {
135    /// The user.
136    pub user: User,
137}
138
139/// Banned (kicked) chat member.
140#[derive(Debug, Clone, Serialize, Deserialize)]
141pub struct ChatMemberBanned {
142    /// The user.
143    pub user: User,
144    /// Unix timestamp when the ban ends. `0` means permanent.
145    #[serde(skip_serializing_if = "Option::is_none")]
146    pub until_date: Option<i64>,
147}