updated the response when creating new users

This commit is contained in:
shockrah 2020-03-10 16:48:21 -07:00
parent 924c15a436
commit 8ae877f3f9
3 changed files with 57 additions and 37 deletions

View File

@ -3,20 +3,13 @@ use diesel::{self, prelude::*};
use rocket_contrib::json::Json; use rocket_contrib::json::Json;
use rand::random; use rand::random;
use crate::DBConn; use crate::DBConn;
use crate::models::{User, Invite}; use crate::models::Invite;
use crate::schema; use crate::schema;
use crate::users::new_user_response;
use crate::payload;
use chrono::{Duration, Utc}; use chrono::{Duration, Utc};
macro_rules! blankNewUser {
() => {
User {
userid: 0,
username: "".into(),
key: "".into(),
date: 0,
}
}
}
/* /*
TODO: both the generation and usage endpoints for invites need the following TODO: both the generation and usage endpoints for invites need the following
* meaningful responses * meaningful responses
@ -26,9 +19,10 @@ TODO: both the generation and usage endpoints for invites need the following
#[get("/generate")] #[get("/generate")]
pub fn generate_invite(conn: DBConn) -> Json<Invite> { pub fn generate_invite(conn: DBConn) -> Json<Invite> {
let dt = Utc::now() + Duration::minutes(30); let dt = Utc::now() + Duration::minutes(30);
// TODO:[maybe] ensure no collisions by doing a quick database check here
let mut new_invite = Invite { let mut new_invite = Invite {
id: random::<u64>(), // hopefully there won't ever be collision with this size of pool id: random::<u64>(), // hopefully there won't ever be collision with this size of pool
uses: Some(1), // default/hardcorded for now uses: 1, // default/hardcorded for now
expires: dt.timestamp() as u64 expires: dt.timestamp() as u64
}; };
// Next we cache this invite // Next we cache this invite
@ -36,6 +30,7 @@ pub fn generate_invite(conn: DBConn) -> Json<Invite> {
.values(&new_invite) .values(&new_invite)
.execute(&conn.0); .execute(&conn.0);
// Finally we attempt to return _something_
match result { match result {
Ok(_val) => { Ok(_val) => {
Json(new_invite) Json(new_invite)
@ -43,34 +38,42 @@ pub fn generate_invite(conn: DBConn) -> Json<Invite> {
Err(_e) => { Err(_e) => {
new_invite.id = 0; new_invite.id = 0;
new_invite.expires = 0; new_invite.expires = 0;
new_invite.uses = Some(0); new_invite.uses = 0;
Json(new_invite) Json(new_invite)
} }
} }
} }
#[get("/<hash>")] #[get("/<hash>")]
pub fn use_invite(hash: u64, conn: DBConn) -> Json<User> { pub fn use_invite(hash: u64, conn: DBConn) -> Json<payload::NewUserResponse> {
// Grab the token from our table to make sure it's even there to begin with // Grab the token from our table to make sure it's even there to begin with
use schema::invites::dsl::*; use schema::invites::dsl::*;
use schema::users::dsl::*; use schema::users::dsl::*;
let diesel_result: Result<Invite, diesel::result::Error> = invites.filter(id.eq(hash)).first(&conn.0); let diesel_result: Result<Invite, diesel::result::Error> = invites.filter(id.eq(hash)).first(&conn.0);
// TODO: this is getting moved elsewhere to clean up so ignore this for now // TODO: this is getting moved elsewhere to clean up so ignore this for now
match diesel_result { if let Ok(data) = diesel_result {
Ok(_data) => { match data.uses {
// we may have an unnecssary struct here but its not that big a deal raelly 1 ... std::i32::MAX => {
let user = crate::users::create_new_user(); let user = crate::users::create_new_user();
if let Ok(_v) = diesel::insert_into(users).values(&user).execute(&conn.0) { match diesel::insert_into(users).values(&user).execute(&conn.0) {
Json(user) Ok(_v) => Json(new_user_response(&Some(user), None)),
// an issue on our end gets a 500 response
Err(_e) => Json(new_user_response(&None, Some("Unable to create user")))
}
}
// The invite has been used up and thus should be removed
std::i32::MIN ... 0 => {
// bruh
let _ = diesel::delete(invites.filter(id.eq(data.id)))
.execute(&conn.0)
.expect("Could not delete invite");
Json(new_user_response(&None, Some("Invalid invite")))
}
}
} }
else { else {
Json(blankNewUser!()) Json(new_user_response(&None, Some("Could not create user")))
}
}
Err(_e) => {
Json(blankNewUser!())
}
} }
} }
@ -121,12 +124,10 @@ mod invite_tests {
let mut r = client.get("/invite/generate").dispatch(); let mut r = client.get("/invite/generate").dispatch();
let invite: Invite = serde_json::from_str(&r.body_string().unwrap()).unwrap(); let invite: Invite = serde_json::from_str(&r.body_string().unwrap()).unwrap();
// request a user account using the new invite data // TODO: for some reason we can't use a regular struct so figure that out at some point
let mut response = client.get(format!("/invite/{}", invite.id)).dispatch(); let mut response = client.get(format!("/invite/{}", invite.id)).dispatch();
let data: User = serde_json::from_str(&response.body_string().unwrap()).unwrap(); let body: String = response.body_string().unwrap();
// make sure the user object isn't fugged println!("{}", body);
assert_ne!(0, data.userid);
assert_ne!(0, data.date);
println!("{:#?}", data);
} }
} }

View File

@ -5,10 +5,10 @@
// This structure allows us to provide some critical data for the client to reconnect to // This structure allows us to provide some critical data for the client to reconnect to
// the server without having to go through a sign in process everytime // the server without having to go through a sign in process everytime
#[derive(Serialize)] #[derive(Serialize, Deserialize, Debug)]
pub struct NewUserInfo { pub struct NewUserResponse {
pub userid: u64, // userid which later will be discoverable via the pub userid: Option<u64>,
pub username: String, pub username: Option<String>,
pub key: String, pub key: Option<String>,
pub valid: bool pub err: Option<&'static str>,
} }

View File

@ -1,6 +1,7 @@
use chrono::Utc; use chrono::Utc;
use crate::rand_utils::{new_user_id, new_key}; use crate::rand_utils::{new_user_id, new_key};
use crate::models::User; use crate::models::User;
use crate::payload::NewUserResponse;
// Returns a struct of payload::NewUserInfo // Returns a struct of payload::NewUserInfo
pub fn create_new_user() -> User { pub fn create_new_user() -> User {
@ -15,6 +16,24 @@ pub fn create_new_user() -> User {
} }
} }
pub fn new_user_response(user: &Option<User>, msg: Option<&'static str>) -> NewUserResponse {
if let Some(u) = user {
NewUserResponse {
userid: Some(u.userid),
username: Some(u.username.clone()),
key: Some(u.key.clone()),
err: None
}
}
else{
NewUserResponse {
userid: None,
username: None,
key: None,
err: Some(msg.unwrap())
}
}
}
#[cfg(test)] #[cfg(test)]
mod user_tests { mod user_tests {
use super::create_new_user; use super::create_new_user;