pub async fn post_revoke_delegate(
request: HttpRequest,
form: web::Form<JoinLeaveData>,
data: web::Data<WebData<'_>>,
data_rw: web::Data<DataBaseRw>,
) -> HttpResponse {
let cached_data = {
let mut cache = data.cache_revoke_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(|x| x.value().to_lowercase())
.map_or(true, |x| x != cached_data.1 && x != cached_data.2)
{
log::warn!("Incorrect user");
return HttpResponse::BadRequest().body("Incorrect");
}
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("update games.players set delegate_name = null where game_uid = $1 and player_name = $2 and delegate_name = $3;")
.await
{
Ok(stmt) => stmt,
Err(e) => {
log::error!("Pool RW statement error {}", e);
return HttpResponse::ServiceUnavailable().body(actix_web::body::None::new());
}
};
let inserted = match dbclient_rw
.execute(&stmt, &[&cached_data.0, &cached_data.1, &cached_data.2])
.await
{
Ok(c) => c,
Err(e) => {
log::error!("Pool RW execute insert error {}", e);
return HttpResponse::ServiceUnavailable().body(actix_web::body::None::new());
}
};
if inserted == 0 {
log::error!("Pool RW execute insert row error");
return HttpResponse::ServiceUnavailable().body(actix_web::body::None::new());
}
HttpResponse::Found()
.append_header((
header::LOCATION,
format!("slow-game-{}.html", cached_data.0),
))
.finish()
}