Better error handling for all routes.

This commit is contained in:
Paul Makles
2020-04-07 17:54:18 +01:00
parent ca0b8411d5
commit da95f6247d
6 changed files with 226 additions and 259 deletions

View File

@@ -1,3 +1,4 @@
use super::Response;
use crate::database::{self, channel::Channel, user::User};
use crate::routes::channel;
@@ -20,12 +21,12 @@ pub fn me(user: User) -> JsonValue {
/// retrieve another user's information
#[get("/<target>")]
pub fn user(user: User, target: User) -> JsonValue {
json!({
pub fn user(user: User, target: User) -> Response {
Response::Success(json!({
"id": target.id,
"username": target.username,
"relationship": get_relationship(&user, &target) as u8
})
}))
}
#[derive(Serialize, Deserialize)]
@@ -36,7 +37,7 @@ pub struct Query {
/// lookup a user on Revolt
/// currently only supports exact username searches
#[post("/lookup", data = "<query>")]
pub fn lookup(user: User, query: Json<Query>) -> JsonValue {
pub fn lookup(user: User, query: Json<Query>) -> Response {
let col = database::get_collection("users");
let users = col
@@ -57,12 +58,12 @@ pub fn lookup(user: User, query: Json<Query>) -> JsonValue {
}));
}
json!(results)
Response::Success(json!(results))
}
/// retrieve all of your DMs
#[get("/@me/dms")]
pub fn dms(user: User) -> JsonValue {
pub fn dms(user: User) -> Response {
let col = database::get_collection("channels");
let results = col
@@ -95,23 +96,20 @@ pub fn dms(user: User) -> JsonValue {
}));
}
json!(channels)
Response::Success(json!(channels))
}
/// open a DM with a user
#[get("/<target>/dm")]
pub fn dm(user: User, target: User) -> JsonValue {
pub fn dm(user: User, target: User) -> Response {
let col = database::get_collection("channels");
match col.find_one(
doc! { "type": channel::ChannelType::DM as i32, "recipients": { "$all": [ user.id.clone(), target.id.clone() ] } },
None
).expect("Failed channel lookup") {
Some(channel) =>
json!({
"success": true,
"id": channel.get_str("_id").unwrap()
}),
Some(channel) =>
Response::Success( json!({ "id": channel.get_str("_id").unwrap() })),
None => {
let id = Ulid::new();
@@ -125,10 +123,7 @@ pub fn dm(user: User, target: User) -> JsonValue {
None
).expect("Failed insert query.");
json!({
"success": true,
"id": id.to_string()
})
Response::Success(json!({ "id": id.to_string() }))
}
}
}
@@ -168,7 +163,7 @@ fn get_relationship(a: &User, b: &User) -> Relationship {
/// retrieve all of your friends
#[get("/@me/friend")]
pub fn get_friends(user: User) -> JsonValue {
pub fn get_friends(user: User) -> Response {
let mut results = Vec::new();
if let Some(arr) = user.relations {
for item in arr {
@@ -179,170 +174,185 @@ pub fn get_friends(user: User) -> JsonValue {
}
}
json!(results)
Response::Success(json!(results))
}
/// retrieve friend status with user
#[get("/<target>/friend")]
pub fn get_friend(user: User, target: User) -> JsonValue {
pub fn get_friend(user: User, target: User) -> Response {
let relationship = get_relationship(&user, &target);
json!({
"id": target.id,
"status": relationship as u8
})
Response::Success(json!({ "status": relationship as u8 }))
}
/// create or accept a friend request
#[put("/<target>/friend")]
pub fn add_friend(user: User, target: User) -> JsonValue {
pub fn add_friend(user: User, target: User) -> Response {
let col = database::get_collection("users");
let relationship = get_relationship(&user, &target);
match relationship {
Relationship::FRIEND => json!({
"success": false,
"error": "Already friends."
}),
Relationship::OUTGOING => json!({
"success": false,
"error": "Already sent a friend request."
}),
Relationship::FRIEND => Response::BadRequest(json!({ "error": "Already friends." })),
Relationship::OUTGOING => {
Response::BadRequest(json!({ "error": "Already sent a friend request." }))
}
Relationship::INCOMING => {
col.update_one(
doc! {
"_id": user.id.clone(),
"relations.id": target.id.clone()
},
doc! {
"$set": {
"relations.$.status": Relationship::FRIEND as i32
}
},
None,
)
.expect("Failed update query.");
col.update_one(
doc! {
"_id": target.id,
"relations.id": user.id
},
doc! {
"$set": {
"relations.$.status": Relationship::FRIEND as i32
}
},
None,
)
.expect("Failed update query.");
json!({
"success": true,
"status": Relationship::FRIEND as u8,
})
if col
.update_one(
doc! {
"_id": user.id.clone(),
"relations.id": target.id.clone()
},
doc! {
"$set": {
"relations.$.status": Relationship::FRIEND as i32
}
},
None,
)
.is_ok()
{
if col
.update_one(
doc! {
"_id": target.id,
"relations.id": user.id
},
doc! {
"$set": {
"relations.$.status": Relationship::FRIEND as i32
}
},
None,
)
.is_ok()
{
Response::Success(json!({ "status": Relationship::FRIEND as u8 }))
} else {
Response::InternalServerError(
json!({ "error": "Failed to commit! Try re-adding them as a friend." }),
)
}
} else {
Response::InternalServerError(
json!({ "error": "Failed to commit to database, try again." }),
)
}
}
Relationship::BLOCKED => {
Response::BadRequest(json!({ "error": "You have blocked this person." }))
}
Relationship::BLOCKEDOTHER => {
Response::Conflict(json!({ "error": "You have been blocked by this person." }))
}
Relationship::BLOCKED => json!({
"success": false,
"error": "You have blocked this person."
}),
Relationship::BLOCKEDOTHER => json!({
"success": false,
"error": "You have been blocked by this person."
}),
Relationship::NONE => {
col.update_one(
doc! {
"_id": user.id.clone()
},
doc! {
"$push": {
"relations": {
"id": target.id.clone(),
"status": Relationship::OUTGOING as i32
if col
.update_one(
doc! {
"_id": user.id.clone()
},
doc! {
"$push": {
"relations": {
"id": target.id.clone(),
"status": Relationship::OUTGOING as i32
}
}
}
},
None,
)
.expect("Failed update query.");
col.update_one(
doc! {
"_id": target.id
},
doc! {
"$push": {
"relations": {
"id": user.id,
"status": Relationship::INCOMING as i32
}
}
},
None,
)
.expect("Failed update query.");
json!({
"success": true,
"status": Relationship::OUTGOING as u8,
})
},
None,
)
.is_ok()
{
if col
.update_one(
doc! {
"_id": target.id
},
doc! {
"$push": {
"relations": {
"id": user.id,
"status": Relationship::INCOMING as i32
}
}
},
None,
)
.is_ok()
{
Response::Success(json!({ "status": Relationship::OUTGOING as u8 }))
} else {
Response::InternalServerError(
json!({ "error": "Failed to commit! Try re-adding them as a friend." }),
)
}
} else {
Response::InternalServerError(
json!({ "error": "Failed to commit to database, try again." }),
)
}
}
Relationship::SELF => {
Response::BadRequest(json!({ "error": "You're already friends with yourself, no? c:" }))
}
Relationship::SELF => json!({
"success": false,
"error": "Cannot add yourself as a friend."
}),
}
}
/// remove a friend or deny a request
#[delete("/<target>/friend")]
pub fn remove_friend(user: User, target: User) -> JsonValue {
pub fn remove_friend(user: User, target: User) -> Response {
let col = database::get_collection("users");
let relationship = get_relationship(&user, &target);
match relationship {
Relationship::FRIEND | Relationship::OUTGOING | Relationship::INCOMING => {
col.update_one(
doc! {
"_id": user.id.clone()
},
doc! {
"$pull": {
"relations": {
"id": target.id.clone()
if col
.update_one(
doc! {
"_id": user.id.clone()
},
doc! {
"$pull": {
"relations": {
"id": target.id.clone()
}
}
}
},
None,
)
.expect("Failed update query.");
col.update_one(
doc! {
"_id": target.id
},
doc! {
"$pull": {
"relations": {
"id": user.id
}
}
},
None,
)
.expect("Failed update query.");
json!({
"success": true
})
},
None,
)
.is_ok()
{
if col
.update_one(
doc! {
"_id": target.id
},
doc! {
"$pull": {
"relations": {
"id": user.id
}
}
},
None,
)
.is_ok()
{
Response::Result(super::Status::Ok)
} else {
Response::InternalServerError(
json!({ "error": "Failed to commit! Target remains in same state." }),
)
}
} else {
Response::InternalServerError(
json!({ "error": "Failed to commit to database, try again." }),
)
}
}
Relationship::BLOCKED
| Relationship::BLOCKEDOTHER
| Relationship::NONE
| Relationship::SELF => json!({
"success": false,
"error": "This has no effect."
}),
| Relationship::SELF => Response::BadRequest(json!({ "error": "This has no effect." })),
}
}