working route for generating invite links
return value may need to change slightly however
This commit is contained in:
parent
fa58724533
commit
03b111c4dd
@ -1,24 +1,30 @@
|
|||||||
// Module handles creating invites for potentially new users
|
// Module handles creating invites for potentially new users
|
||||||
|
use diesel::{self, prelude::*};
|
||||||
use rand::random;
|
use rand::random;
|
||||||
use chrono::{Duration, Utc};
|
use chrono::{Duration, Utc};
|
||||||
|
use crate::DBConn;
|
||||||
#[derive(Debug)]
|
use crate::models::Invite;
|
||||||
struct Invite {
|
use crate::schema;
|
||||||
hash: u64,
|
|
||||||
uses: i32,
|
|
||||||
expire: i64, // comes from a date time object ngl famalam
|
|
||||||
}
|
|
||||||
|
|
||||||
#[get("/generate")]
|
#[get("/generate")]
|
||||||
pub fn generate_invite() -> String {
|
pub fn generate_invite(conn: DBConn) -> Result<String, String> {
|
||||||
let dt = Utc::now() + Duration::minutes(30);
|
let dt = Utc::now() + Duration::minutes(30);
|
||||||
let invite = Invite {
|
let invite = Invite {
|
||||||
hash: random::<u64>(),
|
id: random::<u64>(),
|
||||||
uses: 1,
|
uses: Some(1),
|
||||||
expire: dt.timestamp(),
|
expires: dt.timestamp() as u64
|
||||||
};
|
};
|
||||||
|
let id = invite.id;
|
||||||
|
// Insert our newly made invite into the invites table
|
||||||
|
let _new_invite = diesel::insert_into(schema::invites::table)
|
||||||
|
.values(invite)
|
||||||
|
.execute(&conn.0)
|
||||||
|
.map_err(|err| -> String {
|
||||||
|
println!("Could not insert new invite into table so it was not created {:?}", err);
|
||||||
|
"Failure to create new invite".into()
|
||||||
|
})?;
|
||||||
// Insert the new invite into our database
|
// Insert the new invite into our database
|
||||||
format!("freechat.io/invite/{}", invite.hash)
|
Ok(format!("freechat.io/invite/{}", id))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/<hash>")]
|
#[get("/<hash>")]
|
||||||
|
Loading…
Reference in New Issue
Block a user