Add Nostr event cache

This commit adds a simple in-memory Nostr Event cache, to reduce the
amount of bandwidth used as well as to improve performance.

Testing
-------

Setup:
  - Two iPhone simulators running Damus and on different accounts
  - Damus version: 774da239b92ed630fbf91fce42d9e233661c0d7f
  - Notepush: This commit
  - Push notifications turned on, setup to connect to localhost, and configured to receive DM notifications
  - Run Notepush with `RUST_LOG=DEBUG` env variable for debug logging
Steps:
1. Send a DM from one account to another.
  - Push notification should arrive with a few seconds delay
  - Push notification logs should mention that the event was cached
2. Send a DM again.
  - Push notification should arrive immediately
3. Wait for more than a minute
4. Send a DM again.
  - Push notification should take a few seconds again
  - Push notification logs should mention that the cache item expired and was deleted

Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
Closes: https://github.com/damus-io/notepush/issues/3
This commit is contained in:
Daniel D’Aquino
2024-08-05 18:01:52 -07:00
parent 388d49927b
commit 71258e3736
4 changed files with 177 additions and 13 deletions

View File

@ -27,7 +27,6 @@ pub struct NotificationManager {
db: Mutex<r2d2::Pool<SqliteConnectionManager>>,
apns_topic: String,
apns_client: Mutex<Client>,
nostr_network_helper: Mutex<NostrNetworkHelper>,
}
@ -221,7 +220,7 @@ impl NotificationManager {
let mut pubkeys_to_notify = HashSet::new();
for pubkey in relevant_pubkeys_yet_to_receive {
let should_mute: bool = {
let mute_manager_mutex_guard = self.nostr_network_helper.lock().await;
let mut mute_manager_mutex_guard = self.nostr_network_helper.lock().await;
mute_manager_mutex_guard
.should_mute_notification_for_pubkey(event, &pubkey)
.await
@ -286,7 +285,7 @@ impl NotificationManager {
) -> Result<bool, Box<dyn std::error::Error>> {
let notification_preferences = self.get_user_notification_settings(pubkey, device_token).await?;
if notification_preferences.only_notifications_from_following_enabled {
let nostr_network_helper_mutex_guard = self.nostr_network_helper.lock().await;
let mut nostr_network_helper_mutex_guard = self.nostr_network_helper.lock().await;
if !nostr_network_helper_mutex_guard.does_pubkey_follow_pubkey(pubkey, &event.author()).await {
return Ok(false);
}