time based fix

This commit is contained in:
2021-12-22 14:46:06 +01:00
parent 8cee6579e2
commit 33829768eb
5 changed files with 28 additions and 13 deletions

View File

@@ -7,7 +7,7 @@ pub struct Note {
pub meta: String,
pub contents: String,
pub views: Option<u8>,
pub expiration: Option<u64>,
pub expiration: Option<u32>,
}
#[derive(Serialize, Deserialize, Clone)]

View File

@@ -6,11 +6,11 @@ use crate::note::{generate_id, Note, NoteInfo, NotePublic};
use crate::size::LIMIT;
use crate::store;
fn now() -> u64 {
pub fn now() -> u32 {
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs()
.as_secs() as u32
}
#[derive(Serialize, Deserialize)]
@@ -86,10 +86,12 @@ async fn delete(path: web::Path<NotePath>) -> impl Responder {
}
_ => {}
}
let n = now();
match changed.expiration {
Some(e) => {
store::del(&p.id.clone());
if e < now() {
if e < n {
store::del(&p.id.clone());
return HttpResponse::BadRequest().finish();
}
}

View File

@@ -1,5 +1,6 @@
use memcache;
use crate::note::now;
use crate::note::Note;
lazy_static! {
@@ -12,7 +13,11 @@ lazy_static! {
pub fn set(id: &String, note: &Note) {
let serialized = serde_json::to_string(&note.clone()).unwrap();
CLIENT.set(id, serialized, 0).unwrap();
let expiration: u32 = match note.expiration {
Some(e) => e - now(),
None => 0,
};
CLIENT.set(id, serialized, expiration).unwrap();
}
pub fn get(id: &String) -> Option<Note> {