vaegram 0.2.0

Telegram MTProto client library for users and bots
Documentation
use crate::Client;
use crate::errors::Result;
use crate::raw::functions;
use crate::types::PeerRef;
use crate::utils::CHANNEL_OFFSET;

/// Request built by [`Client::delete_chat_history`]. Awaiting it returns `()`.
#[must_use = "a request does nothing until it is awaited"]
pub struct DeleteChatHistory<'a> {
    client: &'a Client,
    chat_id: PeerRef,
    max_id: i32,
    revoke: bool,
    just_clear: bool,
}

impl Client {
    /// Deletes the message history of a chat.
    ///
    /// Without options it deletes every message, only for you.
    pub fn delete_chat_history(&self, chat_id: impl Into<PeerRef>) -> DeleteChatHistory<'_> {
        DeleteChatHistory { client: self, chat_id: chat_id.into(), max_id: 0, revoke: false, just_clear: false }
    }
}

impl DeleteChatHistory<'_> {
    /// Deletes only messages with an id up to `max_id`. The default 0 deletes everything.
    pub fn max_id(mut self, max_id: i32) -> Self {
        self.max_id = max_id;
        self
    }

    /// Deletes the messages for the other participants too.
    pub fn revoke(mut self) -> Self {
        self.revoke = true;
        self
    }

    /// Clears the messages but keeps the chat in your chat list. Ignored for channels and supergroups.
    pub fn just_clear(mut self) -> Self {
        self.just_clear = true;
        self
    }
}

into_future!(DeleteChatHistory, ());

impl DeleteChatHistory<'_> {
    async fn send(self) -> Result<()> {
        let client = self.client;
        let (chat_id, peer) = client.resolve(&self.chat_id).await?;
        if chat_id <= -CHANNEL_OFFSET {
            let channel = client.resolve_channel(&chat_id.into()).await?;
            let request =
                functions::channels::DeleteHistory { for_everyone: self.revoke, channel, max_id: self.max_id };
            client.invoke(&request).await?;
            client.0.caches.evict_chat(chat_id);
            return Ok(());
        }
        let request = functions::messages::DeleteHistory {
            just_clear: self.just_clear,
            revoke: self.revoke,
            peer,
            max_id: self.max_id,
            min_date: None,
            max_date: None,
        };
        client.affected_history(chat_id, &request).await?;
        client.0.caches.evict_chat(chat_id);
        Ok(())
    }
}