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 chrono::Utc;
|
||||||
|
|
||||||
use db::{UBigInt, BigInt};
|
use db::{UBigInt, BigInt};
|
||||||
|
use db::common::FromDB;
|
||||||
use crate::members::{self, Member};
|
use crate::members::{self, Member};
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct Invite {
|
struct Invite {
|
||||||
id: BigInt,
|
id: BigInt, // doubles as the timestamp for when it dies
|
||||||
uses: Option<BigInt>, // optional because some links are permanent
|
uses: Option<BigInt>, // optional because some links are permanent
|
||||||
expires: bool,
|
expires: bool,
|
||||||
}
|
}
|
||||||
@ -25,34 +26,33 @@ struct Invite {
|
|||||||
* are of the enum mysql_async::error::Error
|
* 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
|
* Fetches an invite from the database to check for validity
|
||||||
*/
|
*/
|
||||||
let conn = pool.get_conn().await?;
|
let query: Option<db::invites::Invite> = match db::invites::Invite::get(pool, id as u64).await {
|
||||||
let db_fetch_result: (Conn, Option<(Option<BigInt>, bool)>) =
|
db::Response::Row(invite) => { Some(invite) },
|
||||||
conn.first_exec("SELECT uses, expires FROM invites WHERE id = :id",
|
_ => { None }
|
||||||
params!{"id" => id}).await?;
|
};
|
||||||
|
|
||||||
if let Some(row) = db_fetch_result.1 {
|
if let Some(invite) = query {
|
||||||
// if expires at all
|
// if expires at all
|
||||||
if row.1 {
|
if invite.expires {
|
||||||
let now = Utc::now().timestamp();
|
let now = Utc::now().timestamp();
|
||||||
// old?
|
// old?
|
||||||
let mut status = now > id;
|
let mut valid_status = now > invite.id;
|
||||||
// used?
|
// used?
|
||||||
if row.0.is_some() && status == false {
|
if invite.uses.is_some() && valid_status == false {
|
||||||
status = row.0.unwrap() <= 0; // safe unwrap since we know its Some(_)
|
valid_status = invite.uses.unwrap() <= 0; // safe unwrap since we know its Some(_)
|
||||||
}
|
}
|
||||||
return Ok(status)
|
return valid_status
|
||||||
}
|
}
|
||||||
// no expiry date? no problem
|
// no expiry date? no problem
|
||||||
return Ok(true);
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// prolly not a real id
|
// prolly not a real id
|
||||||
else {
|
return false
|
||||||
return Ok(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,8 +66,7 @@ async fn use_invite(pool: &Pool, code: Option<BigInt>) -> Option<Member>{
|
|||||||
None => 0
|
None => 0
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Ok(valid) = valid_invite(pool, id).await {
|
if valid_invite(pool, id).await {
|
||||||
if valid {
|
|
||||||
match members::insert_new_member(pool, "Anonymous".into(), GENERAL_NEW).await {
|
match members::insert_new_member(pool, "Anonymous".into(), GENERAL_NEW).await {
|
||||||
Ok(member) => return Some(member),
|
Ok(member) => return Some(member),
|
||||||
Err(_) => return None
|
Err(_) => return None
|
||||||
@ -76,10 +75,6 @@ async fn use_invite(pool: &Pool, code: Option<BigInt>) -> Option<Member>{
|
|||||||
else {
|
else {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn join(pool: &Pool, response: &mut Response<Body>, params: Value) {
|
pub async fn join(pool: &Pool, response: &mut Response<Body>, params: Value) {
|
||||||
|
Loading…
Reference in New Issue
Block a user