From aa9c515b954e31c255dab7487aa20d9a190fcdef Mon Sep 17 00:00:00 2001 From: shockrah Date: Mon, 16 Nov 2020 22:18:37 -0800 Subject: [PATCH] invites::valid_invite now uses proper db-lib function calls --- server-api/src/invites.rs | 45 +++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/server-api/src/invites.rs b/server-api/src/invites.rs index 846b7ad..98a0d9a 100644 --- a/server-api/src/invites.rs +++ b/server-api/src/invites.rs @@ -11,11 +11,12 @@ use hyper::{Response, Body, StatusCode}; use chrono::Utc; use db::{UBigInt, BigInt}; +use db::common::FromDB; use crate::members::{self, Member}; #[derive(Serialize)] struct Invite { - id: BigInt, + id: BigInt, // doubles as the timestamp for when it dies uses: Option, // optional because some links are permanent expires: bool, } @@ -25,34 +26,33 @@ struct Invite { * are of the enum mysql_async::error::Error */ -async fn valid_invite(pool: &Pool, id: BigInt) -> Result{ +async fn valid_invite(pool: &Pool, id: BigInt) -> bool { /* * Fetches an invite from the database to check for validity */ - let conn = pool.get_conn().await?; - let db_fetch_result: (Conn, Option<(Option, bool)>) = - conn.first_exec("SELECT uses, expires FROM invites WHERE id = :id", - params!{"id" => id}).await?; + let query: Option = match db::invites::Invite::get(pool, id as u64).await { + db::Response::Row(invite) => { Some(invite) }, + _ => { None } + }; - if let Some(row) = db_fetch_result.1 { + if let Some(invite) = query { // if expires at all - if row.1 { + if invite.expires { let now = Utc::now().timestamp(); // old? - let mut status = now > id; + let mut valid_status = now > invite.id; // used? - if row.0.is_some() && status == false { - status = row.0.unwrap() <= 0; // safe unwrap since we know its Some(_) + if invite.uses.is_some() && valid_status == false { + valid_status = invite.uses.unwrap() <= 0; // safe unwrap since we know its Some(_) } - return Ok(status) + return valid_status } // no expiry date? no problem - return Ok(true); + return true } + // prolly not a real id - else { - return Ok(false); - } + return false } @@ -66,15 +66,10 @@ async fn use_invite(pool: &Pool, code: Option) -> Option{ None => 0 }; - if let Ok(valid) = valid_invite(pool, id).await { - if valid { - match members::insert_new_member(pool, "Anonymous".into(), GENERAL_NEW).await { - Ok(member) => return Some(member), - Err(_) => return None - } - } - else { - return None; + if valid_invite(pool, id).await { + match members::insert_new_member(pool, "Anonymous".into(), GENERAL_NEW).await { + Ok(member) => return Some(member), + Err(_) => return None } } else {