Support list of notifications

This commit is contained in:
Gregory Schier
2025-02-03 07:12:32 -08:00
parent 17dc1991f1
commit dd0516cc55
+14 -1
View File
@@ -5,6 +5,7 @@ use chrono::{DateTime, Duration, Utc};
use log::debug; use log::debug;
use reqwest::Method; use reqwest::Method;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value;
use tauri::{Emitter, Manager, Runtime, WebviewWindow}; use tauri::{Emitter, Manager, Runtime, WebviewWindow};
use yaak_models::queries::{get_key_value_raw, set_key_value_raw, UpdateSource}; use yaak_models::queries::{get_key_value_raw, set_key_value_raw, UpdateSource};
@@ -74,8 +75,19 @@ impl YaakNotifier {
return Ok(()); return Ok(());
} }
let notification = resp.json::<YaakNotification>().await.map_err(|e| e.to_string())?; let result = resp.json::<Value>().await.map_err(|e| e.to_string())?;
// 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::<Vec<YaakNotification>>(),
a @ _ => vec![serde_json::from_value(a).unwrap()],
};
for notification in notifications {
let age = notification.timestamp.signed_duration_since(Utc::now()); let age = notification.timestamp.signed_duration_since(Utc::now());
let seen = get_kv(window).await?; let seen = get_kv(window).await?;
if seen.contains(&notification.id) || (age > Duration::days(2)) { if seen.contains(&notification.id) || (age > Duration::days(2)) {
@@ -85,6 +97,7 @@ impl YaakNotifier {
debug!("Got notification {:?}", notification); debug!("Got notification {:?}", notification);
let _ = window.emit_to(window.label(), "notification", notification.clone()); let _ = window.emit_to(window.label(), "notification", notification.clone());
}
Ok(()) Ok(())
} }