Updated blanknewUser macro

use_invite now returns a Json<User>
This commit is contained in:
shockrah 2020-03-08 19:50:17 -07:00
parent d7c70afd68
commit d9e892b1e0

View File

@ -2,19 +2,18 @@
use diesel::{self, prelude::*};
use rocket_contrib::json::Json;
use rand::random;
use chrono::{Duration, Utc};
use crate::DBConn;
use crate::models::Invite;
use crate::models::{User, Invite};
use crate::schema;
use crate::payload::NewUserInfo;
use crate::rand_utils::{newUserID, newKeyHash};
use chrono::{Duration, Utc};
macro_rules! blankNewUser {
() => {
NewUserInfo {
User {
userid: 0,
key_hash: None,
valid: false
username: "".into(),
key: "".into(),
date: 0,
}
}
}
@ -51,11 +50,10 @@ pub fn generate_invite(conn: DBConn) -> Json<Invite> {
}
#[get("/<hash>")]
pub fn use_invite(hash: u64, conn: DBConn) -> Json<NewUserInfo> {
pub fn use_invite(hash: u64, conn: DBConn) -> Json<User> {
// Grab the token from our table to make sure it's even there to begin with
use schema::invites::dsl::*;
use rand::{thread_rng, Rng};
use rand::distributions::Alphanumeric;
use schema::users::dsl::*;
// NOTE: collection of 1 item from the table could be done cleaner
let data = invites.select((id, expires, uses))
@ -63,26 +61,16 @@ pub fn use_invite(hash: u64, conn: DBConn) -> Json<NewUserInfo> {
.first::<Invite>(&conn.0)
.unwrap();
let _data: Result<Invite, diesel::result::Error> = invites.filter(id.eq(hash)).first(&conn.0);
// We're going to need more secure random numbers but for now this will do
match data {
Ok(d) => {
if d.id == hash {
// create the new user
let new = NewUserInfo {
userid: newUserID(),
key_hash: newKeyHash(),
valid: true
};
// save the user info and finally return it back to the caller
diesel::insert_into(users)
.values(&new)
.execute(&conn.0);
Json(new)
}
else {
Json(blankNewUser!())
}
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
match diesel_result {
Ok(data) => {
// we may have an unnecssary struct here but its not that big a deal raelly
let user = crate::users::createNewUser();
diesel::insert_into(users)
.values(&user)
.execute(&conn.0);
Json(user)
}
Err(_e) => {
Json(blankNewUser!())
@ -95,7 +83,7 @@ mod invite_tests {
use super::*;
use rocket;
use rocket::local::Client;
use rocket::http::{Status, ContentType};
use rocket::http::Status;
#[test]
fn make_invite() {