From e1bf4c3ea4b09f7948a0b577aaaf6bda92996aa2 Mon Sep 17 00:00:00 2001 From: Franklin Date: Wed, 3 May 2023 16:45:59 -0400 Subject: [PATCH] Added date validation for agent shortcode links --- Cargo.toml | 7 +++++++ src/pages/details.rs | 35 ++++++++++++++++++++++++----------- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 355b1a8..2e9b0f5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,3 +34,10 @@ chrono = "0.4.23" # Core jl-types = { path = "../jl-types", features = ["wasm"] } + + +[profile.release] +lto = true +codegen-units = 1 +panic = "abort" +opt-level = "z" \ No newline at end of file diff --git a/src/pages/details.rs b/src/pages/details.rs index 8792929..6ce68df 100644 --- a/src/pages/details.rs +++ b/src/pages/details.rs @@ -1,3 +1,6 @@ +use std::str::FromStr; + +use chrono::{Utc, DateTime}; use jl_types::{ domain::{agent::Agent, unit::Unit, unit_type::UnitType}, dto::listing::Listing, @@ -30,7 +33,7 @@ pub fn details_page(props: &DetailsPageProps) -> Html { for query_param in query_params { if query_param.0 == String::from("shortcode") { // Store shortcode in localstorage - match storage::store_in_local_storage(StorageKey::AgentShortcode, &query_param.1) { + match storage::store_in_local_storage(StorageKey::AgentShortcode, &format!("{}|{}", &query_param.1, Utc::now().to_string())) { Ok(_) => {} Err(error) => { log::error!("Error storing agent shortcode in localstorage\n: {}", error) @@ -43,17 +46,27 @@ pub fn details_page(props: &DetailsPageProps) -> Html { match persisted_shortcode_res { Ok(persisted_shortcode_opt) => { match persisted_shortcode_opt { - Some(persisted_shortcode) => { + Some(persisted_shortcode_with_date) => { // Get agent from server - let override_agent_handle = override_agent_handle.clone(); - wasm_bindgen_futures::spawn_local(async move { - match get_agent_with_shortcode(&persisted_shortcode).await { - Ok(agent) => override_agent_handle.set(Some(agent)), - Err(error) => { - error!("Error fetching agent with shortcode. Error: {:?}", error) - } - }; - }) + let shortcode_date_split: Vec<&str> = persisted_shortcode_with_date.split('|').collect(); + if let Some(creation_date_str) = shortcode_date_split.get(1) { + + if let Ok(date) = DateTime::::from_str(&creation_date_str) { + if Utc::now().timestamp_millis() - date.timestamp_millis() < 24 * 60 * 60 * 1000 { + let override_agent_handle = override_agent_handle.clone(); + let shortcode = shortcode_date_split.first().expect("Split had a first element but not a second?").to_string(); + wasm_bindgen_futures::spawn_local(async move { + match get_agent_with_shortcode(&shortcode).await { + Ok(agent) => override_agent_handle.set(Some(agent)), + Err(error) => { + error!("Error fetching agent with shortcode. Error: {:?}", error) + } + }; + }) + } + } + } + } None => {} }