Add delete route for bots. Closes #8.
This commit is contained in:
63
src/routes/bots/delete.rs
Normal file
63
src/routes/bots/delete.rs
Normal 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(())
|
||||||
|
}
|
||||||
@@ -39,11 +39,8 @@ pub async fn edit_bot(user: User, target: Ref, data: Json<Data>) -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let bot = target.fetch_bot().await?;
|
let bot = target.fetch_bot().await?;
|
||||||
|
if bot.owner != user.id {
|
||||||
if !bot.public {
|
return Err(Error::MissingPermission);
|
||||||
if bot.owner != user.id {
|
|
||||||
return Err(Error::BotIsPrivate);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(name) = &data.name {
|
if let Some(name) = &data.name {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ mod fetch_public;
|
|||||||
mod fetch;
|
mod fetch;
|
||||||
mod fetch_owned;
|
mod fetch_owned;
|
||||||
mod edit;
|
mod edit;
|
||||||
|
mod delete;
|
||||||
|
|
||||||
pub fn routes() -> Vec<Route> {
|
pub fn routes() -> Vec<Route> {
|
||||||
routes![
|
routes![
|
||||||
@@ -15,5 +16,6 @@ pub fn routes() -> Vec<Route> {
|
|||||||
fetch::fetch_bot,
|
fetch::fetch_bot,
|
||||||
fetch_owned::fetch_owned_bots,
|
fetch_owned::fetch_owned_bots,
|
||||||
edit::edit_bot,
|
edit::edit_bot,
|
||||||
|
delete::delete_bot,
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user