From e2e5a0817a8f1ecbc3d48b11eb8b4b1c047e2ce4 Mon Sep 17 00:00:00 2001 From: Paul Makles <paulmakles@gmail.com> Date: Sat, 8 Feb 2020 12:48:09 +0000 Subject: [PATCH] Finish implementing schema. --- src/guards/auth.rs | 8 ++++---- src/routes/account.rs | 27 +++++++++++++++------------ src/routes/user.rs | 30 ++++++++++++------------------ 3 files changed, 31 insertions(+), 34 deletions(-) diff --git a/src/guards/auth.rs b/src/guards/auth.rs index 6d60c33..3f718da 100644 --- a/src/guards/auth.rs +++ b/src/guards/auth.rs @@ -2,7 +2,7 @@ use rocket::Outcome; use rocket::http::{ Status, RawStr }; use rocket::request::{ self, Request, FromRequest, FromParam }; -use bson::{ bson, doc, ordered::OrderedDocument }; +use bson::{ bson, doc, from_bson }; use ulid::Ulid; use crate::database; @@ -10,7 +10,7 @@ use crate::database; pub struct User( pub Ulid, pub String, - pub OrderedDocument, + pub database::user::User, ); #[derive(Debug)] @@ -36,7 +36,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for User { Outcome::Success(User ( Ulid::from_string(user.get_str("_id").unwrap()).unwrap(), user.get_str("username").unwrap().to_string(), - user + from_bson(bson::Bson::Document(user)).expect("Failed to unwrap user.") )) } else { Outcome::Failure((Status::Forbidden, AuthError::Invalid)) @@ -58,7 +58,7 @@ impl<'r> FromParam<'r> for User { Ok(User ( Ulid::from_string(user.get_str("_id").unwrap()).unwrap(), user.get_str("username").unwrap().to_string(), - user + from_bson(bson::Bson::Document(user)).expect("Failed to unwrap user.") )) } else { Err(param) diff --git a/src/routes/account.rs b/src/routes/account.rs index bf7b694..bae795b 100644 --- a/src/routes/account.rs +++ b/src/routes/account.rs @@ -7,6 +7,7 @@ use rocket_contrib::json::{ Json, JsonValue }; use serde::{ Serialize, Deserialize }; use validator::validate_email; use bcrypt::{ hash, verify }; +use database::user::User; use chrono::prelude::*; use ulid::Ulid; @@ -112,18 +113,18 @@ pub fn verify_email(code: String) -> JsonValue { if let Some(u) = col.find_one(doc! { "email_verification.code": code.clone() }, None).expect("Failed user lookup") { - let ev = u.get_document("email_verification").expect("DOC[email_verification]"); - let expiry = ev.get_utc_datetime("expiry").expect("DOC[expiry]"); + let user: User = from_bson(bson::Bson::Document(u)).expect("Failed to unwrap user."); + let ev = user.email_verification; - if Utc::now() > *expiry { + if Utc::now() > *ev.expiry.unwrap() { json!({ "success": false, "error": "Token has expired!", }) } else { - let target = ev.get_str("target").expect("DOC[target]"); + let target = ev.target.unwrap(); col.update_one( - doc! { "_id": u.get_str("_id").expect("Failed to retrieve user id.") }, + doc! { "_id": user.id }, doc! { "$unset": { "email_verification.code": "", @@ -141,7 +142,7 @@ pub fn verify_email(code: String) -> JsonValue { email::send_welcome_email( target.to_string(), - u.get_str("username").expect("Failed to retrieve username.").to_string() + user.username ); json!({ @@ -171,9 +172,11 @@ pub fn resend_email(info: Json<Resend>) -> JsonValue { if let Some(u) = col.find_one(doc! { "email_verification.target": info.email.clone() }, None).expect("Failed user lookup") { - let ev = u.get_document("email_verification").expect("DOC[email_verification]"); - let expiry = ev.get_utc_datetime("expiry").expect("DOC[expiry]"); - let rate_limit = ev.get_utc_datetime("rate_limit").expect("DOC[rate_limit]"); + let user: User = from_bson(bson::Bson::Document(u)).expect("Failed to unwrap user."); + let ev = user.email_verification; + + let expiry = ev.expiry.unwrap(); + let rate_limit = ev.rate_limit.unwrap(); if Utc::now() < *rate_limit { json!({ @@ -182,7 +185,7 @@ pub fn resend_email(info: Json<Resend>) -> JsonValue { }) } else { let mut new_expiry = UtcDatetime(Utc::now() + chrono::Duration::days(1)); - if info.email.clone() != u.get_str("email").expect("DOC[email]") { + if info.email.clone() != user.email { if Utc::now() > *expiry { return json!({ "success": "false", @@ -195,7 +198,7 @@ pub fn resend_email(info: Json<Resend>) -> JsonValue { let code = gen_token(48); col.update_one( - doc! { "_id": u.get_str("_id").expect("Failed to retrieve user id.") }, + doc! { "_id": user.id }, doc! { "$set": { "email_verification.code": code.clone(), @@ -243,7 +246,7 @@ pub fn login(info: Json<Login>) -> JsonValue { if let Some(u) = col.find_one(doc! { "email": info.email.clone() }, None).expect("Failed user lookup") { - let user: database::user::User = from_bson(bson::Bson::Document(u)).expect("Failed to unwrap user."); + let user: User = from_bson(bson::Bson::Document(u)).expect("Failed to unwrap user."); match verify(info.password.clone(), &user.password) .expect("Failed to check hash of password.") { diff --git a/src/routes/user.rs b/src/routes/user.rs index 52f069f..0190789 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -4,8 +4,8 @@ use crate::routes::channel; use rocket_contrib::json::{ Json, JsonValue }; use serde::{ Serialize, Deserialize }; +use bson::{ bson, doc, from_bson }; use mongodb::options::FindOptions; -use bson::{ bson, doc }; use ulid::Ulid; /// retrieve your user information @@ -16,9 +16,8 @@ pub fn me(user: User) -> JsonValue { json!({ "id": id.to_string(), "username": username, - "email": doc.get_str("email").expect("Missing email in user object!"), - "verified": doc.get_document("email_verification").expect("DOC[email_verification]") - .get_bool("verified").expect("DOC[verified]"), + "email": doc.email, + "verified": doc.email_verification.verified, "created_timestamp": id.datetime().timestamp(), }) } @@ -47,11 +46,11 @@ pub fn lookup(_user: User, query: Json<Query>) -> JsonValue { let mut results = Vec::new(); for user in users { - let u = user.expect("Failed to unwrap user."); + let u: database::user::User = from_bson(bson::Bson::Document(user.unwrap())).expect("Failed to unwrap user."); results.push( json!({ - "id": u.get_str("_id").expect("DB[id]"), - "username": u.get_str("username").expect("DB[username]") + "id": u.id, + "username": u.username }) ); } @@ -158,15 +157,12 @@ fn get_relationship(a: &User, b: &User) -> Relationship { return Relationship::SELF } - if let Ok(arr) = b.2.get_array("relations") { + if let Some(arr) = &b.2.relations { let id = a.0.to_string(); for entry in arr { - let relation = entry.as_document().expect("Expected document in relations array."); - - if relation.get_str("id").expect("DB[id]") == id { - - match relation.get_i32("status").expect("DB[status]") { + if entry.id == id { + match entry.status { 0 => { return Relationship::FRIEND }, @@ -183,7 +179,6 @@ fn get_relationship(a: &User, b: &User) -> Relationship { return Relationship::NONE } } - } } } @@ -195,13 +190,12 @@ fn get_relationship(a: &User, b: &User) -> Relationship { #[get("/@me/friend")] pub fn get_friends(user: User) -> JsonValue { let mut results = Vec::new(); - if let Ok(arr) = user.2.get_array("relations") { + if let Some(arr) = user.2.relations { for item in arr { - let doc = item.as_document().expect("Expected document in relations array."); results.push( json!({ - "id": doc.get_str("id").expect("DB[id]"), - "status": doc.get_i32("status").expect("DB[status]") + "id": item.id, + "status": item.status }) ) } -- GitLab