diff --git a/src-tauri/src/notifications.rs b/src-tauri/src/notifications.rs index 9543eae3..15daad1e 100644 --- a/src-tauri/src/notifications.rs +++ b/src-tauri/src/notifications.rs @@ -5,6 +5,7 @@ use chrono::{DateTime, Duration, Utc}; use log::debug; use reqwest::Method; use serde::{Deserialize, Serialize}; +use serde_json::Value; use tauri::{Emitter, Manager, Runtime, WebviewWindow}; use yaak_models::queries::{get_key_value_raw, set_key_value_raw, UpdateSource}; @@ -74,17 +75,29 @@ impl YaakNotifier { return Ok(()); } - let notification = resp.json::().await.map_err(|e| e.to_string())?; + let result = resp.json::().await.map_err(|e| e.to_string())?; - let age = notification.timestamp.signed_duration_since(Utc::now()); - let seen = get_kv(window).await?; - if seen.contains(¬ification.id) || (age > Duration::days(2)) { - debug!("Already seen notification {}", notification.id); - return Ok(()); + // Support both single and multiple notifications. + // TODO: Remove support for single after April 2025 + let notifications = match result { + Value::Array(a) => a + .into_iter() + .map(|a| serde_json::from_value(a).unwrap()) + .collect::>(), + a @ _ => vec![serde_json::from_value(a).unwrap()], + }; + + for notification in notifications { + let age = notification.timestamp.signed_duration_since(Utc::now()); + let seen = get_kv(window).await?; + if seen.contains(¬ification.id) || (age > Duration::days(2)) { + debug!("Already seen notification {}", notification.id); + return Ok(()); + } + debug!("Got notification {:?}", notification); + + let _ = window.emit_to(window.label(), "notification", notification.clone()); } - debug!("Got notification {:?}", notification); - - let _ = window.emit_to(window.label(), "notification", notification.clone()); Ok(()) }