invites::valid_invite now uses proper db-lib function calls
This commit is contained in:
parent
2448e1b200
commit
aa9c515b95
@ -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<BigInt>, // 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<bool, Error>{
|
||||
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<BigInt>, bool)>) =
|
||||
conn.first_exec("SELECT uses, expires FROM invites WHERE id = :id",
|
||||
params!{"id" => id}).await?;
|
||||
let query: Option<db::invites::Invite> = 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<BigInt>) -> Option<Member>{
|
||||
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 {
|
||||
|
Loading…
Reference in New Issue
Block a user