feat: add date, md5, url server-side on insertion

This commit is contained in:
əlemi 2023-12-23 03:15:06 +01:00
parent 4943339964
commit 7aee304b70
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 29 additions and 2 deletions

View file

@ -16,9 +16,12 @@ serde_json = "1.0.107"
tokio = { version = "1.33.0", features = ["macros", "rt-multi-thread"] }
tracing = "0.1.39"
tracing-subscriber = "0.3.17"
thiserror = "1.0.51"
chrono = { version = "0.4.31", features = ["serde"] }
uuid = { version = "1.6.1", features = ["v4", "fast-rng"] }
md-5 = "0.10.6"
# telegram provider
teloxide = { version = "0.12.2", features = ["macros"], optional = true }
thiserror = "1.0.51"
[features]
default = []

View file

@ -1,7 +1,10 @@
use std::sync::Arc;
use axum::{http::StatusCode, Json, Form, Router, routing::{put, post, get}, extract::{State, Query}, response::IntoResponse};
use chrono::Utc;
use md5::{Md5, Digest};
use tokio::sync::RwLock;
use uuid::Uuid;
use crate::{notifications::NotificationProcessor, model::{GuestBookPage, Acknowledgement, PageOptions, Insertion}, storage::StorageStrategy};
@ -37,7 +40,28 @@ impl Context {
}
}
async fn send_suggestion(payload: Suggestion, state: SafeContext) -> impl IntoResponse {
async fn send_suggestion(payload: Insertion, state: SafeContext) -> impl IntoResponse {
let mut hasher = Md5::new();
let id = payload.contact.clone().unwrap_or(Uuid::new_v4().to_string());
hasher.update(id.as_bytes());
let avatar = hasher.finalize();
let page = GuestBookPage {
avatar: format!("{:x}", avatar),
author: payload.author.map(|x| x[..25].to_string()), // TODO don't hardcode char limits!
contact: payload.contact.clone().map(|x| x[..50].to_string()),
body: payload.body,
date: Utc::now(),
url: match payload.contact {
None => None,
Some(c) => if c.starts_with("http") {
Some(c)
} else if c.contains('@') {
Some(format!("mailto:{}", c))
} else {
None
}
},
};
let mut lock = state.write().await;
lock.process(&page).await;
match lock.storage.archive(page).await {