Skip to content

make session backwards compatible #1528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
make session backwards compatible
  • Loading branch information
chillenberger committed Jun 14, 2024
commit a4b3e743557d88c946b7c03fd1445df02dc4ab29
107 changes: 2 additions & 105 deletions pgml-dashboard/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pgml-dashboard/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ mod test {
async fn rocket() -> Rocket<Build> {
dotenv::dotenv().ok();

pgml_dashboard::migrate(Cluster::default(None).pool()).await.unwrap();
pgml_dashboard::migrate(Cluster::default().pool()).await.unwrap();

let mut site_search = markdown::SiteSearch::new()
.await
Expand Down
46 changes: 26 additions & 20 deletions pgml-dashboard/src/utils/cookies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,14 @@ pub struct NotificationCookie {
pub time_modal_viewed: Option<chrono::DateTime<chrono::Utc>>,
}

impl std::fmt::Display for NotificationCookie {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut rsp = format!(r#"{{"id": "{}""#, self.id.clone());
if self.time_viewed.is_some() {
rsp.push_str(&format!(r#", "time_viewed": "{}""#, self.time_viewed.clone().unwrap()));
}
if self.time_modal_viewed.is_some() {
rsp.push_str(&format!(
r#", "time_modal_viewed": "{}""#,
self.time_modal_viewed.clone().unwrap()
));
}
rsp.push_str("}");
return write!(f, "{}", rsp);
}
}

pub struct Notifications {}

impl Notifications {
pub fn update_viewed(new: &Vec<NotificationCookie>, cookies: &CookieJar<'_>) {
let serialized = new.iter().map(|x| x.to_string()).collect::<Vec<String>>();
let serialized = new
.iter()
.map(|x| serde_json::to_string(x).unwrap())
.collect::<Vec<String>>();

let mut cookie = Cookie::new("session", format!(r#"{{"notifications": [{}]}}"#, serialized.join(",")));
cookie.set_max_age(::time::Duration::weeks(4));
Expand All @@ -40,10 +26,30 @@ impl Notifications {
pub fn get_viewed(cookies: &CookieJar<'_>) -> Vec<NotificationCookie> {
let viewed: Vec<NotificationCookie> = match cookies.get_private("session") {
Some(session) => {
match serde_json::from_str::<serde_json::Value>(session.value()).unwrap()["notifications"].as_array() {
match serde_json::from_str::<serde_json::Value>(session.value())
.unwrap_or_else(|_| serde_json::from_str::<serde_json::Value>(r#"{"notifications": []}"#).unwrap())
["notifications"]
.as_array()
{
Some(items) => items
.into_iter()
.map(|x| serde_json::from_str::<NotificationCookie>(&x.to_string()).unwrap())
.map(|x| {
serde_json::from_str::<NotificationCookie>(&x.to_string()).unwrap_or_else(|_| {
serde_json::from_str::<String>(&x.to_string())
.and_then(|z| {
Ok(NotificationCookie {
id: z,
time_viewed: None,
time_modal_viewed: None,
})
})
.unwrap_or_else(|_| NotificationCookie {
id: "".to_string(),
time_viewed: None,
time_modal_viewed: None,
})
})
})
.collect::<Vec<NotificationCookie>>(),
_ => vec![],
}
Expand Down