Compare commits

..

No commits in common. "0c162d3b423f68579f803df1d456e126940612b6" and "4e1b13dc85ac0be1c9d71edb6e83e1a623c3680a" have entirely different histories.

8 changed files with 28 additions and 76 deletions

2
Cargo.lock generated
View file

@ -578,7 +578,7 @@ checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0"
[[package]]
name = "fabula_votes_server"
version = "1.3.0"
version = "1.2.1"
dependencies = [
"argon2",
"blake2",

View file

@ -3,7 +3,7 @@ name = "fabula_votes_server"
license = "MPL-2.0"
readme = "README.md"
authors = ["trotFunky"]
version = "1.3.0"
version = "1.2.1"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View file

@ -185,32 +185,6 @@ pub async fn login(week: u8, form: Form<AuthForm>, mut db: Connection<Db>, cooki
Redirect::to(uri!(week::week(week)))
}
#[post("/<week>/logout")]
pub async fn logout(week: u8, mut db: Connection<Db>, cookies: &CookieJar<'_>) -> Redirect {
let auth_token: Option<String> = match cookies.get_private("auth_token") {
Some(cookie) => Some(cookie.value().to_string()),
None => None
};
// Should not be able to log out ?
if auth_token.is_none() {
return Redirect::to(uri!(week::week(week)))
}
match sqlx::query("DELETE FROM AuthTokens WHERE token == $1;")
.bind(auth_token)
.execute(&mut **db)
.await {
Ok(_) => debug!("Auth token deletion successful"),
Err(error) => debug!("Auth token could not be removed ({error}), proceeding anyway.")
}
cookies.remove_private("auth_token");
cookies.remove_private("auth_id");
Redirect::to(uri!(week::week(week)))
}
pub fn bypass_auth_debug(cookies: &CookieJar<'_>) {
if cookies.get_private("auth_token").is_some() {
return

View file

@ -1,13 +1,18 @@
#[macro_use] extern crate rocket;
use rocket::{Rocket, Build, futures};
use rocket::fs::{FileServer, relative};
use rocket::http::CookieJar;
use rocket::response::Redirect;
use rocket::serde::{Serialize, Deserialize, json::Json};
use rocket_dyn_templates::Template;
use rocket_dyn_templates::{Template, context};
use rocket_db_pools::{sqlx, sqlx::Row, Connection};
use rocket_db_pools::{sqlx, sqlx::Row, Database, Connection};
use sqlx::Error;
mod auth;
use auth::User;
mod truth;
mod vote;
@ -16,6 +21,7 @@ mod week;
mod database;
mod database_records;
use database_records::*;
use database::Db;
#[get("/")]
@ -40,7 +46,7 @@ fn rocket() -> _ {
vote::fetch_vote_data, vote::vote,
truth::create_truth, truth::edit_truth,
week::week, week::update_week, week::set_last_week, week::create_week,
auth::login, auth::logout])
auth::login])
.attach(database::stage())
.attach(Template::fairing())
}

View file

@ -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};
@ -90,14 +91,12 @@ pub async fn vote(week: u8, form: Form<VoteForm>,
#[derive(Serialize, Deserialize)]
#[serde(crate = "rocket::serde")]
pub struct VoteData {
truth_count: u8,
votes: HashMap<String, Vec<u8>>,
}
// TODO: Cache vote count ? Maintain in state ?
#[get("/<week>/votes", format = "application/json")]
pub async fn fetch_vote_data(week: u8, mut db: Connection<database::Db>, cookies: &CookieJar<'_>) -> Option<Json<VoteData>> {
let user = auth::get_user(week, &mut db, cookies).await;
pub async fn fetch_vote_data(week: u8, mut db: Connection<database::Db>) -> Option<Json<VoteData>> {
let raw_votes: Vec<VotingData> = sqlx::query_as("
SELECT Players.name as votes_for, Truths.number as truth_number, count(*) as votes FROM Votes
JOIN Players ON Votes.voted_id == Players.id
@ -111,25 +110,12 @@ pub async fn fetch_vote_data(week: u8, mut db: Connection<database::Db>, cookies
Vec::<VotingData>::new()
});
let truth_count: u8 = sqlx::query_scalar("SELECT count(id) from Truths WHERE week == $1;")
.bind(week)
.fetch_one(&mut **db)
.await.unwrap_or(0);
let player_count: u8 = sqlx::query_scalar("SELECT count(id) from Players WHERE is_admin == 0;")
.fetch_one(&mut **db)
.await.unwrap_or(0);
// Each player should have a truth assigned to them which they cannot vote on.
let max_vote_count = truth_count * (player_count - 1);
let vote_count = raw_votes.iter().fold(0, |count, votes| {count + votes.votes});
// Only show the graph if we have all the votes and this is not the last week.
if !user.is_admin && (max_vote_count == 0
|| vote_count < max_vote_count
|| week == sqlx::query_scalar("SELECT number from Weeks WHERE is_last_week == 1;")
.fetch_optional(&mut **db)
.await.unwrap_or(Some(0)).unwrap_or(0)) {
// FIXME: Make the 42 not hardcoded
if vote_count < 42 || week == sqlx::query_scalar("SELECT number from Weeks WHERE is_last_week == 1;")
.fetch_optional(&mut **db)
.await.unwrap_or(Some(0)).unwrap_or(0) {
return None;
}
@ -161,7 +147,7 @@ pub async fn fetch_vote_data(week: u8, mut db: Connection<database::Db>, cookies
next_truth_number = raw_vote.truth_number + 1;
}
Some(Json(VoteData{truth_count: truth_count, votes: vote_data}))
Some(Json(VoteData{votes: vote_data}))
}
// FIXME:

View file

@ -23,12 +23,6 @@
padding-bottom: 2em;
}
.individual_truth > h3 {
text-align: center;
padding-bottom: 4px;
border-bottom: slategray solid 0.15em;
}
.editor {
width: calc(100% - 1.25em); /* The width is calculated *inside* the padding, so adjust for it. */
height: 6eM;
@ -83,8 +77,8 @@
hr {
border: none;
border-top: 2px dotted slategray;
color: slategray;
border-top: 2px solid #9ec5fe;
color: #9ec5fe;
overflow: visible;
text-align: center;
height: 5px;

View file

@ -6,20 +6,18 @@ async function main() {
return;
}
let keys = []
const keys = ["Vérité 1", "Vérité 2", "Vérité 3", "Vérité 4", "Vérité 5", "Vérité 6", "Vérité 7"]
let datasets = []
try {
const vote_data = (await vote_response.json());
for (let player in vote_data.votes) {
const vote_data = (await vote_response.json()).votes;
for (let player in vote_data) {
datasets.push({
parsing: true,
label: player,
data: vote_data.votes[player],
data: vote_data[player],
})
}
for (let i = 1; i <= vote_data.truth_count; i++) {
keys.push("Vérité " + i)
}
} catch (error) {
console.error("Failed to parse vote data : \n\t" + error.message);
return;

View file

@ -36,17 +36,11 @@
{% set next_arrow_chara = '⟹' %}
{% endif %}
{# Remove the form if all votes are locked, to reduce confusion. #}
{% set lock_truth_form = user.votes | length + 1 == truths | length and week_data.is_last_week != true %}
<body>
<div class="top_bar">
<h1>{{ title }}</h1>
{% if user.logged_in == true %}
<form class="login" id="logout" action="/{{ week_data.number }}/logout" method="POST">
Connecté en tant que <b>{{ user.name }}</b>
<button form="logout">Déconnecter</button>
</form>
<p>Connecté en tant que <b>{{ user.name }}</b></p>
{% else %}
<form class="login" id="login" action="/{{ week_data.number }}/login" method="POST">
<label>Pseudo <input form="login" type="text" name="name"/></label>
@ -82,7 +76,7 @@
</form>
{% endif %}
</div>
{% if user.logged_in == true and user.is_admin == false and not lock_truth_form %}
{% if user.logged_in == true and user.is_admin == false %}
<form id="truths" action="/{{ week_data.number }}/vote" method="POST">
{% endif %}
@ -107,7 +101,7 @@
{% endif %}
{% endfor %}
{% if user.logged_in == true and user.is_admin == false and not lock_truth_form %}
{% if user.logged_in == true and user.is_admin == false %}
<br/>
<button form="truths">
{%- if user.logged_in == true and user.has_week_vote == true -%}