diff --git a/json-api/db/src/invites.rs b/json-api/db/src/invites.rs index 4e514b6..662d4cf 100644 --- a/json-api/db/src/invites.rs +++ b/json-api/db/src/invites.rs @@ -11,7 +11,7 @@ use crate::common::FromDB; use crate::{Response, no_conn}; #[allow(dead_code)] -#[derive(Serialize)] +#[derive(Serialize, Debug)] pub struct Invite { pub id: BigInt, pub uses: Option, diff --git a/json-api/db/src/lib.rs b/json-api/db/src/lib.rs index d295ace..fb243ad 100644 --- a/json-api/db/src/lib.rs +++ b/json-api/db/src/lib.rs @@ -16,6 +16,7 @@ pub type UInteger = u32; pub type VarChar = String; +#[derive(Debug)] pub enum Response { // A set of rows collected Set(Vec), diff --git a/json-api/src/invites.rs b/json-api/src/invites.rs index 65d7e93..ba9e145 100644 --- a/json-api/src/invites.rs +++ b/json-api/src/invites.rs @@ -16,6 +16,7 @@ use db::invites::Invite; use crate::qs_param; use crate::http; +use crate::meta; /* @@ -38,7 +39,8 @@ async fn valid_invite(pool: &Pool, id: BigInt) -> bool { if invite.expires { let now = Utc::now().timestamp(); // old? - let mut valid_status = now > invite.id; + println!("{} {}", now, invite.id); + let mut valid_status = now < invite.id; // used? if invite.uses.is_some() && valid_status == false { valid_status = invite.uses.unwrap() <= 0; // safe unwrap since we know its Some(_) @@ -68,27 +70,21 @@ async fn use_invite(pool: &Pool, code: Option) -> Option{ // some random comment if valid_invite(pool, id).await { let raw_secret = auth::generate_secret(); - if let Ok(secret) = auth::encrypt_secret(&raw_secret) { - return match db::member::Member::add(pool, "Anonymous".into(), &secret, GENERAL_NEW).await { - Ok(response) => { - match response { - db::Response::Row(member) => Some(member), - _ => None, - } - }, - // TODO: logggin or something idk - Err(_) => return None - } - } - // Returning None because we couldn't actually create a proper secret to store - else { - return None; + let secret = auth::encrypt_secret(&raw_secret).unwrap(); + return match db::member::Member::add(pool, "Anonymous".into(), &secret, GENERAL_NEW).await { + Ok(response) => { + match response { + db::Response::Row(member) => Some(member), + _ => None + } + }, + // TODO: logggin or something idk + Err(_) => None } } // The invite itself was not valid - else { - return None; - } + // Returning None because we couldn't actually create a proper secret to store + return None; } pub async fn join(pool: &Pool, response: &mut Response, params: HashMap) { @@ -112,7 +108,10 @@ pub async fn join(pool: &Pool, response: &mut Response, params: HashMap { - http::set_json_body(response, json!(new_account)) + http::set_json_body(response, json!({ + "user": new_account, + "server": meta::get_config() + })) }, None => { *response.status_mut() = StatusCode::NOT_FOUND; diff --git a/json-api/src/main.rs b/json-api/src/main.rs index 575d31b..90ea88d 100644 --- a/json-api/src/main.rs +++ b/json-api/src/main.rs @@ -58,7 +58,7 @@ async fn route_dispatcher( const DELETE: &Method = &Method::DELETE; match (meth, path) { /* INVITES */ - (GET, routes::INVITE_CREATE) => invites::create(pool, resp, params).await, + (POST, routes::INVITE_CREATE) => invites::create(pool, resp, params).await, (GET, routes::INVITE_JOIN) => invites::join(pool, resp, params).await, /* CHANNELS */ (GET, routes::CHANNELS_LIST) => channels::list_channels(pool, resp).await,