tgbot/types/chat/permissions/
mod.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{
4    api::{Method, Payload},
5    types::ChatId,
6};
7
8#[cfg(test)]
9mod tests;
10
11/// Represents the rights of an administrator in a chat.
12#[serde_with::skip_serializing_none]
13#[derive(Clone, Copy, Debug, Default, Deserialize, PartialEq, PartialOrd, Serialize)]
14pub struct ChatAdministratorRights {
15    /// Indicates whether the user is allowed to change the chat title, photo and other settings.
16    pub can_change_info: bool,
17    /// Indicates whether the administrator can delete messages of other users.
18    pub can_delete_messages: bool,
19    /// Indicates whether the administrator can delete stories posted by other users.
20    pub can_delete_stories: Option<bool>,
21    /// Indicates whether the administrator can edit messages of other users
22    /// and can pin messages; channels only.
23    pub can_edit_messages: Option<bool>,
24    /// Indicates whether the administrator can edit stories posted by other users.
25    pub can_edit_stories: Option<bool>,
26    /// Indicates whether the user is allowed to invite new users to the chat.
27    pub can_invite_users: bool,
28    /// Indicates whether the administrator can
29    /// access the chat event log, boost list in channels,
30    /// see channel members, report spam messages,
31    /// see anonymous administrators in supergroups and ignore slow mode.
32    ///
33    /// Implied by any other administrator privilege.
34    pub can_manage_chat: bool,
35    /// Indicates whether the user is allowed to create, rename,
36    /// close, and reopen forum topics; supergroups only.
37    pub can_manage_topics: Option<bool>,
38    /// Indicates whether the administrator can manage video chats.
39    pub can_manage_video_chats: bool,
40    /// Indicates whether the user is allowed to pin messages; groups and supergroups only.
41    pub can_pin_messages: Option<bool>,
42    /// Indicates whether the administrator can post messages in the channel,
43    /// or access channel statistics; channels only.
44    pub can_post_messages: Option<bool>,
45    /// Indicates whether the administrator can post stories in the chat.
46    pub can_post_stories: Option<bool>,
47    /// Indicates whether the administrator can
48    /// add new administrators with a subset of their own privileges
49    /// or demote administrators that they have promoted,
50    /// directly or indirectly (promoted by administrators that were appointed by the user).
51    pub can_promote_members: bool,
52    /// Indicates whether the administrator can restrict, ban or unban chat members,
53    /// or access supergroup statistics.
54    pub can_restrict_members: bool,
55    /// Indicates whether the user's presence in the chat is hidden.
56    pub is_anonymous: bool,
57}
58
59impl ChatAdministratorRights {
60    /// Creates a new `ChatAdministratorRights` with all flags set to `true`.
61    ///
62    /// To create an object with all flags set to `false` use [`Self::default`] method.
63    pub fn all() -> Self {
64        Self {
65            can_change_info: true,
66            can_delete_messages: true,
67            can_delete_stories: Some(true),
68            can_edit_messages: Some(true),
69            can_edit_stories: Some(true),
70            can_invite_users: true,
71            can_manage_chat: true,
72            can_manage_topics: Some(true),
73            can_manage_video_chats: true,
74            can_pin_messages: Some(true),
75            can_post_messages: Some(true),
76            can_post_stories: Some(true),
77            can_promote_members: true,
78            can_restrict_members: true,
79            is_anonymous: true,
80        }
81    }
82
83    /// Sets a new value for the `can_change_info` flag.
84    ///
85    /// # Arguments
86    ///
87    /// * `value` - Indicates whether the user is allowed
88    ///   to change the chat title,
89    ///   photo and other settings.
90    pub fn with_can_change_info(mut self, value: bool) -> Self {
91        self.can_change_info = value;
92        self
93    }
94
95    /// Sets a new value for the `can_delete_messages` flag.
96    ///
97    /// # Arguments
98    ///
99    /// * `value` - Indicates whether the administrator can
100    ///   delete messages of other users.
101    pub fn with_can_delete_messages(mut self, value: bool) -> Self {
102        self.can_delete_messages = value;
103        self
104    }
105
106    /// Sets a new value for the `can_delete_stories` flag.
107    ///
108    /// # Arguments
109    ///
110    /// * `value` - Indicates whether the administrator can
111    ///   delete stories posted by other users;
112    ///   channels only.
113    pub fn with_can_delete_stories(mut self, value: bool) -> Self {
114        self.can_delete_stories = Some(value);
115        self
116    }
117
118    /// Sets a new value for the `can_edit_messages` flag.
119    ///
120    /// # Arguments
121    ///
122    /// * `value` - Indicates whether the administrator can
123    ///   edit messages of other users and can pin messages;
124    ///   channels only.
125    pub fn with_can_edit_messages(mut self, value: bool) -> Self {
126        self.can_edit_messages = Some(value);
127        self
128    }
129
130    /// Sets a new value for the `can_edit_stories` flag.
131    ///
132    /// # Arguments
133    ///
134    /// * `value` - Indicates whether the administrator can
135    ///   edit stories posted by other users;
136    ///   channels only.
137    pub fn with_can_edit_stories(mut self, value: bool) -> Self {
138        self.can_edit_stories = Some(value);
139        self
140    }
141
142    /// Sets a new value for the `can_invite_users` flag.
143    ///
144    /// # Arguments
145    ///
146    /// * `value` - Indicates whether the user is allowed to
147    ///   invite new users to the chat.
148    pub fn with_can_invite_users(mut self, value: bool) -> Self {
149        self.can_invite_users = value;
150        self
151    }
152
153    /// Sets a new value for the `can_manage_chat` flag.
154    ///
155    /// # Arguments
156    ///
157    /// * `value` - Indicates whether the administrator can
158    ///   access the chat event log, boost list in channels,
159    ///   see channel members, report spam messages,
160    ///   see anonymous administrators in supergroups and ignore slow mode.
161    pub fn with_can_manage_chat(mut self, value: bool) -> Self {
162        self.can_manage_chat = value;
163        self
164    }
165
166    /// Sets a new value for the `can_manage_topics` flag.
167    ///
168    /// # Arguments
169    ///
170    /// * `value` - Indicates whether the user is allowed to
171    ///   create, rename, close, and reopen forum topics;
172    ///   supergroups only.
173    pub fn with_can_manage_topics(mut self, value: bool) -> Self {
174        self.can_manage_topics = Some(value);
175        self
176    }
177
178    /// Sets a new value for the `can_manage_video_chats` flag.
179    ///
180    /// # Arguments
181    ///
182    /// * `value` - Indicates whether the administrator can
183    ///   manage video chats.
184    pub fn with_can_manage_video_chats(mut self, value: bool) -> Self {
185        self.can_manage_video_chats = value;
186        self
187    }
188
189    /// Sets a new value for the `can_pin_messages` flag.
190    ///
191    /// # Arguments
192    ///
193    /// * `value` - Indicates whether the user is allowed to
194    ///   pin messages;
195    ///   groups and supergroups only.
196    pub fn with_can_pin_messages(mut self, value: bool) -> Self {
197        self.can_pin_messages = Some(value);
198        self
199    }
200
201    /// Sets a new value for the `can_post_messages` flag.
202    ///
203    /// # Arguments
204    ///
205    /// * `value` - Indicates whether the administrator can
206    ///   post messages in the channel,
207    ///   or access channel statistics;
208    ///   channels only.
209    pub fn with_can_post_messages(mut self, value: bool) -> Self {
210        self.can_post_messages = Some(value);
211        self
212    }
213
214    /// Sets a new value for the `can_post_stories` flag.
215    ///
216    /// # Arguments
217    ///
218    /// * `value` - Indicates whether the administrator can
219    ///   post stories in the channel;
220    ///   channels only.
221    pub fn with_can_post_stories(mut self, value: bool) -> Self {
222        self.can_post_stories = Some(value);
223        self
224    }
225
226    /// Sets a new value for the `can_promote_members` flag.
227    ///
228    /// # Arguments
229    ///
230    /// * `value` - Indicates whether the administrator can
231    ///   add new administrators with a subset of their own privileges
232    ///   or demote administrators that they have promoted,
233    ///   directly or indirectly (promoted by administrators that
234    ///   were appointed by the user).
235    pub fn with_can_promote_members(mut self, value: bool) -> Self {
236        self.can_promote_members = value;
237        self
238    }
239
240    /// Sets a new value for the `can_restrict_members` flag.
241    ///
242    /// # Arguments
243    ///
244    /// * `value` - Indicates whether the administrator can
245    ///   restrict, ban or unban chat members,
246    ///   or access supergroup statistics.
247    pub fn with_can_restrict_members(mut self, value: bool) -> Self {
248        self.can_restrict_members = value;
249        self
250    }
251
252    /// Sets a new value for the `is_anonymous` flag.
253    ///
254    /// # Arguments
255    ///
256    /// * `value` - Indicates whether the user's presence in the chat is hidden.
257    pub fn with_is_anonymous(mut self, value: bool) -> Self {
258        self.is_anonymous = value;
259        self
260    }
261}
262
263/// Represents actions that a non-administrator user is allowed to take in a chat.
264#[serde_with::skip_serializing_none]
265#[derive(Clone, Copy, Debug, Default, Deserialize, PartialEq, PartialOrd, Serialize)]
266pub struct ChatPermissions {
267    /// Indicates whether the user is allowed to add web page previews to their messages.
268    pub can_add_web_page_previews: Option<bool>,
269    /// Indicates whether the user is allowed to change the chat title, photo and other settings.
270    ///
271    /// Ignored in public supergroups.
272    pub can_change_info: Option<bool>,
273    /// Indicates whether the user is allowed to invite new users to the chat.
274    pub can_invite_users: Option<bool>,
275    /// Indicates whether the user is allowed to create forum topics.
276    ///
277    /// If omitted defaults to the value of `can_pin_messages`.
278    pub can_manage_topics: Option<bool>,
279    /// Indicates whether the user is allowed to pin messages.
280    ///
281    /// Ignored in public supergroups.
282    pub can_pin_messages: Option<bool>,
283    /// Indicates whether the user is allowed to send audios.
284    pub can_send_audios: Option<bool>,
285    /// Indicates whether the user is allowed to send documents.
286    pub can_send_documents: Option<bool>,
287    /// Indicates whether the user is allowed to send text messages, contacts, locations and venues.
288    pub can_send_messages: Option<bool>,
289    /// Indicates whether the user is allowed to send animations,
290    /// games, stickers and use inline bots.
291    pub can_send_other_messages: Option<bool>,
292    /// Indicates whether the user is allowed to send photos.
293    pub can_send_photos: Option<bool>,
294    /// Indicates whether the user is allowed to send polls, implies `can_send_messages`.
295    pub can_send_polls: Option<bool>,
296    /// Indicates whether the user is allowed to send video notes.
297    pub can_send_video_notes: Option<bool>,
298    /// Indicates whether the user is allowed to send videos.
299    pub can_send_videos: Option<bool>,
300    /// Indicates whether the user is allowed to send voice notes.
301    pub can_send_voice_notes: Option<bool>,
302}
303
304impl ChatPermissions {
305    /// Restrict everything.
306    pub fn restricted() -> Self {
307        Self {
308            can_change_info: Some(false),
309            can_add_web_page_previews: Some(false),
310            can_invite_users: Some(false),
311            can_manage_topics: Some(false),
312            can_pin_messages: Some(false),
313            can_send_audios: Some(false),
314            can_send_documents: Some(false),
315            can_send_messages: Some(false),
316            can_send_other_messages: Some(false),
317            can_send_photos: Some(false),
318            can_send_polls: Some(false),
319            can_send_video_notes: Some(false),
320            can_send_videos: Some(false),
321            can_send_voice_notes: Some(false),
322        }
323    }
324
325    /// Allow everything.
326    pub fn allowed() -> Self {
327        Self {
328            can_add_web_page_previews: Some(true),
329            can_change_info: Some(true),
330            can_invite_users: Some(true),
331            can_manage_topics: Some(true),
332            can_pin_messages: Some(true),
333            can_send_audios: Some(true),
334            can_send_documents: Some(true),
335            can_send_messages: Some(true),
336            can_send_other_messages: Some(true),
337            can_send_photos: Some(true),
338            can_send_polls: Some(true),
339            can_send_video_notes: Some(true),
340            can_send_videos: Some(true),
341            can_send_voice_notes: Some(true),
342        }
343    }
344
345    /// Sets a new value for the `can_add_web_page_previews` flag.
346    ///
347    /// # Arguments
348    ///
349    /// * `value` - Permission add web page previews to messages.
350    pub fn with_can_add_web_page_previews(mut self, flag: bool) -> Self {
351        self.can_add_web_page_previews = Some(flag);
352        self
353    }
354
355    /// Sets a new value for the `can_change_info` flag.
356    ///
357    /// # Arguments
358    ///
359    /// * `value` - Permission to change the chat title, photo and other settings.
360    pub fn with_can_change_info(mut self, flag: bool) -> Self {
361        self.can_change_info = Some(flag);
362        self
363    }
364
365    /// Sets a new value for the `can_invite_users` flag.
366    ///
367    /// # Arguments
368    ///
369    /// * `value` - Permission to invite new users to the chat.
370    pub fn with_can_invite_users(mut self, flag: bool) -> Self {
371        self.can_invite_users = Some(flag);
372        self
373    }
374
375    /// Sets a new value for the `can_manage_topics` flag.
376    ///
377    /// # Arguments
378    ///
379    /// * `value` - Permission to manage topics.
380    pub fn with_can_manage_topics(mut self, flag: bool) -> Self {
381        self.can_manage_topics = Some(flag);
382        self
383    }
384
385    /// Sets a new value for the `can_pin_messages` flag.
386    ///
387    /// # Arguments
388    ///
389    /// * `value` - Permission to pin messages.
390    pub fn with_can_pin_messages(mut self, flag: bool) -> Self {
391        self.can_pin_messages = Some(flag);
392        self
393    }
394
395    /// Sets a new value for the `can_send_audios` flag.
396    ///
397    /// # Arguments
398    ///
399    /// * `value` - Permission to send audios.
400    pub fn with_can_send_audios(mut self, flag: bool) -> Self {
401        self.can_send_audios = Some(flag);
402        self
403    }
404
405    /// Sets a new value for the `can_send_documents` flag.
406    ///
407    /// # Arguments
408    ///
409    /// * `value` - Permission to send documents.
410    pub fn with_can_send_documents(mut self, flag: bool) -> Self {
411        self.can_send_documents = Some(flag);
412        self
413    }
414
415    /// Sets a new value for the `can_send_messages` flag.
416    ///
417    /// # Arguments
418    ///
419    /// * `value` - Permission to send text messages, contacts, locations and venues.
420    pub fn with_can_send_messages(mut self, flag: bool) -> Self {
421        self.can_send_messages = Some(flag);
422        self
423    }
424
425    /// Sets a new value for the `can_send_other_messages` flag.
426    ///
427    /// # Arguments
428    ///
429    /// * `value` - Permission to send animations, games, stickers and use inline bots.
430    pub fn with_can_send_other_messages(mut self, flag: bool) -> Self {
431        self.can_send_other_messages = Some(flag);
432        self
433    }
434
435    /// Sets a new value for the `can_send_photos` flag.
436    ///
437    /// # Arguments
438    ///
439    /// * `value` - Permission to send photos.
440    pub fn with_can_send_photos(mut self, flag: bool) -> Self {
441        self.can_send_photos = Some(flag);
442        self
443    }
444
445    /// Sets a new value for the `can_send_polls` flag.
446    ///
447    /// # Arguments
448    ///
449    /// * `value` - Permission to send polls.
450    pub fn with_can_send_polls(mut self, flag: bool) -> Self {
451        self.can_send_polls = Some(flag);
452        self
453    }
454
455    /// Sets a new value for the `can_send_video_notes` flag.
456    ///
457    /// # Arguments
458    ///
459    /// * `value` - Permission to send video notes.
460    pub fn with_can_send_video_notes(mut self, flag: bool) -> Self {
461        self.can_send_video_notes = Some(flag);
462        self
463    }
464
465    /// Sets a new value for the `can_send_videos` flag.
466    ///
467    /// # Arguments
468    ///
469    /// * `value` - Permission to send videos.
470    pub fn with_can_send_videos(mut self, flag: bool) -> Self {
471        self.can_send_videos = Some(flag);
472        self
473    }
474
475    /// Sets a new value for the `can_send_voice_notes` flag.
476    ///
477    /// # Arguments
478    ///
479    /// * `value` - Permission to send voice notes.
480    pub fn with_can_send_voice_notes(mut self, flag: bool) -> Self {
481        self.can_send_voice_notes = Some(flag);
482        self
483    }
484}
485
486/// Sets default chat permissions for all members.
487///
488/// The bot must be an administrator in the group or a supergroup
489/// for this to work and must have the `can_restrict_members` admin rights.
490#[serde_with::skip_serializing_none]
491#[derive(Clone, Debug, Serialize)]
492pub struct SetChatPermissions {
493    chat_id: ChatId,
494    permissions: ChatPermissions,
495    use_independent_chat_permissions: Option<bool>,
496}
497
498impl SetChatPermissions {
499    /// Creates a new `SetChatPermissions`
500    ///
501    /// # Arguments
502    ///
503    /// * `chat_id` - Unique identifier of the target chat.
504    /// * `permissions` - New permissions.
505    pub fn new<T>(chat_id: T, permissions: ChatPermissions) -> Self
506    where
507        T: Into<ChatId>,
508    {
509        SetChatPermissions {
510            chat_id: chat_id.into(),
511            permissions,
512            use_independent_chat_permissions: None,
513        }
514    }
515
516    /// Sets a new value for the `use_independent_chat_permissions` flag.
517    ///
518    /// # Arguments
519    ///
520    /// * `value` - Indicates whether the chat permissions are set independently.
521    ///
522    /// If `false`, the `can_send_other_messages` and `can_add_web_page_previews` permissions
523    /// will imply the `can_send_messages`, `can_send_audios`, `can_send_documents`, `can_send_photos`,
524    /// `can_send_videos`, `can_send_video_notes`, and `can_send_voice_notes` permissions;
525    /// the `can_send_polls` permission will imply the `can_send_messages` permission.
526    pub fn with_use_independent_chat_permissions(mut self, value: bool) -> Self {
527        self.use_independent_chat_permissions = Some(value);
528        self
529    }
530}
531
532impl Method for SetChatPermissions {
533    type Response = bool;
534
535    fn into_payload(self) -> Payload {
536        Payload::json("setChatPermissions", self)
537    }
538}