From c1ef4c6336b0d086338775e1c4e24a4ea06e4797 Mon Sep 17 00:00:00 2001 From: shockrah Date: Wed, 3 Feb 2021 21:26:26 -0800 Subject: [PATCH] -Removed _most_ direct sql manipulation for invite creation - Removed unused/irrelevant unit tests, which now have to be rebuilt for the new codebase --- json-api/src/invites.rs | 48 ++++------------------------------------- 1 file changed, 4 insertions(+), 44 deletions(-) diff --git a/json-api/src/invites.rs b/json-api/src/invites.rs index 0f7bc66..4147033 100644 --- a/json-api/src/invites.rs +++ b/json-api/src/invites.rs @@ -1,9 +1,7 @@ use serde_json::{Value, json}; -use serde::Serialize; use mysql_async; use mysql_async::{Conn, Pool}; -use mysql_async::error::Error; use mysql_async::prelude::{params, Queryable}; use hyper::{Response, Body, StatusCode}; @@ -15,16 +13,11 @@ use std::collections::HashMap; use db::{UBigInt, BigInt}; use db::common::FromDB; use db::member::Member; +use db::invites::Invite; use crate::http; -#[derive(Serialize)] -struct Invite { - id: BigInt, // doubles as the timestamp for when it dies - uses: Option, // optional because some links are permanent - expires: bool, -} /* * Error handling: * All errors raisable from this module come from mysql_async and thus @@ -127,21 +120,10 @@ pub async fn join(pool: &Pool, response: &mut Response, params: HashMap<&s } } -async fn insert_new_invite(pool: &Pool, invite: &Invite) -> Result<(), Error>{ - let conn = pool.get_conn().await?; - conn.prep_exec( - "INSERT INTO invites (id, uses, expires) - VALUES (:id, :uses, :expires)", params!{ - "id" => invite.id, - "uses" => invite.uses, - "expires" => invite.expires - }).await?; - - Ok(()) -} async fn process_expires_parameter(p: &Pool, exp: &Value, id: UBigInt) -> bool { // TODO: fix this somewhat unsafe code + // TODO: BRO THIS SHIT // NOTE: its unsafe because of these lazy as heck unwraps everywhere use crate::perms::{CREATE_PERM_INVITES, CREATE_TMP_INVITES}; let conn = p.get_conn().await.unwrap(); @@ -210,13 +192,9 @@ pub async fn create(pool: &Pool, response: &mut Response, params: HashMap< }; // TODO: prolly add some kind option to set an expire time - let invite = Invite { - id: (Utc::now() + chrono::Duration::minutes(30)).timestamp(), - uses: use_count, - expires: expirey_request - }; + let invite = Invite::new(use_count, expirey_request); - match insert_new_invite(&pool, &invite).await { + match invite.add(pool).await { Ok(_) => { // return the id of the invite // Link format from here is basically hostname.io:4536/join?code= @@ -228,21 +206,3 @@ pub async fn create(pool: &Pool, response: &mut Response, params: HashMap< } } } - - -#[cfg(test)] -mod invites_test { - /* - * INVITE CREATION - * Good - Bad - Malicious - */ - - use crate::testing::{get_pool, hyper_resp}; - use hyper::StatusCode; - use serde_json::Value; - - #[tokio::test] - async fn create_invite_good() { - todo!() - } -}