rust_tdlib/types/
chat_permissions.rs

1use crate::errors::Result;
2use crate::types::*;
3use uuid::Uuid;
4
5/// Describes actions that a user is allowed to take in a chat
6#[derive(Debug, Clone, Default, Serialize, Deserialize)]
7pub struct ChatPermissions {
8    #[doc(hidden)]
9    #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
10    extra: Option<String>,
11    #[serde(rename(serialize = "@client_id", deserialize = "@client_id"))]
12    client_id: Option<i32>,
13    /// True, if the user can send text messages, contacts, locations, and venues
14
15    #[serde(default)]
16    can_send_messages: bool,
17    /// True, if the user can send audio files, documents, photos, videos, video notes, and voice notes. Implies can_send_messages permissions
18
19    #[serde(default)]
20    can_send_media_messages: bool,
21    /// True, if the user can send polls. Implies can_send_messages permissions
22
23    #[serde(default)]
24    can_send_polls: bool,
25    /// True, if the user can send animations, games, stickers, and dice and use inline bots. Implies can_send_messages permissions
26
27    #[serde(default)]
28    can_send_other_messages: bool,
29    /// True, if the user may add a web page preview to their messages. Implies can_send_messages permissions
30
31    #[serde(default)]
32    can_add_web_page_previews: bool,
33    /// True, if the user can change the chat title, photo, and other settings
34
35    #[serde(default)]
36    can_change_info: bool,
37    /// True, if the user can invite new users to the chat
38
39    #[serde(default)]
40    can_invite_users: bool,
41    /// True, if the user can pin messages
42
43    #[serde(default)]
44    can_pin_messages: bool,
45}
46
47impl RObject for ChatPermissions {
48    #[doc(hidden)]
49    fn extra(&self) -> Option<&str> {
50        self.extra.as_deref()
51    }
52    #[doc(hidden)]
53    fn client_id(&self) -> Option<i32> {
54        self.client_id
55    }
56}
57
58impl ChatPermissions {
59    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
60        Ok(serde_json::from_str(json.as_ref())?)
61    }
62    pub fn builder() -> ChatPermissionsBuilder {
63        let mut inner = ChatPermissions::default();
64        inner.extra = Some(Uuid::new_v4().to_string());
65
66        ChatPermissionsBuilder { inner }
67    }
68
69    pub fn can_send_messages(&self) -> bool {
70        self.can_send_messages
71    }
72
73    pub fn can_send_media_messages(&self) -> bool {
74        self.can_send_media_messages
75    }
76
77    pub fn can_send_polls(&self) -> bool {
78        self.can_send_polls
79    }
80
81    pub fn can_send_other_messages(&self) -> bool {
82        self.can_send_other_messages
83    }
84
85    pub fn can_add_web_page_previews(&self) -> bool {
86        self.can_add_web_page_previews
87    }
88
89    pub fn can_change_info(&self) -> bool {
90        self.can_change_info
91    }
92
93    pub fn can_invite_users(&self) -> bool {
94        self.can_invite_users
95    }
96
97    pub fn can_pin_messages(&self) -> bool {
98        self.can_pin_messages
99    }
100}
101
102#[doc(hidden)]
103pub struct ChatPermissionsBuilder {
104    inner: ChatPermissions,
105}
106
107#[deprecated]
108pub type RTDChatPermissionsBuilder = ChatPermissionsBuilder;
109
110impl ChatPermissionsBuilder {
111    pub fn build(&self) -> ChatPermissions {
112        self.inner.clone()
113    }
114
115    pub fn can_send_messages(&mut self, can_send_messages: bool) -> &mut Self {
116        self.inner.can_send_messages = can_send_messages;
117        self
118    }
119
120    pub fn can_send_media_messages(&mut self, can_send_media_messages: bool) -> &mut Self {
121        self.inner.can_send_media_messages = can_send_media_messages;
122        self
123    }
124
125    pub fn can_send_polls(&mut self, can_send_polls: bool) -> &mut Self {
126        self.inner.can_send_polls = can_send_polls;
127        self
128    }
129
130    pub fn can_send_other_messages(&mut self, can_send_other_messages: bool) -> &mut Self {
131        self.inner.can_send_other_messages = can_send_other_messages;
132        self
133    }
134
135    pub fn can_add_web_page_previews(&mut self, can_add_web_page_previews: bool) -> &mut Self {
136        self.inner.can_add_web_page_previews = can_add_web_page_previews;
137        self
138    }
139
140    pub fn can_change_info(&mut self, can_change_info: bool) -> &mut Self {
141        self.inner.can_change_info = can_change_info;
142        self
143    }
144
145    pub fn can_invite_users(&mut self, can_invite_users: bool) -> &mut Self {
146        self.inner.can_invite_users = can_invite_users;
147        self
148    }
149
150    pub fn can_pin_messages(&mut self, can_pin_messages: bool) -> &mut Self {
151        self.inner.can_pin_messages = can_pin_messages;
152        self
153    }
154}
155
156impl AsRef<ChatPermissions> for ChatPermissions {
157    fn as_ref(&self) -> &ChatPermissions {
158        self
159    }
160}
161
162impl AsRef<ChatPermissions> for ChatPermissionsBuilder {
163    fn as_ref(&self) -> &ChatPermissions {
164        &self.inner
165    }
166}