Formatted code

This commit is contained in:
Martin Loffler
2021-01-05 15:01:24 +01:00
parent 66a2930a6f
commit bb73f905e6
8 changed files with 63 additions and 41 deletions

View File

@@ -1,11 +1,18 @@
use crate::{database::get_collection, notifications::events::ClientboundNotification, util::result::{Error, Result}};
use crate::database::guards::reference::Ref; use crate::database::guards::reference::Ref;
use mongodb::{bson::{doc, from_bson, Bson}, options::FindOptions}; use crate::{
database::get_collection,
notifications::events::ClientboundNotification,
util::result::{Error, Result},
};
use futures::StreamExt;
use mongodb::{
bson::{doc, from_bson, Bson},
options::FindOptions,
};
use rauth::auth::Session; use rauth::auth::Session;
use rocket::http::Status; use rocket::http::Status;
use rocket::request::{self, FromRequest, Outcome, Request}; use rocket::request::{self, FromRequest, Outcome, Request};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use futures::StreamExt;
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
pub enum RelationshipStatus { pub enum RelationshipStatus {
@@ -35,7 +42,7 @@ pub struct User {
// ? This should never be pushed to the collection. // ? This should never be pushed to the collection.
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub relationship: Option<RelationshipStatus> pub relationship: Option<RelationshipStatus>,
} }
#[rocket::async_trait] #[rocket::async_trait]
@@ -93,15 +100,21 @@ impl User {
}, },
FindOptions::builder() FindOptions::builder()
.projection(doc! { "_id": 1, "username": 1 }) .projection(doc! { "_id": 1, "username": 1 })
.build() .build(),
) )
.await .await
.map_err(|_| Error::DatabaseError { operation: "find", with: "users" })?; .map_err(|_| Error::DatabaseError {
operation: "find",
with: "users",
})?;
while let Some(result) = cursor.next().await { while let Some(result) = cursor.next().await {
if let Ok(doc) = result { if let Ok(doc) = result {
let mut user: User = from_bson(Bson::Document(doc)) let mut user: User =
.map_err(|_| Error::DatabaseError { operation: "from_bson", with: "user" })?; from_bson(Bson::Document(doc)).map_err(|_| Error::DatabaseError {
operation: "from_bson",
with: "user",
})?;
user.relationship = Some( user.relationship = Some(
relationships relationships
@@ -109,7 +122,7 @@ impl User {
.find(|x| user.id == x.id) .find(|x| user.id == x.id)
.ok_or_else(|| Error::InternalError)? .ok_or_else(|| Error::InternalError)?
.status .status
.clone() .clone(),
); );
users.push(user); users.push(user);

View File

@@ -33,7 +33,7 @@ pub enum ClientboundNotification {
Error(WebSocketError), Error(WebSocketError),
Authenticated, Authenticated,
Ready { Ready {
users: Vec<User> users: Vec<User>,
}, },
/*MessageCreate { /*MessageCreate {

View File

@@ -97,11 +97,10 @@ async fn accept(stream: TcpStream) {
) { ) {
send(ClientboundNotification::Authenticated); send(ClientboundNotification::Authenticated);
match task::block_on( match task::block_on(user.generate_ready_payload())
user.generate_ready_payload() {
) {
Ok(payload) => { Ok(payload) => {
send(payload); send(payload);
} }
Err(_) => { Err(_) => {
send(ClientboundNotification::Error( send(ClientboundNotification::Error(

View File

@@ -1,4 +1,6 @@
use crate::util::variables::{DISABLE_REGISTRATION, HCAPTCHA_SITEKEY, USE_EMAIL, USE_HCAPTCHA, EXTERNAL_WS_URL}; use crate::util::variables::{
DISABLE_REGISTRATION, EXTERNAL_WS_URL, HCAPTCHA_SITEKEY, USE_EMAIL, USE_HCAPTCHA,
};
use mongodb::bson::doc; use mongodb::bson::doc;
use rocket_contrib::json::JsonValue; use rocket_contrib::json::JsonValue;

View File

@@ -13,29 +13,39 @@ use crate::{
}; };
use futures::try_join; use futures::try_join;
use mongodb::bson::doc; use mongodb::bson::doc;
use mongodb::options::{FindOneOptions, Collation}; use mongodb::options::{Collation, FindOneOptions};
use rocket_contrib::json::JsonValue; use rocket_contrib::json::JsonValue;
#[put("/<username>/friend")] #[put("/<username>/friend")]
pub async fn req(user: User, username: String) -> Result<JsonValue> { pub async fn req(user: User, username: String) -> Result<JsonValue> {
let col = get_collection("users"); let col = get_collection("users");
let doc = col.find_one( let doc = col
doc! { .find_one(
"username": username doc! {
"username": username
},
FindOneOptions::builder()
.collation(Collation::builder().locale("en").strength(2).build())
.build(),
)
.await
.map_err(|_| Error::DatabaseError {
operation: "find_one",
with: "user",
})?
.ok_or_else(|| Error::UnknownUser)?;
let target_id = doc.get_str("_id").map_err(|_| Error::DatabaseError {
operation: "get_str(_id)",
with: "user",
})?;
match get_relationship(
&user,
&Ref {
id: target_id.to_string(),
}, },
FindOneOptions::builder() ) {
.collation(Collation::builder().locale("en").strength(2).build())
.build(),
)
.await
.map_err(|_| Error::DatabaseError { operation: "find_one", with: "user" })?
.ok_or_else(|| Error::UnknownUser)?;
let target_id = doc
.get_str("_id")
.map_err(|_| Error::DatabaseError { operation: "get_str(_id)", with: "user" })?;
match get_relationship(&user, &Ref { id: target_id.to_string() }) {
RelationshipStatus::User => return Err(Error::NoEffect), RelationshipStatus::User => return Err(Error::NoEffect),
RelationshipStatus::Friend => return Err(Error::AlreadyFriends), RelationshipStatus::Friend => return Err(Error::AlreadyFriends),
RelationshipStatus::Outgoing => return Err(Error::AlreadySentRequest), RelationshipStatus::Outgoing => return Err(Error::AlreadySentRequest),

View File

@@ -1,12 +1,12 @@
use rocket::Route; use rocket::Route;
mod add_friend; mod add_friend;
mod get_avatar;
mod block_user; mod block_user;
mod fetch_dms; mod fetch_dms;
mod fetch_relationship; mod fetch_relationship;
mod fetch_relationships; mod fetch_relationships;
mod fetch_user; mod fetch_user;
mod get_avatar;
mod open_dm; mod open_dm;
mod remove_friend; mod remove_friend;
mod unblock_user; mod unblock_user;
@@ -16,11 +16,9 @@ pub fn routes() -> Vec<Route> {
// User Information // User Information
fetch_user::req, fetch_user::req,
get_avatar::req, get_avatar::req,
// Direct Messaging // Direct Messaging
fetch_dms::req, fetch_dms::req,
open_dm::req, open_dm::req,
// Relationships // Relationships
fetch_relationships::req, fetch_relationships::req,
fetch_relationship::req, fetch_relationship::req,

View File

@@ -75,7 +75,7 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
with: "user", with: "user",
}), }),
} }
}, }
_ => Err(Error::NoEffect), _ => Err(Error::NoEffect),
} }
} }

View File

@@ -108,7 +108,7 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
} }
_ => Err(Error::InternalError), _ => Err(Error::InternalError),
} }
}, }
_ => Err(Error::NoEffect), _ => Err(Error::NoEffect),
} }
} }