From 635716c04bb0ebdbe5c1bb6f794461df70d26aec Mon Sep 17 00:00:00 2001 From: trotFunky Date: Fri, 26 Jul 2024 00:45:53 +0100 Subject: [PATCH] vote: Change graph display condition, don't fetch for admin Now, the graph will only show when everyone has voted and the week is no longer the last one active. This is to minimize information gathering by looking during voting. Don't fetch the vote data for the admin : it is not used, so no need to query the database. Update README with new TODOs and clean up main includes a bit. --- README.md | 2 ++ src/auth.rs | 3 ++- src/main.rs | 5 +++-- src/vote.rs | 10 ++++++++-- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index cb8a569..800fc5e 100644 --- a/README.md +++ b/README.md @@ -10,12 +10,14 @@ A list of things that could be implemented/added to the application, some of the - [ ] Being able to change from one week to the next - [ ] Create new weeks for the admin - [ ] Proper week redirection + - [ ] Correctly handle non-existing week number - [x] Add introduction to the weekly truths - [ ] Bundle static assets in the binary - [ ] Move the database queries to their own functions - [ ] Cache those results - [ ] Centralize Markdown parsing ? - [ ] Use fairings for the different elements ? + - [ ] Use guards for User calls ? # Dependencies diff --git a/src/auth.rs b/src/auth.rs index ac909f0..1fd62fa 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -86,7 +86,8 @@ pub async fn get_user(week: u8, db: &mut Connection, cookies: &CookieJar<'_> (String::new(), false) }; - let votes: Vec = if logged_in { + // TODO: Move to src/vote.rs + let votes: Vec = if logged_in && !is_admin { sqlx::query_as("SELECT Votes.* FROM Votes JOIN Truths ON Votes.truth_id == Truths.id AND Truths.week == $1 WHERE voter_id == $2 ORDER BY Truths.number;") .bind(week) .bind(&id_str) diff --git a/src/main.rs b/src/main.rs index da44b02..6915128 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,8 @@ use rocket_db_pools::{sqlx, sqlx::Row, Database, Connection}; use sqlx::Error; mod auth; +use auth::User; + mod truth; mod vote; mod week; @@ -19,9 +21,8 @@ mod week; mod database; mod database_records; -use database::Db; use database_records::*; -use auth::User; +use database::Db; #[get("/")] async fn index(mut db: Connection) -> Redirect { diff --git a/src/vote.rs b/src/vote.rs index d4e5d42..831068c 100644 --- a/src/vote.rs +++ b/src/vote.rs @@ -2,6 +2,7 @@ use std::collections::hash_map::Entry; use std::collections::HashMap; use rocket::fairing::AdHoc; use rocket::form::Form; +use rocket::futures::TryFutureExt; use rocket::http::CookieJar; use rocket::response::Redirect; use rocket::serde::{Serialize, Deserialize}; @@ -61,7 +62,7 @@ pub async fn vote(week: u8, form: Form, } None => { debug!("Player {:?} voting {voted_id} for truth {truth_id}", user.id); - // TODO: Find a way to use only one statement ? + // TODO: Find a way to use only one statement ? --> query_many // Cannot all launch and await because all connect to DB match sqlx::query("INSERT INTO Votes (truth_id, voter_id, voted_id) VALUES ($1, $2, $3);") .bind(truth_id) @@ -110,7 +111,12 @@ pub async fn fetch_vote_data(week: u8, mut db: Connection) -> Opti }); let vote_count = raw_votes.iter().fold(0, |count, votes| {count + votes.votes}); - if vote_count < 17 { + // Only show the graph if we have all the votes and this is not the last week. + // FIXME: Make the 42 not hardcoded + if vote_count < 42 || week != sqlx::query_scalar("SELECT number from Weeks WHERE number == $1 AND is_last_week == 1;") + .bind(week) + .fetch_optional(&mut **db) + .await.unwrap_or(Some(0)).unwrap_or(0) { return None; }