Skip to main content

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