From 8cfa5d7091e717d7c3235924f40c8e3ef56cd169 Mon Sep 17 00:00:00 2001 From: Paul <paulmakles@gmail.com> Date: Sat, 1 May 2021 17:29:31 +0100 Subject: [PATCH] Run cargo fmt. --- src/database/entities/autumn.rs | 11 +- src/database/entities/channel.rs | 11 +- src/database/entities/message.rs | 2 +- src/database/entities/user.rs | 9 +- src/database/permissions/user.rs | 7 +- src/notifications/events.rs | 2 +- src/routes/channels/message_send.rs | 5 +- src/routes/users/add_friend.rs | 16 ++- src/routes/users/block_user.rs | 16 ++- src/routes/users/edit_user.rs | 28 +++-- src/routes/users/remove_friend.rs | 8 +- src/routes/users/unblock_user.rs | 166 ++++++++++++++-------------- 12 files changed, 164 insertions(+), 117 deletions(-) diff --git a/src/database/entities/autumn.rs b/src/database/entities/autumn.rs index 0838f99..d9cf3c5 100644 --- a/src/database/entities/autumn.rs +++ b/src/database/entities/autumn.rs @@ -1,8 +1,8 @@ use mongodb::bson::{doc, from_document}; use serde::{Deserialize, Serialize}; -use crate::util::result::{Error, Result}; use crate::database::*; +use crate::util::result::{Error, Result}; #[derive(Serialize, Deserialize, Debug, Clone)] #[serde(tag = "type")] @@ -23,7 +23,7 @@ pub struct File { metadata: Metadata, content_type: String, size: isize, - + #[serde(skip_serializing_if = "Option::is_none")] deleted: Option<bool>, @@ -38,7 +38,12 @@ pub struct File { } impl File { - pub async fn find_and_use(attachment_id: &str, tag: &str, parent_type: &str, parent_id: &str) -> Result<File> { + pub async fn find_and_use( + attachment_id: &str, + tag: &str, + parent_type: &str, + parent_id: &str, + ) -> Result<File> { let attachments = get_collection("attachments"); let key = format!("{}_id", parent_type); if let Some(doc) = attachments diff --git a/src/database/entities/channel.rs b/src/database/entities/channel.rs index dce0672..6eff356 100644 --- a/src/database/entities/channel.rs +++ b/src/database/entities/channel.rs @@ -1,10 +1,13 @@ use crate::database::*; use crate::notifications::events::ClientboundNotification; use crate::util::result::{Error, Result}; -use mongodb::{bson::{Document, doc, from_document, to_document}, options::FindOptions}; +use futures::StreamExt; +use mongodb::{ + bson::{doc, from_document, to_document, Document}, + options::FindOptions, +}; use rocket_contrib::json::JsonValue; use serde::{Deserialize, Serialize}; -use futures::StreamExt; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct LastMessage { @@ -132,7 +135,7 @@ impl Channel { .into_iter() .filter_map(|x| x.get_str("_id").ok().map(|x| x.to_string())) .collect::<Vec<String>>(); - + // If we found any, mark them as deleted. if message_ids.len() > 0 { get_collection("attachments") @@ -147,7 +150,7 @@ impl Channel { "deleted": true } }, - None + None, ) .await .map_err(|_| Error::DatabaseError { diff --git a/src/database/entities/message.rs b/src/database/entities/message.rs index 35f484d..4960b78 100644 --- a/src/database/entities/message.rs +++ b/src/database/entities/message.rs @@ -271,7 +271,7 @@ impl Message { "deleted": true } }, - None + None, ) .await .map_err(|_| Error::DatabaseError { diff --git a/src/database/entities/user.rs b/src/database/entities/user.rs index 04220db..fe8ec39 100644 --- a/src/database/entities/user.rs +++ b/src/database/entities/user.rs @@ -116,10 +116,15 @@ impl User { /// Mutate the user object to appear as seen by user. /// Also overrides the relationship status. - pub async fn from_override(mut self, user: &User, relationship: RelationshipStatus) -> Result<User> { + pub async fn from_override( + mut self, + user: &User, + relationship: RelationshipStatus, + ) -> Result<User> { let permissions = PermissionCalculator::new(&user) .with_relationship(&relationship) - .for_user(&self.id).await?; + .for_user(&self.id) + .await?; self.relations = None; self.relationship = Some(relationship); diff --git a/src/database/permissions/user.rs b/src/database/permissions/user.rs index f4d7c58..c4d523a 100644 --- a/src/database/permissions/user.rs +++ b/src/database/permissions/user.rs @@ -49,7 +49,12 @@ impl<'a> PermissionCalculator<'a> { } let mut permissions: u32 = 0; - match self.relationship.clone().map(|v| v.to_owned()).unwrap_or_else(|| get_relationship(&self.perspective, &target)) { + match self + .relationship + .clone() + .map(|v| v.to_owned()) + .unwrap_or_else(|| get_relationship(&self.perspective, &target)) + { RelationshipStatus::Friend => return Ok(u32::MAX), RelationshipStatus::Blocked | RelationshipStatus::BlockedOther => { return Ok(UserPermission::Access as u32) diff --git a/src/notifications/events.rs b/src/notifications/events.rs index a71f0ae..c67c7d1 100644 --- a/src/notifications/events.rs +++ b/src/notifications/events.rs @@ -81,7 +81,7 @@ pub enum ClientboundNotification { UserRelationship { id: String, user: User, - status: RelationshipStatus + status: RelationshipStatus, }, UserPresence { id: String, diff --git a/src/routes/channels/message_send.rs b/src/routes/channels/message_send.rs index 2984e02..3f2e439 100644 --- a/src/routes/channels/message_send.rs +++ b/src/routes/channels/message_send.rs @@ -1,10 +1,7 @@ use crate::database::*; use crate::util::result::{Error, Result}; -use mongodb::{ - bson::{doc}, - options::FindOneOptions, -}; +use mongodb::{bson::doc, options::FindOneOptions}; use rocket_contrib::json::{Json, JsonValue}; use serde::{Deserialize, Serialize}; use ulid::Ulid; diff --git a/src/routes/users/add_friend.rs b/src/routes/users/add_friend.rs index 8d5cbef..4b1441b 100644 --- a/src/routes/users/add_friend.rs +++ b/src/routes/users/add_friend.rs @@ -67,8 +67,12 @@ pub async fn req(user: User, username: String) -> Result<JsonValue> { ) ) { Ok(_) => { - let target_user = target_user.from_override(&user, RelationshipStatus::Friend).await?; - let user = user.from_override(&target_user, RelationshipStatus::Friend).await?; + let target_user = target_user + .from_override(&user, RelationshipStatus::Friend) + .await?; + let user = user + .from_override(&target_user, RelationshipStatus::Friend) + .await?; try_join!( ClientboundNotification::UserRelationship { @@ -126,8 +130,12 @@ pub async fn req(user: User, username: String) -> Result<JsonValue> { ) ) { Ok(_) => { - let target_user = target_user.from_override(&user, RelationshipStatus::Outgoing).await?; - let user = user.from_override(&target_user, RelationshipStatus::Incoming).await?; + let target_user = target_user + .from_override(&user, RelationshipStatus::Outgoing) + .await?; + let user = user + .from_override(&target_user, RelationshipStatus::Incoming) + .await?; try_join!( ClientboundNotification::UserRelationship { id: user.id.clone(), diff --git a/src/routes/users/block_user.rs b/src/routes/users/block_user.rs index a554c28..10f69f2 100644 --- a/src/routes/users/block_user.rs +++ b/src/routes/users/block_user.rs @@ -76,8 +76,12 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> { ) ) { Ok(_) => { - let target = target.from_override(&user, RelationshipStatus::Friend).await?; - let user = user.from_override(&target, RelationshipStatus::Friend).await?; + let target = target + .from_override(&user, RelationshipStatus::Friend) + .await?; + let user = user + .from_override(&target, RelationshipStatus::Friend) + .await?; let target_id = target.id.clone(); try_join!( @@ -134,8 +138,12 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> { ) ) { Ok(_) => { - let target = target.from_override(&user, RelationshipStatus::Blocked).await?; - let user = user.from_override(&target, RelationshipStatus::BlockedOther).await?; + let target = target + .from_override(&user, RelationshipStatus::Blocked) + .await?; + let user = user + .from_override(&target, RelationshipStatus::BlockedOther) + .await?; let target_id = target.id.clone(); try_join!( diff --git a/src/routes/users/edit_user.rs b/src/routes/users/edit_user.rs index eace4eb..0c4a7da 100644 --- a/src/routes/users/edit_user.rs +++ b/src/routes/users/edit_user.rs @@ -23,31 +23,39 @@ pub struct Data { #[patch("/<_ignore_id>", data = "<data>")] pub async fn req(user: User, mut data: Json<Data>, _ignore_id: String) -> Result<()> { if data.0.status.is_none() && data.0.profile.is_none() && data.0.avatar.is_none() { - return Ok(()) + return Ok(()); } data.validate() .map_err(|error| Error::FailedValidation { error })?; - let mut set = to_document(&data.0).map_err(|_| Error::DatabaseError { operation: "to_document", with: "data" })?; + let mut set = to_document(&data.0).map_err(|_| Error::DatabaseError { + operation: "to_document", + with: "data", + })?; let avatar = std::mem::replace(&mut data.0.avatar, None); let attachment = if let Some(attachment_id) = avatar { let attachment = File::find_and_use(&attachment_id, "avatars", "user", &user.id).await?; - set.insert("avatar", to_document(&attachment).map_err(|_| Error::DatabaseError { operation: "to_document", with: "attachment" })?); + set.insert( + "avatar", + to_document(&attachment).map_err(|_| Error::DatabaseError { + operation: "to_document", + with: "attachment", + })?, + ); Some(attachment) } else { None }; get_collection("users") - .update_one( - doc! { "_id": &user.id }, - doc! { "$set": set }, - None - ) - .await - .map_err(|_| Error::DatabaseError { operation: "update_one", with: "user" })?; + .update_one(doc! { "_id": &user.id }, doc! { "$set": set }, None) + .await + .map_err(|_| Error::DatabaseError { + operation: "update_one", + with: "user", + })?; if let Some(status) = data.0.status { ClientboundNotification::UserUpdate { diff --git a/src/routes/users/remove_friend.rs b/src/routes/users/remove_friend.rs index 882594d..ec0176a 100644 --- a/src/routes/users/remove_friend.rs +++ b/src/routes/users/remove_friend.rs @@ -45,8 +45,12 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> { ) ) { Ok(_) => { - let target = target.from_override(&user, RelationshipStatus::None).await?; - let user = user.from_override(&target, RelationshipStatus::None).await?; + let target = target + .from_override(&user, RelationshipStatus::None) + .await?; + let user = user + .from_override(&target, RelationshipStatus::None) + .await?; let target_id = target.id.clone(); try_join!( diff --git a/src/routes/users/unblock_user.rs b/src/routes/users/unblock_user.rs index 8be4303..48db2e7 100644 --- a/src/routes/users/unblock_user.rs +++ b/src/routes/users/unblock_user.rs @@ -12,100 +12,104 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> { let target = target.fetch_user().await?; match get_relationship(&user, &target.id) { - RelationshipStatus::Blocked => { - match get_relationship(&target, &user.id) { - RelationshipStatus::Blocked => { + RelationshipStatus::Blocked => match get_relationship(&target, &user.id) { + RelationshipStatus::Blocked => { + col.update_one( + doc! { + "_id": &user.id, + "relations._id": &target.id + }, + doc! { + "$set": { + "relations.$.status": "BlockedOther" + } + }, + None, + ) + .await + .map_err(|_| Error::DatabaseError { + operation: "update_one", + with: "user", + })?; + + let target = target + .from_override(&user, RelationshipStatus::BlockedOther) + .await?; + ClientboundNotification::UserRelationship { + id: user.id.clone(), + user: target, + status: RelationshipStatus::BlockedOther, + } + .publish(user.id.clone()) + .await + .ok(); + + Ok(json!({ "status": "BlockedOther" })) + } + RelationshipStatus::BlockedOther => { + match try_join!( + col.update_one( + doc! { + "_id": &user.id + }, + doc! { + "$pull": { + "relations": { + "_id": &target.id + } + } + }, + None + ), col.update_one( doc! { - "_id": &user.id, - "relations._id": &target.id + "_id": &target.id }, doc! { - "$set": { - "relations.$.status": "BlockedOther" + "$pull": { + "relations": { + "_id": &user.id + } } }, - None, + None ) - .await - .map_err(|_| Error::DatabaseError { - operation: "update_one", - with: "user", - })?; - - let target = target.from_override(&user, RelationshipStatus::BlockedOther).await?; - ClientboundNotification::UserRelationship { - id: user.id.clone(), - user: target, - status: RelationshipStatus::BlockedOther, - } - .publish(user.id.clone()) - .await - .ok(); + ) { + Ok(_) => { + let target = target + .from_override(&user, RelationshipStatus::None) + .await?; + let user = user + .from_override(&target, RelationshipStatus::None) + .await?; + let target_id = target.id.clone(); - Ok(json!({ "status": "BlockedOther" })) - } - RelationshipStatus::BlockedOther => { - match try_join!( - col.update_one( - doc! { - "_id": &user.id - }, - doc! { - "$pull": { - "relations": { - "_id": &target.id - } - } - }, - None - ), - col.update_one( - doc! { - "_id": &target.id - }, - doc! { - "$pull": { - "relations": { - "_id": &user.id - } - } - }, - None + try_join!( + ClientboundNotification::UserRelationship { + id: user.id.clone(), + user: target, + status: RelationshipStatus::None + } + .publish(user.id.clone()), + ClientboundNotification::UserRelationship { + id: target_id.clone(), + user: user, + status: RelationshipStatus::None + } + .publish(target_id) ) - ) { - Ok(_) => { - let target = target.from_override(&user, RelationshipStatus::None).await?; - let user = user.from_override(&target, RelationshipStatus::None).await?; - let target_id = target.id.clone(); - - try_join!( - ClientboundNotification::UserRelationship { - id: user.id.clone(), - user: target, - status: RelationshipStatus::None - } - .publish(user.id.clone()), - ClientboundNotification::UserRelationship { - id: target_id.clone(), - user: user, - status: RelationshipStatus::None - } - .publish(target_id) - ) - .ok(); + .ok(); - Ok(json!({ "status": "None" })) - } - Err(_) => Err(Error::DatabaseError { - operation: "update_one", - with: "user", - }), + Ok(json!({ "status": "None" })) } + Err(_) => Err(Error::DatabaseError { + operation: "update_one", + with: "user", + }), } - _ => Err(Error::InternalError), } - } + _ => Err(Error::InternalError), + }, _ => Err(Error::NoEffect), } } -- GitLab