chore(thanos): strip down codebase to just API routes
This commit is contained in:
@@ -1,13 +1,9 @@
|
||||
use crate::database::*;
|
||||
use crate::util::result::{Error, Result};
|
||||
use crate::util::variables::MAX_BOT_COUNT;
|
||||
use crate::util::regex::RE_USERNAME;
|
||||
|
||||
use mongodb::bson::{doc, to_document};
|
||||
use revolt_quark::Result;
|
||||
|
||||
use rocket::serde::json::{Json, Value};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use ulid::Ulid;
|
||||
use nanoid::nanoid;
|
||||
use validator::Validate;
|
||||
|
||||
#[derive(Validate, Serialize, Deserialize)]
|
||||
@@ -17,73 +13,6 @@ pub struct Data {
|
||||
}
|
||||
|
||||
#[post("/create", data = "<info>")]
|
||||
pub async fn create_bot(user: User, info: Json<Data>) -> Result<Value> {
|
||||
if user.bot.is_some() {
|
||||
return Err(Error::IsBot)
|
||||
}
|
||||
|
||||
let info = info.into_inner();
|
||||
info.validate()
|
||||
.map_err(|error| Error::FailedValidation { error })?;
|
||||
|
||||
if get_collection("bots")
|
||||
.count_documents(
|
||||
doc! {
|
||||
"owner": &user.id
|
||||
},
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "count_documents",
|
||||
with: "bots",
|
||||
})? as usize >= *MAX_BOT_COUNT {
|
||||
return Err(Error::ReachedMaximumBots)
|
||||
}
|
||||
|
||||
let id = Ulid::new().to_string();
|
||||
let token = nanoid!(64);
|
||||
let bot = Bot {
|
||||
id: id.clone(),
|
||||
owner: user.id.clone(),
|
||||
token,
|
||||
public: false,
|
||||
analytics: false,
|
||||
discoverable: false,
|
||||
interactions_url: None
|
||||
};
|
||||
|
||||
if User::is_username_taken(&info.name).await? {
|
||||
return Err(Error::UsernameTaken);
|
||||
}
|
||||
|
||||
get_collection("users")
|
||||
.insert_one(
|
||||
doc! {
|
||||
"_id": &id,
|
||||
"username": &info.name,
|
||||
"bot": {
|
||||
"owner": &user.id
|
||||
}
|
||||
},
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "insert_one",
|
||||
with: "user",
|
||||
})?;
|
||||
|
||||
get_collection("bots")
|
||||
.insert_one(
|
||||
to_document(&bot).map_err(|_| Error::DatabaseError { with: "bot", operation: "to_document" })?,
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "insert_one",
|
||||
with: "user",
|
||||
})?;
|
||||
|
||||
Ok(json!(bot))
|
||||
pub async fn create_bot(/* user: User ,*/ info: Json<Data>) -> Result<Value> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user