Compare commits

..

No commits in common. "a0b79a17ea0a583c6773db43b9dbc1b29b334aa9" and "8547312a78642e827f041dadc331ee476d6ff0cb" have entirely different histories.

10 changed files with 70 additions and 254 deletions

View file

@ -7,17 +7,15 @@ vote and who we think wrote them and see some stats !
A list of things that could be implemented/added to the application, some of them are needed for "feature completeness" !
- [x] Being able to change from one week to the next
- [x] Create new weeks for the admin
- [x] Proper week redirection
- [ ] Correctly handle non-existing week number
- [ ] Being able to change from one week to the next
- [ ] Create new weeks for the admin
- [ ] Proper week redirection
- [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

View file

@ -9,7 +9,7 @@ use argon2::{Argon2, PasswordHash, PasswordVerifier};
use blake2::{Blake2b512, Digest};
use blake2::digest::FixedOutput;
use crate::database_records::{AuthTokens, PlayerLoginInfo, Vote};
use crate::{database, week};
use crate::database;
use database::Db;
// TODO: Make FromRequest guard https://api.rocket.rs/v0.5/rocket/request/trait.FromRequest and split admin
@ -86,8 +86,7 @@ pub async fn get_user(week: u8, db: &mut Connection<Db>, cookies: &CookieJar<'_>
(String::new(), false)
};
// TODO: Move to src/vote.rs
let votes: Vec<Vote> = if logged_in && !is_admin {
let votes: Vec<Vote> = if logged_in {
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)
@ -126,8 +125,8 @@ pub struct AuthForm {
password: String
}
#[post("/<week>/login", data="<form>")]
pub async fn login(week: u8, form: Form<AuthForm>, mut db: Connection<Db>, cookies: &CookieJar<'_>) -> Redirect {
#[post("/login", data="<form>")]
pub async fn login(form: Form<AuthForm>, mut db: Connection<Db>, cookies: &CookieJar<'_>) -> Redirect {
let user_search: Result<PlayerLoginInfo, _> = sqlx::query_as("SELECT id, is_admin, name, pwd_hash FROM Players WHERE name == $1")
.bind(&form.name)
.fetch_one(&mut **db)
@ -136,7 +135,7 @@ pub async fn login(week: u8, form: Form<AuthForm>, mut db: Connection<Db>, cooki
if user_search.is_err() {
error!("Login failed : invalid user {:?}, err: {:?}", form.name, user_search.err());
cookies.add(("toast_error", "Impossible de se connecter !"));
return Redirect::to(uri!(week::week(week)));
return Redirect::to(uri!("/"));
}
let new_user = user_search.unwrap();
@ -144,7 +143,7 @@ pub async fn login(week: u8, form: Form<AuthForm>, mut db: Connection<Db>, cooki
if password_hash_parse.is_err() {
error!("Login failed : could not parse password hash {:?}", password_hash_parse.err());
cookies.add(("toast_error", "Impossible de se connecter !"));
return Redirect::to(uri!(week::week(week)));
return Redirect::to(uri!("/"));
}
let password_hash = password_hash_parse.unwrap();
@ -168,7 +167,7 @@ pub async fn login(week: u8, form: Form<AuthForm>, mut db: Connection<Db>, cooki
Err(error) => {
error!("Login failed : coult not store auth token in database : {error}");
cookies.add(("toast_error", "Impossible de se connecter !"));
return Redirect::to(uri!(week::week(week)));
return Redirect::to(uri!("/"));
}
}
@ -178,11 +177,11 @@ pub async fn login(week: u8, form: Form<AuthForm>, mut db: Connection<Db>, cooki
Err(err) => {
error!("Login failed : invalid password for {:?}\nError : {err}", new_user.name);
cookies.add(("toast_error", "Impossible de se connecter !"));
return Redirect::to(uri!(week::week(week)));
return Redirect::to(uri!("/"));
}
}
Redirect::to(uri!(week::week(week)))
Redirect::to(uri!("/"))
}
pub fn bypass_auth_debug(cookies: &CookieJar<'_>) {

View file

@ -12,8 +12,6 @@ use rocket_db_pools::{sqlx, sqlx::Row, Database, Connection};
use sqlx::Error;
mod auth;
use auth::User;
mod truth;
mod vote;
mod week;
@ -21,8 +19,9 @@ mod week;
mod database;
mod database_records;
use database_records::*;
use database::Db;
use database_records::*;
use auth::User;
#[get("/")]
async fn index(mut db: Connection<Db>) -> Redirect {
@ -42,11 +41,7 @@ async fn index(mut db: Connection<Db>) -> Redirect {
fn rocket() -> _ {
rocket::build()
.mount("/", FileServer::from(relative!("static_files")))
.mount("/", routes![index,
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])
.mount("/", routes![index, vote::fetch_vote_data, vote::vote, truth::create_truth, truth::edit_truth, week::week, week::update_week, auth::login])
.attach(database::stage())
.attach(Template::fairing())
}

View file

@ -6,7 +6,7 @@ use rocket_db_pools::{sqlx, Connection};
use pulldown_cmark::{Parser, Options};
use sqlx::Row;
use crate::{auth, database, week};
use crate::{auth, database};
#[derive(FromForm)]
pub struct TruthUpdateForm {
@ -20,7 +20,7 @@ pub async fn edit_truth(week: u8, truth_number: u8, form: Form<TruthUpdateForm>,
let user = auth::get_user(week, &mut db, cookies).await;
if !user.is_admin {
cookies.add(("toast_error", "Vous n'avez pas la permission de changer la vérité."));
return Redirect::to(uri!(week::week(week)));
return Redirect::to(uri!("/"));
}
let mut options = Options::empty();
@ -50,16 +50,16 @@ pub async fn edit_truth(week: u8, truth_number: u8, form: Form<TruthUpdateForm>,
}
};
Redirect::to(uri!(week::week(week)))
Redirect::to(uri!("/"))
}
#[post("/<week>/new_truth", data="<form>")]
#[post("/<week>/new", data="<form>")]
pub async fn create_truth(week: u8, form: Form<TruthUpdateForm>,
mut db: Connection<database::Db>, cookies: &CookieJar<'_>) -> Redirect {
let user = auth::get_user(week, &mut db, cookies).await;
if !user.is_admin {
cookies.add(("toast_error", "Vous n'avez pas la permission d'ajouter de vérité."));
return Redirect::to(uri!(week::week(week)));
return Redirect::to(uri!("/"));
}
let truth_number: u8 = match sqlx::query("SELECT max(number) from Truths WHERE week == $1;")
@ -76,7 +76,7 @@ pub async fn create_truth(week: u8, form: Form<TruthUpdateForm>,
if truth_number == 0 {
error!("Error while getting max truth number.");
cookies.add(("toast_error", "Erreur lors de l'ajout de la vérité..."));
return Redirect::to(uri!(week::week(week)));
return Redirect::to(uri!("/"));
}
let mut options = Options::empty();
@ -108,5 +108,5 @@ pub async fn create_truth(week: u8, form: Form<TruthUpdateForm>,
debug!("Truth was successfully added");
Redirect::to(uri!(week::week(week)))
Redirect::to(uri!("/"))
}

View file

@ -2,7 +2,6 @@ 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};
@ -10,7 +9,7 @@ use rocket::serde::json::Json;
use rocket_db_pools::{sqlx, Connection};
use crate::{auth, database, week};
use crate::{auth, database};
use crate::database_records::{Vote, VotingData};
#[derive(FromForm)]
@ -25,7 +24,7 @@ pub async fn vote(week: u8, form: Form<VoteForm>,
if !user.logged_in {
cookies.add(("toast_error", "Vous n'avez pas la permission de changer de vote."));
return Redirect::to(uri!(week::week(week)));
return Redirect::to(uri!("/"));
}
let filtered_votes = form.truth_votes.iter().filter_map(
@ -62,7 +61,7 @@ pub async fn vote(week: u8, form: Form<VoteForm>,
}
None => {
debug!("Player {:?} voting {voted_id} for truth {truth_id}", user.id);
// TODO: Find a way to use only one statement ? --> query_many
// TODO: Find a way to use only one statement ?
// 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)
@ -85,7 +84,7 @@ pub async fn vote(week: u8, form: Form<VoteForm>,
debug!("Vote successful")
}
Redirect::to(uri!(week::week(week)))
Redirect::to(uri!("/"))
}
#[derive(Serialize, Deserialize)]
@ -111,18 +110,11 @@ pub async fn fetch_vote_data(week: u8, mut db: Connection<database::Db>) -> Opti
});
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.
// 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) {
if vote_count < 17 {
return None;
}
// The hash map makes storing and retrieving the data really easy, *but*
// it does lose the order of the original array (which is sorted via the SQL).
let mut vote_data = HashMap::<String, Vec<u8>>::new();
let mut next_truth_number = 1;
for raw_vote in raw_votes {

View file

@ -6,7 +6,6 @@ use rocket::response::Redirect;
use rocket_db_pools::{sqlx, Connection};
use rocket_dyn_templates::{context, Template};
use sqlx::{Acquire, Executor};
use crate::auth;
use crate::auth::User;
use crate::database::Db;
@ -22,7 +21,7 @@ pub async fn week(week_number: u8, mut db: Connection<Db>, cookies: &CookieJar<'
.fetch_all(&mut **db).await {
Ok(v) => v,
Err(error) => {
error!("Some error while getting players : {error}");
println!("Some error while getting players : {error}");
Vec::<Player>::new()
}
}
@ -85,7 +84,7 @@ pub async fn update_week(week: u8, raw_intro: Form<String>,
let user = auth::get_user(week, &mut db, cookies).await;
if !user.is_admin {
cookies.add(("toast_error", "Vous n'avez pas la permission de changer la semaine."));
return Redirect::to(uri!(week(week)));
return Redirect::to(uri!("/"));
}
let mut options = Options::empty();
@ -113,100 +112,5 @@ pub async fn update_week(week: u8, raw_intro: Form<String>,
}
};
Redirect::to(uri!(week(week)))
}
#[post("/<week>/set_last")]
pub async fn set_last_week(week: u8, mut db: Connection<Db>, cookies: &CookieJar<'_>) -> Redirect {
let user = auth::get_user(week, &mut db, cookies).await;
if !user.is_admin {
cookies.add(("toast_error", "Vous n'avez pas la permission de changer la semaine."));
return Redirect::to(uri!(week(week)));
}
let add_error_cookie = ||
cookies.add(("toast_error", "Erreur lors du changement d'état de la semaine."));
let db_transaction_connection = db.begin().await;
if db_transaction_connection.is_err() {
error!("Could not start database transaction for last week change : {:?}", db_transaction_connection.unwrap_err());
add_error_cookie();
return Redirect::to(uri!(week(week)));
}
let mut db_transaction = db_transaction_connection.unwrap();
// Remove the last flag from other weeks, if set.
match db_transaction.execute("UPDATE Weeks SET is_last_week = 0 WHERE is_last_week == 1;")
.await {
Ok(_) => debug!("Succesfully cleared is_last_week"),
Err(error) => {
error!("Failed to clear last week status : {error}");
add_error_cookie();
return Redirect::to(uri!(week(week)));
}
};
// We should set one week, if not there's something wrong : rollback.
if match sqlx::query("UPDATE Weeks SET is_last_week = 1 WHERE number == $1;")
.bind(week)
.execute(&mut *db_transaction)
.await {
Ok(result) => result.rows_affected(),
Err(error) => {
error!("Error while setting last week status : {error}");
0
}
} == 1 {
db_transaction.commit().await.unwrap_or_else(|error| {
error!("Error while committing week is last transaction : {error}");
add_error_cookie();
})
} else {
db_transaction.rollback().await.unwrap_or_else(|error| {
error!("Error while rolling back week is last transaction : {error}");
add_error_cookie();
})
}
Redirect::to(uri!(week(week)))
}
#[get("/<week>/create")]
pub async fn create_week(week: u8, mut db: Connection<Db>, cookies: &CookieJar<'_>) -> Redirect {
let user = auth::get_user(week, &mut db, cookies).await;
if !user.is_admin {
cookies.add(("toast_error", "Vous n'avez pas la permission de changer la semaine."));
return Redirect::to(uri!(week(week - 1)));
}
let week_check: Result<Option<u8>, sqlx::Error> = sqlx::query_scalar("SELECT number from Weeks WHERE number == $1;")
.bind(week)
.fetch_optional(&mut **db)
.await;
if week_check.is_err() {
error!("Error while checking week existence : {:?}", week_check.unwrap_err());
cookies.add(("toast_error", "Erreur en vérifiant que la semaine n'existe pas déjà"));
return Redirect::to(uri!(week(week - 1)));
}
if week_check.unwrap().is_some() {
debug!("Week {week} already exists, not creating.");
return Redirect::to(uri!(week(week)));
}
match sqlx::query("INSERT INTO Weeks (number, is_last_week, rendered_text, raw_text) VALUES ($1, 0, \"\", \"\");")
.bind(week)
.execute(&mut **db)
.await {
Ok(_) => {
debug!("Succesfully created new week {week}");
Redirect::to(uri!(week(week)))
},
Err(error) => {
error!("Error while creating new week {week} : {error}");
cookies.add(("toast_error", "Erreur en créant la nouvelle selmaine."));
Redirect::to(uri!(week(week - 1)))
}
}
Redirect::to(uri!("/"))
}

View file

@ -24,7 +24,7 @@
}
.editor {
width: calc(100% - 1.25em); /* The width is calculated *inside* the padding, so adjust for it. */
width: 100%;
height: 6eM;
font-size: large;
padding: 0.5em;
@ -47,34 +47,6 @@
top: 25%;
}
.week_change:link, .week_change:visited {
color: black;
text-decoration: none;
transition: all .2s ease-in-out;
}
.week_change:hover, .week_change:focus {
color: mediumpurple;
text-shadow: 0 2px 2px slategray;
}
.week_change_hidden {
padding-left: 0.5eM;
padding-right: 0.5eM;
}
@media (orientation: portrait) {
.truth_list {
width: 60vw;
}
.graph {
width: 35vw;
}
}
/* Global styling */
hr {
border: none;
border-top: 2px solid #9ec5fe;
@ -102,3 +74,13 @@ body {
body > h2 {
padding-left: 0.25eM;
}
@media (orientation: portrait) {
.truth_list {
width: 60vw;
}
.graph {
width: 35vw;
}
}

View file

@ -1,5 +1,14 @@
const limit_ratio = 1.3
const colors = ['#b6b8fc', '#b6f4fc', '#fcb6cc', '#e0fcb6', '#fcdcb6', '#b6fcc8', '#f0b6fc']
// const names = ["Bystus", "Dory", "Fen", "Lucky", "Nico", "Peran", "trot"]
//
//
// let data = [];
// for (let i = 0; i < 7; i++) {
// data.push({
// parsing: true,
// label: names[i],
// data: Array.from(keys, () => Math.round(Math.random()*1.33))})
// }
async function main() {
const vote_response = await fetch(document.URL+"/votes");
if (!vote_response.ok) {
@ -23,41 +32,19 @@ async function main() {
return;
}
// Sort by label to maintain the same graph order, as it goes through a hash map in the backend.
datasets.sort((a, b) => a.label > b.label)
for (let i = 0; i < datasets.length; i++) {
datasets[i].backgroundColor = colors[i % colors.length]
}
const chart_canvas = document.getElementById("vote_chart")
let chart
let previous_orientation
function create_chart(keys, data) {
let main_axis;
let aspect_ratio;
let orientation
if (window.innerWidth / window.innerHeight > limit_ratio) {
orientation = "landscape"
if (window.innerWidth > window.innerHeight) {
main_axis = 'x'
aspect_ratio = 2
} else {
orientation = "portrait"
main_axis = 'y'
aspect_ratio = 0.5
}
// Don't re-create the chart for no reason.
if (orientation === previous_orientation) {
console.log("bijour")
return;
} else {
console.log("badour")
}
previous_orientation = orientation
if ( chart ) {
chart.destroy()
}
@ -79,13 +66,6 @@ async function main() {
y: {
stacked: true
}
},
plugins: {
title: {
display: true,
position: 'bottom',
text: 'Répartition des suppositions'
}
}
}
})
@ -95,7 +75,7 @@ async function main() {
create_chart(keys, datasets)
}
const orientation_query = matchMedia(`(aspect-ratio < ${limit_ratio})`);
const orientation_query = matchMedia("screen and (orientation:portrait)");
orientation_query.onchange = update_chart_ratio
create_chart(keys, datasets)

View file

@ -12,29 +12,12 @@
<script defer="defer" type="text/javascript" src="/vote_chart.js"></script>
</head>
{#{% import "week_change_arrows" as week_macro %}#}
{# For some reason the import does not work ? Figure it out at some point... #}
{%- macro display(display_character, to, enabled) -%}
{%- set class = "week_change" -%}
{%- if enabled == true %}
{% set target = ("href=/" ~ to) %}
{%- else -%}
{% set class = class ~ " week_change_hidden" -%}
{% set target = "" %}
{%- endif -%}
<a {{ target }} class="{{ class }}">{%- if enabled == true -%}{{- display_character -}}{%- endif -%}</a>
{%- endmacro display -%}
{% set back_arrow_enabled = week_data.number > 1 %}
{% set next_arrow_enabled = (week_data.is_last_week != true or user.is_admin == true) %}
{% set next_arrow_href = (week_data.number + 1) %}
{% if user.is_admin == true %}
{% set next_arrow_href = next_arrow_href ~ "/create" %}
{% set next_arrow_chara = '⥅' %}
{# Check if the user has a vote in advance, for readability #}
{% if user.logged_in == true and user.has_week_vote == true%}
{% set has_vote = true %}
{% else %}
{% set next_arrow_chara = '⟹' %}
{% endif %}
{% set has_vote = false %}
{% endif -%}
<body>
<div class="top_bar">
@ -42,7 +25,7 @@
{% if user.logged_in == true %}
<p>Connecté en tant que <b>{{ user.name }}</b></p>
{% else %}
<form class="login" id="login" action="/{{ week_data.number }}/login" method="POST">
<form class="login" id="login" action="/login" method="POST">
<label>Pseudo <input form="login" type="text" name="name"/></label>
<label>Mot de passe <input form="login" type="password" name="password"/></label>
<button form="login">Se connecter</button>
@ -50,18 +33,9 @@
{% endif %}
</div>
<h2>{{- self::display(display_character='⟸', to=(week_data.number - 1), enabled=back_arrow_enabled) }}
Semaine {{ week_data.number }}
{{- self::display(display_character=next_arrow_chara, to=next_arrow_href, enabled=next_arrow_enabled) -}}</h2>
<h2>Semaine {{ week_data.number }}</h2>
<div class="page_body">
<div class="truth_list">
{% if user.is_admin == true and week_data.is_last_week != true %}
<form action="/{{ week_data.number }}/set_last" method="post">
<button>
Définir comme dernière semaine active
</button>
</form>
{% endif %}
<div class="week_intro">
{{ week_data.rendered_text | safe }}
{%- if user.is_admin == true -%}
@ -104,7 +78,7 @@
{% 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 -%}
{%- if has_vote == true -%}
Changer de vote
{% else %}
À voter !
@ -117,7 +91,7 @@
{% if user.is_admin == true %}
<div class="individual_truth">
<h3>Nouvelle vérité</h3>
<form action="/{{ week_data.number }}/new_truth" method="POST">
<form action="/{{ week_data.number }}/new" method="POST">
{% include "truth_editor" %}
</form>
</div>

View file

@ -1,9 +1,3 @@
{%- set is_disabled = "" -%}
{# If we are not during the active week, prevent changing vote but not sending a missing one. #}
{%- if week_data.is_last_week != true and user.votes | filter(attribute="truth_id", value=truth.id) -%}
{%- set is_disabled = "disabled" -%}
{%- endif -%}
<div class="individual_truth">
<h3>Vérité {{ truth.number }}</h3>
<p>{{ truth.rendered_text | safe }}</p>
@ -14,17 +8,15 @@
Tu l'as fait :)
{%- else -%}
Qui l'a fait ?
<select form="truths" name="truth_votes[{{ truth.id }}]" {{ is_disabled }}>
<select form="truths" name="truth_votes[{{ truth.id }}]">
<option value="0">---</option>
{% for player in other_players %}
{# Check if we should pre-select an existing vote. #}
{% set_global is_selected = "" %}
{% for vote in user.votes %}
{% if truth.id == vote.truth_id and player.id == vote.voted_id %}
{% set_global is_selected = "selected" %}
{% break %}
{# Check if we should pre-select an existing vote #}
{% if has_vote == true and player.id == user.votes[truth_index].voted_id %}
{% set is_selected = "selected" %}
{% else %}
{% set is_selected = "" %}
{% endif %}
{% endfor %}
<option value="{{ player.id }}" {{- is_selected -}}>{{ player.name }}</option>
{% endfor %}
</select>