94 lines
2.8 KiB
Rust
94 lines
2.8 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::errors::ServiceResult;
|
|
use crate::pages::errors::*;
|
|
use crate::settings::Settings;
|
|
use crate::AppCtx;
|
|
|
|
pub use super::*;
|
|
|
|
pub const DASH_FORGEJO_WEBHOOK_LIST: TemplateFile =
|
|
TemplateFile::new("dash_forgejo_webhook_list", "pages/dash/forgejo/list.html");
|
|
|
|
pub struct List {
|
|
ctx: RefCell<Context>,
|
|
}
|
|
|
|
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, hooks: Option<&[TemplateForgejoWebhook]>) -> Self {
|
|
let ctx = RefCell::new(context(settings));
|
|
if let Some(hooks) = hooks {
|
|
ctx.borrow_mut().insert(PAYLOAD_KEY, hooks);
|
|
}
|
|
Self { ctx }
|
|
}
|
|
|
|
pub fn render(&self) -> String {
|
|
TEMPLATES
|
|
.render(DASH_FORGEJO_WEBHOOK_LIST.name, &self.ctx.borrow())
|
|
.unwrap()
|
|
}
|
|
}
|
|
|
|
async fn get_webhook_data(
|
|
ctx: &AppCtx,
|
|
id: &Identity,
|
|
) -> ServiceResult<Vec<TemplateForgejoWebhook>> {
|
|
let db_hooks = ctx
|
|
.db
|
|
.list_all_webhooks_with_owner(&id.identity().unwrap())
|
|
.await?;
|
|
let mut hooks = Vec::with_capacity(db_hooks.len());
|
|
for hook in db_hooks {
|
|
hooks.push(TemplateForgejoWebhook::new(ctx, hook));
|
|
}
|
|
Ok(hooks)
|
|
}
|
|
|
|
#[actix_web_codegen_const_routes::get(
|
|
path = "PAGES.dash.forgejo_webhook.list",
|
|
wrap = "get_auth_middleware()"
|
|
)]
|
|
#[tracing::instrument(name = "List all Forgejo webhooks", skip(ctx, id))]
|
|
pub async fn list_hooks(ctx: AppCtx, id: Identity) -> PageResult<impl Responder, List> {
|
|
let sites = get_webhook_data(&ctx, &id)
|
|
.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_hooks);
|
|
}
|