don't hash the slug with the visit key

this makes it easy to see if a single visitor visited multiple pages
This commit is contained in:
Joe Ardent 2024-04-03 13:52:23 -07:00
parent d1f45e98ef
commit c7fc10069a
2 changed files with 11 additions and 6 deletions

View File

@ -1,8 +1,9 @@
create table if not exists hits ( create table if not exists hits (
id integer primary key, id integer primary key,
page text not null, -- the slug from the page page text not null, -- the slug from the page
hit_key text not null unique, visit_key text not null unique,
viewed timestamp not null default CURRENT_TIMESTAMP viewed timestamp not null default CURRENT_TIMESTAMP,
unique(page, visit_key)
); );
create index if not exists hits_page_dex on hits(page); create index if not exists hits_page_dex on hits(page);

View File

@ -103,15 +103,19 @@ async fn register_hit(
// we can't just enumerate all the possible hashes based on IP, page, and // we can't just enumerate all the possible hashes based on IP, page, and
// time alone. // time alone.
let salt = *SESSION_SALT; let salt = *SESSION_SALT;
let key = format!("{now}{host}{slug}{salt}").into_bytes(); let key = format!("{now}{host}{salt}").into_bytes();
let key = hex::encode(shasum(&key)); let key = hex::encode(shasum(&key));
let tx = db.begin().await; let tx = db.begin().await;
if let Ok(mut tx) = tx { if let Ok(mut tx) = tx {
match sqlx::query!("insert into hits (page, hit_key) values (?, ?)", slug, key,) match sqlx::query!(
.execute(&mut *tx) "insert into hits (page, visit_key) values (?, ?)",
.await slug,
key,
)
.execute(&mut *tx)
.await
{ {
Ok(_) => tx.commit().await.unwrap_or_default(), Ok(_) => tx.commit().await.unwrap_or_default(),
_ => { /* whatevs, fine */ } _ => { /* whatevs, fine */ }