Skip to main content

rustigram_api/methods/
chat_management.rs

1use crate::client::BotClient;
2use crate::error::Result;
3use rustigram_types::chat::{ChatInviteLink, ChatPermissions};
4use rustigram_types::user::ChatId;
5use serde::Serialize;
6use std::future::{Future, IntoFuture};
7use std::pin::Pin;
8
9// ─── banChatMember ────────────────────────────────────────────────────────────
10
11#[derive(Serialize)]
12struct BanChatMemberParams {
13    chat_id: ChatId,
14    user_id: i64,
15    #[serde(skip_serializing_if = "Option::is_none")]
16    until_date: Option<i64>,
17    #[serde(skip_serializing_if = "Option::is_none")]
18    revoke_messages: Option<bool>,
19}
20
21/// Builder for the [`banChatMember`](https://core.telegram.org/bots/api#banchatmember) method.
22pub struct BanChatMember {
23    client: BotClient,
24    params: BanChatMemberParams,
25}
26
27impl BanChatMember {
28    pub(crate) fn new(client: BotClient, chat_id: impl Into<ChatId>, user_id: i64) -> Self {
29        Self {
30            client,
31            params: BanChatMemberParams {
32                chat_id: chat_id.into(),
33                user_id,
34                until_date: None,
35                revoke_messages: None,
36            },
37        }
38    }
39    /// Bans the user until this Unix timestamp. Omit or set to 0 for a permanent ban.
40    pub fn until_date(mut self, ts: i64) -> Self {
41        self.params.until_date = Some(ts);
42        self
43    }
44    /// Deletes all messages from this user in the chat on ban.
45    pub fn revoke_messages(mut self, v: bool) -> Self {
46        self.params.revoke_messages = Some(v);
47        self
48    }
49}
50
51impl IntoFuture for BanChatMember {
52    type Output = Result<bool>;
53    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
54    fn into_future(self) -> Self::IntoFuture {
55        Box::pin(async move { self.client.post_json("banChatMember", &self.params).await })
56    }
57}
58
59// ─── unbanChatMember ──────────────────────────────────────────────────────────
60
61#[derive(Serialize)]
62struct UnbanChatMemberParams {
63    chat_id: ChatId,
64    user_id: i64,
65    #[serde(skip_serializing_if = "Option::is_none")]
66    only_if_banned: Option<bool>,
67}
68
69/// Builder for the [`unbanChatMember`](https://core.telegram.org/bots/api#unbanchatmember) method.
70pub struct UnbanChatMember {
71    client: BotClient,
72    params: UnbanChatMemberParams,
73}
74
75impl UnbanChatMember {
76    pub(crate) fn new(client: BotClient, chat_id: impl Into<ChatId>, user_id: i64) -> Self {
77        Self {
78            client,
79            params: UnbanChatMemberParams {
80                chat_id: chat_id.into(),
81                user_id,
82                only_if_banned: None,
83            },
84        }
85    }
86    /// Only unbans the user if they are currently banned (ignores non-banned users).
87    pub fn only_if_banned(mut self, v: bool) -> Self {
88        self.params.only_if_banned = Some(v);
89        self
90    }
91}
92
93impl IntoFuture for UnbanChatMember {
94    type Output = Result<bool>;
95    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
96    fn into_future(self) -> Self::IntoFuture {
97        Box::pin(async move { self.client.post_json("unbanChatMember", &self.params).await })
98    }
99}
100
101// ─── restrictChatMember ───────────────────────────────────────────────────────
102
103#[derive(Serialize)]
104struct RestrictChatMemberParams {
105    chat_id: ChatId,
106    user_id: i64,
107    permissions: ChatPermissions,
108    #[serde(skip_serializing_if = "Option::is_none")]
109    use_independent_chat_permissions: Option<bool>,
110    #[serde(skip_serializing_if = "Option::is_none")]
111    until_date: Option<i64>,
112}
113
114/// Builder for the [`restrictChatMember`](https://core.telegram.org/bots/api#restrictchatmember) method.
115pub struct RestrictChatMember {
116    client: BotClient,
117    params: RestrictChatMemberParams,
118}
119
120impl RestrictChatMember {
121    pub(crate) fn new(
122        client: BotClient,
123        chat_id: impl Into<ChatId>,
124        user_id: i64,
125        permissions: ChatPermissions,
126    ) -> Self {
127        Self {
128            client,
129            params: RestrictChatMemberParams {
130                chat_id: chat_id.into(),
131                user_id,
132                permissions,
133                use_independent_chat_permissions: None,
134                until_date: None,
135            },
136        }
137    }
138    /// Restricts the user until this Unix timestamp. Omit or set to 0 for a permanent restriction.
139    pub fn until_date(mut self, ts: i64) -> Self {
140        self.params.until_date = Some(ts);
141        self
142    }
143    /// Sets whether to apply permissions independently (supergroups only).
144    pub fn use_independent_chat_permissions(mut self, v: bool) -> Self {
145        self.params.use_independent_chat_permissions = Some(v);
146        self
147    }
148}
149
150impl IntoFuture for RestrictChatMember {
151    type Output = Result<bool>;
152    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
153    fn into_future(self) -> Self::IntoFuture {
154        Box::pin(async move {
155            self.client
156                .post_json("restrictChatMember", &self.params)
157                .await
158        })
159    }
160}
161
162// ─── promoteChatMember ────────────────────────────────────────────────────────
163
164#[derive(Serialize)]
165/// Parameters for a `promoteChatMember` request.
166pub struct PromoteChatMemberParams {
167    chat_id: ChatId,
168    user_id: i64,
169    /// `true` to make the admin anonymous.
170    #[serde(skip_serializing_if = "Option::is_none")]
171    pub is_anonymous: Option<bool>,
172    /// Allows the admin to manage the chat.
173    #[serde(skip_serializing_if = "Option::is_none")]
174    pub can_manage_chat: Option<bool>,
175    /// Allows the admin to delete messages of other users.
176    #[serde(skip_serializing_if = "Option::is_none")]
177    pub can_delete_messages: Option<bool>,
178    /// Allows the admin to manage video chats.
179    #[serde(skip_serializing_if = "Option::is_none")]
180    pub can_manage_video_chats: Option<bool>,
181    /// Allows the admin to restrict, ban, or unban chat members.
182    #[serde(skip_serializing_if = "Option::is_none")]
183    pub can_restrict_members: Option<bool>,
184    /// Allows the admin to add new administrators.
185    #[serde(skip_serializing_if = "Option::is_none")]
186    pub can_promote_members: Option<bool>,
187    /// Allows the admin to change the chat title, photo, and other settings.
188    #[serde(skip_serializing_if = "Option::is_none")]
189    pub can_change_info: Option<bool>,
190    /// Allows the admin to invite new users to the chat.
191    #[serde(skip_serializing_if = "Option::is_none")]
192    pub can_invite_users: Option<bool>,
193    /// Allows the admin to post messages in channels.
194    #[serde(skip_serializing_if = "Option::is_none")]
195    pub can_post_messages: Option<bool>,
196    /// Allows the admin to edit messages in channels.
197    #[serde(skip_serializing_if = "Option::is_none")]
198    pub can_edit_messages: Option<bool>,
199    /// Allows the admin to pin messages.
200    #[serde(skip_serializing_if = "Option::is_none")]
201    pub can_pin_messages: Option<bool>,
202    /// Allows the admin to manage forum topics.
203    #[serde(skip_serializing_if = "Option::is_none")]
204    pub can_manage_topics: Option<bool>,
205    /// Allows the admin to post stories.
206    #[serde(skip_serializing_if = "Option::is_none")]
207    pub can_post_stories: Option<bool>,
208    /// Allows the admin to edit stories posted by others.
209    #[serde(skip_serializing_if = "Option::is_none")]
210    pub can_edit_stories: Option<bool>,
211    /// Allows the admin to delete stories.
212    #[serde(skip_serializing_if = "Option::is_none")]
213    pub can_delete_stories: Option<bool>,
214    /// Allows the admin to manage direct messages.
215    #[serde(skip_serializing_if = "Option::is_none")]
216    pub can_manage_direct_messages: Option<bool>,
217    /// Allows the admin to manage tags.
218    #[serde(skip_serializing_if = "Option::is_none")]
219    pub can_manage_tags: Option<bool>,
220}
221
222/// Builder for the [`promoteChatMember`](https://core.telegram.org/bots/api#promotechatmember) method.
223pub struct PromoteChatMember {
224    client: BotClient,
225    params: PromoteChatMemberParams,
226}
227
228impl PromoteChatMember {
229    pub(crate) fn new(client: BotClient, chat_id: impl Into<ChatId>, user_id: i64) -> Self {
230        Self {
231            client,
232            params: PromoteChatMemberParams {
233                chat_id: chat_id.into(),
234                user_id,
235                is_anonymous: None,
236                can_manage_chat: None,
237                can_delete_messages: None,
238                can_manage_video_chats: None,
239                can_restrict_members: None,
240                can_promote_members: None,
241                can_change_info: None,
242                can_invite_users: None,
243                can_post_messages: None,
244                can_edit_messages: None,
245                can_pin_messages: None,
246                can_manage_topics: None,
247                can_post_stories: None,
248                can_edit_stories: None,
249                can_delete_stories: None,
250                can_manage_direct_messages: None,
251                can_manage_tags: None,
252            },
253        }
254    }
255    /// Allows the admin to manage the chat.
256    pub fn can_manage_chat(mut self, v: bool) -> Self {
257        self.params.can_manage_chat = Some(v);
258        self
259    }
260    /// Allows the admin to delete messages of other users.
261    pub fn can_delete_messages(mut self, v: bool) -> Self {
262        self.params.can_delete_messages = Some(v);
263        self
264    }
265    /// Allows the admin to manage video chats.
266    pub fn can_manage_video_chats(mut self, v: bool) -> Self {
267        self.params.can_manage_video_chats = Some(v);
268        self
269    }
270    /// Allows the admin to restrict, ban, or unban chat members.
271    pub fn can_restrict_members(mut self, v: bool) -> Self {
272        self.params.can_restrict_members = Some(v);
273        self
274    }
275    /// Allows the admin to add new administrators with a subset of their own privileges or demote administrators that they have promoted, directly or indirectly (promoted by administrators that were appointed by the user).
276    pub fn can_promote_members(mut self, v: bool) -> Self {
277        self.params.can_promote_members = Some(v);
278        self
279    }
280    /// Allows the admin to change the chat title, photo, and other settings.
281    pub fn can_change_info(mut self, v: bool) -> Self {
282        self.params.can_change_info = Some(v);
283        self
284    }
285    /// Allows the admin to invite new users to the chat.
286    pub fn can_invite_users(mut self, v: bool) -> Self {
287        self.params.can_invite_users = Some(v);
288        self
289    }
290    /// Allows the admin to pin messages.
291    pub fn can_pin_messages(mut self, v: bool) -> Self {
292        self.params.can_pin_messages = Some(v);
293        self
294    }
295    /// Allows the admin to manage forum topics.
296    pub fn can_manage_topics(mut self, v: bool) -> Self {
297        self.params.can_manage_topics = Some(v);
298        self
299    }
300    /// Allows the admin to post stories.
301    pub fn can_post_stories(mut self, v: bool) -> Self {
302        self.params.can_post_stories = Some(v);
303        self
304    }
305    /// Allows the admin to manage direct messages.
306    pub fn can_manage_direct_messages(mut self, v: bool) -> Self {
307        self.params.can_manage_direct_messages = Some(v);
308        self
309    }
310    /// Allows the admin to manage tags.
311    pub fn can_manage_tags(mut self, v: bool) -> Self {
312        self.params.can_manage_tags = Some(v);
313        self
314    }
315}
316
317impl IntoFuture for PromoteChatMember {
318    type Output = Result<bool>;
319    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
320    fn into_future(self) -> Self::IntoFuture {
321        Box::pin(async move {
322            self.client
323                .post_json("promoteChatMember", &self.params)
324                .await
325        })
326    }
327}
328
329// ─── createChatInviteLink ─────────────────────────────────────────────────────
330
331#[derive(Serialize)]
332struct CreateChatInviteLinkParams {
333    chat_id: ChatId,
334    #[serde(skip_serializing_if = "Option::is_none")]
335    name: Option<String>,
336    #[serde(skip_serializing_if = "Option::is_none")]
337    expire_date: Option<i64>,
338    #[serde(skip_serializing_if = "Option::is_none")]
339    member_limit: Option<u32>,
340    #[serde(skip_serializing_if = "Option::is_none")]
341    creates_join_request: Option<bool>,
342}
343
344/// Builder for the [`createChatInviteLink`](https://core.telegram.org/bots/api#createchatinvitelink) method.
345pub struct CreateChatInviteLink {
346    client: BotClient,
347    params: CreateChatInviteLinkParams,
348}
349
350impl CreateChatInviteLink {
351    pub(crate) fn new(client: BotClient, chat_id: impl Into<ChatId>) -> Self {
352        Self {
353            client,
354            params: CreateChatInviteLinkParams {
355                chat_id: chat_id.into(),
356                name: None,
357                expire_date: None,
358                member_limit: None,
359                creates_join_request: None,
360            },
361        }
362    }
363    /// Sets the name of the invite link (up to 32 characters).
364    pub fn name(mut self, n: impl Into<String>) -> Self {
365        self.params.name = Some(n.into());
366        self
367    }
368    /// Sets the Unix timestamp when the invite link expires.
369    pub fn expire_date(mut self, ts: i64) -> Self {
370        self.params.expire_date = Some(ts);
371        self
372    }
373    /// Limits how many users can join via this link.
374    pub fn member_limit(mut self, n: u32) -> Self {
375        self.params.member_limit = Some(n);
376        self
377    }
378    /// Makes the link require admin approval for each join request.
379    pub fn creates_join_request(mut self, v: bool) -> Self {
380        self.params.creates_join_request = Some(v);
381        self
382    }
383}
384
385impl IntoFuture for CreateChatInviteLink {
386    type Output = Result<ChatInviteLink>;
387    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
388    fn into_future(self) -> Self::IntoFuture {
389        Box::pin(async move {
390            self.client
391                .post_json("createChatInviteLink", &self.params)
392                .await
393        })
394    }
395}
396
397// ─── pinChatMessage ───────────────────────────────────────────────────────────
398
399#[derive(Serialize)]
400struct PinChatMessageParams {
401    chat_id: ChatId,
402    message_id: i64,
403    #[serde(skip_serializing_if = "Option::is_none")]
404    disable_notification: Option<bool>,
405}
406
407/// Builder for the [`pinChatMessage`](https://core.telegram.org/bots/api#pinchatmessage) method.
408pub struct PinChatMessage {
409    client: BotClient,
410    params: PinChatMessageParams,
411}
412
413impl PinChatMessage {
414    pub(crate) fn new(client: BotClient, chat_id: impl Into<ChatId>, message_id: i64) -> Self {
415        Self {
416            client,
417            params: PinChatMessageParams {
418                chat_id: chat_id.into(),
419                message_id,
420                disable_notification: None,
421            },
422        }
423    }
424    /// Pins the message without notifying members.
425    pub fn disable_notification(mut self, v: bool) -> Self {
426        self.params.disable_notification = Some(v);
427        self
428    }
429}
430
431impl IntoFuture for PinChatMessage {
432    type Output = Result<bool>;
433    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
434    fn into_future(self) -> Self::IntoFuture {
435        Box::pin(async move { self.client.post_json("pinChatMessage", &self.params).await })
436    }
437}
438
439// ─── unpinChatMessage ─────────────────────────────────────────────────────────
440
441#[derive(Serialize)]
442struct UnpinChatMessageParams {
443    chat_id: ChatId,
444    #[serde(skip_serializing_if = "Option::is_none")]
445    message_id: Option<i64>,
446}
447
448/// Builder for the [`unpinChatMessage`](https://core.telegram.org/bots/api#unpinchatmessage) method.
449pub struct UnpinChatMessage {
450    client: BotClient,
451    params: UnpinChatMessageParams,
452}
453
454impl UnpinChatMessage {
455    pub(crate) fn new(client: BotClient, chat_id: impl Into<ChatId>) -> Self {
456        Self {
457            client,
458            params: UnpinChatMessageParams {
459                chat_id: chat_id.into(),
460                message_id: None,
461            },
462        }
463    }
464    /// Unpins a specific message. Omit to unpin the most recent pinned message.
465    pub fn message_id(mut self, id: i64) -> Self {
466        self.params.message_id = Some(id);
467        self
468    }
469}
470
471impl IntoFuture for UnpinChatMessage {
472    type Output = Result<bool>;
473    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
474    fn into_future(self) -> Self::IntoFuture {
475        Box::pin(async move {
476            self.client
477                .post_json("unpinChatMessage", &self.params)
478                .await
479        })
480    }
481}