Skip to content

Commit

Permalink
Disable mutiny plus profiles instead of delete
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyGiorgio committed May 8, 2024
1 parent 256876a commit 98f4e2e
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 42 deletions.
74 changes: 34 additions & 40 deletions mutiny-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2098,6 +2098,8 @@ impl<S: MutinyStorage> MutinyWallet<S> {
self.ensure_mutiny_nwc_profile(subscription_client, autopay)
.await?;

// FIXME: switch the subscription from disabled to enabled if it was disabled

self.check_blind_tokens();

Ok(())
Expand All @@ -2117,47 +2119,39 @@ impl<S: MutinyStorage> MutinyWallet<S> {
.iter()
.find(|profile| profile.index == reserved_profile_index);

match profile_opt {
None => {
// profile with the reserved index does not exist, create a new one
let nwc = if autopay {
self.nostr
.create_new_nwc_profile(
ProfileType::Reserved(ReservedProfile::MutinySubscription),
SpendingConditions::Budget(BudgetedSpendingConditions {
budget: 21_000,
single_max: None,
payments: vec![],
period: BudgetPeriod::Month,
}),
NwcProfileTag::Subscription,
vec![Method::PayInvoice], // subscription only needs pay invoice
)
.await?
.nwc_uri
} else {
self.nostr
.create_new_nwc_profile(
ProfileType::Reserved(ReservedProfile::MutinySubscription),
SpendingConditions::RequireApproval,
NwcProfileTag::Subscription,
vec![Method::PayInvoice], // subscription only needs pay invoice
)
.await?
.nwc_uri
};
if profile_opt.is_none() {
log_debug!(self.logger, "Did not find a mutiny+ nwc profile");
// profile with the reserved index does not exist, create a new one
let nwc = if autopay {
self.nostr
.create_new_nwc_profile(
ProfileType::Reserved(ReservedProfile::MutinySubscription),
SpendingConditions::Budget(BudgetedSpendingConditions {
budget: 21_000,
single_max: None,
payments: vec![],
period: BudgetPeriod::Month,
}),
NwcProfileTag::Subscription,
vec![Method::PayInvoice], // subscription only needs pay invoice
)
.await?
.nwc_uri
} else {
self.nostr
.create_new_nwc_profile(
ProfileType::Reserved(ReservedProfile::MutinySubscription),
SpendingConditions::RequireApproval,
NwcProfileTag::Subscription,
vec![Method::PayInvoice], // subscription only needs pay invoice
)
.await?
.nwc_uri
};

if let Some(nwc) = nwc {
// only should have to submit the NWC if never created locally before
subscription_client.submit_nwc(nwc).await?;
}
}
Some(profile) => {
if profile.tag != NwcProfileTag::Subscription {
let mut nwc = profile.clone();
nwc.tag = NwcProfileTag::Subscription;
self.nostr.edit_nwc_profile(nwc)?;
}
if let Some(nwc) = nwc {
// only should have to submit the NWC if never created locally before
subscription_client.submit_nwc(nwc).await?;
}
}

Expand Down
47 changes: 45 additions & 2 deletions mutiny-core/src/nostr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,15 @@ impl<S: MutinyStorage, P: PrimalApi, C: NostrClient> NostrManager<S, P, C> {
}

pub fn profiles(&self) -> Vec<NwcProfile> {
self.nwc
.read()
.unwrap()
.iter()
.map(|x| x.nwc_profile())
.collect()
}

pub fn active_profiles(&self) -> Vec<NwcProfile> {
self.nwc
.read()
.unwrap()
Expand All @@ -738,7 +747,8 @@ impl<S: MutinyStorage, P: PrimalApi, C: NostrClient> NostrManager<S, P, C> {
pub(crate) fn remove_inactive_profiles(&self) -> Result<(), MutinyError> {
let mut profiles = self.nwc.write().unwrap();

profiles.retain(|x| x.profile.active());
let mutiny_plus_index = ReservedProfile::MutinySubscription.info().1;
profiles.retain(|x| x.profile.active() || x.profile.index == mutiny_plus_index);

// save to storage
{
Expand Down Expand Up @@ -836,6 +846,7 @@ impl<S: MutinyStorage, P: PrimalApi, C: NostrClient> NostrManager<S, P, C> {

// save to storage
{
log_info!(self.logger, "Saving nwc to storage");
let profiles = profiles
.iter()
.map(|x| x.profile.clone())
Expand Down Expand Up @@ -992,6 +1003,8 @@ impl<S: MutinyStorage, P: PrimalApi, C: NostrClient> NostrManager<S, P, C> {
tag: NwcProfileTag,
commands: Vec<Method>,
) -> Result<NwcProfile, MutinyError> {
log_info!(self.logger, "Creating new internal nwc profile");

let mut profiles = self.nwc.try_write()?;

let (name, index, child_key_index) = get_next_nwc_index(profile_type, &profiles)?;
Expand Down Expand Up @@ -1569,9 +1582,15 @@ impl<S: MutinyStorage, P: PrimalApi, C: NostrClient> NostrManager<S, P, C> {
}

pub fn delete_nwc_profile(&self, index: u32) -> Result<(), MutinyError> {
let mut vec = self.nwc.write().unwrap();
log_info!(self.logger, "Deleting nwc profile: {index}");

// TODO don't delete mutiny+ profile
if index == ReservedProfile::MutinySubscription.info().1 {
return self.disable_mutiny_plus_profile();
}

// update the profile
let mut vec = self.nwc.write().unwrap();
vec.retain(|x| x.profile.index != index);

let profiles = vec.iter().map(|x| x.profile.clone()).collect::<Vec<_>>();
Expand All @@ -1582,6 +1601,30 @@ impl<S: MutinyStorage, P: PrimalApi, C: NostrClient> NostrManager<S, P, C> {
Ok(())
}

pub fn disable_mutiny_plus_profile(&self) -> Result<(), MutinyError> {
log_info!(self.logger, "Disabling mutiny+ subscription");

let mut vec = self.nwc.write().unwrap();

let profile_opt = vec
.iter_mut()
.find(|p| p.profile.index == ReservedProfile::MutinySubscription.info().1);

match profile_opt {
Some(p) => {
p.profile.enabled = Some(false);

let profiles = vec.iter().map(|x| x.profile.clone()).collect::<Vec<_>>();

self.storage
.set_data(NWC_STORAGE_KEY.to_string(), profiles, None)?;

Ok(())
}
None => Err(MutinyError::NotFound),
}
}

pub async fn claim_single_use_nwc(
&self,
amount_sats: u64,
Expand Down
8 changes: 8 additions & 0 deletions mutiny-wasm/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,7 @@ pub struct NwcProfile {
nwc_uri: Option<String>,
tag: String,
label: Option<String>,
enabled: bool,
}

impl Serialize for NwcProfile {
Expand All @@ -869,6 +870,7 @@ impl Serialize for NwcProfile {
"active_payments": self._active_payments(),
"spending_conditions_type": self.spending_conditions_type(),
"url_suffix": self.url_suffix(),
"enabled": self.enabled(),
});

json.serialize(serializer)
Expand Down Expand Up @@ -983,6 +985,11 @@ impl NwcProfile {
None
}
}

#[wasm_bindgen(getter)]
pub fn enabled(&self) -> bool {
self.enabled
}
}

impl From<nostr::nwc::NwcProfile> for NwcProfile {
Expand All @@ -1009,6 +1016,7 @@ impl From<nostr::nwc::NwcProfile> for NwcProfile {
nwc_uri: value.nwc_uri,
tag: value.tag.to_string(),
label: value.label,
enabled: value.enabled.unwrap_or(true),
}
}
}
Expand Down

0 comments on commit 98f4e2e

Please sign in to comment.