From 5e21811b8d167f98949843eba2dd6a309693c23f Mon Sep 17 00:00:00 2001 From: shockrah Date: Thu, 18 Jun 2020 22:04:45 -0700 Subject: [PATCH] converting route_join_invite_code to use new json Value types --- server/src/invites.rs | 31 +++++++++++++++---------------- server/src/main.rs | 30 ++++++++++++++++++------------ 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/server/src/invites.rs b/server/src/invites.rs index 52dc15b..967fec1 100644 --- a/server/src/invites.rs +++ b/server/src/invites.rs @@ -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, params: &HashMap<&str, &str>) -> Result<(), Error> { +pub async fn route_join_invite_code(pool: &Pool, response: &mut Response, 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(()) } diff --git a/server/src/main.rs b/server/src/main.rs index 38407e4..e222398 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -31,11 +31,11 @@ mod badges; mod http_params; -async fn route_dispatcher(pool: &Pool, resp: &mut Response, meth: &Method, path: &str, params: &HashMap<&str, &str>) { +async fn route_dispatcher(pool: &Pool, resp: &mut Response, 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, meth: &Method, async fn main_responder(request: Request) -> Result, 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, ¶ms).await { - // Deal with permissions errors at this point - match auth_result { - OpenAuth | Good => route_dispatcher(&pool, &mut response, &method, path, ¶ms).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, ¶ms).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) }