From ac5f82f6892c6b2c314ed031deadf26ccabb21a9 Mon Sep 17 00:00:00 2001 From: muji Date: Tue, 28 Nov 2023 14:19:57 +0800 Subject: [PATCH] Rename contacts methods. --- src/commands/account.rs | 4 +-- tests/integration/audit_trail.rs | 4 +-- tests/integration/local_account/contacts.rs | 28 +++++++++++++++----- workspace/net/src/client/account/contacts.rs | 21 +++++++-------- workspace/sdk/src/account/contacts.rs | 20 +++++++------- 5 files changed, 46 insertions(+), 31 deletions(-) diff --git a/src/commands/account.rs b/src/commands/account.rs index 388f0e212e..dd6cb8d711 100644 --- a/src/commands/account.rs +++ b/src/commands/account.rs @@ -553,7 +553,7 @@ async fn contacts_export( return Err(Error::FileExists(output)); } let mut owner = user.write().await; - owner.export_all_vcards(output).await?; + owner.export_all_contacts(output).await?; Ok(()) } @@ -561,6 +561,6 @@ async fn contacts_export( async fn contacts_import(user: Owner, input: PathBuf) -> Result<()> { let mut owner = user.write().await; let content = vfs::read_to_string(&input).await?; - owner.import_vcard(&content, |_| {}).await?; + owner.import_contacts(&content, |_| {}).await?; Ok(()) } diff --git a/tests/integration/audit_trail.rs b/tests/integration/audit_trail.rs index 12d252a042..8084f957e8 100644 --- a/tests/integration/audit_trail.rs +++ b/tests/integration/audit_trail.rs @@ -207,10 +207,10 @@ async fn simulate_session( let contacts = "tests/fixtures/contacts.vcf"; let vcard = vfs::read_to_string(contacts).await?; - owner.import_vcard(&vcard, |_| {}).await?; + owner.import_contacts(&vcard, |_| {}).await?; let exported_contacts = "target/audit-trail-exported-contacts.vcf"; - owner.export_all_vcards(exported_contacts).await?; + owner.export_all_contacts(exported_contacts).await?; // Delete the account owner.delete_account().await?; diff --git a/tests/integration/local_account/contacts.rs b/tests/integration/local_account/contacts.rs index d6d3817e7d..aebeb37b0a 100644 --- a/tests/integration/local_account/contacts.rs +++ b/tests/integration/local_account/contacts.rs @@ -7,9 +7,10 @@ use sos_net::sdk::{ }; const TEST_ID: &str = "contacts"; -const VCARD: &str = include_str!("../../../workspace/sdk/fixtures/contact.vcf"); +const VCARD: &str = + include_str!("../../../workspace/sdk/fixtures/contact.vcf"); -/// Tests importing and exporting contacts from vCard +/// Tests importing and exporting contacts from vCard /// files. #[tokio::test] async fn integration_contacts() -> Result<()> { @@ -32,16 +33,31 @@ async fn integration_contacts() -> Result<()> { ) .await?; - let default_folder = new_account.default_folder(); account.sign_in(password.clone()).await?; - account.open_folder(&default_folder).await?; - account.import_vcard(VCARD, |_| {}).await?; + let contacts = account.contacts_folder().await.unwrap(); + account.open_folder(&contacts).await?; + + let ids = account.import_contacts(VCARD, |_| {}).await?; + assert_eq!(1, ids.len()); + + let id = ids.get(0).unwrap(); + let contact = data_dir.join("contact.vcf"); + account.export_contact(&contact, id, None).await?; + assert!(vfs::try_exists(&contact).await?); + + let contact_content = vfs::read_to_string(&contact).await?; + let contact_content = contact_content.replace('\r', ""); + assert_eq!(VCARD, &contact_content); let contacts = data_dir.join("contacts.vcf"); - account.export_all_vcards(&contacts).await?; + account.export_all_contacts(&contacts).await?; assert!(vfs::try_exists(&contacts).await?); + let contacts_content = vfs::read_to_string(&contacts).await?; + let contacts_content = contacts_content.replace('\r', ""); + assert_eq!(VCARD, &contacts_content); + teardown(TEST_ID).await; Ok(()) diff --git a/workspace/net/src/client/account/contacts.rs b/workspace/net/src/client/account/contacts.rs index 05f348afcb..c5372fc363 100644 --- a/workspace/net/src/client/account/contacts.rs +++ b/workspace/net/src/client/account/contacts.rs @@ -17,33 +17,30 @@ impl NetworkAccount { Ok(self.account.load_avatar(secret_id, folder).await?) } - /// Export a contact secret to vCard file. - pub async fn export_vcard_file>( + /// Export a contact secret to a vCard file. + pub async fn export_contact>( &mut self, path: P, secret_id: &SecretId, folder: Option, ) -> Result<()> { - Ok(self - .account - .export_vcard_file(path, secret_id, folder) - .await?) + Ok(self.account.export_contact(path, secret_id, folder).await?) } /// Export all contacts to a single vCard. - pub async fn export_all_vcards>( + pub async fn export_all_contacts>( &mut self, path: P, ) -> Result<()> { - Ok(self.account.export_all_vcards(path).await?) + Ok(self.account.export_all_contacts(path).await?) } - /// Import vCards from a string buffer. - pub async fn import_vcard( + /// Import contacts from a vCard string buffer. + pub async fn import_contacts( &mut self, content: &str, progress: impl Fn(ContactImportProgress), - ) -> Result<()> { - Ok(self.account.import_vcard(content, progress).await?) + ) -> Result> { + Ok(self.account.import_contacts(content, progress).await?) } } diff --git a/workspace/sdk/src/account/contacts.rs b/workspace/sdk/src/account/contacts.rs index ff44f2d3ba..4d8b266868 100644 --- a/workspace/sdk/src/account/contacts.rs +++ b/workspace/sdk/src/account/contacts.rs @@ -50,8 +50,8 @@ impl Account { Ok(None) } - /// Export a contact secret to vCard file. - pub async fn export_vcard_file>( + /// Export a contact secret to a vCard file. + pub async fn export_contact>( &mut self, path: P, secret_id: &SecretId, @@ -86,7 +86,7 @@ impl Account { } /// Export all contacts to a single vCard. - pub async fn export_all_vcards>( + pub async fn export_all_contacts>( &mut self, path: P, ) -> Result<()> { @@ -129,14 +129,15 @@ impl Account { Ok(()) } - /// Import vCards from a string buffer. - pub async fn import_vcard( + /// Import contacts from a vCard string buffer. + pub async fn import_contacts( &mut self, content: &str, progress: impl Fn(ContactImportProgress), - ) -> Result<()> { + ) -> Result> { use crate::vcard4::parse; + let mut ids = Vec::new(); let current = { let storage = self.storage()?; let reader = storage.read().await; @@ -171,7 +172,9 @@ impl Account { }); let meta = SecretMeta::new(label, secret.kind()); - self.create_secret(meta, secret, Default::default()).await?; + let (id, _, _, _) = + self.create_secret(meta, secret, Default::default()).await?; + ids.push(id); } if let Some(folder) = current { @@ -184,7 +187,6 @@ impl Account { None, ); self.append_audit_logs(vec![audit_event]).await?; - - Ok(()) + Ok(ids) } }