fix: always same avatar for anon users

This commit is contained in:
əlemi 2024-01-04 04:56:15 +01:00
parent 76ae5bcdd2
commit 189226f841
Signed by: alemi
GPG key ID: A4895B84D311642C
3 changed files with 1 additions and 12 deletions

1
Cargo.lock generated
View file

@ -906,7 +906,6 @@ dependencies = [
"html-escape", "html-escape",
"mail-send", "mail-send",
"md-5", "md-5",
"rand",
"sailfish", "sailfish",
"serde", "serde",
"serde-inline-default", "serde-inline-default",

View file

@ -15,7 +15,6 @@ edition = "2021"
# core # core
tracing = "0.1" tracing = "0.1"
tracing-subscriber = "0.3" tracing-subscriber = "0.3"
rand = "0.8"
md-5 = "0.10" md-5 = "0.10"
toml = "0.8" toml = "0.8"
chrono = { version = "0.4", features = ["serde"] } chrono = { version = "0.4", features = ["serde"] }

View file

@ -1,5 +1,4 @@
use md5::{Md5, Digest}; use md5::{Md5, Digest};
use rand::{distributions::Alphanumeric, Rng};
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use sqlx::Row; use sqlx::Row;
@ -52,7 +51,7 @@ pub struct PageView {
impl From<&Page> for PageView { impl From<&Page> for PageView {
fn from(page: &Page) -> Self { fn from(page: &Page) -> Self {
let mut hasher = Md5::new(); let mut hasher = Md5::new();
hasher.update(page.contact.as_deref().unwrap_or(&random_string(36)).as_bytes()); hasher.update(page.contact.as_deref().unwrap_or(&format!("{} - {}", page.body, page.timestamp)).as_bytes());
let avatar = format!("{:x}", hasher.finalize()); let avatar = format!("{:x}", hasher.finalize());
let url = match page.contact.as_deref() { let url = match page.contact.as_deref() {
@ -127,11 +126,3 @@ fn _non_empty_string(input: String) -> Option<String> {
false => Some(input), false => Some(input),
} }
} }
fn random_string(len: usize) -> String {
rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(len)
.map(char::from)
.collect()
}