Skip to content

Commit

Permalink
fix email service
Browse files Browse the repository at this point in the history
  • Loading branch information
lazhenyi committed Dec 17, 2024
1 parent 62686de commit 99c22b9
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,6 @@ lazy_static = "1.5.0"
bytes = "1.9.0"
futures = "0.3.31"
regex = { version = "1.11.1" ,features = ["std"] }
async-stream = "0.3.6"
async-stream = "0.3.6"
rand = "0.8.5"
log = "0.4.22"
5 changes: 5 additions & 0 deletions src/api/app_docs.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::api::handler::email::captcha::__path_api_email_rand_captcha;
use crate::api::handler::email::captcha::__path_api_email_captcha_check;
use crate::api::handler::repos::info::__path_api_repo_info_get;
use crate::api::handler::users::starred::__path_api_users_starred;
use crate::api::handler::users::search::__path_api_users_search;
Expand Down Expand Up @@ -69,6 +71,9 @@ use utoipa::OpenApi;
api_users_repos,
api_users_search,
api_users_starred,
api_email_rand_captcha,
api_email_captcha_check,
api_user_avatar,
Expand Down
3 changes: 3 additions & 0 deletions src/api/app_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use actix_web::web;
use utoipa::OpenApi;
use utoipa_redoc::{Redoc, Servable};
use crate::api::app_docs::ApiDoc;
use crate::api::handler::email::captcha::{api_email_captcha_check, api_email_rand_captcha};
use crate::api::handler::groups::avatar::{api_groups_avatar, api_groups_avatar_upload};
use crate::api::handler::groups::create::api_groups_create;
use crate::api::handler::groups::info::api_groups_info;
Expand Down Expand Up @@ -234,6 +235,8 @@ pub fn routes(cfg: &mut web::ServiceConfig){
)
.service(
web::scope("/email")
.route("/captcha", web::post().to(api_email_rand_captcha))
.route("/captcha", web::put().to(api_email_captcha_check))
)
;
}
63 changes: 63 additions & 0 deletions src/api/handler/email/captcha.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use actix_session::Session;
use actix_web::{web, Responder};
use crate::api::app_write::AppWrite;
use crate::api::dto::email_dto::{EmailCaptcha, EmailCaptchaCheck};
use crate::metadata::service::MetaService;
pub const ALLOW_NEXT_KEY: &str = "allow_next";
pub const CAPTCHA: &str = "captcha";

#[utoipa::path(
post,
tag = "email",
path = "/api/v1/email/captcha",
request_body = EmailCaptcha,
responses(
( status = 200, description = "Success" ),
( status = 400, description = "Bad Request" )
)
)]
pub async fn api_email_rand_captcha(
session: Session,
service: web::Data<MetaService>,
dto: web::Json<EmailCaptcha>
)
-> impl Responder
{
match service.email_service().generate_and_send_captcha(dto.email.clone()).await{
Ok(result) => {
session.insert(CAPTCHA, result).ok();
AppWrite::<String>::ok_msg("[Ok]".to_string())
}
Err(e) => {
AppWrite::error(e.to_string())
}
}
}

#[utoipa::path(
put,
tag = "email",
path = "/api/v1/email/captcha",
request_body = EmailCaptchaCheck,
responses(
( status = 200, description = "Success" ),
( status = 400, description = "Bad Request" )
)
)]
pub async fn api_email_captcha_check(
session: Session,
dto: web::Json<EmailCaptchaCheck>
)
-> impl Responder
{
let captcha = session.get::<String>(CAPTCHA).unwrap();
if captcha.is_none(){
return AppWrite::<String>::error("[Error] Captcha Expired".to_string())
}
if captcha.unwrap() == dto.code {
session.insert(ALLOW_NEXT_KEY, true).ok();
AppWrite::ok_msg("[Ok]".to_string())
} else {
AppWrite::error("[Error] Captcha Error".to_string())
}
}
1 change: 1 addition & 0 deletions src/api/handler/email/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod captcha;
1 change: 1 addition & 0 deletions src/api/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod user;
pub mod users;
pub mod groups;
pub mod repos;
pub mod email;

pub async fn check_session(session: Session) -> anyhow::Result<SessionModel>{
match session.get::<SessionModel>(SessionModelKey){
Expand Down
15 changes: 15 additions & 0 deletions src/metadata/service/email_service/captcha.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use rand::distributions::Alphanumeric;
use rand::Rng;
use crate::metadata::service::email_service::EmailService;

impl EmailService {
pub async fn generate_and_send_captcha(&self, email: String) -> anyhow::Result<String>{
let rng = rand::thread_rng();
let code: String = rng.sample_iter(&Alphanumeric).take(6).map(char::from).collect();
self.email.send_captcha(email.parse().map_err(|e|{
log::error!("[Error] {}", e);
anyhow::anyhow!("[Email Err] {}", e)
})?, code.clone()).await;
Ok(code)
}
}
Empty file.
23 changes: 23 additions & 0 deletions src/metadata/service/email_service/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use sea_orm::DatabaseConnection;
use crate::metadata::service::MetaService;
use crate::server::email::EmailServer;

pub mod captcha;
pub mod forget;

#[allow(unused)]
pub struct EmailService{
db: DatabaseConnection,
redis: deadpool_redis::Pool,
email: EmailServer,
}

impl From<&MetaService> for EmailService {
fn from(value: &MetaService) -> Self {
Self{
db: value.pg(),
redis: value.redis(),
email: value.email(),
}
}
}
5 changes: 5 additions & 0 deletions src/metadata/service/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use sea_orm::DatabaseConnection;
use crate::metadata::service::email_service::EmailService;
use crate::metadata::service::groups_service::GroupService;
use crate::metadata::service::repos_service::RepoService;
use crate::metadata::service::users_service::UserService;
Expand All @@ -9,6 +10,7 @@ use crate::server::email::{EmailServer, EMAIL_SERVICE};
pub mod users_service;
pub mod repos_service;
pub mod groups_service;
pub mod email_service;
#[derive(Clone)]
pub struct MetaService{
pg: DatabaseConnection,
Expand Down Expand Up @@ -54,4 +56,7 @@ impl MetaService{
pub fn group_service(&self) -> GroupService {
GroupService::from(self)
}
pub fn email_service(&self) -> EmailService {
EmailService::from(self)
}
}

0 comments on commit 99c22b9

Please sign in to comment.