Format and clean up code.

This commit is contained in:
Paul Makles
2020-04-09 18:43:23 +01:00
parent 8a4a386ec5
commit 83f1fbe747
5 changed files with 41 additions and 42 deletions

View File

@@ -133,7 +133,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for User {
0 => Outcome::Failure((Status::Forbidden, AuthError::Missing)), 0 => Outcome::Failure((Status::Forbidden, AuthError::Missing)),
1 => { 1 => {
let key = keys[0]; 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(); let result = col.find_one(doc! { "access_token": key }, None).unwrap();
if let Some(user) = result { if let Some(user) = result {
@@ -165,7 +165,7 @@ impl<'r> FromParam<'r> for User {
type Error = &'r RawStr; type Error = &'r RawStr;
fn from_param(param: &'r RawStr) -> Result<Self, Self::Error> { 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 let result = col
.find_one(doc! { "_id": param.to_string() }, None) .find_one(doc! { "_id": param.to_string() }, None)
.unwrap(); .unwrap();

View File

@@ -13,7 +13,7 @@ impl<'r> FromParam<'r> for Channel {
type Error = &'r RawStr; type Error = &'r RawStr;
fn from_param(param: &'r RawStr) -> Result<Self, Self::Error> { 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 let result = col
.find_one(doc! { "_id": param.to_string() }, None) .find_one(doc! { "_id": param.to_string() }, None)
.unwrap(); .unwrap();
@@ -82,7 +82,7 @@ impl<'r> FromParam<'r> for Message {
type Error = &'r RawStr; type Error = &'r RawStr;
fn from_param(param: &'r RawStr) -> Result<Self, Self::Error> { 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 let result = col
.find_one(doc! { "_id": param.to_string() }, None) .find_one(doc! { "_id": param.to_string() }, None)
.unwrap(); .unwrap();

View File

@@ -79,7 +79,7 @@ impl<'r> FromParam<'r> for Guild {
type Error = &'r RawStr; type Error = &'r RawStr;
fn from_param(param: &'r RawStr) -> Result<Self, Self::Error> { 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 let result = col
.find_one(doc! { "_id": param.to_string() }, None) .find_one(doc! { "_id": param.to_string() }, None)
.unwrap(); .unwrap();

View File

@@ -129,7 +129,7 @@ pub fn channel(user: UserRef, target: ChannelRef) -> Option<Response> {
} else { } else {
None None
} }
}, }
2 => { 2 => {
if let Some(info) = target.fetch_data(doc! { if let Some(info) = target.fetch_data(doc! {
"name": 1, "name": 1,
@@ -167,20 +167,21 @@ pub fn delete(user: UserRef, target: ChannelRef) -> Option<Response> {
let try_delete = || { let try_delete = || {
let messages = database::get_collection("messages"); let messages = database::get_collection("messages");
if messages.delete_many( if messages
doc! { "channel": &target_id }, .delete_many(doc! { "channel": &target_id }, None)
None .is_ok()
).is_ok() { {
if col.delete_one( if col.delete_one(doc! { "_id": &target_id }, None).is_ok() {
doc! { "_id": &target_id },
None
).is_ok() {
Some(Response::Result(super::Status::Ok)) Some(Response::Result(super::Status::Ok))
} else { } else {
Some(Response::InternalServerError(json!({ "error": "Failed to delete group." }))) Some(Response::InternalServerError(
json!({ "error": "Failed to delete group." }),
))
} }
} else { } 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 &owner
}; };
if col.update_one( if col
doc! { "_id": target_id }, .update_one(
doc! { doc! { "_id": target_id },
"$set": { doc! {
"owner": new_owner, "$set": {
"owner": new_owner,
},
"$pull": {
"recipients": &user.id,
}
}, },
"$pull": { None,
"recipients": &user.id, )
} .is_ok()
}, {
None
).is_ok() {
Some(Response::Result(super::Status::Ok)) Some(Response::Result(super::Status::Ok))
} else { } 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 => { 2 => try_delete(),
try_delete()
}
_ => Some(Response::InternalServerError( _ => Some(Response::InternalServerError(
json!({ "error": "Unknown error has occurred." }), json!({ "error": "Unknown error has occurred." }),
)), )),

View File

@@ -1,10 +1,9 @@
use super::Response; use super::Response;
use crate::database::{self, channel::Channel}; use crate::database::{self, get_relationship, get_relationship_internal, Relationship};
use crate::database::{get_relationship, get_relationship_internal, Relationship};
use crate::guards::auth::UserRef; use crate::guards::auth::UserRef;
use crate::routes::channel; use crate::routes::channel;
use bson::{doc, from_bson}; use bson::doc;
use mongodb::options::FindOptions; use mongodb::options::FindOptions;
use rocket_contrib::json::Json; use rocket_contrib::json::Json;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@@ -92,11 +91,7 @@ pub fn dms(user: UserRef) -> Response {
], ],
"recipients": user.id "recipients": user.id
}, },
FindOptions::builder() FindOptions::builder().projection(doc! {}).build(),
.projection(doc! {
})
.build(),
) )
.expect("Failed channel lookup"); .expect("Failed channel lookup");
@@ -113,7 +108,7 @@ pub fn dms(user: UserRef) -> Response {
"type": 0, "type": 0,
"recipients": recipients, "recipients": recipients,
})); }));
}, }
1 => { 1 => {
channels.push(json!({ channels.push(json!({
"id": id, "id": id,
@@ -123,8 +118,8 @@ pub fn dms(user: UserRef) -> Response {
"owner": doc.get_str("owner").unwrap(), "owner": doc.get_str("owner").unwrap(),
"description": doc.get_str("description").unwrap_or(""), "description": doc.get_str("description").unwrap_or(""),
})); }));
}, }
_ => unreachable!() _ => unreachable!(),
} }
} }
} }