nekrochan/src/web/staff/actions/create_board.rs

57 řádky
1.4 KiB
Rust
Spustitelný soubor

use actix_web::{post, web::Data, HttpRequest, HttpResponse};
use lazy_static::lazy_static;
use regex::Regex;
use serde::Deserialize;
use crate::{
ctx::Ctx, db::models::Board, error::NekrochanError, qsform::QsForm, web::tcx::account_from_auth,
};
lazy_static! {
static ref ID_REGEX: Regex = Regex::new(r"^\w{1,16}$").unwrap();
}
#[derive(Deserialize)]
pub struct CreateBoardForm {
id: String,
name: String,
description: String,
}
#[post("/staff/actions/create-board")]
pub async fn create_board(
ctx: Data<Ctx>,
req: HttpRequest,
QsForm(form): QsForm<CreateBoardForm>,
) -> Result<HttpResponse, NekrochanError> {
let account = account_from_auth(&ctx, &req).await?;
if !account.perms().owner() {
return Err(NekrochanError::InsufficientPermissionError);
}
let id = form.id.trim().to_owned();
let name = form.name.trim().to_owned();
let description = form.description.trim().to_owned();
if !ID_REGEX.is_match(&id) {
return Err(NekrochanError::IdFormatError);
}
if name.is_empty() || name.len() > 32 {
return Err(NekrochanError::BoardNameFormatError);
}
if description.len() > 128 {
return Err(NekrochanError::DescriptionFormatError);
}
let _ = Board::create(&ctx, id, name, description).await?;
let res = HttpResponse::SeeOther()
.append_header(("Location", "/staff/boards"))
.finish();
Ok(res)
}