1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use crate::Client;
use crate::errors::Result;
use crate::raw::{base, functions};
use crate::types::{ChatPermissions, PeerRef};
/// Request built by [`Client::restrict_chat_member`].
#[must_use = "a request does nothing until it is awaited"]
pub struct RestrictChatMember<'a> {
client: &'a Client,
chat_id: PeerRef,
user_id: PeerRef,
permissions: ChatPermissions,
until_date: i32,
}
impl Client {
/// Limits what a member can do in a supergroup. Pass `ChatPermissions::default()` to mute the user.
///
/// Every permission is set on its own, like the Bot API's `use_independent_chat_permissions`: allowing other
/// messages, link previews or polls does not also allow text messages or media. Set those fields together if
/// one should imply the other.
///
/// Basic groups have no per-user restrictions, so a basic group fails with `Error::InvalidArgument` and nothing
/// is sent. Restrict every member there with [`Client::set_chat_permissions`], or upgrade the group to a
/// supergroup first.
pub fn restrict_chat_member(
&self,
chat_id: impl Into<PeerRef>,
user_id: impl Into<PeerRef>,
permissions: ChatPermissions,
) -> RestrictChatMember<'_> {
RestrictChatMember {
client: self,
chat_id: chat_id.into(),
user_id: user_id.into(),
permissions,
until_date: 0,
}
}
}
impl RestrictChatMember<'_> {
/// Lifts the restriction at this unix time. By default it never ends.
pub fn until_date(mut self, until_date: i32) -> Self {
self.until_date = until_date;
self
}
async fn send(self) -> Result<()> {
let client = self.client;
let request = functions::channels::EditBanned {
channel: client.resolve_channel(&self.chat_id).await?,
participant: client.resolve_peer(self.user_id).await?,
banned_rights: base::ChatBannedRights::ChatBannedRights(Box::new(self.permissions.write(self.until_date))),
};
client.invoke(&request).await?;
Ok(())
}
}
into_future!(RestrictChatMember, ());