Add delete route for bots. Closes #8.

This commit is contained in:
Paul
2021-08-12 14:28:46 +01:00
parent bd3606176a
commit b3bdf403cf
3 changed files with 67 additions and 5 deletions

63
src/routes/bots/delete.rs Normal file
View File

@@ -0,0 +1,63 @@
use crate::database::*;
use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result};
use mongodb::bson::doc;
#[delete("/<target>")]
pub async fn delete_bot(user: User, target: Ref) -> Result<()> {
let bot = target.fetch_bot().await?;
if bot.owner != user.id {
return Err(Error::MissingPermission);
}
let username = format!("Deleted User {}", &bot.id);
get_collection("users")
.update_one(
doc! {
"_id": &bot.id
},
doc! {
"$set": {
"username": &username,
"flags": 2
},
"$unset": {
"avatar": 1,
"status": 1,
"profile": 1
}
},
None
)
.await
.map_err(|_| Error::DatabaseError {
with: "user",
operation: "update_one"
})?;
ClientboundNotification::UserUpdate {
id: target.id.clone(),
data: json!({
"username": username,
"flags": 2
}),
clear: None,
}
.publish_as_user(target.id.clone());
get_collection("bots")
.delete_one(
doc! {
"_id": &bot.id
},
None
)
.await
.map_err(|_| Error::DatabaseError {
with: "bot",
operation: "delete_one"
})?;
Ok(())
}

View File

@@ -39,11 +39,8 @@ pub async fn edit_bot(user: User, target: Ref, data: Json<Data>) -> Result<()> {
}
let bot = target.fetch_bot().await?;
if !bot.public {
if bot.owner != user.id {
return Err(Error::BotIsPrivate);
}
if bot.owner != user.id {
return Err(Error::MissingPermission);
}
if let Some(name) = &data.name {

View File

@@ -6,6 +6,7 @@ mod fetch_public;
mod fetch;
mod fetch_owned;
mod edit;
mod delete;
pub fn routes() -> Vec<Route> {
routes![
@@ -15,5 +16,6 @@ pub fn routes() -> Vec<Route> {
fetch::fetch_bot,
fetch_owned::fetch_owned_bots,
edit::edit_bot,
delete::delete_bot,
]
}