tgbot/types/definitions/chat/
permissions.rs

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