mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-07-14 05:26:59 +00:00
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?;
|
||||
|
||||
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 {
|
||||
|
||||
@@ -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,
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user