Skip to content

Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.

fix(core): the phone's notification history no longer grows without limit

Beea8841a9ef849
diff --git a/core/nectarlink-core/src/node.rs b/core/nectarlink-core/src/node.rs
index 3d785d5..458a0db 100644
--- a/core/nectarlink-core/src/node.rs
+++ b/core/nectarlink-core/src/node.rs
@@ -119,6 +119,11 @@ struct Supervisor {
pub(crate) type MirrorStop = (DeviceId, u32, &'static str);
/// State shared by the node, its sessions and background tasks.
+/// Notifications kept for listing and search as they arrive, at most: the
+/// oldest goes first. (A full snapshot is what the phone shows, which Android
+/// itself limits.)
+const NOTIFICATION_HISTORY_MAX: usize = 1000;
+
pub(crate) struct Shared {
pub id: DeviceId,
pub endpoint: Endpoint,
@@ -3139,6 +3144,12 @@ impl Node {
let mut hist = lock(&self.shared.notification_history);
hist.retain(|n| n.key != clean.key);
hist.push(clean);
+ // Kept in memory with icons and pictures: drop the oldest past the cap.
+ if hist.len() > NOTIFICATION_HISTORY_MAX
+ && let Some(oldest) = hist.iter().enumerate().min_by_key(|(_, n)| n.when).map(|(i, _)| i)
+ {
+ hist.remove(oldest);
+ }
}
self.shared.notification_posted(notification).await;
}
diff --git a/core/nectarlink-core/tests/two_devices.rs b/core/nectarlink-core/tests/two_devices.rs
index ef06b20..2806a0c 100644
--- a/core/nectarlink-core/tests/two_devices.rs
+++ b/core/nectarlink-core/tests/two_devices.rs
@@ -6413,3 +6413,32 @@ async fn one_time_screen_consent_ends_with_the_pairing() {
pc.node.shutdown().await;
phone.node.shutdown().await;
}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn notification_history_keeps_the_newest_thousand() {
+ let phone = device("phone", DeviceKind::Phone).await;
+ for i in 0..1005i64 {
+ phone
+ .node
+ .notification_posted(Notification {
+ key: format!("app|{i:04}"),
+ app: "org.example.chat".into(),
+ app_name: "Chat".into(),
+ title: Some(format!("Message {i}")),
+ text: None,
+ sub: None,
+ when: 1_760_000_000_000 + i * 1000,
+ silent: false,
+ actions: Vec::new(),
+ icon: None,
+ image: None,
+ live: None,
+ conversation: None,
+ })
+ .await;
+ }
+ let history = phone.node.notification_history(None, 5000);
+ assert_eq!(history.len(), 1000);
+ assert!(history.iter().all(|n| n.key.as_str() >= "app|0005"), "the oldest five are gone");
+ phone.node.shutdown().await;
+}