2022-01-01 16:32:55 +01:00
|
|
|
use axum::extract::{Multipart, Path};
|
2021-12-29 16:11:27 +01:00
|
|
|
use axum::http::StatusCode;
|
|
|
|
use axum::Json;
|
2022-02-28 22:44:06 +01:00
|
|
|
use diesel::OptionalExtension;
|
2021-12-30 23:41:47 +01:00
|
|
|
use rand::distributions::Alphanumeric;
|
2021-12-19 00:16:46 +01:00
|
|
|
use rand::Rng;
|
|
|
|
use serde::{Deserialize, Serialize};
|
2021-12-31 11:24:25 +01:00
|
|
|
use serde_json::{json, value::Value as JsonValue};
|
2021-12-19 00:16:46 +01:00
|
|
|
use std::io::Cursor;
|
2022-01-01 16:32:55 +01:00
|
|
|
use std::path::PathBuf;
|
2021-12-19 00:16:46 +01:00
|
|
|
|
|
|
|
use crate::db::bots::{self, CodeBundle};
|
|
|
|
use crate::db::users::User;
|
2022-02-28 22:44:06 +01:00
|
|
|
use crate::modules::bots::save_code_bundle;
|
2022-01-01 16:32:55 +01:00
|
|
|
use crate::{DatabaseConnection, BOTS_DIR};
|
2021-12-19 00:16:46 +01:00
|
|
|
use bots::Bot;
|
|
|
|
|
2022-02-28 22:44:06 +01:00
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
pub struct SaveBotParams {
|
|
|
|
pub bot_name: String,
|
|
|
|
pub code: String,
|
|
|
|
}
|
|
|
|
pub async fn save_bot(
|
|
|
|
Json(params): Json<SaveBotParams>,
|
|
|
|
conn: DatabaseConnection,
|
2022-03-01 20:23:31 +01:00
|
|
|
) -> Result<Json<Bot>, StatusCode> {
|
2022-02-28 22:44:06 +01:00
|
|
|
// TODO: authorization
|
|
|
|
let res = bots::find_bot_by_name(¶ms.bot_name, &conn)
|
|
|
|
.optional()
|
|
|
|
.expect("could not run query");
|
|
|
|
let bot = match res {
|
2022-03-01 20:23:31 +01:00
|
|
|
Some(_bot) => return Err(StatusCode::FORBIDDEN),
|
2022-02-28 22:44:06 +01:00
|
|
|
None => {
|
|
|
|
let new_bot = bots::NewBot {
|
|
|
|
owner_id: None,
|
|
|
|
name: ¶ms.bot_name,
|
|
|
|
};
|
|
|
|
let bot = bots::create_bot(&new_bot, &conn).expect("could not create bot");
|
|
|
|
bot
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let _code_bundle =
|
|
|
|
save_code_bundle(¶ms.code, Some(bot.id), &conn).expect("failed to save code bundle");
|
2022-03-01 20:23:31 +01:00
|
|
|
Ok(Json(bot))
|
2022-02-28 22:44:06 +01:00
|
|
|
}
|
|
|
|
|
2021-12-19 00:16:46 +01:00
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
pub struct BotParams {
|
|
|
|
name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn create_bot(
|
2021-12-29 16:11:27 +01:00
|
|
|
conn: DatabaseConnection,
|
2021-12-19 00:16:46 +01:00
|
|
|
user: User,
|
|
|
|
params: Json<BotParams>,
|
2021-12-29 16:11:27 +01:00
|
|
|
) -> (StatusCode, Json<Bot>) {
|
|
|
|
let bot_params = bots::NewBot {
|
2022-02-26 23:07:13 +01:00
|
|
|
owner_id: Some(user.id),
|
2021-12-29 16:11:27 +01:00
|
|
|
name: ¶ms.name,
|
|
|
|
};
|
|
|
|
let bot = bots::create_bot(&bot_params, &conn).unwrap();
|
|
|
|
(StatusCode::CREATED, Json(bot))
|
2021-12-19 00:16:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: handle errors
|
2021-12-31 11:24:25 +01:00
|
|
|
pub async fn get_bot(
|
|
|
|
conn: DatabaseConnection,
|
|
|
|
Path(bot_id): Path<i32>,
|
|
|
|
) -> Result<Json<JsonValue>, StatusCode> {
|
|
|
|
let bot = bots::find_bot(bot_id, &conn).map_err(|_| StatusCode::NOT_FOUND)?;
|
|
|
|
let bundles = bots::find_bot_code_bundles(bot.id, &conn)
|
|
|
|
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
|
|
|
Ok(Json(json!({
|
|
|
|
"bot": bot,
|
|
|
|
"bundles": bundles,
|
|
|
|
})))
|
2021-12-30 23:40:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_my_bots(
|
|
|
|
conn: DatabaseConnection,
|
|
|
|
user: User,
|
|
|
|
) -> Result<Json<Vec<Bot>>, StatusCode> {
|
|
|
|
bots::find_bots_by_owner(user.id, &conn)
|
|
|
|
.map(Json)
|
|
|
|
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
|
2021-12-19 00:16:46 +01:00
|
|
|
}
|
|
|
|
|
2022-01-01 11:26:49 +01:00
|
|
|
pub async fn list_bots(conn: DatabaseConnection) -> Result<Json<Vec<Bot>>, StatusCode> {
|
|
|
|
bots::find_all_bots(&conn)
|
|
|
|
.map(Json)
|
|
|
|
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
|
|
|
|
}
|
|
|
|
|
2021-12-30 23:41:47 +01:00
|
|
|
// TODO: currently this only implements the happy flow
|
|
|
|
pub async fn upload_code_multipart(
|
2021-12-29 16:11:27 +01:00
|
|
|
conn: DatabaseConnection,
|
2021-12-19 00:16:46 +01:00
|
|
|
user: User,
|
2021-12-29 16:11:27 +01:00
|
|
|
Path(bot_id): Path<i32>,
|
2021-12-30 23:41:47 +01:00
|
|
|
mut multipart: Multipart,
|
|
|
|
) -> Result<Json<CodeBundle>, StatusCode> {
|
|
|
|
let bots_dir = PathBuf::from(BOTS_DIR);
|
2021-12-19 00:16:46 +01:00
|
|
|
|
2021-12-30 23:41:47 +01:00
|
|
|
let bot = bots::find_bot(bot_id, &conn).map_err(|_| StatusCode::NOT_FOUND)?;
|
2021-12-19 00:16:46 +01:00
|
|
|
|
2022-02-26 23:07:13 +01:00
|
|
|
if Some(user.id) != bot.owner_id {
|
2021-12-30 23:41:47 +01:00
|
|
|
return Err(StatusCode::FORBIDDEN);
|
|
|
|
}
|
2021-12-19 00:16:46 +01:00
|
|
|
|
2021-12-30 23:41:47 +01:00
|
|
|
let data = multipart
|
|
|
|
.next_field()
|
|
|
|
.await
|
|
|
|
.map_err(|_| StatusCode::BAD_REQUEST)?
|
|
|
|
.ok_or(StatusCode::BAD_REQUEST)?
|
|
|
|
.bytes()
|
|
|
|
.await
|
|
|
|
.map_err(|_| StatusCode::BAD_REQUEST)?;
|
2021-12-19 00:16:46 +01:00
|
|
|
|
2021-12-30 23:41:47 +01:00
|
|
|
// TODO: this random path might be a bit redundant
|
|
|
|
let folder_name: String = rand::thread_rng()
|
|
|
|
.sample_iter(&Alphanumeric)
|
|
|
|
.take(16)
|
|
|
|
.map(char::from)
|
|
|
|
.collect();
|
2021-12-19 00:16:46 +01:00
|
|
|
|
2021-12-30 23:41:47 +01:00
|
|
|
zip::ZipArchive::new(Cursor::new(data))
|
|
|
|
.map_err(|_| StatusCode::BAD_REQUEST)?
|
|
|
|
.extract(bots_dir.join(&folder_name))
|
|
|
|
.map_err(|_| StatusCode::BAD_REQUEST)?;
|
2021-12-19 00:16:46 +01:00
|
|
|
|
2021-12-29 16:11:27 +01:00
|
|
|
let bundle = bots::NewCodeBundle {
|
2022-02-27 20:35:22 +01:00
|
|
|
bot_id: Some(bot.id),
|
2021-12-30 23:41:47 +01:00
|
|
|
path: &folder_name,
|
2021-12-29 16:11:27 +01:00
|
|
|
};
|
|
|
|
let code_bundle =
|
|
|
|
bots::create_code_bundle(&bundle, &conn).expect("Failed to create code bundle");
|
2021-12-19 00:16:46 +01:00
|
|
|
|
2021-12-30 23:41:47 +01:00
|
|
|
Ok(Json(code_bundle))
|
2021-12-19 00:16:46 +01:00
|
|
|
}
|