Skip to content

Commit

Permalink
Rename contacts methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpfs committed Nov 28, 2023
1 parent 92161cd commit ac5f82f
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/commands/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,14 +553,14 @@ 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(())
}

/// Import contacts from a vCard.
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(())
}
4 changes: 2 additions & 2 deletions tests/integration/audit_trail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
Expand Down
28 changes: 22 additions & 6 deletions tests/integration/local_account/contacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand All @@ -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(())
Expand Down
21 changes: 9 additions & 12 deletions workspace/net/src/client/account/contacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P: AsRef<Path>>(
/// Export a contact secret to a vCard file.
pub async fn export_contact<P: AsRef<Path>>(
&mut self,
path: P,
secret_id: &SecretId,
folder: Option<Summary>,
) -> 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<P: AsRef<Path>>(
pub async fn export_all_contacts<P: AsRef<Path>>(
&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<Vec<SecretId>> {
Ok(self.account.import_contacts(content, progress).await?)
}
}
20 changes: 11 additions & 9 deletions workspace/sdk/src/account/contacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ impl<D> Account<D> {
Ok(None)
}

/// Export a contact secret to vCard file.
pub async fn export_vcard_file<P: AsRef<Path>>(
/// Export a contact secret to a vCard file.
pub async fn export_contact<P: AsRef<Path>>(
&mut self,
path: P,
secret_id: &SecretId,
Expand Down Expand Up @@ -86,7 +86,7 @@ impl<D> Account<D> {
}

/// Export all contacts to a single vCard.
pub async fn export_all_vcards<P: AsRef<Path>>(
pub async fn export_all_contacts<P: AsRef<Path>>(
&mut self,
path: P,
) -> Result<()> {
Expand Down Expand Up @@ -129,14 +129,15 @@ impl<D> Account<D> {
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<Vec<SecretId>> {
use crate::vcard4::parse;

let mut ids = Vec::new();
let current = {
let storage = self.storage()?;
let reader = storage.read().await;
Expand Down Expand Up @@ -171,7 +172,9 @@ impl<D> Account<D> {
});

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 {
Expand All @@ -184,7 +187,6 @@ impl<D> Account<D> {
None,
);
self.append_audit_logs(vec![audit_event]).await?;

Ok(())
Ok(ids)
}
}

0 comments on commit ac5f82f

Please sign in to comment.