Added date validation for agent shortcode links

This commit is contained in:
Franklin 2023-05-03 16:45:59 -04:00
parent e63f2eb515
commit e1bf4c3ea4
2 changed files with 31 additions and 11 deletions

View File

@ -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"

View File

@ -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::<Utc>::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 => {}
}