vaegram 0.2.0

Telegram MTProto client library for users and bots
Documentation
use crate::Client;
use crate::errors::{Error, Result};
use crate::raw::{base, functions, types};

/// Request built by [`Client::upgrade_gift`].
#[must_use = "a request does nothing until it is awaited"]
pub struct UpgradeGift<'a> {
    client: &'a Client,
    owned_gift_id: String,
    keep_original_details: bool,
    pay_for_upgrade: bool,
    connection_id: Option<String>,
}

impl Client {
    /// Upgrades a received gift to a unique gift.
    ///
    /// `owned_gift_id` is the `owned_gift_id` of an owned gift, or a unique gift's slug. Awaiting fails with
    /// `Error::InvalidArgument` if it is empty.
    pub fn upgrade_gift(&self, owned_gift_id: impl Into<String>) -> UpgradeGift<'_> {
        UpgradeGift {
            client: self,
            owned_gift_id: owned_gift_id.into(),
            keep_original_details: false,
            pay_for_upgrade: false,
            connection_id: None,
        }
    }
}

impl UpgradeGift<'_> {
    /// Keeps the original sender, text and date on the upgraded gift.
    pub fn keep_original_details(mut self) -> Self {
        self.keep_original_details = true;
        self
    }

    /// Pays the upgrade price in Stars. Needed unless the sender prepaid the upgrade.
    pub fn pay_for_upgrade(mut self) -> Self {
        self.pay_for_upgrade = true;
        self
    }

    /// Upgrades the gift of the business account behind this connection.
    pub fn business_connection_id(mut self, id: impl Into<String>) -> Self {
        self.connection_id = Some(id.into());
        self
    }

    async fn send(self) -> Result<()> {
        if self.owned_gift_id.is_empty() {
            return Err(Error::InvalidArgument("owned_gift_id is empty"));
        }
        let client = self.client;
        let stargift = client.input_saved_gift(&self.owned_gift_id).await?;
        let keep_original_details = self.keep_original_details;
        if self.pay_for_upgrade {
            let invoice = types::InputInvoiceStarGiftUpgrade { keep_original_details, stargift };
            let invoice = base::InputInvoice::InputInvoiceStarGiftUpgrade(Box::new(invoice));
            return client.pay_with_stars(self.connection_id, invoice).await;
        }
        let request = functions::payments::UpgradeStarGift { keep_original_details, stargift };
        client.invoke_business(self.connection_id.as_deref(), request).await.map(drop)
    }
}

into_future!(UpgradeGift, ());