Change how usernames, introduce display names.

This commit is contained in:
Paul Makles
2020-07-25 11:36:43 +02:00
parent c271054613
commit 5e59c553f3
9 changed files with 640 additions and 504 deletions

1075
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
[package]
name = "revolt"
version = "0.2.0"
version = "0.2.2"
authors = ["Paul Makles <paulmakles@gmail.com>"]
edition = "2018"

View File

@@ -23,6 +23,7 @@ pub struct User {
pub email: String,
pub username: String,
pub password: String,
pub display_name: String,
pub access_token: Option<String>,
pub email_verification: UserEmailVerification,
pub relations: Option<Vec<UserRelationship>>,

View File

@@ -12,6 +12,7 @@ use database::user::{User, UserRelationship};
pub struct UserRef {
pub id: String,
pub username: String,
pub display_name: String,
pub email_verified: bool,
}
@@ -23,6 +24,7 @@ impl UserRef {
.projection(doc! {
"_id": 1,
"username": 1,
"display_name": 1,
"email_verification.verified": 1,
})
.build(),
@@ -31,6 +33,7 @@ impl UserRef {
Some(doc) => Some(UserRef {
id: doc.get_str("_id").unwrap().to_string(),
username: doc.get_str("username").unwrap().to_string(),
display_name: doc.get_str("display_name").unwrap().to_string(),
email_verified: doc
.get_document("email_verification")
.unwrap()
@@ -99,6 +102,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for UserRef {
.projection(doc! {
"_id": 1,
"username": 1,
"display_name": 1,
"email_verification.verified": 1,
})
.build(),
@@ -109,6 +113,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for UserRef {
Outcome::Success(UserRef {
id: user.get_str("_id").unwrap().to_string(),
username: user.get_str("username").unwrap().to_string(),
display_name: user.get_str("display_name").unwrap().to_string(),
email_verified: user
.get_document("email_verification")
.unwrap()

View File

@@ -1,4 +1,5 @@
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate rocket;
#[macro_use]

View File

@@ -53,6 +53,13 @@ pub fn create(info: Json<Create>) -> Response {
return Response::Conflict(json!({ "error": "Email already in use!" }));
}
if let Some(_) = col
.find_one(doc! { "username": info.username.clone() }, None)
.expect("Failed user lookup")
{
return Response::Conflict(json!({ "error": "Username already in use!" }));
}
if let Ok(hashed) = hash(info.password.clone(), 10) {
let access_token = gen_token(92);
let code = gen_token(48);
@@ -62,6 +69,7 @@ pub fn create(info: Json<Create>) -> Response {
"_id": Ulid::new().to_string(),
"email": info.email.clone(),
"username": info.username.clone(),
"display_name": info.username.clone(),
"password": hashed,
"access_token": access_token,
"email_verification": {
@@ -133,7 +141,7 @@ pub fn verify_email(code: String) -> Response {
email::send_welcome_email(target.to_string(), user.username);
Response::Redirect(
super::Redirect::to("https://example.com"), // ! FIXME; redirect to landing page
super::Redirect::to("https://app.revolt.chat"),
)
}
} else {

View File

@@ -84,7 +84,7 @@ pub fn mount(rocket: Rocket) -> Rocket {
routes![
user::me,
user::user,
user::lookup,
user::query,
user::dms,
user::dm,
user::get_friends,

View File

@@ -6,7 +6,7 @@ use bson::doc;
#[get("/")]
pub fn root() -> Response {
Response::Success(json!({
"revolt": "0.2.0"
"revolt": "0.2.2"
}))
}

View File

@@ -8,7 +8,7 @@ use crate::notifications::{
use crate::routes::channel;
use bson::doc;
use mongodb::options::FindOptions;
use mongodb::options::{Collation, FindOptions, FindOneOptions};
use rocket_contrib::json::Json;
use serde::{Deserialize, Serialize};
use ulid::Ulid;
@@ -20,6 +20,7 @@ pub fn me(user: UserRef) -> Response {
Response::Success(json!({
"id": user.id,
"username": user.username,
"display_name": user.display_name,
"email": info.get_str("email").unwrap(),
"verified": user.email_verified,
}))
@@ -36,6 +37,7 @@ pub fn user(user: UserRef, target: UserRef) -> Response {
Response::Success(json!({
"id": target.id,
"username": target.username,
"display_name": target.display_name,
"relationship": get_relationship(&user, &target) as i32,
"mutual": {
"guilds": mutual::find_mutual_guilds(&user.id, &target.id),
@@ -46,6 +48,46 @@ pub fn user(user: UserRef, target: UserRef) -> Response {
}
#[derive(Serialize, Deserialize)]
pub struct UserQuery {
username: String,
}
/// find a user by their username
#[post("/query", data = "<query>")]
pub fn query(user: UserRef, query: Json<UserQuery>) -> Response {
let relationships = user.fetch_relationships();
let col = database::get_collection("users");
if let Ok(result) = col.find_one(
doc! { "username": query.username.clone() },
FindOneOptions::builder()
.collation(
Collation::builder()
.locale("en")
.strength(2)
.build()
)
.build()
) {
if let Some(doc) = result {
let id = doc.get_str("_id").unwrap();
Response::Success(json!({
"id": id,
"username": doc.get_str("username").unwrap(),
"display_name": doc.get_str("display_name").unwrap(),
"relationship": get_relationship_internal(&user.id, &id, &relationships) as i32
}))
} else {
Response::NotFound(json!({
"error": "User not found!"
}))
}
} else {
Response::InternalServerError(json!({ "error": "Failed database query." }))
}
}
/*#[derive(Serialize, Deserialize)]
pub struct LookupQuery {
username: String,
}
@@ -80,7 +122,7 @@ pub fn lookup(user: UserRef, query: Json<LookupQuery>) -> Response {
} else {
Response::InternalServerError(json!({ "error": "Failed database query." }))
}
}
}*/
/// retrieve all of your DMs
#[get("/@me/dms")]