Skip to content

Commit

Permalink
ffi
Browse files Browse the repository at this point in the history
  • Loading branch information
codabrink committed Jan 15, 2025
1 parent 6f0778d commit dd77f69
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
2 changes: 2 additions & 0 deletions bindings_ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ pub enum GenericError {
DeviceSync(#[from] xmtp_mls::groups::device_sync::DeviceSyncError),
#[error(transparent)]
Identity(#[from] xmtp_mls::identity::IdentityError),
#[error(transparent)]
JoinError(#[from] tokio::task::JoinError),
}

#[derive(uniffi::Error, thiserror::Error, Debug)]
Expand Down
43 changes: 43 additions & 0 deletions bindings_ffi/src/mls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use xmtp_id::{
},
InboxId,
};
use xmtp_mls::groups::device_sync::backup::BackupOptions;
use xmtp_mls::groups::device_sync::preference_sync::UserPreferenceUpdate;
use xmtp_mls::groups::scoped_client::LocalScopedGroupClient;
use xmtp_mls::groups::HmacKey;
Expand Down Expand Up @@ -49,6 +50,7 @@ use xmtp_mls::{
},
AbortHandle, GenericStreamHandle, StreamHandle,
};
use xmtp_proto::xmtp::device_sync::BackupElementSelection;
use xmtp_proto::xmtp::mls::message_contents::content_types::ReactionV2;
use xmtp_proto::xmtp::mls::message_contents::{DeviceSyncKind, EncodedContent};
pub type RustXmtpClient = MlsClient<TonicApiClient>;
Expand Down Expand Up @@ -556,6 +558,47 @@ impl FfiXmtpClient {
scw_verifier: self.inner_client.scw_verifier().clone().clone(),
}))
}

pub async fn backup(&self, path: String, opts: FfiBackupOptions) -> Result<(), GenericError> {
let provider = self.inner_client.mls_provider()?;
tokio::task::spawn_blocking(move || {
let opts: BackupOptions = opts.into();
opts.export_to_file(provider, path)
})
.await??;

Ok(())
}
}

#[derive(uniffi::Record)]
pub struct FfiBackupOptions {
start_ns: Option<i64>,
end_ns: Option<i64>,
elements: Vec<FfiBackupElementSelection>,
}
impl From<FfiBackupOptions> for BackupOptions {
fn from(value: FfiBackupOptions) -> Self {
Self {
start_ns: value.start_ns,
end_ns: value.start_ns,
elements: value.elements.into_iter().map(Into::into).collect(),
}
}
}

#[derive(uniffi::Enum)]
pub enum FfiBackupElementSelection {
Messages,
Consent,
}
impl From<FfiBackupElementSelection> for BackupElementSelection {
fn from(value: FfiBackupElementSelection) -> Self {
match value {
FfiBackupElementSelection::Consent => Self::Consent,
FfiBackupElementSelection::Messages => Self::Messages,
}
}
}

impl From<HmacKey> for FfiHmacKey {
Expand Down
28 changes: 25 additions & 3 deletions xmtp_mls/src/groups/device_sync/backup.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::{path::Path, sync::Arc};

use thiserror::Error;
use xmtp_common::time::now_ns;
use xmtp_proto::xmtp::device_sync::{BackupElementSelection, BackupMetadata};
Expand All @@ -9,16 +11,22 @@ mod backup_exporter;
mod backup_importer;
mod export_stream;

pub use backup_exporter::BackupExporter;

use crate::storage::xmtp_openmls_provider::XmtpOpenMlsProvider;

use super::DeviceSyncError;

#[derive(Debug, Error)]
pub enum BackupError {
#[error("Missing metadata")]
MissingMetadata,
}

pub struct BackupOptions {
start_ns: Option<i64>,
end_ns: Option<i64>,
elements: Vec<BackupElementSelection>,
pub start_ns: Option<i64>,
pub end_ns: Option<i64>,
pub elements: Vec<BackupElementSelection>,
}

impl From<BackupOptions> for BackupMetadata {
Expand All @@ -33,6 +41,20 @@ impl From<BackupOptions> for BackupMetadata {
}
}

impl BackupOptions {
pub fn export_to_file(
self,
provider: XmtpOpenMlsProvider,
path: impl AsRef<Path>,
) -> Result<(), DeviceSyncError> {
let provider = Arc::new(provider);
let mut exporter = BackupExporter::new(self, &provider);
exporter.write_to_file(path)?;

Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit dd77f69

Please sign in to comment.