vaegram 0.2.0

Telegram MTProto client library for users and bots
Documentation
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, ());