converting route_join_invite_code to use new json Value types

This commit is contained in:
shockrah 2020-06-18 22:04:45 -07:00
parent 3b0b79c525
commit 5e21811b8d
2 changed files with 33 additions and 28 deletions

View File

@ -1,4 +1,4 @@
use std::collections::HashMap;
use serde_json::Value;
use mysql_async;
use mysql_async::{Conn, Pool};
@ -86,25 +86,24 @@ async fn record_invite_usage(pool: &Pool, data: &InviteRow) -> Result<(), Error>
Ok(())
}
pub async fn join_invite_code(pool: &Pool, response: &mut Response<Body>, params: &HashMap<&str, &str>) -> Result<(), Error> {
pub async fn route_join_invite_code(pool: &Pool, response: &mut Response<Body>, params: Value) -> Result<(), Error> {
// First check that the code is there
match params.get("code") {
Some(p) => {
if let Some(row) = get_invite_by_code(pool, Some(*p)).await? {
// since we have a row make sure the invite is valid
let now = Utc::now().timestamp() as u64;
// usable and expires in the future
if row.uses > 0 && row.expires > now {
record_invite_usage(pool, &row).await?;
// TODO: assign some actual data to the body
*response.status_mut() = StatusCode::OK;
}
if let Some(code) = params.get("code") {
if let Some(row) = get_invite_by_code(pool, code.as_str()).await? {
// since we have a row make sure the invite is valid
let now = Utc::now().timestamp() as u64;
// usable and expires in the future
if row.uses > 0 && row.expires > now {
record_invite_usage(pool, &row).await?;
// TODO: assign some actual data to the body
*response.status_mut() = StatusCode::OK;
}
},
None => {
*response.status_mut() = StatusCode::BAD_REQUEST;
}
}
else{
*response.status_mut() = StatusCode::BAD_REQUEST;
}
Ok(())
}

View File

@ -31,11 +31,11 @@ mod badges;
mod http_params;
async fn route_dispatcher(pool: &Pool, resp: &mut Response<Body>, meth: &Method, path: &str, params: &HashMap<&str, &str>) {
async fn route_dispatcher(pool: &Pool, resp: &mut Response<Body>, meth: &Method, path: &str, params: serde_json::Value) {
// At some point we should have some way of hiding this obnoxious complexity
match (meth, path) {
(&Method::GET, routes::INVITE_JOIN) => {
if let Err(_) = invites::join_invite_code(pool, resp, params).await {
if let Err(_) = invites::route_join_invite_code(pool, resp, params).await {
*resp.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
}
},
@ -55,23 +55,29 @@ async fn route_dispatcher(pool: &Pool, resp: &mut Response<Body>, meth: &Method,
async fn main_responder(request: Request<Body>) -> Result<Response<Body>, hyper::Error>{
use AuthReason::*;
let mut response = Response::new(Body::empty());
let (parts, mut body) = request.into_parts();
let method = parts.method;
let path = parts.uri.path();
let params = http_params::parse_params(parts.uri.query(), &mut body).await;
let params_res = http_params::parse_params(&mut body).await;
let pool = Pool::new(&env::var("DATABASE_URL").unwrap());
// some more information in the response would be great right about here
if let Ok(auth_result) = auth::wall_entry(path, &pool, &params).await {
// Deal with permissions errors at this point
match auth_result {
OpenAuth | Good => route_dispatcher(&pool, &mut response, &method, path, &params).await,
LimitPassed => *response.status_mut() = StatusCode::UNAUTHORIZED,
NoKey => *response.status_mut() = StatusCode::UNAUTHORIZED,
if let Ok(params) = params_res {
let pool = Pool::new(&env::var("DATABASE_URL").unwrap());
// some more information in the response would be great right about here
if let Ok(auth_result) = auth::wall_entry(path, &pool, &params).await {
// Deal with permissions errors at this point
match auth_result {
OpenAuth | Good => route_dispatcher(&pool, &mut response, &method, path, params).await,
LimitPassed => *response.status_mut() = StatusCode::UNAUTHORIZED,
NoKey => *response.status_mut() = StatusCode::UNAUTHORIZED,
}
}
else {
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
}
}
else {
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
*response.status_mut() = StatusCode::BAD_REQUEST;
}
Ok(response)
}