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
|
||||
use diesel::{self, prelude::*};
|
||||
use rand::random;
|
||||
use chrono::{Duration, Utc};
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Invite {
|
||||
hash: u64,
|
||||
uses: i32,
|
||||
expire: i64, // comes from a date time object ngl famalam
|
||||
}
|
||||
use crate::DBConn;
|
||||
use crate::models::Invite;
|
||||
use crate::schema;
|
||||
|
||||
#[get("/generate")]
|
||||
pub fn generate_invite() -> String {
|
||||
pub fn generate_invite(conn: DBConn) -> Result<String, String> {
|
||||
let dt = Utc::now() + Duration::minutes(30);
|
||||
let invite = Invite {
|
||||
hash: random::<u64>(),
|
||||
uses: 1,
|
||||
expire: dt.timestamp(),
|
||||
id: random::<u64>(),
|
||||
uses: Some(1),
|
||||
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
|
||||
format!("freechat.io/invite/{}", invite.hash)
|
||||
Ok(format!("freechat.io/invite/{}", id))
|
||||
}
|
||||
|
||||
#[get("/<hash>")]
|
||||
|
Loading…
Reference in New Issue
Block a user