Skip to content

Commit a4b3e74

Browse files
committed
make session backwards compatible
1 parent 43bda76 commit a4b3e74

File tree

3 files changed

+29
-126
lines changed

3 files changed

+29
-126
lines changed

pgml-dashboard/Cargo.lock

Lines changed: 2 additions & 105 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pgml-dashboard/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ mod test {
137137
async fn rocket() -> Rocket<Build> {
138138
dotenv::dotenv().ok();
139139

140-
pgml_dashboard::migrate(Cluster::default(None).pool()).await.unwrap();
140+
pgml_dashboard::migrate(Cluster::default().pool()).await.unwrap();
141141

142142
let mut site_search = markdown::SiteSearch::new()
143143
.await

pgml-dashboard/src/utils/cookies.rs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,14 @@ pub struct NotificationCookie {
99
pub time_modal_viewed: Option<chrono::DateTime<chrono::Utc>>,
1010
}
1111

12-
impl std::fmt::Display for NotificationCookie {
13-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14-
let mut rsp = format!(r#"{{"id": "{}""#, self.id.clone());
15-
if self.time_viewed.is_some() {
16-
rsp.push_str(&format!(r#", "time_viewed": "{}""#, self.time_viewed.clone().unwrap()));
17-
}
18-
if self.time_modal_viewed.is_some() {
19-
rsp.push_str(&format!(
20-
r#", "time_modal_viewed": "{}""#,
21-
self.time_modal_viewed.clone().unwrap()
22-
));
23-
}
24-
rsp.push_str("}");
25-
return write!(f, "{}", rsp);
26-
}
27-
}
28-
2912
pub struct Notifications {}
3013

3114
impl Notifications {
3215
pub fn update_viewed(new: &Vec<NotificationCookie>, cookies: &CookieJar<'_>) {
33-
let serialized = new.iter().map(|x| x.to_string()).collect::<Vec<String>>();
16+
let serialized = new
17+
.iter()
18+
.map(|x| serde_json::to_string(x).unwrap())
19+
.collect::<Vec<String>>();
3420

3521
let mut cookie = Cookie::new("session", format!(r#"{{"notifications": [{}]}}"#, serialized.join(",")));
3622
cookie.set_max_age(::time::Duration::weeks(4));
@@ -40,10 +26,30 @@ impl Notifications {
4026
pub fn get_viewed(cookies: &CookieJar<'_>) -> Vec<NotificationCookie> {
4127
let viewed: Vec<NotificationCookie> = match cookies.get_private("session") {
4228
Some(session) => {
43-
match serde_json::from_str::<serde_json::Value>(session.value()).unwrap()["notifications"].as_array() {
29+
match serde_json::from_str::<serde_json::Value>(session.value())
30+
.unwrap_or_else(|_| serde_json::from_str::<serde_json::Value>(r#"{"notifications": []}"#).unwrap())
31+
["notifications"]
32+
.as_array()
33+
{
4434
Some(items) => items
4535
.into_iter()
46-
.map(|x| serde_json::from_str::<NotificationCookie>(&x.to_string()).unwrap())
36+
.map(|x| {
37+
serde_json::from_str::<NotificationCookie>(&x.to_string()).unwrap_or_else(|_| {
38+
serde_json::from_str::<String>(&x.to_string())
39+
.and_then(|z| {
40+
Ok(NotificationCookie {
41+
id: z,
42+
time_viewed: None,
43+
time_modal_viewed: None,
44+
})
45+
})
46+
.unwrap_or_else(|_| NotificationCookie {
47+
id: "".to_string(),
48+
time_viewed: None,
49+
time_modal_viewed: None,
50+
})
51+
})
52+
})
4753
.collect::<Vec<NotificationCookie>>(),
4854
_ => vec![],
4955
}

0 commit comments

Comments
 (0)