From 83f1fbe74742f308d48447771fa023f3917c4b6a Mon Sep 17 00:00:00 2001 From: Paul Makles <paulmakles@gmail.com> Date: Thu, 9 Apr 2020 18:43:23 +0100 Subject: [PATCH] Format and clean up code. --- src/guards/auth.rs | 4 ++-- src/guards/channel.rs | 4 ++-- src/guards/guild.rs | 2 +- src/routes/channel.rs | 56 +++++++++++++++++++++++-------------------- src/routes/user.rs | 17 +++++-------- 5 files changed, 41 insertions(+), 42 deletions(-) diff --git a/src/guards/auth.rs b/src/guards/auth.rs index b2842b4..c9b2974 100644 --- a/src/guards/auth.rs +++ b/src/guards/auth.rs @@ -133,7 +133,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for User { 0 => Outcome::Failure((Status::Forbidden, AuthError::Missing)), 1 => { let key = keys[0]; - let col = database::get_db().collection("users"); + let col = database::get_collection("users"); let result = col.find_one(doc! { "access_token": key }, None).unwrap(); if let Some(user) = result { @@ -165,7 +165,7 @@ impl<'r> FromParam<'r> for User { type Error = &'r RawStr; fn from_param(param: &'r RawStr) -> Result<Self, Self::Error> { - let col = database::get_db().collection("users"); + let col = database::get_collection("users"); let result = col .find_one(doc! { "_id": param.to_string() }, None) .unwrap(); diff --git a/src/guards/channel.rs b/src/guards/channel.rs index 756f8f3..10f4116 100644 --- a/src/guards/channel.rs +++ b/src/guards/channel.rs @@ -13,7 +13,7 @@ impl<'r> FromParam<'r> for Channel { type Error = &'r RawStr; fn from_param(param: &'r RawStr) -> Result<Self, Self::Error> { - let col = database::get_db().collection("channels"); + let col = database::get_collection("channels"); let result = col .find_one(doc! { "_id": param.to_string() }, None) .unwrap(); @@ -82,7 +82,7 @@ impl<'r> FromParam<'r> for Message { type Error = &'r RawStr; fn from_param(param: &'r RawStr) -> Result<Self, Self::Error> { - let col = database::get_db().collection("messages"); + let col = database::get_collection("messages"); let result = col .find_one(doc! { "_id": param.to_string() }, None) .unwrap(); diff --git a/src/guards/guild.rs b/src/guards/guild.rs index 6e2aeb6..aa57c55 100644 --- a/src/guards/guild.rs +++ b/src/guards/guild.rs @@ -79,7 +79,7 @@ impl<'r> FromParam<'r> for Guild { type Error = &'r RawStr; fn from_param(param: &'r RawStr) -> Result<Self, Self::Error> { - let col = database::get_db().collection("guilds"); + let col = database::get_collection("guilds"); let result = col .find_one(doc! { "_id": param.to_string() }, None) .unwrap(); diff --git a/src/routes/channel.rs b/src/routes/channel.rs index a9fedb6..0b6b3d1 100644 --- a/src/routes/channel.rs +++ b/src/routes/channel.rs @@ -129,7 +129,7 @@ pub fn channel(user: UserRef, target: ChannelRef) -> Option<Response> { } else { None } - }, + } 2 => { if let Some(info) = target.fetch_data(doc! { "name": 1, @@ -167,20 +167,21 @@ pub fn delete(user: UserRef, target: ChannelRef) -> Option<Response> { let try_delete = || { let messages = database::get_collection("messages"); - if messages.delete_many( - doc! { "channel": &target_id }, - None - ).is_ok() { - if col.delete_one( - doc! { "_id": &target_id }, - None - ).is_ok() { + if messages + .delete_many(doc! { "channel": &target_id }, None) + .is_ok() + { + if col.delete_one(doc! { "_id": &target_id }, None).is_ok() { Some(Response::Result(super::Status::Ok)) } else { - Some(Response::InternalServerError(json!({ "error": "Failed to delete group." }))) + Some(Response::InternalServerError( + json!({ "error": "Failed to delete group." }), + )) } } else { - Some(Response::InternalServerError(json!({ "error": "Failed to delete messages." }))) + Some(Response::InternalServerError( + json!({ "error": "Failed to delete messages." }), + )) } }; @@ -216,27 +217,30 @@ pub fn delete(user: UserRef, target: ChannelRef) -> Option<Response> { &owner }; - if col.update_one( - doc! { "_id": target_id }, - doc! { - "$set": { - "owner": new_owner, + if col + .update_one( + doc! { "_id": target_id }, + doc! { + "$set": { + "owner": new_owner, + }, + "$pull": { + "recipients": &user.id, + } }, - "$pull": { - "recipients": &user.id, - } - }, - None - ).is_ok() { + None, + ) + .is_ok() + { Some(Response::Result(super::Status::Ok)) } else { - Some(Response::InternalServerError(json!({ "error": "Failed to remove you from the group." }))) + Some(Response::InternalServerError( + json!({ "error": "Failed to remove you from the group." }), + )) } } } - 2 => { - try_delete() - } + 2 => try_delete(), _ => Some(Response::InternalServerError( json!({ "error": "Unknown error has occurred." }), )), diff --git a/src/routes/user.rs b/src/routes/user.rs index 0126424..004a977 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -1,10 +1,9 @@ use super::Response; -use crate::database::{self, channel::Channel}; -use crate::database::{get_relationship, get_relationship_internal, Relationship}; +use crate::database::{self, get_relationship, get_relationship_internal, Relationship}; use crate::guards::auth::UserRef; use crate::routes::channel; -use bson::{doc, from_bson}; +use bson::doc; use mongodb::options::FindOptions; use rocket_contrib::json::Json; use serde::{Deserialize, Serialize}; @@ -92,11 +91,7 @@ pub fn dms(user: UserRef) -> Response { ], "recipients": user.id }, - FindOptions::builder() - .projection(doc! { - - }) - .build(), + FindOptions::builder().projection(doc! {}).build(), ) .expect("Failed channel lookup"); @@ -113,7 +108,7 @@ pub fn dms(user: UserRef) -> Response { "type": 0, "recipients": recipients, })); - }, + } 1 => { channels.push(json!({ "id": id, @@ -123,8 +118,8 @@ pub fn dms(user: UserRef) -> Response { "owner": doc.get_str("owner").unwrap(), "description": doc.get_str("description").unwrap_or(""), })); - }, - _ => unreachable!() + } + _ => unreachable!(), } } } -- GitLab