/join 'ing for new users code steramlined a bit

*mostly a coding qol change
This commit is contained in:
shockrah 2020-05-05 23:20:47 -07:00
parent 04ca53dc79
commit 5652388f4c
3 changed files with 26 additions and 20 deletions

View File

@ -1,6 +1,7 @@
// Handlers for the base auth routes
use crate::users;
use crate::users{self, Member};
use std::{error, fmt};
use crate::rand_utils::new_key;
#[derive(FromForm)]
@ -39,15 +40,19 @@ impl<'r> Responder<'r> for AuthErr {
}
}
/*
#[post("/login")]
pub fn login() {
}
*/
#[post("/join", data="<data>")]
pub fn join(conn: DBConn, params: JoinParams) -> AuthResult<Json<>, AuthErr>{
#[post("/join", data="<params>")]
pub fn join(conn: DBConn, params: JoinParams) -> AuthResult<Json<User>, AuthErr>{
/*
* Requires <code:int> -> body
* Requires <name:string> -> body
* Struct JoinParams enforces this for us so if something is missing then rocket should 404
*/
const expired: &'static str = "Invite expired";
const negate: &'static str = "Malformed request";
@ -58,13 +63,13 @@ pub fn join(conn: DBConn, params: JoinParams) -> AuthResult<Json<>, AuthErr>{
if let Ok(data) = diesel_result {
match data.uses {
1 ... std::i32::MAX => {
let user = users::new_user();
let new_user = users::new_member(conn):
// update the uses counter
diesel::update(users.filter(userid.eq(user.userid)))
.set(uses.eq(data.uses - 1))
.execute(&conn.0)
AuthResult(Json(user))
AuthResult(Json(new_user))
}
// The invite has been used up and thus should be removed
std::i32::MIN ... 0 => {
@ -81,6 +86,8 @@ pub fn join(conn: DBConn, params: JoinParams) -> AuthResult<Json<>, AuthErr>{
}
}
/*
#[post("/leave", data = "<>")]
pub fn leave() {
}
@ -89,4 +96,4 @@ pub fn leave() {
pub fn close() {
}
*/

View File

@ -9,19 +9,15 @@ pub fn new_user_id() -> u64 {
}
}
pub fn new_key() -> String { // Returns a random string which we later hash with bcrypt
let mut raw_slice = [0u8; 32];
if let Ok(_ignored) = getrandom(&mut raw_slice) {
let mut buf = String::new();
for i in raw_slice.iter() {
let mut cv: u8 = *i;
if cv > 126 { cv %= 126; }
if cv < 33 { cv += 34; }
buf.push(cv as char);
}
buf
}
else {
"".into() // like with new_user_id if this really fires we're screwed anyway
pub fn new_key() -> String {
let mut raw_slice = [0u8; 64];
let _ignored = getrandom(&mut raw_slice).unwrap();
let mut buf = String::new();
for i in raw_slice.iter() {
let mut cv: u8 = *i;
if cv > 126 { cv %= 126; }
if cv < 33 { cv += 34; }
buf.push(cv as char);
}
buf
}

View File

@ -8,6 +8,9 @@ use diesel::{self, prelude::*};
use diesel::result::Error;
pub fn create_new_user(conn: DBConn, name: String) -> User {
/*
* Should only eveer be called under good circumstances
*/
use schema::users::dsl::*;
use diesel::result::Error;
use crate::models::InsertableUser;