vaegram 0.3.0

Telegram MTProto client library for users and bots
Documentation
use super::enable_cloud_password::new_settings;
use crate::Client;
use crate::crypto::random;
use crate::errors::Result;
use crate::methods::accepted;
use crate::raw::{base, functions};
use crate::utils::compute_password_check;

#[must_use = "a request does nothing until it is awaited"]
/// Request built by [`Client::change_cloud_password`].
pub struct ChangeCloudPassword<'a> {
    client: &'a Client,
    current: String,
    new: String,
    hint: Option<String>,
}

impl Client {
    /// Replaces the cloud password with `new_password`, given the current one.
    ///
    /// An empty `new_password` fails with `Error::InvalidArgument`.
    pub fn change_cloud_password(
        &self,
        current_password: impl Into<String>,
        new_password: impl Into<String>,
    ) -> ChangeCloudPassword<'_> {
        ChangeCloudPassword { client: self, current: current_password.into(), new: new_password.into(), hint: None }
    }
}

impl ChangeCloudPassword<'_> {
    /// Hint shown to whoever has to enter the new password.
    pub fn hint(mut self, hint: impl Into<String>) -> Self {
        self.hint = Some(hint.into());
        self
    }

    async fn send(self) -> Result<()> {
        let base::account::Password::Password(p) = self.client.invoke(&functions::account::GetPassword {}).await?;
        let request = functions::account::UpdatePasswordSettings {
            password: compute_password_check(&p, &self.current)?,
            new_settings: new_settings(&p, &self.new, self.hint, None, &random())?,
        };
        accepted(self.client.invoke(&request).await?)
    }
}

into_future!(ChangeCloudPassword, ());