#
mcaptcha/pages/panel/
settings.rs

    1// Copyright (C) 2022  Aravinth Manivannan <realaravinth@batsense.net>
    2// SPDX-FileCopyrightText: 2023 Aravinth Manivannan <realaravinth@batsense.net>
    3//
    4// SPDX-License-Identifier: AGPL-3.0-or-later
    5
    6use actix_identity::Identity;
    7use actix_web::{HttpResponse, Responder};
    8use sailfish::TemplateOnce;
    9
    10use crate::errors::PageResult;
    11use crate::pages::auth::sudo::SudoPage;
    12use crate::AppData;
    13
    14pub mod routes {
    15    pub struct Settings {
    16        pub home: &'static str,
    17        pub delete_account: &'static str,
    18        pub update_secret: &'static str,
    19    }
    20
    21    impl Settings {
    22        pub const fn new() -> Self {
    23            Settings {
    24                home: "/settings",
    25                delete_account: "/settings/account/delete",
    26                update_secret: "/settings/secret/update",
    27            }
    28        }
    29
    30        pub const fn get_sitemap() -> [&'static str; 1] {
    31            const S: Settings = Settings::new();
    32
    33            [S.home]
    34        }
    35    }
    36}
    37
    38pub fn services(cfg: &mut actix_web::web::ServiceConfig) {
    39    cfg.service(settings);
    40    cfg.service(update_secret);
    41    cfg.service(delete_account);
    42}
    43
    44const PAGE: &str = "Settings";
    45
    46#[derive(TemplateOnce, Clone)]
    47#[template(path = "panel/settings/index.html")]
    48pub struct IndexPage<'a> {
    49    email: Option<String>,
    50    secret: String,
    51    username: &'a str,
    52}
    53
    54#[my_codegen::get(
    55    path = "crate::PAGES.panel.settings.home",
    56    wrap = "crate::pages::get_middleware()"
    57)]
    58async fn settings(data: AppData, id: Identity) -> PageResult<impl Responder> {
    59    let username = id.identity().unwrap();
    60
    61    let secret = data.db.get_secret(&username).await?;
    62    let secret = secret.secret;
    63    let email = data.db.get_email(&username).await?;
    64
    65    let data = IndexPage {
    66        email,
    67        secret,
    68        username: &username,
    69    };
    70
    71    let body = data.render_once().unwrap();
    72    Ok(HttpResponse::Ok()
    73        .content_type("text/html; charset=utf-8")
    74        .body(body))
    75}
    76
    77#[my_codegen::get(
    78    path = "crate::PAGES.panel.settings.delete_account",
    79    wrap = "crate::pages::get_middleware()"
    80)]
    81async fn delete_account() -> impl Responder {
    82    let page = SudoPage::<u8, u8>::new(crate::V1_API_ROUTES.account.delete, None)
    83        .render_once()
    84        .unwrap();
    85    HttpResponse::Ok()
    86        .content_type("text/html; charset=utf-8")
    87        .body(page)
    88}
    89
    90#[my_codegen::get(
    91    path = "crate::PAGES.panel.settings.update_secret",
    92    wrap = "crate::pages::get_middleware()"
    93)]
    94async fn update_secret() -> impl Responder {
    95    let page = SudoPage::<u8, u8>::new(crate::V1_API_ROUTES.account.update_secret, None)
    96        .render_once()
    97        .unwrap();
    98    HttpResponse::Ok()
    99        .content_type("text/html; charset=utf-8")
    100        .body(page)
    101}