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

Commit 66f0474

Browse files
authored
Merge pull request #1012 from MutinyWallet/cache-subscription-timestamp
Cache subscription timestamp
2 parents 91e582e + 9ae653c commit 66f0474

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

mutiny-core/src/lib.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ use uuid::Uuid;
101101

102102
use crate::labels::LabelItem;
103103
use crate::nostr::NostrKeySource;
104+
use crate::storage::SUBSCRIPTION_TIMESTAMP;
104105
use crate::utils::parse_profile_metadata;
105106
#[cfg(test)]
106107
use mockall::{automock, predicate::*};
@@ -1483,8 +1484,24 @@ impl<S: MutinyStorage> MutinyWallet<S> {
14831484
/// Returns Some(u64) for their unix expiration timestamp, which may be in the
14841485
/// past or in the future, depending on whether or not it is currently active.
14851486
pub async fn check_subscribed(&self) -> Result<Option<u64>, MutinyError> {
1486-
if let Some(subscription_client) = self.subscription_client.clone() {
1487-
Ok(subscription_client.check_subscribed().await?)
1487+
if let Some(ref subscription_client) = self.subscription_client {
1488+
let now = utils::now().as_secs();
1489+
match self.storage.get_data::<u64>(SUBSCRIPTION_TIMESTAMP) {
1490+
Ok(Some(timestamp)) if timestamp > now => {
1491+
// if we have a timestamp and it is in the future, we are subscribed
1492+
Ok(Some(timestamp))
1493+
}
1494+
_ => {
1495+
// if we don't have a timestamp or it is in the past, check with the server
1496+
let time = subscription_client.check_subscribed().await?;
1497+
// if we are subscribed, save the timestamp
1498+
if let Some(time) = time.filter(|t| *t > now) {
1499+
self.storage
1500+
.set_data(SUBSCRIPTION_TIMESTAMP.to_string(), time, None)?;
1501+
}
1502+
Ok(time)
1503+
}
1504+
}
14881505
} else {
14891506
Ok(None)
14901507
}

mutiny-core/src/storage.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use std::collections::HashMap;
2626
use std::sync::{Arc, RwLock};
2727
use uuid::Uuid;
2828

29+
pub const SUBSCRIPTION_TIMESTAMP: &str = "subscription_timestamp";
2930
pub const KEYCHAIN_STORE_KEY: &str = "bdk_keychain";
3031
pub const MNEMONIC_KEY: &str = "mnemonic";
3132
pub(crate) const NEED_FULL_SYNC_KEY: &str = "needs_full_sync";

0 commit comments

Comments
 (0)