forked from jmug/stoatchat
Format and clean up code.
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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." }),
|
||||
)),
|
||||
|
||||
@@ -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!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user