mirror of
https://github.com/nostrlabs-io/notepush.git
synced 2025-06-15 19:38:24 +00:00
Make max cache age adjustable in env, and change default to 1 hour
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
This commit is contained in:
@ -40,6 +40,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|||||||
env.apns_team_id.clone(),
|
env.apns_team_id.clone(),
|
||||||
env.apns_environment.clone(),
|
env.apns_environment.clone(),
|
||||||
env.apns_topic.clone(),
|
env.apns_topic.clone(),
|
||||||
|
env.nostr_event_cache_max_age,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.expect("Failed to create notification manager"),
|
.expect("Failed to create notification manager"),
|
||||||
|
@ -6,6 +6,7 @@ const DEFAULT_DB_PATH: &str = "./apns_notifications.db";
|
|||||||
const DEFAULT_HOST: &str = "0.0.0.0";
|
const DEFAULT_HOST: &str = "0.0.0.0";
|
||||||
const DEFAULT_PORT: &str = "8000";
|
const DEFAULT_PORT: &str = "8000";
|
||||||
const DEFAULT_RELAY_URL: &str = "wss://relay.damus.io";
|
const DEFAULT_RELAY_URL: &str = "wss://relay.damus.io";
|
||||||
|
const DEFAULT_NOSTR_EVENT_CACHE_MAX_AGE: u64 = 60 * 60; // 1 hour
|
||||||
|
|
||||||
pub struct NotePushEnv {
|
pub struct NotePushEnv {
|
||||||
// The path to the Apple private key .p8 file
|
// The path to the Apple private key .p8 file
|
||||||
@ -26,6 +27,8 @@ pub struct NotePushEnv {
|
|||||||
pub api_base_url: String, // The base URL of where the API server is hosted for NIP-98 auth checks
|
pub api_base_url: String, // The base URL of where the API server is hosted for NIP-98 auth checks
|
||||||
// The URL of the Nostr relay server to connect to for getting mutelists
|
// The URL of the Nostr relay server to connect to for getting mutelists
|
||||||
pub relay_url: String,
|
pub relay_url: String,
|
||||||
|
// The max age of the Nostr event cache, in seconds
|
||||||
|
pub nostr_event_cache_max_age: std::time::Duration,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NotePushEnv {
|
impl NotePushEnv {
|
||||||
@ -47,6 +50,11 @@ impl NotePushEnv {
|
|||||||
_ => a2::client::Endpoint::Sandbox,
|
_ => a2::client::Endpoint::Sandbox,
|
||||||
};
|
};
|
||||||
let apns_topic = env::var("APNS_TOPIC")?;
|
let apns_topic = env::var("APNS_TOPIC")?;
|
||||||
|
let nostr_event_cache_max_age = env::var("NOSTR_EVENT_CACHE_MAX_AGE")
|
||||||
|
.unwrap_or(DEFAULT_NOSTR_EVENT_CACHE_MAX_AGE.to_string())
|
||||||
|
.parse::<u64>()
|
||||||
|
.map(|s| std::time::Duration::from_secs(s))
|
||||||
|
.unwrap_or(std::time::Duration::from_secs(DEFAULT_NOSTR_EVENT_CACHE_MAX_AGE));
|
||||||
|
|
||||||
Ok(NotePushEnv {
|
Ok(NotePushEnv {
|
||||||
apns_private_key_path,
|
apns_private_key_path,
|
||||||
@ -59,6 +67,7 @@ impl NotePushEnv {
|
|||||||
port,
|
port,
|
||||||
api_base_url,
|
api_base_url,
|
||||||
relay_url,
|
relay_url,
|
||||||
|
nostr_event_cache_max_age
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,7 +6,6 @@ use super::nostr_event_cache::Cache;
|
|||||||
use tokio::time::{timeout, Duration};
|
use tokio::time::{timeout, Duration};
|
||||||
|
|
||||||
const NOTE_FETCH_TIMEOUT: Duration = Duration::from_secs(5);
|
const NOTE_FETCH_TIMEOUT: Duration = Duration::from_secs(5);
|
||||||
const CACHE_MAX_AGE: Duration = Duration::from_secs(60);
|
|
||||||
|
|
||||||
pub struct NostrNetworkHelper {
|
pub struct NostrNetworkHelper {
|
||||||
client: Client,
|
client: Client,
|
||||||
@ -16,12 +15,15 @@ pub struct NostrNetworkHelper {
|
|||||||
impl NostrNetworkHelper {
|
impl NostrNetworkHelper {
|
||||||
// MARK: - Initialization
|
// MARK: - Initialization
|
||||||
|
|
||||||
pub async fn new(relay_url: String) -> Result<Self, Box<dyn std::error::Error>> {
|
pub async fn new(relay_url: String, cache_max_age: Duration) -> Result<Self, Box<dyn std::error::Error>> {
|
||||||
let client = Client::new(&Keys::generate());
|
let client = Client::new(&Keys::generate());
|
||||||
client.add_relay(relay_url.clone()).await?;
|
client.add_relay(relay_url.clone()).await?;
|
||||||
client.connect().await;
|
client.connect().await;
|
||||||
|
|
||||||
Ok(NostrNetworkHelper { client, cache: Mutex::new(Cache::new(CACHE_MAX_AGE)) })
|
Ok(NostrNetworkHelper {
|
||||||
|
client,
|
||||||
|
cache: Mutex::new(Cache::new(cache_max_age)),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Answering questions about a user
|
// MARK: - Answering questions about a user
|
||||||
|
@ -41,6 +41,7 @@ impl NotificationManager {
|
|||||||
apns_team_id: String,
|
apns_team_id: String,
|
||||||
apns_environment: a2::client::Endpoint,
|
apns_environment: a2::client::Endpoint,
|
||||||
apns_topic: String,
|
apns_topic: String,
|
||||||
|
cache_max_age: std::time::Duration,
|
||||||
) -> Result<Self, Box<dyn std::error::Error>> {
|
) -> Result<Self, Box<dyn std::error::Error>> {
|
||||||
let connection = db.get()?;
|
let connection = db.get()?;
|
||||||
Self::setup_database(&connection)?;
|
Self::setup_database(&connection)?;
|
||||||
@ -58,7 +59,7 @@ impl NotificationManager {
|
|||||||
apns_topic,
|
apns_topic,
|
||||||
apns_client: Mutex::new(client),
|
apns_client: Mutex::new(client),
|
||||||
db: Mutex::new(db),
|
db: Mutex::new(db),
|
||||||
nostr_network_helper: NostrNetworkHelper::new(relay_url.clone()).await?,
|
nostr_network_helper: NostrNetworkHelper::new(relay_url.clone(), cache_max_age).await?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user