Implement query for delegation
Dependencies
- [2]
YDWTHWAIShow form to revoke delegation - [3]
D3RL62X5Implement revoking delegation - [4]
LTQCLSBUSplit database usage in pages - [5]
NY766BOQAccept form to add player - [6]
5RQCVFRHStart leave game form - [7]
OJO4B4QOAdd login form and empty handler - [8]
DNFB7TNIAdd new pages and links to slow games and reset game password - [9]
WW3KRXX6Add page for reset game password - [10]
WVHXYKCVAdd postgresql pools - [11]
HZDCKIXQUse constants for templates - [12]
EVP2FSBHSplit index page - [13]
6NYILMKIAdd page for slow game - [*]
B7PYGLH3Show delegate bold if it's logged user itself - [*]
6BDGQ4VMShow player names for registered players - [*]
TEB4R7OUAdd form to join game - [*]
4MZ4VIR7Initial commit - [*]
IXY6NZLMStart to use migrations files for PostgreSQL database
Change contents
- edit in src/templates/slow-game.html at line 80
{{/if}}{{#if player.query_delegate_data}}<form id="query-delegate-{{ player.query_delegate_data.csrf }}" action="query-delegate.do" method="post"><fieldset><label for="delegate_name">Delegate name:</label><input name="delegate_name" id="delegate_name-{{ player.query_delegate_data.csrf }}" type="text" placeholder="Username" required /><input name="csrf" type="hidden" value="{{ player.query_delegate_data.csrf }}"><input name="submit" type="submit" value="Query Delegate"></fieldset></form> - edit in src/pages/slow_game.rs at line 17
query_delegate_data: Option<JoinLeaveData>, - edit in src/pages/slow_game.rs at line 22
csrf: Uuid,}#[derive(serde_derive::Deserialize)]pub struct QueryDelegationData { - edit in src/pages/slow_game.rs at line 28
delegate_name: String, - edit in src/pages/slow_game.rs at line 192
let query_delegate_data = if status.map_or(true, |x| x == "started")&& player_itself&& delegate_name.is_none(){let csrf = Uuid::new_v4();let mut cache = data.cache_query_delegation_game.lock().await;cache.insert(csrf,(gameuid.clone(), user.to_lowercase()),std::time::Duration::from_secs(data.cache_duration_sec),);Some(JoinLeaveData { csrf })} else {None}; - edit in src/pages/slow_game.rs at line 213
query_delegate_data, - edit in src/pages/slow_game.rs at line 467
}HttpResponse::Found().append_header((header::LOCATION,format!("slow-game-{}.html", cached_data.0),)).finish()}pub async fn post_query_delegate(request: HttpRequest,form: web::Form<QueryDelegationData>,data: web::Data<WebData<'_>>,data_rw: web::Data<DataBaseRw>,) -> HttpResponse {let cached_data = {let mut cache = data.cache_query_delegation_game.lock().await;cache.remove(&form.csrf)};let cached_data = if let Some(cd) = cached_data {cd} else {log::warn!("Unknown data for CSRF: {}", form.csrf);return HttpResponse::BadRequest().body("Incorrect");};let jar = request_to_jar(request);if jar.private(&data.cookies_key).get("auth").map_or(true, |x| x.value().to_lowercase() != cached_data.1){log::warn!("Incorrect user");return HttpResponse::BadRequest().body("Incorrect"); - edit in src/pages/slow_game.rs at line 505
let dbclient_rw = match data_rw.0.get().await {Ok(c) => c,Err(e) => {log::error!("Pool RW error {}", e);return HttpResponse::ServiceUnavailable().body(actix_web::body::None::new());}};let stmt = match dbclient_rw.prepare("insert into games.query_delegation (game_uid, player_name, delegate_name) values ($1, $2, $3) on conflict do nothing;").await{Ok(stmt) => stmt,Err(e) => {log::error!("Pool RW statement error {}", e);return HttpResponse::ServiceUnavailable().body(actix_web::body::None::new());}};match dbclient_rw.execute(&stmt,&[&cached_data.0, &cached_data.1, &form.delegate_name],).await{Ok(_) => (),Err(e) => {log::error!("Pool RW execute insert error {}", e);return HttpResponse::ServiceUnavailable().body(actix_web::body::None::new());}}; - edit in src/pages/mod.rs at line 50
pub cache_query_delegation_game: Mutex<TtlCache<Uuid, (String, String)>>, - replacement in src/main.rs at line 22
use pages::slow_game::{post_join_game, post_leave_game, post_revoke_delegate, slow_game};use pages::slow_game::{post_join_game, post_leave_game, post_query_delegate, post_revoke_delegate, slow_game,}; - edit in src/main.rs at line 222
cache_query_delegation_game: tokio::sync::Mutex::new(ttl_cache::TtlCache::new(cache_capacity,)), - edit in src/main.rs at line 279
.route("/query-delegate.do", web::post().to(post_query_delegate)) - file addition: 20221022_0_query_delegate.sql[19.15]
-- Add table to store queries to delegationCREATE TABLE IF NOT EXISTS games.query_delegation (game_uid VARCHAR(20) REFERENCES games.games(game_uid),player_name CITEXT REFERENCES auth.users(player_name),delegate_name CITEXT REFERENCES auth.users(player_name),CONSTRAINT pk_query_delegation PRIMARY KEY (game_uid, player_name, delegate_name));GRANT SELECT, INSERT, DELETE ON games.query_delegation TO freeorion;