forked from jmug/stoatchat
Formatted code
This commit is contained in:
@@ -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
|
||||
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",
|
||||
})?;
|
||||
|
||||
match get_relationship(
|
||||
&user,
|
||||
&Ref {
|
||||
id: target_id.to_string(),
|
||||
},
|
||||
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" })?;
|
||||
|
||||
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),
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -75,7 +75,7 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
with: "user",
|
||||
}),
|
||||
}
|
||||
},
|
||||
}
|
||||
_ => Err(Error::NoEffect),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,7 +108,7 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
}
|
||||
_ => Err(Error::InternalError),
|
||||
}
|
||||
},
|
||||
}
|
||||
_ => Err(Error::NoEffect),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user