#
mcaptcha/api/v1/account/
username.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 super::{AccountCheckPayload, AccountCheckResp};
    11use crate::errors::*;
    12use crate::AppData;
    13
    14#[my_codegen::post(path = "crate::V1_API_ROUTES.account.username_exists")]
    15async fn username_exists(
    16    payload: web::Json<AccountCheckPayload>,
    17    data: AppData,
    18) -> ServiceResult<impl Responder> {
    19    let resp = runners::username_exists(&payload, &data).await?;
    20    Ok(HttpResponse::Ok().json(resp))
    21}
    22
    23pub mod runners {
    24    use super::*;
    25
    26    pub async fn username_exists(
    27        payload: &AccountCheckPayload,
    28        data: &AppData,
    29    ) -> ServiceResult<AccountCheckResp> {
    30        let exists = data.db.username_exists(&payload.val).await?;
    31
    32        Ok(AccountCheckResp { exists })
    33    }
    34}
    35
    36#[derive(Clone, Debug, Deserialize, Serialize)]
    37pub struct Username {
    38    pub username: String,
    39}
    40
    41/// update username
    42#[my_codegen::post(
    43    path = "crate::V1_API_ROUTES.account.update_username",
    44    wrap = "crate::api::v1::get_middleware()"
    45)]
    46async fn set_username(
    47    id: Identity,
    48    payload: web::Json<Username>,
    49    data: AppData,
    50) -> ServiceResult<impl Responder> {
    51    let username = id.identity().unwrap();
    52
    53    let processed_uname = data.creds.username(&payload.username)?;
    54
    55    data.db.update_username(&username, &processed_uname).await?;
    56
    57    id.forget();
    58    id.remember(processed_uname);
    59
    60    Ok(HttpResponse::Ok())
    61}
    62
    63pub fn services(cfg: &mut actix_web::web::ServiceConfig) {
    64    cfg.service(username_exists);
    65    cfg.service(set_username);
    66}