#
mcaptcha/api/v1/mcaptcha/
stats.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::{web, HttpResponse, Responder};
    8use serde::{Deserialize, Serialize};
    9
    10use crate::errors::*;
    11use crate::AppData;
    12
    13pub mod routes {
    14    pub struct Stats {
    15        pub get: &'static str,
    16    }
    17
    18    impl Stats {
    19        pub const fn new() -> Self {
    20            Self {
    21                get: "/api/v1/mcaptcha/stats",
    22            }
    23        }
    24    }
    25}
    26#[derive(Debug, Clone, Deserialize, Serialize)]
    27pub struct StatsPayload {
    28    pub key: String,
    29}
    30
    31#[my_codegen::post(
    32    path = "crate::V1_API_ROUTES.captcha.stats.get",
    33    wrap = "crate::api::v1::get_middleware()"
    34)]
    35pub async fn get(
    36    payload: web::Json<StatsPayload>,
    37    data: AppData,
    38    id: Identity,
    39) -> ServiceResult<impl Responder> {
    40    let username = id.identity().unwrap();
    41    let stats = data.stats.fetch(&data, &username, &payload.key).await?;
    42    Ok(HttpResponse::Ok().json(&stats))
    43}