feat(bots): implement bot routes
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
use crate::util::regex::RE_USERNAME;
|
||||
|
||||
use revolt_quark::{models::bot::FieldsBot, EmptyResponse, Result};
|
||||
use revolt_quark::{
|
||||
models::{
|
||||
bot::{FieldsBot, PartialBot},
|
||||
Bot, User,
|
||||
},
|
||||
Db, Error, Ref, Result,
|
||||
};
|
||||
|
||||
use mongodb::bson::doc;
|
||||
use rocket::serde::json::Json;
|
||||
@@ -15,13 +21,67 @@ pub struct Data {
|
||||
public: Option<bool>,
|
||||
analytics: Option<bool>,
|
||||
interactions_url: Option<String>,
|
||||
remove: Option<FieldsBot>,
|
||||
#[validate(length(min = 1))]
|
||||
remove: Option<Vec<FieldsBot>>,
|
||||
}
|
||||
|
||||
#[patch("/<target>", data = "<data>")]
|
||||
pub async fn edit_bot(
|
||||
/*user: UserRef, target: Ref,*/ target: String,
|
||||
data: Json<Data>,
|
||||
) -> Result<EmptyResponse> {
|
||||
todo!()
|
||||
pub async fn edit_bot(db: &Db, user: User, target: Ref, data: Json<Data>) -> Result<Json<Bot>> {
|
||||
if user.bot.is_some() {
|
||||
return Err(Error::IsBot);
|
||||
}
|
||||
|
||||
let data = data.into_inner();
|
||||
data.validate()
|
||||
.map_err(|error| Error::FailedValidation { error })?;
|
||||
|
||||
let mut bot = target.as_bot(db).await?;
|
||||
if bot.owner != user.id {
|
||||
return Err(Error::NotFound);
|
||||
}
|
||||
|
||||
if let Some(name) = data.name {
|
||||
if db.is_username_taken(&name).await? {
|
||||
return Err(Error::UsernameTaken);
|
||||
}
|
||||
|
||||
let mut user = db.fetch_user(&bot.id).await?;
|
||||
user.update_username(db, name).await?;
|
||||
}
|
||||
|
||||
if data.public.is_none()
|
||||
|| data.analytics.is_none()
|
||||
|| data.interactions_url.is_none()
|
||||
|| data.remove.is_none()
|
||||
{
|
||||
return Ok(Json(bot));
|
||||
}
|
||||
|
||||
let Data {
|
||||
public,
|
||||
analytics,
|
||||
interactions_url,
|
||||
remove,
|
||||
..
|
||||
} = data;
|
||||
let mut partial = PartialBot {
|
||||
public,
|
||||
analytics,
|
||||
interactions_url,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
if let Some(remove) = &remove {
|
||||
for field in remove {
|
||||
bot.remove(field);
|
||||
}
|
||||
|
||||
if remove.iter().any(|x| x == &FieldsBot::Token) {
|
||||
partial.token = Some(bot.token.clone());
|
||||
}
|
||||
}
|
||||
|
||||
db.update_bot(&bot.id, &partial, remove.unwrap_or_else(Vec::new))
|
||||
.await?;
|
||||
Ok(Json(bot))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user