Skip to main content

rustigram_types/
chat_member.rs

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