From 7aee304b7058a94abb2961e40305175fedf65bb4 Mon Sep 17 00:00:00 2001 From: alemi Date: Sat, 23 Dec 2023 03:15:06 +0100 Subject: [PATCH] feat: add date, md5, url server-side on insertion --- Cargo.toml | 5 ++++- src/routes.rs | 26 +++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f1c48e3..4bfc089 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 = [] diff --git a/src/routes.rs b/src/routes.rs index 3fad4b0..63aa786 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -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 {