diff --git a/pgml-dashboard/src/components/layouts/head/mod.rs b/pgml-dashboard/src/components/layouts/head/mod.rs index 1111815ad..76d86dac1 100644 --- a/pgml-dashboard/src/components/layouts/head/mod.rs +++ b/pgml-dashboard/src/components/layouts/head/mod.rs @@ -134,7 +134,7 @@ mod default_head_template_test { #[test] fn set_head() { - let mut head = Head::new() + let head = Head::new() .title("test title") .description("test description") .image("image/test_image.jpg"); diff --git a/pgml-dashboard/src/lib.rs b/pgml-dashboard/src/lib.rs index 3bdc0ecdd..bc4933fd5 100644 --- a/pgml-dashboard/src/lib.rs +++ b/pgml-dashboard/src/lib.rs @@ -407,12 +407,10 @@ pub fn replace_banner_product( Notifications::update_viewed(&all_notification_cookies, cookies); - // Get the notification that triggered this call.. - // unwrap notifications if fine since we should panic if this is missing. - let last_notification = context + let last_notification: Option = context .notifications .as_ref() - .unwrap() + .unwrap_or(&vec![] as &Vec) .clone() .into_iter() .find(|n: &Notification| -> bool { n.id == id }); @@ -562,7 +560,7 @@ mod test { .time_modal_viewed; // Update modal view time for existing notification cookie - assert_eq!(time_modal_viewed.is_some(), true); + assert!(time_modal_viewed.is_some()); let response = client .get("/notifications/product/modal/remove_modal?id=3") @@ -576,7 +574,7 @@ mod test { .time_modal_viewed; // Update modal view time for new notification cookie - assert_eq!(time_modal_viewed.is_some(), true); + assert!(time_modal_viewed.is_some()); } #[sqlx::test] @@ -623,7 +621,7 @@ mod test { .time_viewed; // Update view time for new notification cookie - assert_eq!(time_viewed.is_some(), true); + assert!(time_viewed.is_some()); } #[sqlx::test] diff --git a/pgml-dashboard/src/utils/cookies.rs b/pgml-dashboard/src/utils/cookies.rs index 370e3d4b1..b1ea2404e 100644 --- a/pgml-dashboard/src/utils/cookies.rs +++ b/pgml-dashboard/src/utils/cookies.rs @@ -1,25 +1,52 @@ use chrono; use rocket::http::{Cookie, CookieJar}; use rocket::serde::{Deserialize, Serialize}; +use time::Duration; +/// Session data. +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct Notifications { + /// App-wide notifications. + notifications: Vec, +} + +/// App-wide notifications. #[derive(Debug, Clone, Deserialize, Serialize, Default, PartialEq)] pub struct NotificationCookie { + /// Unique ID of the notification. pub id: String, + /// Time the notification was viewed. Used for reshowing the notification. pub time_viewed: Option>, + /// Time the notification modal was viewed. Used for reshowing the notification modal. pub time_modal_viewed: Option>, } -pub struct Notifications {} +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct NotificationsCookieOld { + pub notifications: Vec, +} + +impl From for NotificationCookie { + fn from(old: NotificationsCookieOld) -> Self { + NotificationCookie { + id: old.notifications[0].clone(), + time_viewed: None, + time_modal_viewed: None, + } + } +} impl Notifications { - pub fn update_viewed(all_desired_notifications: &Vec, cookies: &CookieJar<'_>) { - let session = Notifications::safe_serialize_session(all_desired_notifications); + /// Update the viewed notifications in the session. + pub fn update_viewed(notifications: &[NotificationCookie], cookies: &CookieJar<'_>) { + let session = Notifications::safe_serialize_session(notifications); let mut cookie = Cookie::new("session", session); - cookie.set_max_age(::time::Duration::weeks(4)); + cookie.set_max_age(Duration::weeks(52 * 100)); // Keep the cookie "forever" cookies.add_private(cookie); } + /// Get viewed notifications from the session. pub fn get_viewed(cookies: &CookieJar<'_>) -> Vec { match cookies.get_private("session") { Some(session) => Notifications::safe_deserialize_session(session.value()), @@ -28,40 +55,21 @@ impl Notifications { } pub fn safe_deserialize_session(session: &str) -> Vec { - match serde_json::from_str::(session).unwrap_or_else(|_| { - serde_json::from_str::(&Notifications::safe_serialize_session(&vec![])).unwrap() - })["notifications"] - .as_array() - { - Some(items) => items - .into_iter() - .map(|notification| { - serde_json::from_str::(¬ification.to_string()).unwrap_or_else(|_| { - serde_json::from_str::(¬ification.to_string()) - .and_then(|id| { - Ok(NotificationCookie { - id, - time_viewed: None, - time_modal_viewed: None, - }) - }) - .unwrap_or_else(|_| NotificationCookie::default()) - }) - }) - .collect::>(), - _ => vec![], + match serde_json::from_str::(session) { + Ok(notifications) => notifications.notifications, + Err(_) => match serde_json::from_str::(session) { + Ok(notifications) => vec![NotificationCookie::from(notifications)], + Err(_) => vec![], + }, } } - pub fn safe_serialize_session(cookies: &Vec) -> String { - let serialized = cookies - .iter() - .map(|x| serde_json::to_string(x)) - .filter(|x| x.is_ok()) - .map(|x| x.unwrap()) - .collect::>(); + pub fn safe_serialize_session(notifications: &[NotificationCookie]) -> String { + let notifications = Notifications { + notifications: notifications.to_vec(), + }; - format!(r#"{{"notifications": [{}]}}"#, serialized.join(",")) + serde_json::to_string(¬ifications).unwrap() } } @@ -118,7 +126,7 @@ mod test { time_viewed: None, time_modal_viewed: None, }]; - let expected = r#"{"notifications": [{"id":"1","time_viewed":null,"time_modal_viewed":null}]}"#; + let expected = r#"{"notifications":[{"id":"1","time_viewed":null,"time_modal_viewed":null}]}"#; assert_eq!(Notifications::safe_serialize_session(&cookies), expected); } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy