59 lines
1.3 KiB
Rust
59 lines
1.3 KiB
Rust
use actix_web::{
|
|
get,
|
|
web::{Data, Json, Path},
|
|
HttpRequest,
|
|
};
|
|
use askama::Template;
|
|
use std::{collections::HashMap, vec};
|
|
|
|
use crate::{
|
|
ctx::Ctx,
|
|
db::models::{Board, Post},
|
|
error::NekrochanError,
|
|
web::tcx::TemplateCtx,
|
|
PostTemplate,
|
|
};
|
|
|
|
#[get("/thread-json/{board}/{id}")]
|
|
pub async fn thread_json(
|
|
ctx: Data<Ctx>,
|
|
req: HttpRequest,
|
|
path: Path<(String, i64)>,
|
|
) -> Result<Json<HashMap<i64, String>>, NekrochanError> {
|
|
let (board, id) = path.into_inner();
|
|
|
|
let tcx = TemplateCtx::new(&ctx, &req).await?;
|
|
|
|
let board = Board::read(&ctx, board.clone())
|
|
.await?
|
|
.ok_or(NekrochanError::BoardNotFound(board))?;
|
|
|
|
let thread = Post::read(&ctx, board.id.clone(), id)
|
|
.await?
|
|
.ok_or(NekrochanError::PostNotFound(board.id.clone(), id))?;
|
|
|
|
if thread.thread.is_some() {
|
|
return Err(NekrochanError::IsReplyError);
|
|
}
|
|
|
|
let mut res = HashMap::new();
|
|
|
|
let replies = thread.read_replies(&ctx).await?;
|
|
let posts = [vec![thread], replies].concat();
|
|
|
|
for post in posts {
|
|
let id = post.id;
|
|
let tcx = &tcx;
|
|
let board = &board;
|
|
let post = &post;
|
|
|
|
let html = PostTemplate { tcx, board, post }
|
|
.render()
|
|
.unwrap_or_default();
|
|
|
|
res.insert(id, html);
|
|
}
|
|
|
|
Ok(Json(res))
|
|
}
|