49 lines
1.3 KiB
Rust
49 lines
1.3 KiB
Rust
use actix_web::{post, web::Data, HttpRequest, HttpResponse};
|
|
use serde::Deserialize;
|
|
|
|
use crate::{
|
|
ctx::Ctx, db::models::NewsPost, error::NekrochanError, markup::markup, qsform::QsForm,
|
|
web::tcx::account_from_auth,
|
|
};
|
|
|
|
#[derive(Deserialize)]
|
|
pub struct CreateNewsForm {
|
|
title: String,
|
|
content: String,
|
|
}
|
|
|
|
#[post("/staff/actions/create-news")]
|
|
pub async fn create_news(
|
|
ctx: Data<Ctx>,
|
|
req: HttpRequest,
|
|
QsForm(form): QsForm<CreateNewsForm>,
|
|
) -> Result<HttpResponse, NekrochanError> {
|
|
let account = account_from_auth(&ctx, &req).await?;
|
|
|
|
if !(account.perms().owner() || account.perms().news()) {
|
|
return Err(NekrochanError::InsufficientPermissionError);
|
|
}
|
|
|
|
let title = form.title.trim().to_owned();
|
|
let content = form.content.trim().to_owned();
|
|
|
|
if title.is_empty() || title.len() > 100 {
|
|
return Err(NekrochanError::NewsTitleFormatError);
|
|
}
|
|
|
|
if content.is_empty() || content.len() > 10000 {
|
|
return Err(NekrochanError::NewsContentFormatError);
|
|
}
|
|
|
|
let content_nomarkup = content;
|
|
let (content, _) = markup(&ctx, &account.perms(), None, None, &content_nomarkup).await?;
|
|
|
|
NewsPost::create(&ctx, title, content, content_nomarkup, account.username).await?;
|
|
|
|
let res = HttpResponse::SeeOther()
|
|
.append_header(("Location", "/staff/news"))
|
|
.finish();
|
|
|
|
Ok(res)
|
|
}
|