// Copyright (C) 2021 Aravinth Manivannan // SPDX-FileCopyrightText: 2023 Aravinth Manivannan // // SPDX-License-Identifier: AGPL-3.0-or-later use std::cell::RefCell; use std::str::FromStr; use actix_web::http::header::ContentType; use actix_web::{web, HttpResponse, Responder}; use sqlx::types::Uuid; use tera::Context; use crate::errors::ServiceError; use crate::settings::Settings; use crate::AppData; pub use super::*; pub struct Intro { ctx: RefCell, } pub const INTRO: TemplateFile = TemplateFile::new("intro", "index.html"); impl CtxError for Intro { fn with_error(&self, e: &ReadableError) -> String { self.ctx.borrow_mut().insert(ERROR_KEY, e); self.render() } } impl Intro { pub fn new(settings: &Settings, payload: Option<&str>) -> Self { let ctx = RefCell::new(context(settings, "Campaign Homepage")); if let Some(uuid) = payload { let payload = crate::PAGES.panel.campaigns.get_bench_route(uuid); ctx.borrow_mut().insert(PAYLOAD_KEY, &payload); } Self { ctx } } pub fn render(&self) -> String { TEMPLATES.render(INTRO.name, &self.ctx.borrow()).unwrap() } } #[actix_web_codegen_const_routes::get(path = "PAGES.panel.campaigns.about")] pub async fn about( data: AppData, path: web::Path, ) -> PageResult { let path = path.into_inner(); match Uuid::from_str(&path) { Err(_) => Err(PageError::new( Intro::new(&data.settings, None), ServiceError::CampaignDoesntExist, )), Ok(_) => { let about = Intro::new(&data.settings, Some(&path)).render(); let html = ContentType::html(); Ok(HttpResponse::Ok().content_type(html).body(about)) } } } pub fn services(cfg: &mut actix_web::web::ServiceConfig) { cfg.service(about); }