diff --git a/src/database/entities/user.rs b/src/database/entities/user.rs index 53bd531e849c74b3bb438037c708d903a3626d97..9bfa8bfe47e08184d6181f85bda27f3646faab74 100644 --- a/src/database/entities/user.rs +++ b/src/database/entities/user.rs @@ -1,11 +1,18 @@ -use crate::{database::get_collection, notifications::events::ClientboundNotification, util::result::{Error, Result}}; 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 rocket::http::Status; use rocket::request::{self, FromRequest, Outcome, Request}; use serde::{Deserialize, Serialize}; -use futures::StreamExt; #[derive(Serialize, Deserialize, Debug, Clone)] pub enum RelationshipStatus { @@ -35,7 +42,7 @@ pub struct User { // ? This should never be pushed to the collection. #[serde(skip_serializing_if = "Option::is_none")] - pub relationship: Option<RelationshipStatus> + pub relationship: Option<RelationshipStatus>, } #[rocket::async_trait] @@ -77,13 +84,13 @@ impl User { pub async fn generate_ready_payload(self) -> Result<ClientboundNotification> { let mut users = vec![]; - + if let Some(relationships) = &self.relations { let user_ids: Vec<String> = relationships .iter() .map(|relationship| relationship.id.clone()) .collect(); - + let mut cursor = get_collection("users") .find( doc! { @@ -93,23 +100,29 @@ impl User { }, FindOptions::builder() .projection(doc! { "_id": 1, "username": 1 }) - .build() + .build(), ) .await - .map_err(|_| Error::DatabaseError { operation: "find", with: "users" })?; - + .map_err(|_| Error::DatabaseError { + operation: "find", + with: "users", + })?; + while let Some(result) = cursor.next().await { if let Ok(doc) = result { - let mut user: User = from_bson(Bson::Document(doc)) - .map_err(|_| Error::DatabaseError { operation: "from_bson", with: "user" })?; - + let mut user: User = + from_bson(Bson::Document(doc)).map_err(|_| Error::DatabaseError { + operation: "from_bson", + with: "user", + })?; + user.relationship = Some( relationships .iter() .find(|x| user.id == x.id) .ok_or_else(|| Error::InternalError)? .status - .clone() + .clone(), ); users.push(user); diff --git a/src/notifications/events.rs b/src/notifications/events.rs index eb289fefc715c3932cd819dcf385d67a61b11465..8a68ca6067a73587dcaab848163d3ac771a7706c 100644 --- a/src/notifications/events.rs +++ b/src/notifications/events.rs @@ -33,7 +33,7 @@ pub enum ClientboundNotification { Error(WebSocketError), Authenticated, Ready { - users: Vec<User> + users: Vec<User>, }, /*MessageCreate { diff --git a/src/notifications/websocket.rs b/src/notifications/websocket.rs index 6bc89361104bd7091407aa160f25a3a4947abc4d..9845c6c3f3fb32c8222d95a78a3b104f8dfefadb 100644 --- a/src/notifications/websocket.rs +++ b/src/notifications/websocket.rs @@ -97,11 +97,10 @@ async fn accept(stream: TcpStream) { ) { send(ClientboundNotification::Authenticated); - match task::block_on( - user.generate_ready_payload() - ) { + match task::block_on(user.generate_ready_payload()) + { Ok(payload) => { - send(payload); + send(payload); } Err(_) => { send(ClientboundNotification::Error( diff --git a/src/routes/root.rs b/src/routes/root.rs index a00b1193491a5123d624e1c131cbeb53d361abaa..3465ac269e276e00489bdbe5620f2fc50345df5c 100644 --- a/src/routes/root.rs +++ b/src/routes/root.rs @@ -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 rocket_contrib::json::JsonValue; diff --git a/src/routes/users/add_friend.rs b/src/routes/users/add_friend.rs index 091728a9d2ba838d107d713adc096c7683d4d8d5..cddd884375346c0151ad5aa8f6583753c9c6c207 100644 --- a/src/routes/users/add_friend.rs +++ b/src/routes/users/add_friend.rs @@ -13,29 +13,39 @@ use crate::{ }; use futures::try_join; use mongodb::bson::doc; -use mongodb::options::{FindOneOptions, Collation}; +use mongodb::options::{Collation, FindOneOptions}; use rocket_contrib::json::JsonValue; #[put("/<username>/friend")] pub async fn req(user: User, username: String) -> Result<JsonValue> { let col = get_collection("users"); - let doc = col.find_one( - 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 doc = col + .find_one( + 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" })?; + 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() }) { + match get_relationship( + &user, + &Ref { + id: target_id.to_string(), + }, + ) { RelationshipStatus::User => return Err(Error::NoEffect), RelationshipStatus::Friend => return Err(Error::AlreadyFriends), RelationshipStatus::Outgoing => return Err(Error::AlreadySentRequest), diff --git a/src/routes/users/mod.rs b/src/routes/users/mod.rs index 0f748d85adcc7f026e16308fc1c59a4e350f9f59..b80ce7d16170eefc26653a6e759517566561a6b0 100644 --- a/src/routes/users/mod.rs +++ b/src/routes/users/mod.rs @@ -1,12 +1,12 @@ use rocket::Route; mod add_friend; -mod get_avatar; mod block_user; mod fetch_dms; mod fetch_relationship; mod fetch_relationships; mod fetch_user; +mod get_avatar; mod open_dm; mod remove_friend; mod unblock_user; @@ -16,11 +16,9 @@ pub fn routes() -> Vec<Route> { // User Information fetch_user::req, get_avatar::req, - // Direct Messaging fetch_dms::req, open_dm::req, - // Relationships fetch_relationships::req, fetch_relationship::req, diff --git a/src/routes/users/remove_friend.rs b/src/routes/users/remove_friend.rs index ebec7c6b9bc0c18439315ad75f84537fed011771..2c108f78b1c7f6bd6ed4ab103323d6ad62f7c63c 100644 --- a/src/routes/users/remove_friend.rs +++ b/src/routes/users/remove_friend.rs @@ -16,10 +16,6 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> { let col = get_collection("users"); match get_relationship(&user, &target) { - RelationshipStatus::Blocked - | RelationshipStatus::BlockedOther - | RelationshipStatus::User - | RelationshipStatus::None => Err(Error::NoEffect), RelationshipStatus::Friend | RelationshipStatus::Outgoing | RelationshipStatus::Incoming => { @@ -80,5 +76,6 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> { }), } } + _ => Err(Error::NoEffect), } } diff --git a/src/routes/users/unblock_user.rs b/src/routes/users/unblock_user.rs index 2d7a13b6eee1b674ddd8b8fe53bbf7d52cf42dfc..7fc7ddf17341b8108aa8f301eeb4a8ebc34f5fdf 100644 --- a/src/routes/users/unblock_user.rs +++ b/src/routes/users/unblock_user.rs @@ -16,12 +16,6 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> { let col = get_collection("users"); match get_relationship(&user, &target) { - RelationshipStatus::None - | RelationshipStatus::User - | RelationshipStatus::BlockedOther - | RelationshipStatus::Incoming - | RelationshipStatus::Outgoing - | RelationshipStatus::Friend => Err(Error::NoEffect), RelationshipStatus::Blocked => { match get_relationship(&target.fetch_user().await?, &user.as_ref()) { RelationshipStatus::Blocked => { @@ -115,5 +109,6 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> { _ => Err(Error::InternalError), } } + _ => Err(Error::NoEffect), } } diff --git a/src/util/variables.rs b/src/util/variables.rs index 282698402ed71f5bf45c7dbb5ee7433f871210b0..9e630ddf28ef90d3cb34b9f107f3c47ecf91502a 100644 --- a/src/util/variables.rs +++ b/src/util/variables.rs @@ -42,12 +42,8 @@ lazy_static! { pub fn preflight_checks() { if *USE_EMAIL == false { #[cfg(not(debug_assertions))] - { - if !env::var("REVOLT_UNSAFE_NO_EMAIL").map_or(false, |v| v == *"1") { - panic!( - "Not letting you run this in production, set REVOLT_UNSAFE_NO_EMAIL=1 to run." - ); - } + if !env::var("REVOLT_UNSAFE_NO_EMAIL").map_or(false, |v| v == *"1") { + panic!("Running in production without email is not recommended, set REVOLT_UNSAFE_NO_EMAIL=1 to override."); } #[cfg(debug_assertions)] @@ -56,10 +52,8 @@ pub fn preflight_checks() { if *USE_HCAPTCHA == false { #[cfg(not(debug_assertions))] - { - if !env::var("REVOLT_UNSAFE_NO_CAPTCHA").map_or(false, |v| v == *"1") { - panic!("Not letting you run this in production, set REVOLT_UNSAFE_NO_CAPTCHA=1 to run."); - } + if !env::var("REVOLT_UNSAFE_NO_CAPTCHA").map_or(false, |v| v == *"1") { + panic!("Running in production without CAPTCHA is not recommended, set REVOLT_UNSAFE_NO_CAPTCHA=1 to override."); } #[cfg(debug_assertions)]