Finish implementing schema.
This commit is contained in:
@@ -2,7 +2,7 @@ use rocket::Outcome;
|
|||||||
use rocket::http::{ Status, RawStr };
|
use rocket::http::{ Status, RawStr };
|
||||||
use rocket::request::{ self, Request, FromRequest, FromParam };
|
use rocket::request::{ self, Request, FromRequest, FromParam };
|
||||||
|
|
||||||
use bson::{ bson, doc, ordered::OrderedDocument };
|
use bson::{ bson, doc, from_bson };
|
||||||
use ulid::Ulid;
|
use ulid::Ulid;
|
||||||
|
|
||||||
use crate::database;
|
use crate::database;
|
||||||
@@ -10,7 +10,7 @@ use crate::database;
|
|||||||
pub struct User(
|
pub struct User(
|
||||||
pub Ulid,
|
pub Ulid,
|
||||||
pub String,
|
pub String,
|
||||||
pub OrderedDocument,
|
pub database::user::User,
|
||||||
);
|
);
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -36,7 +36,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for User {
|
|||||||
Outcome::Success(User (
|
Outcome::Success(User (
|
||||||
Ulid::from_string(user.get_str("_id").unwrap()).unwrap(),
|
Ulid::from_string(user.get_str("_id").unwrap()).unwrap(),
|
||||||
user.get_str("username").unwrap().to_string(),
|
user.get_str("username").unwrap().to_string(),
|
||||||
user
|
from_bson(bson::Bson::Document(user)).expect("Failed to unwrap user.")
|
||||||
))
|
))
|
||||||
} else {
|
} else {
|
||||||
Outcome::Failure((Status::Forbidden, AuthError::Invalid))
|
Outcome::Failure((Status::Forbidden, AuthError::Invalid))
|
||||||
@@ -58,7 +58,7 @@ impl<'r> FromParam<'r> for User {
|
|||||||
Ok(User (
|
Ok(User (
|
||||||
Ulid::from_string(user.get_str("_id").unwrap()).unwrap(),
|
Ulid::from_string(user.get_str("_id").unwrap()).unwrap(),
|
||||||
user.get_str("username").unwrap().to_string(),
|
user.get_str("username").unwrap().to_string(),
|
||||||
user
|
from_bson(bson::Bson::Document(user)).expect("Failed to unwrap user.")
|
||||||
))
|
))
|
||||||
} else {
|
} else {
|
||||||
Err(param)
|
Err(param)
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ use rocket_contrib::json::{ Json, JsonValue };
|
|||||||
use serde::{ Serialize, Deserialize };
|
use serde::{ Serialize, Deserialize };
|
||||||
use validator::validate_email;
|
use validator::validate_email;
|
||||||
use bcrypt::{ hash, verify };
|
use bcrypt::{ hash, verify };
|
||||||
|
use database::user::User;
|
||||||
use chrono::prelude::*;
|
use chrono::prelude::*;
|
||||||
use ulid::Ulid;
|
use ulid::Ulid;
|
||||||
|
|
||||||
@@ -112,18 +113,18 @@ pub fn verify_email(code: String) -> JsonValue {
|
|||||||
|
|
||||||
if let Some(u) =
|
if let Some(u) =
|
||||||
col.find_one(doc! { "email_verification.code": code.clone() }, None).expect("Failed user lookup") {
|
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 user: User = from_bson(bson::Bson::Document(u)).expect("Failed to unwrap user.");
|
||||||
let expiry = ev.get_utc_datetime("expiry").expect("DOC[expiry]");
|
let ev = user.email_verification;
|
||||||
|
|
||||||
if Utc::now() > *expiry {
|
if Utc::now() > *ev.expiry.unwrap() {
|
||||||
json!({
|
json!({
|
||||||
"success": false,
|
"success": false,
|
||||||
"error": "Token has expired!",
|
"error": "Token has expired!",
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
let target = ev.get_str("target").expect("DOC[target]");
|
let target = ev.target.unwrap();
|
||||||
col.update_one(
|
col.update_one(
|
||||||
doc! { "_id": u.get_str("_id").expect("Failed to retrieve user id.") },
|
doc! { "_id": user.id },
|
||||||
doc! {
|
doc! {
|
||||||
"$unset": {
|
"$unset": {
|
||||||
"email_verification.code": "",
|
"email_verification.code": "",
|
||||||
@@ -141,7 +142,7 @@ pub fn verify_email(code: String) -> JsonValue {
|
|||||||
|
|
||||||
email::send_welcome_email(
|
email::send_welcome_email(
|
||||||
target.to_string(),
|
target.to_string(),
|
||||||
u.get_str("username").expect("Failed to retrieve username.").to_string()
|
user.username
|
||||||
);
|
);
|
||||||
|
|
||||||
json!({
|
json!({
|
||||||
@@ -171,9 +172,11 @@ pub fn resend_email(info: Json<Resend>) -> JsonValue {
|
|||||||
|
|
||||||
if let Some(u) =
|
if let Some(u) =
|
||||||
col.find_one(doc! { "email_verification.target": info.email.clone() }, None).expect("Failed user lookup") {
|
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 user: User = from_bson(bson::Bson::Document(u)).expect("Failed to unwrap user.");
|
||||||
let expiry = ev.get_utc_datetime("expiry").expect("DOC[expiry]");
|
let ev = user.email_verification;
|
||||||
let rate_limit = ev.get_utc_datetime("rate_limit").expect("DOC[rate_limit]");
|
|
||||||
|
let expiry = ev.expiry.unwrap();
|
||||||
|
let rate_limit = ev.rate_limit.unwrap();
|
||||||
|
|
||||||
if Utc::now() < *rate_limit {
|
if Utc::now() < *rate_limit {
|
||||||
json!({
|
json!({
|
||||||
@@ -182,7 +185,7 @@ pub fn resend_email(info: Json<Resend>) -> JsonValue {
|
|||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
let mut new_expiry = UtcDatetime(Utc::now() + chrono::Duration::days(1));
|
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 {
|
if Utc::now() > *expiry {
|
||||||
return json!({
|
return json!({
|
||||||
"success": "false",
|
"success": "false",
|
||||||
@@ -195,7 +198,7 @@ pub fn resend_email(info: Json<Resend>) -> JsonValue {
|
|||||||
|
|
||||||
let code = gen_token(48);
|
let code = gen_token(48);
|
||||||
col.update_one(
|
col.update_one(
|
||||||
doc! { "_id": u.get_str("_id").expect("Failed to retrieve user id.") },
|
doc! { "_id": user.id },
|
||||||
doc! {
|
doc! {
|
||||||
"$set": {
|
"$set": {
|
||||||
"email_verification.code": code.clone(),
|
"email_verification.code": code.clone(),
|
||||||
@@ -243,7 +246,7 @@ pub fn login(info: Json<Login>) -> JsonValue {
|
|||||||
|
|
||||||
if let Some(u) =
|
if let Some(u) =
|
||||||
col.find_one(doc! { "email": info.email.clone() }, None).expect("Failed user lookup") {
|
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)
|
match verify(info.password.clone(), &user.password)
|
||||||
.expect("Failed to check hash of password.") {
|
.expect("Failed to check hash of password.") {
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ use crate::routes::channel;
|
|||||||
|
|
||||||
use rocket_contrib::json::{ Json, JsonValue };
|
use rocket_contrib::json::{ Json, JsonValue };
|
||||||
use serde::{ Serialize, Deserialize };
|
use serde::{ Serialize, Deserialize };
|
||||||
|
use bson::{ bson, doc, from_bson };
|
||||||
use mongodb::options::FindOptions;
|
use mongodb::options::FindOptions;
|
||||||
use bson::{ bson, doc };
|
|
||||||
use ulid::Ulid;
|
use ulid::Ulid;
|
||||||
|
|
||||||
/// retrieve your user information
|
/// retrieve your user information
|
||||||
@@ -16,9 +16,8 @@ pub fn me(user: User) -> JsonValue {
|
|||||||
json!({
|
json!({
|
||||||
"id": id.to_string(),
|
"id": id.to_string(),
|
||||||
"username": username,
|
"username": username,
|
||||||
"email": doc.get_str("email").expect("Missing email in user object!"),
|
"email": doc.email,
|
||||||
"verified": doc.get_document("email_verification").expect("DOC[email_verification]")
|
"verified": doc.email_verification.verified,
|
||||||
.get_bool("verified").expect("DOC[verified]"),
|
|
||||||
"created_timestamp": id.datetime().timestamp(),
|
"created_timestamp": id.datetime().timestamp(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -47,11 +46,11 @@ pub fn lookup(_user: User, query: Json<Query>) -> JsonValue {
|
|||||||
|
|
||||||
let mut results = Vec::new();
|
let mut results = Vec::new();
|
||||||
for user in users {
|
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(
|
results.push(
|
||||||
json!({
|
json!({
|
||||||
"id": u.get_str("_id").expect("DB[id]"),
|
"id": u.id,
|
||||||
"username": u.get_str("username").expect("DB[username]")
|
"username": u.username
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -158,15 +157,12 @@ fn get_relationship(a: &User, b: &User) -> Relationship {
|
|||||||
return Relationship::SELF
|
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();
|
let id = a.0.to_string();
|
||||||
|
|
||||||
for entry in arr {
|
for entry in arr {
|
||||||
let relation = entry.as_document().expect("Expected document in relations array.");
|
if entry.id == id {
|
||||||
|
match entry.status {
|
||||||
if relation.get_str("id").expect("DB[id]") == id {
|
|
||||||
|
|
||||||
match relation.get_i32("status").expect("DB[status]") {
|
|
||||||
0 => {
|
0 => {
|
||||||
return Relationship::FRIEND
|
return Relationship::FRIEND
|
||||||
},
|
},
|
||||||
@@ -183,7 +179,6 @@ fn get_relationship(a: &User, b: &User) -> Relationship {
|
|||||||
return Relationship::NONE
|
return Relationship::NONE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -195,13 +190,12 @@ fn get_relationship(a: &User, b: &User) -> Relationship {
|
|||||||
#[get("/@me/friend")]
|
#[get("/@me/friend")]
|
||||||
pub fn get_friends(user: User) -> JsonValue {
|
pub fn get_friends(user: User) -> JsonValue {
|
||||||
let mut results = Vec::new();
|
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 {
|
for item in arr {
|
||||||
let doc = item.as_document().expect("Expected document in relations array.");
|
|
||||||
results.push(
|
results.push(
|
||||||
json!({
|
json!({
|
||||||
"id": doc.get_str("id").expect("DB[id]"),
|
"id": item.id,
|
||||||
"status": doc.get_i32("status").expect("DB[status]")
|
"status": item.status
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user