Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit

Permalink
Change archive contact to delete contact
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Jan 23, 2024
1 parent 24b74a9 commit 877b6ca
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 36 deletions.
86 changes: 58 additions & 28 deletions mutiny-core/src/labels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use lnurl::lightning_address::LightningAddress;
use lnurl::lnurl::LnUrl;
use nostr::{key::XOnlyPublicKey, Metadata};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use std::str::FromStr;
use uuid::Uuid;
Expand Down Expand Up @@ -37,8 +38,6 @@ pub struct Contact {
pub lnurl: Option<LnUrl>,
#[serde(skip_serializing_if = "Option::is_none")]
pub image_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub archived: Option<bool>,
pub last_used: u64,
}

Expand Down Expand Up @@ -140,8 +139,8 @@ pub trait LabelStorage {
) -> Result<String, MutinyError>;
/// Create a new contact and return the identifying label
fn create_new_contact(&self, contact: Contact) -> Result<String, MutinyError>;
/// Marks a contact as archived
fn archive_contact(&self, id: impl AsRef<str>) -> Result<(), MutinyError>;
/// Deletes a contact and all labels associated with it
fn delete_contact(&self, id: impl AsRef<str>) -> Result<(), MutinyError>;
/// Edits an existing contact and replaces the existing contact
fn edit_contact(&self, id: impl AsRef<str>, contact: Contact) -> Result<(), MutinyError>;
/// Gets all the existing tags (labels and contacts)
Expand Down Expand Up @@ -287,10 +286,6 @@ impl<S: MutinyStorage> LabelStorage for S {
let mut contacts = HashMap::with_capacity(all.len());
for (key, contact) in all {
let label = key.replace(CONTACT_PREFIX, "");
// skip archived contacts
if contact.archived.unwrap_or(false) {
continue;
}
contacts.insert(label, contact);
}

Expand Down Expand Up @@ -375,12 +370,32 @@ impl<S: MutinyStorage> LabelStorage for S {
Ok(id)
}

fn archive_contact(&self, id: impl AsRef<str>) -> Result<(), MutinyError> {
let contact = self.get_contact(&id)?;
if let Some(mut contact) = contact {
contact.archived = Some(true);
self.set_data(get_contact_key(&id), contact, None)?;
fn delete_contact(&self, id: impl AsRef<str>) -> Result<(), MutinyError> {
// first remove from all labels
let mut inv_labels = self.get_invoice_labels()?;
for value in inv_labels.values_mut() {
value.retain(|s| *s != id.as_ref());
}
let mut addr_labels = self.get_address_labels()?;
for value in addr_labels.values_mut() {
value.retain(|s| *s != id.as_ref());
}
let to_set: Vec<(String, Value)> = vec![
(
ADDRESS_LABELS_MAP_KEY.to_string(),
serde_json::to_value(addr_labels)?,
),
(
INVOICE_LABELS_MAP_KEY.to_string(),
serde_json::to_value(inv_labels)?,
),
];
self.set(to_set)?;

// then delete actual label
let contact_key = get_contact_key(&id);
let label_item_key = get_label_item_key(&id);
self.delete(&[contact_key, label_item_key])?;
Ok(())
}

Expand Down Expand Up @@ -466,8 +481,8 @@ impl<S: MutinyStorage> LabelStorage for NodeManager<S> {
self.storage.create_new_contact(contact)
}

fn archive_contact(&self, id: impl AsRef<str>) -> Result<(), MutinyError> {
self.storage.archive_contact(id)
fn delete_contact(&self, id: impl AsRef<str>) -> Result<(), MutinyError> {
self.storage.delete_contact(id)
}

fn edit_contact(&self, id: impl AsRef<str>, contact: Contact) -> Result<(), MutinyError> {
Expand Down Expand Up @@ -566,7 +581,6 @@ mod tests {
npub: None,
ln_address: None,
lnurl: None,
archived: Some(false),
image_url: None,
last_used: 0,
},
Expand All @@ -578,7 +592,6 @@ mod tests {
npub: None,
ln_address: None,
lnurl: None,
archived: Some(false),
image_url: None,
last_used: 0,
},
Expand All @@ -590,7 +603,6 @@ mod tests {
npub: None,
ln_address: None,
lnurl: None,
archived: Some(false),
image_url: None,
last_used: 0,
},
Expand Down Expand Up @@ -744,7 +756,6 @@ mod tests {
npub: None,
ln_address: None,
lnurl: None,
archived: Some(false),
image_url: None,
last_used: 0,
};
Expand All @@ -766,7 +777,6 @@ mod tests {
npub: None,
ln_address: None,
lnurl: None,
archived: Some(false),
image_url: None,
last_used: 0,
};
Expand All @@ -781,8 +791,8 @@ mod tests {
}

#[test]
fn test_archive_contact() {
let test_name = "test_archive_contact";
fn test_delete_contact() {
let test_name = "test_delete_contact";
log!("{}", test_name);

let storage = MemoryStorage::default();
Expand All @@ -792,21 +802,41 @@ mod tests {
npub: None,
ln_address: None,
lnurl: None,
archived: Some(false),
image_url: None,
last_used: 0,
};
let id = storage.create_new_contact(contact).unwrap();
let contact = storage.get_contact(&id).unwrap();
assert!(contact.is_some());

let mut contact = storage.get_contact(&id).unwrap().unwrap();
contact.archived = Some(true);
storage.archive_contact(&id).unwrap();
// set labels for invoice and address
let invoice = Bolt11Invoice::from_str(INVOICE).unwrap();
storage
.set_invoice_labels(invoice.clone(), vec![id.clone()])
.unwrap();
let address = Address::from_str(ADDRESS).unwrap();
storage
.set_address_labels(address, vec![id.clone()])
.unwrap();

let result = storage.get_contact(&id).unwrap();
assert_eq!(result.unwrap(), contact);
// delete contact
storage.delete_contact(&id).unwrap();

// make sure it is deleted
let result = storage.get_contact(&id).unwrap();
assert!(result.is_none());
let contacts = storage.get_contacts().unwrap();
assert!(contacts.get(&id).is_none());

// check invoice labels are empty
let inv_labels = storage.get_invoice_labels().unwrap();
let labels = inv_labels.get(&invoice).cloned().unwrap_or_default();
assert!(labels.is_empty());

// check address labels are empty
let addr_labels = storage.get_address_labels().unwrap();
let labels = addr_labels.get(ADDRESS).cloned().unwrap_or_default();
assert!(labels.is_empty());
}

#[test]
Expand Down
3 changes: 0 additions & 3 deletions mutiny-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1319,7 +1319,6 @@ impl<S: MutinyStorage> MutinyWallet<S> {
ln_address: None,
lnurl: None,
image_url: Some("https://void.cat/d/CZPXhnwjqRhULSjPJ3sXTE.webp".to_string()),
archived: None,
last_used: utils::now().as_secs(),
};
self.storage.set_data(key, contact, None)?;
Expand Down Expand Up @@ -2179,7 +2178,6 @@ mod tests {
assert_eq!(contacts.len(), 1);
let contact = contacts.first().unwrap();
assert_eq!(contact.npub, Some(ben));
assert!(!contact.archived.unwrap_or(false));
assert!(contact.image_url.is_some());
assert!(contact.ln_address.is_some());
assert!(!contact.name.is_empty());
Expand All @@ -2202,7 +2200,6 @@ mod tests {
assert_eq!(contacts.len(), 2);
let contact = contacts.get(&id).unwrap();
assert_eq!(contact.npub, Some(tony));
assert!(!contact.archived.unwrap_or(false));
assert!(contact.image_url.is_some());
assert!(contact.ln_address.is_some());
assert_ne!(contact.name, incorrect_name);
Expand Down
7 changes: 2 additions & 5 deletions mutiny-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1178,7 +1178,6 @@ impl MutinyWallet {
.transpose()?,
lnurl: lnurl.map(|l| LnUrl::from_str(&l)).transpose()?,
image_url,
archived: None,
last_used: now().as_secs(),
};

Expand All @@ -1204,14 +1203,13 @@ impl MutinyWallet {
.transpose()?,
lnurl: lnurl.map(|l| LnUrl::from_str(&l)).transpose()?,
image_url,
archived: None,
last_used: now().as_secs(),
};
Ok(self.inner.node_manager.create_new_contact(contact)?)
}

pub fn archive_contact(&self, id: String) -> Result<(), MutinyJsError> {
Ok(self.inner.node_manager.archive_contact(id)?)
pub fn delete_contact(&self, id: String) -> Result<(), MutinyJsError> {
Ok(self.inner.node_manager.delete_contact(id)?)
}

pub fn edit_contact(
Expand All @@ -1231,7 +1229,6 @@ impl MutinyWallet {
.transpose()?,
lnurl: lnurl.map(|l| LnUrl::from_str(&l)).transpose()?,
image_url,
archived: None,
last_used: now().as_secs(),
};

Expand Down

0 comments on commit 877b6ca

Please sign in to comment.