110 lines
3.2 KiB
Rust
110 lines
3.2 KiB
Rust
/*
|
|
* Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
* License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
use std::cell::RefCell;
|
|
|
|
use actix_identity::Identity;
|
|
use actix_web::http::header::ContentType;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use tera::Context;
|
|
|
|
use super::get_auth_middleware;
|
|
use crate::api::v1::forms::Host;
|
|
use crate::errors::ServiceResult;
|
|
use crate::pages::errors::*;
|
|
use crate::settings::Settings;
|
|
use crate::AppCtx;
|
|
|
|
pub use super::*;
|
|
|
|
pub const DASH_SITE_FORM_HOST_LIST: TemplateFile = TemplateFile::new(
|
|
"dash_site_form_host_list",
|
|
"pages/dash/sites/forms/host-list.html",
|
|
);
|
|
|
|
pub struct List {
|
|
ctx: RefCell<Context>,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
|
|
pub struct Form {
|
|
pub path: String,
|
|
pub view: String,
|
|
}
|
|
|
|
impl Form {
|
|
pub fn new(path: String, host: &str) -> Self {
|
|
let view = PAGES.dash.site.forms.get_view(0, host, &path);
|
|
Self { path, view }
|
|
}
|
|
}
|
|
|
|
impl CtxError for List {
|
|
fn with_error(&self, e: &ReadableError) -> String {
|
|
self.ctx.borrow_mut().insert(ERROR_KEY, e);
|
|
self.render()
|
|
}
|
|
}
|
|
|
|
impl List {
|
|
pub fn new(settings: &Settings, forms: Option<&[Form]>) -> Self {
|
|
let ctx = RefCell::new(context(settings));
|
|
if let Some(forms) = forms {
|
|
ctx.borrow_mut().insert(PAYLOAD_KEY, forms);
|
|
}
|
|
Self { ctx }
|
|
}
|
|
|
|
pub fn render(&self) -> String {
|
|
TEMPLATES
|
|
.render(DASH_SITE_FORM_HOST_LIST.name, &self.ctx.borrow())
|
|
.unwrap()
|
|
}
|
|
}
|
|
|
|
async fn get_forms(ctx: &AppCtx, id: &Identity, host: &str) -> ServiceResult<Vec<Form>> {
|
|
let owner = id.identity().unwrap();
|
|
let mut db_forms = ctx.get_all_forms_for_host(&owner, host).await?;
|
|
let mut forms = Vec::with_capacity(db_forms.len());
|
|
for form in db_forms.drain(0..) {
|
|
forms.push(Form::new(form, host));
|
|
}
|
|
Ok(forms)
|
|
}
|
|
|
|
#[actix_web_codegen_const_routes::get(
|
|
path = "PAGES.dash.site.forms.list_forms",
|
|
wrap = "get_auth_middleware()"
|
|
)]
|
|
#[tracing::instrument(name = "List all forms belonging to host" skip(ctx, id))]
|
|
pub async fn list_forms(
|
|
ctx: AppCtx,
|
|
id: Identity,
|
|
query: web::Query<Host>,
|
|
) -> PageResult<impl Responder, List> {
|
|
let sites = get_forms(&ctx, &id, &query.host)
|
|
.await
|
|
.map_err(|e| PageError::new(List::new(&ctx.settings, None), e))?;
|
|
let home = List::new(&ctx.settings, Some(&sites)).render();
|
|
let html = ContentType::html();
|
|
Ok(HttpResponse::Ok().content_type(html).body(home))
|
|
}
|
|
|
|
pub fn services(cfg: &mut web::ServiceConfig) {
|
|
cfg.service(list_forms);
|
|
}
|