synpad 0.1.0

A full-featured Matrix chat client built with Dioxus
use matrix_sdk::Client;
use matrix_sdk::encryption::backups::BackupState;

/// Key backup state.
#[derive(Clone, Debug, PartialEq)]
pub enum KeyBackupState {
    /// No backup configured
    NotSetUp,
    /// Backup is being created
    Creating,
    /// Backup is active and up to date
    Active,
    /// Backup exists but keys need to be restored
    NeedsRestore,
    /// Error with backup
    Error(String),
}

impl Default for KeyBackupState {
    fn default() -> Self {
        Self::NotSetUp
    }
}

/// Check the current key backup status.
pub fn get_backup_state(client: &Client) -> KeyBackupState {
    let state = client.encryption().backups().state();
    match state {
        BackupState::Unknown => KeyBackupState::NotSetUp,
        BackupState::Enabled => KeyBackupState::Active,
        BackupState::Creating => KeyBackupState::Creating,
        BackupState::Resuming => KeyBackupState::Active,
        BackupState::Disabling => KeyBackupState::NotSetUp,
        BackupState::Downloading => KeyBackupState::NeedsRestore,
        _ => KeyBackupState::NotSetUp,
    }
}

/// Enable key backup. Creates a new backup if none exists.
pub async fn enable_backup(client: &Client) -> Result<(), String> {
    let backups = client.encryption().backups();

    tracing::info!("Enabling key backup...");

    backups
        .create()
        .await
        .map_err(|e| format!("Failed to create key backup: {e}"))?;

    tracing::info!("Key backup enabled successfully");
    Ok(())
}

/// Check if a backup exists on the server.
pub async fn backup_exists_on_server(client: &Client) -> Result<bool, String> {
    let backups = client.encryption().backups();
    let exists = backups.exists_on_server().await
        .map_err(|e| format!("Failed to check backup: {e}"))?;
    Ok(exists)
}