From 3c22130674ee6b7e5b90e484c5fa11395840da64 Mon Sep 17 00:00:00 2001 From: 4t145 Date: Wed, 18 Oct 2023 11:16:56 +0800 Subject: [PATCH 1/7] remove mq event listener and add ci web api --- services/bios-all/Cargo.toml | 2 +- support/reach/src/api/ci/message.rs | 31 +++++ support/reach/src/api/ci/mod.rs | 2 + support/reach/src/api/mod.rs | 5 +- support/reach/src/dto/message/send_req.rs | 15 +-- support/reach/src/serv/message_send.rs | 116 ++++++++++++++++++ support/reach/src/serv/mod.rs | 3 +- support/reach/src/task/event_listener.rs | 136 ---------------------- support/reach/src/task/mod.rs | 3 - support/reach/tests/test_message_send.rs | 50 ++++---- 10 files changed, 182 insertions(+), 181 deletions(-) create mode 100644 support/reach/src/api/ci/message.rs create mode 100644 support/reach/src/api/ci/mod.rs create mode 100644 support/reach/src/serv/message_send.rs delete mode 100644 support/reach/src/task/event_listener.rs diff --git a/services/bios-all/Cargo.toml b/services/bios-all/Cargo.toml index f76cd774e..26c1a9d38 100644 --- a/services/bios-all/Cargo.toml +++ b/services/bios-all/Cargo.toml @@ -32,4 +32,4 @@ bios-mw-schedule = { path = "../../middleware/schedule" } bios-mw-flow = { path = "../../middleware/flow" } # clients -bios-client-hwsms = { path = "../../clients/hwsms", features = ["reach"] } \ No newline at end of file +bios-client-hwsms = { path = "../../clients/hwsms", features = ["reach"] } diff --git a/support/reach/src/api/ci/message.rs b/support/reach/src/api/ci/message.rs new file mode 100644 index 000000000..26af7b1fa --- /dev/null +++ b/support/reach/src/api/ci/message.rs @@ -0,0 +1,31 @@ +use tardis::log as tracing; + +use tardis::web::context_extractor::TardisContextExtractor; + +use tardis::web::poem_openapi; +use tardis::web::poem_openapi::payload::Json; +use tardis::web::web_resp::{TardisApiResult, TardisResp, Void}; + +use crate::consts::*; +use crate::dto::*; +#[cfg(feature = "simple-client")] +use crate::invoke::Client; +use crate::serv::*; + +#[derive(Debug, Default, Clone)] +pub struct ReachMessageCiApi; + +#[cfg_attr(feature = "simple-client", bios_sdk_invoke::simple_invoke_client(Client<'_>))] +#[poem_openapi::OpenApi(prefix_path = "/ci/message", tag = "bios_basic::ApiTag::App")] +impl ReachMessageCiApi { + /// 根据模板id发送信息 + #[oai(method = "put", path = "/send")] + #[tardis::log::instrument(skip_all, fields(module = "reach"))] + pub async fn message_send(&self, body: Json, ctx: TardisContextExtractor) -> TardisApiResult { + let ctx = ctx.0; + let funs = get_tardis_inst(); + let body = body.0; + message_send(body, &funs, &ctx).await?; + TardisResp::ok(VOID) + } +} diff --git a/support/reach/src/api/ci/mod.rs b/support/reach/src/api/ci/mod.rs new file mode 100644 index 000000000..b1bd3227e --- /dev/null +++ b/support/reach/src/api/ci/mod.rs @@ -0,0 +1,2 @@ +mod message; +pub use message::ReachMessageCiApi; diff --git a/support/reach/src/api/mod.rs b/support/reach/src/api/mod.rs index a44701904..a4c2f4860 100644 --- a/support/reach/src/api/mod.rs +++ b/support/reach/src/api/mod.rs @@ -4,11 +4,14 @@ pub use cc::*; mod ct; pub use ct::*; + +mod ci; +pub use ci::*; use tardis::{basic::result::TardisResult, web::web_server::TardisWebServer}; use crate::consts::DOMAIN_CODE; -pub type ReachApi = (ReachCcApi, ReachCtApi); +pub type ReachApi = (ReachCcApi, ReachCtApi, ReachMessageCiApi); pub async fn init(web_server: &TardisWebServer) -> TardisResult<()> { web_server.add_module(DOMAIN_CODE, ReachApi::default()).await; Ok(()) diff --git a/support/reach/src/dto/message/send_req.rs b/support/reach/src/dto/message/send_req.rs index 2f150c0f7..6cfdad39d 100644 --- a/support/reach/src/dto/message/send_req.rs +++ b/support/reach/src/dto/message/send_req.rs @@ -1,27 +1,16 @@ use super::*; use std::collections::HashMap; -use tardis::basic::dto::TardisContext; /// 用户触达消息发送请求 -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, poem_openapi::Object)] pub struct ReachMsgSendReq { pub scene_code: String, pub receives: Vec, pub rel_item_id: String, pub replace: HashMap, - pub own_paths: String, } -impl ReachMsgSendReq { - pub fn get_ctx(&self) -> TardisContext { - TardisContext { - own_paths: self.own_paths.clone(), - ..Default::default() - } - } -} - -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, poem_openapi::Object)] pub struct ReachMsgReceive { pub receive_group_code: String, pub receive_kind: ReachReceiveKind, diff --git a/support/reach/src/serv/message_send.rs b/support/reach/src/serv/message_send.rs new file mode 100644 index 000000000..56692ca3e --- /dev/null +++ b/support/reach/src/serv/message_send.rs @@ -0,0 +1,116 @@ +use std::collections::{HashMap, HashSet}; + +use bios_basic::rbum::{dto::rbum_item_dto::RbumItemAddReq, serv::rbum_crud_serv::RbumCrudOperation}; +use tardis::{ + basic::{result::TardisResult, dto::TardisContext}, + db::sea_orm::{sea_query::Query, ColumnTrait, Iterable}, + TardisFunsInst, +}; + +use crate::{consts::*, domain, dto::*, serv::*}; + +pub async fn message_send(send_req: ReachMsgSendReq, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<()> { + let err = |msg: &str| funs.err().not_found("reach", "event_listener", msg, ""); + + if send_req.receives.is_empty() { + return Err(err("receiver is empty")); + } + // trigger scene + let scene = funs + .db() + .get_dto::( + Query::select() + .columns(domain::trigger_scene::Column::iter()) + .from(domain::trigger_scene::Entity) + .and_where(domain::trigger_scene::Column::Code.eq(&send_req.scene_code)), + ) + .await? + .ok_or_else(|| funs.err().not_found("reach", "event_listener", "cannot find scene", ""))?; + let filter = &ReachTriggerGlobalConfigFilterReq { + rel_reach_trigger_scene_id: Some(scene.id.clone()), + ..Default::default() + }; + + // retrive all related global configs, group by channel + let global_configs = ReachTriggerGlobalConfigService::find_detail_rbums(filter, None, None, funs, ctx).await?.into_iter().fold(HashMap::new(), |mut map, item| { + map.entry(item.rel_reach_channel).or_insert(item); + map + }); + if global_configs.is_empty() { + return Err(err("global_configs is empty")); + } + + // retrive all instance configs, group by group_code + let filter = &ReachTriggerInstanceConfigFilterReq { + rel_reach_trigger_scene_id: Some(scene.id), + rel_item_id: Some(send_req.rel_item_id), + ..Default::default() + }; + let instances = ReachTriggerInstanceConfigService::find_detail_rbums(filter, None, None, funs, ctx).await?; + if instances.is_empty() { + return Ok(()); + } + let receive_group_code = send_req.receives.into_iter().fold(HashMap::>::new(), |mut map, item| { + map.entry(item.receive_group_code.clone()).or_default().push(item); + map + }); + + let mut instance_group_code = instances.into_iter().filter(|inst| receive_group_code.contains_key(&inst.receive_group_code.clone())).fold(HashMap::>::new(), |mut map, item| { + map.entry(item.receive_group_code.clone()).or_default().push(item); + map + }); + + if instance_group_code.is_empty() { + return Ok(()); + } + + let (other_receive_collect, other_group_code) = receive_group_code.into_iter().fold( + (HashMap::new(), HashSet::new()), + |(mut other_receive_collect, mut other_group_code), (group_code, receives)| { + if let Some(instance_list) = instance_group_code.get_mut(&group_code) { + if !instance_list.is_empty() { + for r in receives { + other_receive_collect.entry(r.receive_kind).or_insert(Vec::new()).extend(r.receive_ids) + } + for i in instance_list { + other_group_code.insert(i.rel_reach_channel); + } + } + } + (other_receive_collect, other_group_code) + }, + ); + + for (_kind, gc) in global_configs { + for (receive_kind, to_res_ids) in &other_receive_collect { + if other_group_code.contains(&gc.rel_reach_channel) { + ReachMessageServ::add_rbum( + &mut ReachMessageAddReq { + rbum_item_add_req: RbumItemAddReq { + id: Default::default(), + code: Default::default(), + name: "".into(), + rel_rbum_kind_id: RBUM_KIND_CODE_REACH_MESSAGE.into(), + rel_rbum_domain_id: DOMAIN_CODE.into(), + scope_level: Default::default(), + disabled: Default::default(), + }, + from_res: Default::default(), + rel_reach_channel: gc.rel_reach_channel, + receive_kind: *receive_kind, + to_res_ids: to_res_ids.join(";"), + rel_reach_msg_signature_id: gc.rel_reach_msg_signature_id.clone(), + rel_reach_msg_template_id: gc.rel_reach_msg_template_id.clone(), + reach_status: ReachStatusKind::Pending, + content_replace: tardis::serde_json::to_string(&send_req.replace).expect("convert from string:string map shouldn't fail"), + }, + funs, + ctx, + ) + .await?; + } + } + } + + Ok(()) +} diff --git a/support/reach/src/serv/mod.rs b/support/reach/src/serv/mod.rs index 787e035a6..ccc91172a 100644 --- a/support/reach/src/serv/mod.rs +++ b/support/reach/src/serv/mod.rs @@ -9,7 +9,8 @@ mod message_template; pub use message_template::ReachMessageTemplateServ; mod message_log; pub use message_log::ReachMessageLogServ; - +mod message_send; +pub use message_send::*; /* trigger */ diff --git a/support/reach/src/task/event_listener.rs b/support/reach/src/task/event_listener.rs deleted file mode 100644 index 97f58d5d4..000000000 --- a/support/reach/src/task/event_listener.rs +++ /dev/null @@ -1,136 +0,0 @@ -use std::collections::{HashMap, HashSet}; - -use bios_basic::rbum::{dto::rbum_item_dto::RbumItemAddReq, serv::rbum_crud_serv::RbumCrudOperation}; -use tardis::{ - basic::result::TardisResult, - db::sea_orm::{sea_query::Query, ColumnTrait, Iterable}, - log, TardisFuns, -}; - -use crate::{consts::*, domain::*, dto::*, serv::*}; - -#[derive(Debug, Default, Clone)] -pub struct EventListener {} - - -// logic of this part could be modified. - -impl EventListener { - pub async fn run(&self) -> TardisResult<()> { - let funs = get_tardis_inst(); - funs.mq() - .subscribe(MQ_REACH_TOPIC_MESSAGE, |(_, msg)| { - log::debug!("Receive message : {msg}"); - async move { - let funs = get_tardis_inst(); - let err = |msg: &str| funs.err().not_found("reach", "event_listener", msg, ""); - let send_req: ReachMsgSendReq = TardisFuns::json.str_to_obj(&msg)?; - if send_req.receives.is_empty() { - return Err(err("receiver is empty")); - } - let ctx = send_req.get_ctx(); - // trigger scene - let scene = funs - .db() - .get_dto::( - Query::select() - .columns(trigger_scene::Column::iter()) - .from(trigger_scene::Entity) - .and_where(trigger_scene::Column::Code.eq(&send_req.scene_code)), - ) - .await? - .ok_or_else(|| funs.err().not_found("reach", "event_listener", "cannot find scene", ""))?; - let filter = &ReachTriggerGlobalConfigFilterReq { - rel_reach_trigger_scene_id: Some(scene.id.clone()), - ..Default::default() - }; - - // retrive all related global configs, group by channel - let global_configs = - ReachTriggerGlobalConfigService::find_detail_rbums(filter, None, None, &funs, &ctx).await?.into_iter().fold(HashMap::new(), |mut map, item| { - map.entry(item.rel_reach_channel).or_insert(item); - map - }); - if global_configs.is_empty() { - return Err(err("global_configs is empty")); - } - - // retrive all instance configs, group by group_code - let filter = &ReachTriggerInstanceConfigFilterReq { - rel_reach_trigger_scene_id: Some(scene.id), - rel_item_id: Some(send_req.rel_item_id), - ..Default::default() - }; - let instances = ReachTriggerInstanceConfigService::find_detail_rbums(filter, None, None, &funs, &ctx).await?; - if instances.is_empty() { - return Ok(()); - } - let receive_group_code = send_req.receives.into_iter().fold(HashMap::new(), |mut map, item| { - map.entry(item.receive_group_code.clone()).or_insert(Vec::new()).push(item); - map - }); - - let mut instance_group_code = - instances.into_iter().filter(|inst| receive_group_code.contains_key(&inst.receive_group_code.clone())).fold(HashMap::new(), |mut map, item| { - map.entry(item.receive_group_code.clone()).or_insert(Vec::new()).push(item); - map - }); - - if instance_group_code.is_empty() { - return Ok(()); - } - - let (other_receive_collect, other_group_code) = receive_group_code.into_iter().fold( - (HashMap::new(), HashSet::new()), - |(mut other_receive_collect, mut other_group_code), (group_code, receives)| { - if let Some(instance_list) = instance_group_code.get_mut(&group_code) { - if !instance_list.is_empty() { - for r in receives { - other_receive_collect.entry(r.receive_kind).or_insert(Vec::new()).extend(r.receive_ids) - } - for i in instance_list { - other_group_code.insert(i.rel_reach_channel); - } - } - } - (other_receive_collect, other_group_code) - }, - ); - - for (_kind, gc) in global_configs { - for (receive_kind, to_res_ids) in &other_receive_collect { - if other_group_code.contains(&gc.rel_reach_channel) { - ReachMessageServ::add_rbum( - &mut ReachMessageAddReq { - rbum_item_add_req: RbumItemAddReq { - id: Default::default(), - code: Default::default(), - name: "".into(), - rel_rbum_kind_id: RBUM_KIND_CODE_REACH_MESSAGE.into(), - rel_rbum_domain_id: DOMAIN_CODE.into(), - scope_level: Default::default(), - disabled: Default::default(), - }, - from_res: Default::default(), - rel_reach_channel: gc.rel_reach_channel, - receive_kind: *receive_kind, - to_res_ids: to_res_ids.join(";"), - rel_reach_msg_signature_id: gc.rel_reach_msg_signature_id.clone(), - rel_reach_msg_template_id: gc.rel_reach_msg_template_id.clone(), - reach_status: ReachStatusKind::Pending, - content_replace: tardis::serde_json::to_string(&send_req.replace).expect("convert from string:string map shouldn't fail"), - }, - &funs, - &ctx, - ) - .await?; - } - } - } - TardisResult::Ok(()) - } - }) - .await?; - Ok(()) - } -} diff --git a/support/reach/src/task/mod.rs b/support/reach/src/task/mod.rs index a55a79fb8..6db54ad99 100644 --- a/support/reach/src/task/mod.rs +++ b/support/reach/src/task/mod.rs @@ -2,12 +2,9 @@ use std::sync::Arc; use tardis::{basic::result::TardisResult, tokio}; -mod event_listener; mod message_send_listener; pub async fn init() -> TardisResult<()> { - // deprecate mq tunnel - event_listener::EventListener::default().run().await?; tokio::task::spawn(async move { let period = tokio::time::Duration::from_secs(2); let mut interval = tokio::time::interval(period); diff --git a/support/reach/tests/test_message_send.rs b/support/reach/tests/test_message_send.rs index 17a36ed18..427099b45 100644 --- a/support/reach/tests/test_message_send.rs +++ b/support/reach/tests/test_message_send.rs @@ -179,7 +179,7 @@ pub async fn test_ct_api() -> TardisResult<()> { // test trigger // find trigger scene - let (trigger_scene_id, _trigger_scene_code) = { + let (trigger_scene_id, trigger_scene_code) = { log::info!("find all trigger scene"); let trigger_scenes = client.find_trigger_scene().await?; let name_codes_map = trigger_scenes.iter().map(|s| (s.name.clone(), s.code.clone())).collect::>(); @@ -232,31 +232,29 @@ pub async fn test_ct_api() -> TardisResult<()> { }; // test mq trigger - // deprecated - // { - // let name = "David"; - // let code = random_string(6); - // let send_req = ReachMsgSendReq { - // scene_code: trigger_scene_code, - // receives: vec![ReachMsgReceive { - // receive_group_code: receive_group_code.clone(), - // receive_ids: vec![name.into()], - // receive_kind: ReachReceiveKind::Account, - // }], - // rel_item_id, - // replace: [("name".to_owned(), name.to_owned()), ("code".to_owned(), code.clone())].into(), - // own_paths: ctx.own_paths.clone(), - // }; - // log::info!("send trigger message"); - // // plan to replace mq - // funs.mq().publish(MQ_REACH_TOPIC_MESSAGE, TardisFuns::json.obj_to_string(&send_req)?, &HashMap::new()).await?; - // // wait for about 3 seconds - // tokio::time::sleep(tokio::time::Duration::from_secs(3)).await; - // let msg = holder.sms_mocker.get_latest_message(name).await; - // log::info!("latest message for {name}: {:?}", msg); - // let msg = msg.expect("message is empty"); - // assert_eq!(msg, expected_content(name, &code)); - // } + { + let name = "David"; + let code = random_string(6); + let send_req = ReachMsgSendReq { + scene_code: trigger_scene_code, + receives: vec![ReachMsgReceive { + receive_group_code: receive_group_code.clone(), + receive_ids: vec![name.into()], + receive_kind: ReachReceiveKind::Account, + }], + rel_item_id, + replace: [("name".to_owned(), name.to_owned()), ("code".to_owned(), code.clone())].into(), + }; + log::info!("send trigger message"); + // plan to replace mq + client.message_send(&send_req).await?; + // wait for about 3 seconds + tokio::time::sleep(tokio::time::Duration::from_secs(3)).await; + let msg = holder.sms_mocker.get_latest_message(name).await; + log::info!("latest message for {name}: {:?}", msg); + let msg = msg.expect("message is empty"); + assert_eq!(msg, expected_content(name, &code)); + } drop(holder); Ok(()) From 678967373c0f8986219ad803978f26a29af725ce Mon Sep 17 00:00:00 2001 From: 4t145 Date: Wed, 18 Oct 2023 17:55:10 +0800 Subject: [PATCH 2/7] make more fields have a default value --- support/reach/src/domain/message_template.rs | 10 +++----- support/reach/src/dto/message/mod.rs | 1 + support/reach/src/dto/message/send_req.rs | 3 +++ support/reach/src/dto/message/template.rs | 26 ++++++++++++++------ support/reach/tests/test_message_send.rs | 6 ++--- 5 files changed, 28 insertions(+), 18 deletions(-) diff --git a/support/reach/src/domain/message_template.rs b/support/reach/src/domain/message_template.rs index 89cff165d..8239e751d 100644 --- a/support/reach/src/domain/message_template.rs +++ b/support/reach/src/domain/message_template.rs @@ -96,6 +96,9 @@ impl From<&ReachMessageTemplateAddReq> for ActiveModel { fill_by_add_req!(add_req => { note, icon, + code, + name, + scope_level, sort, disabled, variables, @@ -111,13 +114,6 @@ impl From<&ReachMessageTemplateAddReq> for ActiveModel { sms_signature, sms_from, } model); - fill_by_mod_req! { - add_req => { - code, - name, - scope_level, - } model - } model } } diff --git a/support/reach/src/dto/message/mod.rs b/support/reach/src/dto/message/mod.rs index be7d50204..114f8aeec 100644 --- a/support/reach/src/dto/message/mod.rs +++ b/support/reach/src/dto/message/mod.rs @@ -97,6 +97,7 @@ pub struct ReachMessageSummaryResp { pub rel_reach_msg_signature_id: String, #[oai(validator(max_length = "255"))] pub rel_reach_msg_template_id: String, + #[oai(default)] pub reach_status: ReachStatusKind, pub content_replace: String, pub template_content: String, diff --git a/support/reach/src/dto/message/send_req.rs b/support/reach/src/dto/message/send_req.rs index 6cfdad39d..f274c6dc0 100644 --- a/support/reach/src/dto/message/send_req.rs +++ b/support/reach/src/dto/message/send_req.rs @@ -5,8 +5,10 @@ use std::collections::HashMap; #[derive(Debug, Serialize, Deserialize, poem_openapi::Object)] pub struct ReachMsgSendReq { pub scene_code: String, + #[oai(default)] pub receives: Vec, pub rel_item_id: String, + #[oai(default)] pub replace: HashMap, } @@ -14,5 +16,6 @@ pub struct ReachMsgSendReq { pub struct ReachMsgReceive { pub receive_group_code: String, pub receive_kind: ReachReceiveKind, + #[oai(default)] pub receive_ids: Vec, } diff --git a/support/reach/src/dto/message/template.rs b/support/reach/src/dto/message/template.rs index ea9b5e869..b407a84fb 100644 --- a/support/reach/src/dto/message/template.rs +++ b/support/reach/src/dto/message/template.rs @@ -15,13 +15,14 @@ pub struct ReachMessageTemplateAddReq { // pub create_time: DateTime, // pub update_time: DateTime, /// 用户触达等级类型 - pub scope_level: Option, + #[oai(default)] + pub scope_level: i16, /// 编码 - #[oai(validator(max_length = "255"))] - pub code: Option, + #[oai(validator(max_length = "255"), default)] + pub code: String, /// 名称 - #[oai(validator(max_length = "255"))] - pub name: Option, + #[oai(validator(max_length = "255"), default)] + pub name: String, /// 说明 #[oai(validator(max_length = "2000"), default)] pub note: String, @@ -37,6 +38,7 @@ pub struct ReachMessageTemplateAddReq { /// 参数 #[oai(default)] pub variables: String, + #[oai(default)] /// 用户触达等级类型 pub level_kind: ReachLevelKind, /// 主题 @@ -48,10 +50,13 @@ pub struct ReachMessageTemplateAddReq { /// 确认超时时间 pub timeout_sec: i32, /// 确认超时策略 + #[oai(default)] pub timeout_strategy: ReachTimeoutStrategyKind, /// 关联的触达通道 + #[oai(default)] pub rel_reach_channel: ReachChannelKind, /// 模板类型 + #[oai(default)] pub kind: ReachTemplateKind, /// 用户触达验证码策略Id #[oai(validator(max_length = "255"))] @@ -95,6 +100,7 @@ pub struct ReachMessageTemplateModifyReq { /// 参数 #[oai(default)] pub variables: Option, + #[oai(default)] /// 用户触达等级类型 pub level_kind: Option, /// 主题 @@ -103,22 +109,26 @@ pub struct ReachMessageTemplateModifyReq { /// 内容 #[oai(validator(max_length = "2000"))] pub content: Option, + #[oai(default)] /// 确认超时时间 pub timeout_sec: Option, /// 确认超时策略 + #[oai(default)] pub timeout_strategy: Option, + #[oai(default)] /// 关联的触达通道 pub rel_reach_channel: Option, + #[oai(default)] /// 模板类型 pub kind: Option, /// 用户触达验证码策略Id - #[oai(validator(max_length = "255"))] + #[oai(validator(max_length = "255"), default)] pub rel_reach_verify_code_strategy_id: Option, /// 第三方插件-模板Id - #[oai(validator(max_length = "255"))] + #[oai(validator(max_length = "255"), default)] pub sms_template_id: Option, /// 第三方插件-签名 - #[oai(validator(max_length = "255"))] + #[oai(validator(max_length = "255"), default)] pub sms_signature: Option, /// 第三方插件-短信发送方的号码 #[oai(validator(max_length = "255"))] diff --git a/support/reach/tests/test_message_send.rs b/support/reach/tests/test_message_send.rs index 427099b45..031410304 100644 --- a/support/reach/tests/test_message_send.rs +++ b/support/reach/tests/test_message_send.rs @@ -37,9 +37,9 @@ pub async fn test_ct_api() -> TardisResult<()> { sms_template_id: "sms-tempalte-id".into(), sms_signature: "sms-signature".into(), sms_from: "reach@bios.dev".into(), - scope_level: Some(0), - code: Some("test-code".into()), - name: Some(template_name.clone()), + scope_level: 0, + code: "test-code".into(), + name: template_name.clone(), note: "test-note".into(), icon: "test-icon".into(), ..Default::default() From 650d8b96c77bb1715971d9bbe0ba59196249e853 Mon Sep 17 00:00:00 2001 From: 4t145 Date: Thu, 19 Oct 2023 09:32:39 +0800 Subject: [PATCH 3/7] rename files in reach --- clients/hwsms/src/ext/reach.rs | 6 ++-- services/bios-all/src/initializer.rs | 4 +-- support/reach/src/{api/mod.rs => api.rs} | 14 ++++----- support/reach/src/api/cc/mod.rs | 6 ---- support/reach/src/api/ci/mod.rs | 2 -- support/reach/src/api/ct/mod.rs | 30 ------------------- support/reach/src/api/reach_api_cc.rs | 6 ++++ .../reach_api_cc_message.rs} | 10 +++---- .../reach_api_cc_trigger_scene.rs} | 4 +-- support/reach/src/api/reach_api_ci.rs | 2 ++ .../reach_api_ci_message.rs} | 4 +-- support/reach/src/api/reach_api_ct.rs | 30 +++++++++++++++++++ .../reach_api_ct_message.rs} | 4 +-- .../reach_api_ct_msg_log.rs} | 4 +-- .../reach_api_ct_msg_signature.rs} | 4 +-- .../reach_api_ct_msg_template.rs} | 4 +-- .../reach_api_ct_trigger_global.rs} | 4 +-- .../reach_api_ct_trigger_instance.rs} | 4 +-- support/reach/src/config/mail.rs | 1 - .../reach/src/{domain/mod.rs => domain.rs} | 0 support/reach/src/{dto/mod.rs => dto.rs} | 0 support/reach/src/errors.rs | 1 - support/reach/src/lib.rs | 15 +++++----- .../src/{config/mod.rs => reach_config.rs} | 6 ++-- .../reach_config_sms.rs} | 0 .../reach/src/{consts.rs => reach_consts.rs} | 0 support/reach/src/{init.rs => reach_init.rs} | 6 ++-- .../reach/src/{invoke.rs => reach_invoke.rs} | 2 +- .../src/{client.rs => reach_send_channel.rs} | 2 +- support/reach/src/{serv/mod.rs => serv.rs} | 0 support/reach/src/serv/message_send.rs | 2 +- support/reach/src/{task/mod.rs => task.rs} | 0 .../reach/src/task/message_send_listener.rs | 2 +- support/reach/tests/test_message_send.rs | 4 +-- support/reach/tests/test_reach_common.rs | 4 +-- support/reach/tests/test_send_client.rs | 6 ++-- 36 files changed, 94 insertions(+), 99 deletions(-) rename support/reach/src/{api/mod.rs => api.rs} (63%) delete mode 100644 support/reach/src/api/cc/mod.rs delete mode 100644 support/reach/src/api/ci/mod.rs delete mode 100644 support/reach/src/api/ct/mod.rs create mode 100644 support/reach/src/api/reach_api_cc.rs rename support/reach/src/api/{cc/message.rs => reach_api_cc/reach_api_cc_message.rs} (95%) rename support/reach/src/api/{cc/trigger_scene.rs => reach_api_cc/reach_api_cc_trigger_scene.rs} (96%) create mode 100644 support/reach/src/api/reach_api_ci.rs rename support/reach/src/api/{ci/message.rs => reach_api_ci/reach_api_ci_message.rs} (94%) create mode 100644 support/reach/src/api/reach_api_ct.rs rename support/reach/src/api/{ct/message.rs => reach_api_ct/reach_api_ct_message.rs} (97%) rename support/reach/src/api/{ct/msg_log.rs => reach_api_ct/reach_api_ct_msg_log.rs} (94%) rename support/reach/src/api/{ct/msg_signature.rs => reach_api_ct/reach_api_ct_msg_signature.rs} (98%) rename support/reach/src/api/{ct/msg_template.rs => reach_api_ct/reach_api_ct_msg_template.rs} (98%) rename support/reach/src/api/{ct/trigger_global.rs => reach_api_ct/reach_api_ct_trigger_global.rs} (95%) rename support/reach/src/api/{ct/trigger_instance.rs => reach_api_ct/reach_api_ct_trigger_instance.rs} (96%) delete mode 100644 support/reach/src/config/mail.rs rename support/reach/src/{domain/mod.rs => domain.rs} (100%) rename support/reach/src/{dto/mod.rs => dto.rs} (100%) delete mode 100644 support/reach/src/errors.rs rename support/reach/src/{config/mod.rs => reach_config.rs} (83%) rename support/reach/src/{config/sms.rs => reach_config/reach_config_sms.rs} (100%) rename support/reach/src/{consts.rs => reach_consts.rs} (100%) rename support/reach/src/{init.rs => reach_init.rs} (94%) rename support/reach/src/{invoke.rs => reach_invoke.rs} (89%) rename support/reach/src/{client.rs => reach_send_channel.rs} (98%) rename support/reach/src/{serv/mod.rs => serv.rs} (100%) rename support/reach/src/{task/mod.rs => task.rs} (100%) diff --git a/clients/hwsms/src/ext/reach.rs b/clients/hwsms/src/ext/reach.rs index 5dad34430..0e323b3ea 100644 --- a/clients/hwsms/src/ext/reach.rs +++ b/clients/hwsms/src/ext/reach.rs @@ -4,9 +4,9 @@ use std::{ }; use bios_reach::{ - client::{GenericTemplate, SendChannel}, - config::ReachConfig, - consts::MODULE_CODE, + reach_send_channel::{GenericTemplate, SendChannel}, + reach_config::ReachConfig, + reach_consts::MODULE_CODE, dto::{ContentReplace, ReachChannelKind}, }; use tardis::{ diff --git a/services/bios-all/src/initializer.rs b/services/bios-all/src/initializer.rs index 75f49c2e3..3e2cdf8db 100644 --- a/services/bios-all/src/initializer.rs +++ b/services/bios-all/src/initializer.rs @@ -1,6 +1,6 @@ use std::sync::Arc; -use bios_reach::client::SendChannelMap; +use bios_reach::reach_send_channel::SendChannelMap; use tardis::basic::result::TardisResult; use tardis::web::web_server::TardisWebServer; @@ -11,7 +11,7 @@ pub async fn init(web_server: &TardisWebServer) -> TardisResult<()> { web_server, SendChannelMap::new() .with_arc_channel(bios_client_hwsms::SmsClient::from_reach_config()) - .with_arc_channel(Arc::new(tardis::TardisFuns::mail_by_module(bios_reach::consts::MODULE_CODE))), + .with_arc_channel(Arc::new(tardis::TardisFuns::mail_by_module(bios_reach::reach_consts::MODULE_CODE))), ) .await?; diff --git a/support/reach/src/api/mod.rs b/support/reach/src/api.rs similarity index 63% rename from support/reach/src/api/mod.rs rename to support/reach/src/api.rs index a4c2f4860..4fc0cc579 100644 --- a/support/reach/src/api/mod.rs +++ b/support/reach/src/api.rs @@ -1,15 +1,15 @@ -mod cc; +mod reach_api_cc; -pub use cc::*; +pub use reach_api_cc::*; -mod ct; -pub use ct::*; +mod reach_api_ct; +pub use reach_api_ct::*; -mod ci; -pub use ci::*; +mod reach_api_ci; +pub use reach_api_ci::*; use tardis::{basic::result::TardisResult, web::web_server::TardisWebServer}; -use crate::consts::DOMAIN_CODE; +use crate::reach_consts::DOMAIN_CODE; pub type ReachApi = (ReachCcApi, ReachCtApi, ReachMessageCiApi); pub async fn init(web_server: &TardisWebServer) -> TardisResult<()> { diff --git a/support/reach/src/api/cc/mod.rs b/support/reach/src/api/cc/mod.rs deleted file mode 100644 index e3123bb2a..000000000 --- a/support/reach/src/api/cc/mod.rs +++ /dev/null @@ -1,6 +0,0 @@ -mod message; -pub use message::ReachMessageCcApi; -mod trigger_scene; -pub use trigger_scene::ReachTriggerSceneCcApi; - -pub type ReachCcApi = (ReachTriggerSceneCcApi, ReachMessageCcApi); diff --git a/support/reach/src/api/ci/mod.rs b/support/reach/src/api/ci/mod.rs deleted file mode 100644 index b1bd3227e..000000000 --- a/support/reach/src/api/ci/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -mod message; -pub use message::ReachMessageCiApi; diff --git a/support/reach/src/api/ct/mod.rs b/support/reach/src/api/ct/mod.rs deleted file mode 100644 index fb99b0fff..000000000 --- a/support/reach/src/api/ct/mod.rs +++ /dev/null @@ -1,30 +0,0 @@ -mod message; -pub use message::ReachMessageCtApi; -mod msg_log; -pub use msg_log::ReachMsgLogCtApi; -mod msg_signature; -pub use msg_signature::ReachMsgSignatureCtApi; -mod msg_template; -pub use msg_template::ReachMessageTemplateCtApi; -mod trigger_global; -use tardis::basic::{error::TardisError, result::TardisResult}; -pub use trigger_global::ReachTriggerGlobalConfigCtApi; -mod trigger_instance; -pub use trigger_instance::ReachTriggerInstanceConfigCtApi; - -pub type ReachCtApi = ( - ReachMessageCtApi, - ReachMsgSignatureCtApi, - ReachMsgLogCtApi, - ReachMessageTemplateCtApi, - ReachTriggerGlobalConfigCtApi, - ReachTriggerInstanceConfigCtApi, -); - -fn map_notfound_to_false(e: TardisError) -> TardisResult { - if e.code.contains("404") { - Ok(false) - } else { - Err(e) - } -} diff --git a/support/reach/src/api/reach_api_cc.rs b/support/reach/src/api/reach_api_cc.rs new file mode 100644 index 000000000..5718dff55 --- /dev/null +++ b/support/reach/src/api/reach_api_cc.rs @@ -0,0 +1,6 @@ +mod reach_api_cc_message; +pub use reach_api_cc_message::ReachMessageCcApi; +mod reach_api_cc_trigger_scene; +pub use reach_api_cc_trigger_scene::ReachTriggerSceneCcApi; + +pub type ReachCcApi = (ReachTriggerSceneCcApi, ReachMessageCcApi); diff --git a/support/reach/src/api/cc/message.rs b/support/reach/src/api/reach_api_cc/reach_api_cc_message.rs similarity index 95% rename from support/reach/src/api/cc/message.rs rename to support/reach/src/api/reach_api_cc/reach_api_cc_message.rs index c09f0f2c1..3b5b0d9cb 100644 --- a/support/reach/src/api/cc/message.rs +++ b/support/reach/src/api/reach_api_cc/reach_api_cc_message.rs @@ -9,13 +9,13 @@ use tardis::web::poem_openapi; use tardis::web::poem_openapi::payload::Json; use tardis::web::web_resp::{TardisApiResult, TardisResp, Void}; -use crate::client::{GenericTemplate, SendChannelMap}; -use crate::config::ReachConfig; -use crate::consts::*; +use crate::reach_send_channel::{GenericTemplate, SendChannelMap}; +use crate::reach_config::ReachConfig; +use crate::reach_consts::*; use crate::dto::*; -use crate::init::REACH_SEND_CHANNEL_MAP; +use crate::reach_init::REACH_SEND_CHANNEL_MAP; #[cfg(feature = "simple-client")] -use crate::invoke::Client; +use crate::reach_invoke::Client; use crate::serv::*; #[derive(Clone)] diff --git a/support/reach/src/api/cc/trigger_scene.rs b/support/reach/src/api/reach_api_cc/reach_api_cc_trigger_scene.rs similarity index 96% rename from support/reach/src/api/cc/trigger_scene.rs rename to support/reach/src/api/reach_api_cc/reach_api_cc_trigger_scene.rs index 2b011395a..44bb7ff80 100644 --- a/support/reach/src/api/cc/trigger_scene.rs +++ b/support/reach/src/api/reach_api_cc/reach_api_cc_trigger_scene.rs @@ -6,10 +6,10 @@ use tardis::web::poem_openapi; use tardis::web::poem_openapi::param::Query; use tardis::web::web_resp::{TardisApiResult, TardisResp}; -use crate::consts::get_tardis_inst; +use crate::reach_consts::get_tardis_inst; use crate::dto::*; #[cfg(feature = "simple-client")] -use crate::invoke::Client; +use crate::reach_invoke::Client; use crate::serv::*; #[derive(Clone, Default)] diff --git a/support/reach/src/api/reach_api_ci.rs b/support/reach/src/api/reach_api_ci.rs new file mode 100644 index 000000000..90898347a --- /dev/null +++ b/support/reach/src/api/reach_api_ci.rs @@ -0,0 +1,2 @@ +mod reach_api_ci_message; +pub use reach_api_ci_message::ReachMessageCiApi; diff --git a/support/reach/src/api/ci/message.rs b/support/reach/src/api/reach_api_ci/reach_api_ci_message.rs similarity index 94% rename from support/reach/src/api/ci/message.rs rename to support/reach/src/api/reach_api_ci/reach_api_ci_message.rs index 26af7b1fa..bbef6e331 100644 --- a/support/reach/src/api/ci/message.rs +++ b/support/reach/src/api/reach_api_ci/reach_api_ci_message.rs @@ -6,10 +6,10 @@ use tardis::web::poem_openapi; use tardis::web::poem_openapi::payload::Json; use tardis::web::web_resp::{TardisApiResult, TardisResp, Void}; -use crate::consts::*; +use crate::reach_consts::*; use crate::dto::*; #[cfg(feature = "simple-client")] -use crate::invoke::Client; +use crate::reach_invoke::Client; use crate::serv::*; #[derive(Debug, Default, Clone)] diff --git a/support/reach/src/api/reach_api_ct.rs b/support/reach/src/api/reach_api_ct.rs new file mode 100644 index 000000000..7d4ed9dd3 --- /dev/null +++ b/support/reach/src/api/reach_api_ct.rs @@ -0,0 +1,30 @@ +mod reach_api_ct_message; +pub use reach_api_ct_message::ReachMessageCtApi; +mod reach_api_ct_msg_log; +pub use reach_api_ct_msg_log::ReachMsgLogCtApi; +mod reach_api_ct_msg_signature; +pub use reach_api_ct_msg_signature::ReachMsgSignatureCtApi; +mod reach_api_ct_msg_template; +pub use reach_api_ct_msg_template::ReachMessageTemplateCtApi; +mod reach_api_ct_trigger_global; +use tardis::basic::{error::TardisError, result::TardisResult}; +pub use reach_api_ct_trigger_global::ReachTriggerGlobalConfigCtApi; +mod reach_api_ct_trigger_instance; +pub use reach_api_ct_trigger_instance::ReachTriggerInstanceConfigCtApi; + +pub type ReachCtApi = ( + ReachMessageCtApi, + ReachMsgSignatureCtApi, + ReachMsgLogCtApi, + ReachMessageTemplateCtApi, + ReachTriggerGlobalConfigCtApi, + ReachTriggerInstanceConfigCtApi, +); + +fn map_notfound_to_false(e: TardisError) -> TardisResult { + if e.code.contains("404") { + Ok(false) + } else { + Err(e) + } +} diff --git a/support/reach/src/api/ct/message.rs b/support/reach/src/api/reach_api_ct/reach_api_ct_message.rs similarity index 97% rename from support/reach/src/api/ct/message.rs rename to support/reach/src/api/reach_api_ct/reach_api_ct_message.rs index 39fc69a5f..7e83c817b 100644 --- a/support/reach/src/api/ct/message.rs +++ b/support/reach/src/api/reach_api_ct/reach_api_ct_message.rs @@ -7,10 +7,10 @@ use tardis::web::poem_openapi::param::{Path, Query}; use tardis::web::poem_openapi::payload::Json; use tardis::web::web_resp::{TardisApiResult, TardisPage, TardisResp}; -use crate::consts::get_tardis_inst; +use crate::reach_consts::get_tardis_inst; use crate::dto::*; #[cfg(feature = "simple-client")] -use crate::invoke::Client; +use crate::reach_invoke::Client; use crate::serv::*; #[derive(Clone, Default)] /// 用户触达消息-公共控制台 diff --git a/support/reach/src/api/ct/msg_log.rs b/support/reach/src/api/reach_api_ct/reach_api_ct_msg_log.rs similarity index 94% rename from support/reach/src/api/ct/msg_log.rs rename to support/reach/src/api/reach_api_ct/reach_api_ct_msg_log.rs index ce1438488..fb5dfc55e 100644 --- a/support/reach/src/api/ct/msg_log.rs +++ b/support/reach/src/api/reach_api_ct/reach_api_ct_msg_log.rs @@ -1,7 +1,7 @@ -use crate::consts::get_tardis_inst; +use crate::reach_consts::get_tardis_inst; use crate::dto::*; #[cfg(feature = "simple-client")] -use crate::invoke::Client; +use crate::reach_invoke::Client; use crate::serv::*; use bios_basic::rbum::serv::rbum_crud_serv::RbumCrudOperation; use bios_sdk_invoke::simple_invoke_client; diff --git a/support/reach/src/api/ct/msg_signature.rs b/support/reach/src/api/reach_api_ct/reach_api_ct_msg_signature.rs similarity index 98% rename from support/reach/src/api/ct/msg_signature.rs rename to support/reach/src/api/reach_api_ct/reach_api_ct_msg_signature.rs index c98274608..9db69467b 100644 --- a/support/reach/src/api/ct/msg_signature.rs +++ b/support/reach/src/api/reach_api_ct/reach_api_ct_msg_signature.rs @@ -7,12 +7,12 @@ use tardis::web::poem_openapi::payload::Json; use tardis::web::poem_openapi; use tardis::web::web_resp::{TardisApiResult, TardisPage, TardisResp}; -use crate::consts::get_tardis_inst; +use crate::reach_consts::get_tardis_inst; use crate::dto::*; use crate::serv::*; #[cfg(feature = "simple-client")] -use crate::invoke::Client; +use crate::reach_invoke::Client; use super::map_notfound_to_false; #[derive(Clone, Default)] diff --git a/support/reach/src/api/ct/msg_template.rs b/support/reach/src/api/reach_api_ct/reach_api_ct_msg_template.rs similarity index 98% rename from support/reach/src/api/ct/msg_template.rs rename to support/reach/src/api/reach_api_ct/reach_api_ct_msg_template.rs index 195ecb5ff..c34ed5c88 100644 --- a/support/reach/src/api/ct/msg_template.rs +++ b/support/reach/src/api/reach_api_ct/reach_api_ct_msg_template.rs @@ -7,12 +7,12 @@ use tardis::web::poem_openapi::payload::Json; use tardis::web::poem_openapi; use tardis::web::web_resp::{TardisApiResult, TardisPage, TardisResp}; -use crate::consts::get_tardis_inst; +use crate::reach_consts::get_tardis_inst; use crate::dto::*; use crate::serv::*; #[cfg(feature = "simple-client")] -use crate::invoke::Client; +use crate::reach_invoke::Client; use super::map_notfound_to_false; #[derive(Clone, Default)] diff --git a/support/reach/src/api/ct/trigger_global.rs b/support/reach/src/api/reach_api_ct/reach_api_ct_trigger_global.rs similarity index 95% rename from support/reach/src/api/ct/trigger_global.rs rename to support/reach/src/api/reach_api_ct/reach_api_ct_trigger_global.rs index 561a3755e..ee1d481ba 100644 --- a/support/reach/src/api/ct/trigger_global.rs +++ b/support/reach/src/api/reach_api_ct/reach_api_ct_trigger_global.rs @@ -6,12 +6,12 @@ use tardis::web::poem::web::Json; use tardis::web::poem_openapi; use tardis::web::web_resp::{TardisApiResult, TardisResp, Void}; -use crate::consts::get_tardis_inst; +use crate::reach_consts::get_tardis_inst; use crate::dto::*; use crate::serv::*; #[cfg(feature = "simple-client")] -use crate::invoke::Client; +use crate::reach_invoke::Client; #[derive(Clone, Default)] /// 用户触达触发全局配置-租户控制台 pub struct ReachTriggerGlobalConfigCtApi; diff --git a/support/reach/src/api/ct/trigger_instance.rs b/support/reach/src/api/reach_api_ct/reach_api_ct_trigger_instance.rs similarity index 96% rename from support/reach/src/api/ct/trigger_instance.rs rename to support/reach/src/api/reach_api_ct/reach_api_ct_trigger_instance.rs index a2d7b2bd7..4175ed694 100644 --- a/support/reach/src/api/ct/trigger_instance.rs +++ b/support/reach/src/api/reach_api_ct/reach_api_ct_trigger_instance.rs @@ -7,12 +7,12 @@ use tardis::web::poem_openapi::param::Query; use tardis::web::poem_openapi::payload::Json; use tardis::web::web_resp::{TardisApiResult, TardisResp, Void}; -use crate::consts::get_tardis_inst; +use crate::reach_consts::get_tardis_inst; use crate::dto::*; use crate::serv::*; #[cfg(feature = "simple-client")] -use crate::invoke::Client; +use crate::reach_invoke::Client; #[derive(Clone, Default)] /// 用户触达触发实例配置-租户控制台 pub struct ReachTriggerInstanceConfigCtApi; diff --git a/support/reach/src/config/mail.rs b/support/reach/src/config/mail.rs deleted file mode 100644 index 8b1378917..000000000 --- a/support/reach/src/config/mail.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/support/reach/src/domain/mod.rs b/support/reach/src/domain.rs similarity index 100% rename from support/reach/src/domain/mod.rs rename to support/reach/src/domain.rs diff --git a/support/reach/src/dto/mod.rs b/support/reach/src/dto.rs similarity index 100% rename from support/reach/src/dto/mod.rs rename to support/reach/src/dto.rs diff --git a/support/reach/src/errors.rs b/support/reach/src/errors.rs deleted file mode 100644 index 8b1378917..000000000 --- a/support/reach/src/errors.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/support/reach/src/lib.rs b/support/reach/src/lib.rs index 3156fb479..dd673ccfa 100644 --- a/support/reach/src/lib.rs +++ b/support/reach/src/lib.rs @@ -1,19 +1,18 @@ #![warn(clippy::dbg_macro)] #![warn(clippy::unwrap_used)] -pub mod api; -pub mod client; -pub mod config; -pub mod consts; +mod api; +pub mod reach_send_channel; +pub mod reach_config; +pub mod reach_consts; mod domain; pub mod dto; -mod errors; -mod init; +mod reach_init; #[cfg(feature = "simple-client")] -pub mod invoke; +pub mod reach_invoke; mod task; -pub use init::init; +pub use reach_init::init; pub mod reach_initializer { pub use crate::init; } diff --git a/support/reach/src/config/mod.rs b/support/reach/src/reach_config.rs similarity index 83% rename from support/reach/src/config/mod.rs rename to support/reach/src/reach_config.rs index f58f1ea95..c959fd69c 100644 --- a/support/reach/src/config/mod.rs +++ b/support/reach/src/reach_config.rs @@ -1,14 +1,12 @@ use bios_basic::rbum::rbum_config::RbumConfig; use bios_sdk_invoke::invoke_config::InvokeConfig; use serde::{Deserialize, Serialize}; - -mod mail; -mod sms; +mod reach_config_sms; #[derive(Debug, Serialize, Deserialize, Clone, Default)] #[serde(default)] pub struct ReachConfig { - pub sms: sms::SmsConfig, + pub sms: reach_config_sms::SmsConfig, pub rbum: RbumConfig, pub invoke: InvokeConfig, pub iam_get_account: String, diff --git a/support/reach/src/config/sms.rs b/support/reach/src/reach_config/reach_config_sms.rs similarity index 100% rename from support/reach/src/config/sms.rs rename to support/reach/src/reach_config/reach_config_sms.rs diff --git a/support/reach/src/consts.rs b/support/reach/src/reach_consts.rs similarity index 100% rename from support/reach/src/consts.rs rename to support/reach/src/reach_consts.rs diff --git a/support/reach/src/init.rs b/support/reach/src/reach_init.rs similarity index 94% rename from support/reach/src/init.rs rename to support/reach/src/reach_init.rs index 2cb357e6c..69cf51154 100644 --- a/support/reach/src/init.rs +++ b/support/reach/src/reach_init.rs @@ -14,9 +14,9 @@ use tardis::{ use crate::{ api, - client::SendChannelMap, - config::ReachConfig, - consts::{get_tardis_inst, DOMAIN_CODE, DOMAIN_REACH_ID, RBUM_EXT_TABLE_REACH_MESSAGE, RBUM_KIND_CODE_REACH_MESSAGE, REACH_INIT_OWNER}, + reach_send_channel::SendChannelMap, + reach_config::ReachConfig, + reach_consts::{get_tardis_inst, DOMAIN_CODE, DOMAIN_REACH_ID, RBUM_EXT_TABLE_REACH_MESSAGE, RBUM_KIND_CODE_REACH_MESSAGE, REACH_INIT_OWNER}, serv::ReachTriggerSceneService, task, }; diff --git a/support/reach/src/invoke.rs b/support/reach/src/reach_invoke.rs similarity index 89% rename from support/reach/src/invoke.rs rename to support/reach/src/reach_invoke.rs index ad6395265..f320f15b7 100644 --- a/support/reach/src/invoke.rs +++ b/support/reach/src/reach_invoke.rs @@ -14,7 +14,7 @@ impl<'a> Client<'a> { } impl SimpleInvokeClient for Client<'_> { - const DOMAIN_CODE: &'static str = crate::consts::DOMAIN_CODE; + const DOMAIN_CODE: &'static str = crate::reach_consts::DOMAIN_CODE; fn get_ctx(&self) -> &tardis::basic::dto::TardisContext { self.ctx diff --git a/support/reach/src/client.rs b/support/reach/src/reach_send_channel.rs similarity index 98% rename from support/reach/src/client.rs rename to support/reach/src/reach_send_channel.rs index 53138b341..42c3dba50 100644 --- a/support/reach/src/client.rs +++ b/support/reach/src/reach_send_channel.rs @@ -9,7 +9,7 @@ use tardis::{ mail::mail_client::TardisMailSendReq, }; -use crate::{config::ReachConfig, domain::message_template, dto::*}; +use crate::{reach_config::ReachConfig, domain::message_template, dto::*}; #[derive(Default, Debug)] pub struct GenericTemplate<'t> { diff --git a/support/reach/src/serv/mod.rs b/support/reach/src/serv.rs similarity index 100% rename from support/reach/src/serv/mod.rs rename to support/reach/src/serv.rs diff --git a/support/reach/src/serv/message_send.rs b/support/reach/src/serv/message_send.rs index 56692ca3e..58208a411 100644 --- a/support/reach/src/serv/message_send.rs +++ b/support/reach/src/serv/message_send.rs @@ -7,7 +7,7 @@ use tardis::{ TardisFunsInst, }; -use crate::{consts::*, domain, dto::*, serv::*}; +use crate::{reach_consts::*, domain, dto::*, serv::*}; pub async fn message_send(send_req: ReachMsgSendReq, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<()> { let err = |msg: &str| funs.err().not_found("reach", "event_listener", msg, ""); diff --git a/support/reach/src/task/mod.rs b/support/reach/src/task.rs similarity index 100% rename from support/reach/src/task/mod.rs rename to support/reach/src/task.rs diff --git a/support/reach/src/task/message_send_listener.rs b/support/reach/src/task/message_send_listener.rs index a9919d6ce..d92864122 100644 --- a/support/reach/src/task/message_send_listener.rs +++ b/support/reach/src/task/message_send_listener.rs @@ -1,6 +1,6 @@ use std::{collections::HashSet, sync::Arc}; -use crate::{client::*, config::ReachConfig, consts::*, domain::*, dto::*, init::get_reach_send_channel_map, serv::*}; +use crate::{reach_send_channel::*, reach_config::ReachConfig, reach_consts::*, domain::*, dto::*, reach_init::get_reach_send_channel_map, serv::*}; use bios_basic::rbum::helper::rbum_scope_helper; use bios_sdk_invoke::clients::iam_client::IamClient; use tardis::{ diff --git a/support/reach/tests/test_message_send.rs b/support/reach/tests/test_message_send.rs index 031410304..f3a0c7d04 100644 --- a/support/reach/tests/test_message_send.rs +++ b/support/reach/tests/test_message_send.rs @@ -2,7 +2,7 @@ use bios_basic::rbum::dto::rbum_item_dto::RbumItemAddReq; use tardis::{basic::result::TardisResult, log, serde_json::json, testcontainers, tokio}; mod test_reach_common; -use bios_reach::{consts::*, dto::*, invoke}; +use bios_reach::{reach_consts::*, dto::*, reach_invoke}; use test_reach_common::*; #[tokio::test(flavor = "multi_thread")] pub async fn test_ct_api() -> TardisResult<()> { @@ -14,7 +14,7 @@ pub async fn test_ct_api() -> TardisResult<()> { let holder = init_tardis(&docker).await?; let ctx = get_test_ctx(); let funs = get_tardis_inst(); - let client = invoke::Client::new("http://localhost:8080/reach", ctx, &funs); + let client = reach_invoke::Client::new("http://localhost:8080/reach", ctx, &funs); const CONTENT_TEMPLATE: &str = "hello {name}, your code is {code}"; let template_name = random_string(16); fn expected_content(name: &str, code: &str) -> String { diff --git a/support/reach/tests/test_reach_common.rs b/support/reach/tests/test_reach_common.rs index bebcc3b3b..b680414f7 100644 --- a/support/reach/tests/test_reach_common.rs +++ b/support/reach/tests/test_reach_common.rs @@ -7,8 +7,8 @@ use bios_basic::rbum::serv::rbum_item_serv::RbumItemServ; use bios_basic::rbum::serv::rbum_kind_serv::RbumKindServ; use bios_basic::{rbum::rbum_config::RbumConfig, test::test_http_client::TestHttpClient}; use bios_client_hwsms::{SmsId, SmsResponse}; -use bios_reach::client::SendChannelMap; -use bios_reach::consts::{get_tardis_inst, DOMAIN_CODE, IAM_KEY_PHONE_V_CODE, RBUM_KIND_CODE_REACH_MESSAGE, REACH_INIT_OWNER}; +use bios_reach::reach_send_channel::SendChannelMap; +use bios_reach::reach_consts::{get_tardis_inst, DOMAIN_CODE, IAM_KEY_PHONE_V_CODE, RBUM_KIND_CODE_REACH_MESSAGE, REACH_INIT_OWNER}; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::sync::{Arc, OnceLock}; diff --git a/support/reach/tests/test_send_client.rs b/support/reach/tests/test_send_client.rs index cb4de974d..2bfaa31f8 100644 --- a/support/reach/tests/test_send_client.rs +++ b/support/reach/tests/test_send_client.rs @@ -4,7 +4,7 @@ use std::time::Duration; use tardis::{basic::result::TardisResult, testcontainers, tokio}; mod test_reach_common; -use bios_reach::{consts::*, invoke}; +use bios_reach::{reach_consts::*, reach_invoke}; use test_reach_common::*; #[derive(Deserialize)] pub struct TestConfig { @@ -30,7 +30,7 @@ pub async fn test_hw_sms() -> TardisResult<()> { let holder = init_tardis(&docker).await?; let ctx = get_test_ctx(); let funs = get_tardis_inst(); - let client = invoke::Client::new("http://localhost:8080/reach", ctx, &funs); + let client = reach_invoke::Client::new("http://localhost:8080/reach", ctx, &funs); // client.pwd_send(&phone, &code, &()).await?; client.vcode_send(&phone, &code, &()).await?; @@ -50,7 +50,7 @@ pub async fn test_mail() -> TardisResult<()> { let holder = init_tardis(&docker).await?; let ctx = get_test_ctx(); let funs = get_tardis_inst(); - let client = invoke::Client::new("http://localhost:8080/reach", ctx, &funs); + let client = reach_invoke::Client::new("http://localhost:8080/reach", ctx, &funs); client.mail_pwd_send(&mail, &content, "测试", &()).await?; // wait for send tokio::time::sleep(Duration::from_secs(10)).await; From 1d6d4bcef848e855664dd4584c8c5d3802756b7a Mon Sep 17 00:00:00 2001 From: 4t145 Date: Thu, 19 Oct 2023 09:43:36 +0800 Subject: [PATCH 4/7] delete duplicate file --- support/reach/src/api/ci/message.rs | 31 ----------------------------- support/reach/src/api/ci/mod.rs | 2 -- 2 files changed, 33 deletions(-) delete mode 100644 support/reach/src/api/ci/message.rs delete mode 100644 support/reach/src/api/ci/mod.rs diff --git a/support/reach/src/api/ci/message.rs b/support/reach/src/api/ci/message.rs deleted file mode 100644 index 26af7b1fa..000000000 --- a/support/reach/src/api/ci/message.rs +++ /dev/null @@ -1,31 +0,0 @@ -use tardis::log as tracing; - -use tardis::web::context_extractor::TardisContextExtractor; - -use tardis::web::poem_openapi; -use tardis::web::poem_openapi::payload::Json; -use tardis::web::web_resp::{TardisApiResult, TardisResp, Void}; - -use crate::consts::*; -use crate::dto::*; -#[cfg(feature = "simple-client")] -use crate::invoke::Client; -use crate::serv::*; - -#[derive(Debug, Default, Clone)] -pub struct ReachMessageCiApi; - -#[cfg_attr(feature = "simple-client", bios_sdk_invoke::simple_invoke_client(Client<'_>))] -#[poem_openapi::OpenApi(prefix_path = "/ci/message", tag = "bios_basic::ApiTag::App")] -impl ReachMessageCiApi { - /// 根据模板id发送信息 - #[oai(method = "put", path = "/send")] - #[tardis::log::instrument(skip_all, fields(module = "reach"))] - pub async fn message_send(&self, body: Json, ctx: TardisContextExtractor) -> TardisApiResult { - let ctx = ctx.0; - let funs = get_tardis_inst(); - let body = body.0; - message_send(body, &funs, &ctx).await?; - TardisResp::ok(VOID) - } -} diff --git a/support/reach/src/api/ci/mod.rs b/support/reach/src/api/ci/mod.rs deleted file mode 100644 index b1bd3227e..000000000 --- a/support/reach/src/api/ci/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -mod message; -pub use message::ReachMessageCiApi; From f285efc74d8738b6f6765526462634d76ff05c0a Mon Sep 17 00:00:00 2001 From: 4t145 Date: Thu, 19 Oct 2023 15:02:16 +0800 Subject: [PATCH 5/7] fix hwsms client to compat old template --- clients/hwsms/src/ext/reach.rs | 12 ++++++++---- support/reach/src/reach_send_channel.rs | 4 ++-- support/reach/tests/test_send_client.rs | 4 ++-- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/clients/hwsms/src/ext/reach.rs b/clients/hwsms/src/ext/reach.rs index 0e323b3ea..712a3021d 100644 --- a/clients/hwsms/src/ext/reach.rs +++ b/clients/hwsms/src/ext/reach.rs @@ -4,15 +4,15 @@ use std::{ }; use bios_reach::{ - reach_send_channel::{GenericTemplate, SendChannel}, + dto::{ContentReplace, ReachChannelKind}, reach_config::ReachConfig, reach_consts::MODULE_CODE, - dto::{ContentReplace, ReachChannelKind}, + reach_send_channel::{GenericTemplate, SendChannel}, }; use tardis::{ async_trait::async_trait, basic::{error::TardisError, result::TardisResult}, - TardisFuns, + serde_json, TardisFuns, }; use crate::{SendSmsRequest, SmsClient, SmsContent}; @@ -24,6 +24,10 @@ impl SendChannel for crate::SmsClient { } async fn send(&self, template: GenericTemplate<'_>, content: &ContentReplace, to: &HashSet<&str>) -> TardisResult<()> { let content = content.render_final_content::<20>(template.content); + // content should be a json string array + let content_as_json_string_array: Vec = + serde_json::from_str(&content).map_err(|e| TardisError::conflict(&format!("hwsms content should be a json string array: {e}"), "409-reach-bad-template"))?; + let template_paras = content_as_json_string_array.iter().map(|s| s.as_str()).collect(); tardis::log::trace!("send sms {content}"); let sms_content = SmsContent { to: &to.iter().fold( @@ -38,7 +42,7 @@ impl SendChannel for crate::SmsClient { }, ), template_id: template.sms_template_id.ok_or_else(|| TardisError::conflict("template missing field template_id", "409-reach-bad-template"))?, - template_paras: vec![&content], + template_paras, signature: template.sms_signature, }; let from = template.sms_from.ok_or_else(|| TardisError::conflict("template missing field sms_from", "409-reach-bad-template"))?; diff --git a/support/reach/src/reach_send_channel.rs b/support/reach/src/reach_send_channel.rs index 42c3dba50..603317085 100644 --- a/support/reach/src/reach_send_channel.rs +++ b/support/reach/src/reach_send_channel.rs @@ -9,7 +9,7 @@ use tardis::{ mail::mail_client::TardisMailSendReq, }; -use crate::{reach_config::ReachConfig, domain::message_template, dto::*}; +use crate::{domain::message_template, dto::*, reach_config::ReachConfig}; #[derive(Default, Debug)] pub struct GenericTemplate<'t> { @@ -24,7 +24,7 @@ impl<'t> GenericTemplate<'t> { pub fn pwd_template(config: &'t ReachConfig) -> Self { Self { name: None, - content: "{pwd}", + content: r#"["{pwd}"]"#, sms_from: Some(&config.sms.sms_general_from), sms_template_id: Some(&config.sms.sms_pwd_template_id), sms_signature: config.sms.sms_general_signature.as_deref(), diff --git a/support/reach/tests/test_send_client.rs b/support/reach/tests/test_send_client.rs index 2bfaa31f8..7e05960f3 100644 --- a/support/reach/tests/test_send_client.rs +++ b/support/reach/tests/test_send_client.rs @@ -31,9 +31,9 @@ pub async fn test_hw_sms() -> TardisResult<()> { let ctx = get_test_ctx(); let funs = get_tardis_inst(); let client = reach_invoke::Client::new("http://localhost:8080/reach", ctx, &funs); - // client.pwd_send(&phone, &code, &()).await?; + client.pwd_send(&phone, &code, &()).await?; - client.vcode_send(&phone, &code, &()).await?; + // client.vcode_send(&phone, &code, &()).await?; // wait for send tokio::time::sleep(Duration::from_secs(10)).await; drop(holder); From f9093adea362a769de7cb5cf810dfdaa9fe8210b Mon Sep 17 00:00:00 2001 From: 4t145 Date: Thu, 19 Oct 2023 17:26:20 +0800 Subject: [PATCH 6/7] reach: use default module, add reach log --- basic/src/rbum/dto/rbum_item_dto.rs | 13 ++++++ services/bios-all/src/initializer.rs | 2 +- support/reach/src/serv/message_send.rs | 14 +++--- .../reach/src/task/message_send_listener.rs | 45 +++++++++++++++++-- 4 files changed, 62 insertions(+), 12 deletions(-) diff --git a/basic/src/rbum/dto/rbum_item_dto.rs b/basic/src/rbum/dto/rbum_item_dto.rs index 3380d66ac..ee1d4f537 100644 --- a/basic/src/rbum/dto/rbum_item_dto.rs +++ b/basic/src/rbum/dto/rbum_item_dto.rs @@ -27,6 +27,19 @@ pub struct RbumItemAddReq { pub disabled: Option, } +impl Default for RbumItemAddReq { + fn default() -> Self { + Self { + id: Default::default(), + code: Default::default(), + name: TrimString::from(""), + rel_rbum_kind_id: Default::default(), + rel_rbum_domain_id: Default::default(), + scope_level: Default::default(), + disabled: Default::default(), + } + } +} /// For security reasons, this object cannot be used as an input to the API #[derive(Serialize, Deserialize, Debug)] #[serde(default)] diff --git a/services/bios-all/src/initializer.rs b/services/bios-all/src/initializer.rs index 3e2cdf8db..88cf409a1 100644 --- a/services/bios-all/src/initializer.rs +++ b/services/bios-all/src/initializer.rs @@ -11,7 +11,7 @@ pub async fn init(web_server: &TardisWebServer) -> TardisResult<()> { web_server, SendChannelMap::new() .with_arc_channel(bios_client_hwsms::SmsClient::from_reach_config()) - .with_arc_channel(Arc::new(tardis::TardisFuns::mail_by_module(bios_reach::reach_consts::MODULE_CODE))), + .with_arc_channel(Arc::new(tardis::TardisFuns::mail_by_module_or_default(bios_reach::reach_consts::MODULE_CODE))), ) .await?; diff --git a/support/reach/src/serv/message_send.rs b/support/reach/src/serv/message_send.rs index 58208a411..2177594b5 100644 --- a/support/reach/src/serv/message_send.rs +++ b/support/reach/src/serv/message_send.rs @@ -2,12 +2,12 @@ use std::collections::{HashMap, HashSet}; use bios_basic::rbum::{dto::rbum_item_dto::RbumItemAddReq, serv::rbum_crud_serv::RbumCrudOperation}; use tardis::{ - basic::{result::TardisResult, dto::TardisContext}, + basic::{dto::TardisContext, result::TardisResult}, db::sea_orm::{sea_query::Query, ColumnTrait, Iterable}, TardisFunsInst, }; -use crate::{reach_consts::*, domain, dto::*, serv::*}; +use crate::{domain, dto::*, reach_consts::*, serv::*}; pub async fn message_send(send_req: ReachMsgSendReq, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<()> { let err = |msg: &str| funs.err().not_found("reach", "event_listener", msg, ""); @@ -54,11 +54,11 @@ pub async fn message_send(send_req: ReachMsgSendReq, funs: &TardisFunsInst, ctx: map.entry(item.receive_group_code.clone()).or_default().push(item); map }); - - let mut instance_group_code = instances.into_iter().filter(|inst| receive_group_code.contains_key(&inst.receive_group_code.clone())).fold(HashMap::>::new(), |mut map, item| { - map.entry(item.receive_group_code.clone()).or_default().push(item); - map - }); + let mut instance_group_code = + instances.into_iter().filter(|inst| receive_group_code.contains_key(&inst.receive_group_code.clone())).fold(HashMap::>::new(), |mut map, item| { + map.entry(item.receive_group_code.clone()).or_default().push(item); + map + }); if instance_group_code.is_empty() { return Ok(()); diff --git a/support/reach/src/task/message_send_listener.rs b/support/reach/src/task/message_send_listener.rs index d92864122..6486b0403 100644 --- a/support/reach/src/task/message_send_listener.rs +++ b/support/reach/src/task/message_send_listener.rs @@ -1,10 +1,11 @@ use std::{collections::HashSet, sync::Arc}; -use crate::{reach_send_channel::*, reach_config::ReachConfig, reach_consts::*, domain::*, dto::*, reach_init::get_reach_send_channel_map, serv::*}; -use bios_basic::rbum::helper::rbum_scope_helper; +use crate::{domain::*, dto::*, reach_config::ReachConfig, reach_consts::*, reach_init::get_reach_send_channel_map, reach_send_channel::*, serv::*}; +use bios_basic::rbum::{helper::rbum_scope_helper, serv::rbum_crud_serv::RbumCrudOperation}; use bios_sdk_invoke::clients::iam_client::IamClient; use tardis::{ basic::{dto::TardisContext, result::TardisResult}, + chrono::Utc, db::sea_orm::{sea_query::Query, *}, log, tokio, TardisFunsInst, }; @@ -28,6 +29,7 @@ impl Default for MessageSendListener { impl MessageSendListener { async fn execute_send_account(&self, message: message::Model, template: message_template::Model) -> TardisResult<()> { + let content_replace: ContentReplace = message.content_replace.parse()?; let cfg = self.funs.conf::(); let _lock = self.sync.lock().await; let ctx = TardisContext { @@ -44,7 +46,7 @@ impl MessageSendListener { return Ok(()); } let mut to = HashSet::new(); - + let start_time = Utc::now(); let owner_path = rbum_scope_helper::get_pre_paths(RBUM_SCOPE_LEVEL_TENANT as i16, &message.own_paths).unwrap_or_default(); for account_id in message.to_res_ids.split(ACCOUNT_SPLIT) { if let Ok(mut resp) = iam_client.get_account(account_id, &owner_path).await { @@ -55,12 +57,47 @@ impl MessageSendListener { to.insert(phone); } } - match self.channel.send(message.rel_reach_channel, &template, &message.content_replace.parse()?, &to).await { + match self.channel.send(message.rel_reach_channel, &template, &content_replace, &to).await { Ok(_) => { ReachMessageServ::update_status(&message.id, ReachStatusKind::Sending, ReachStatusKind::SendSuccess, &self.funs, &ctx).await?; + for rel_account_id in to { + ReachMessageLogServ::add_rbum( + &mut ReachMsgLogAddReq { + rbum_add_req: Default::default(), + dnd_time: Default::default(), + rel_account_id, + dnd_strategy: ReachDndStrategyKind::Ignore, + start_time, + end_time: Utc::now(), + failure: false, + fail_message: Default::default(), + rel_reach_message_id: message.id.clone(), + }, + &self.funs, + &ctx, + ).await?; + } } Err(e) => { ReachMessageServ::update_status(&message.id, ReachStatusKind::Sending, ReachStatusKind::Fail, &self.funs, &ctx).await?; + for rel_account_id in to { + ReachMessageLogServ::add_rbum( + &mut ReachMsgLogAddReq { + rbum_add_req: Default::default(), + dnd_time: Default::default(), + rel_account_id, + dnd_strategy: ReachDndStrategyKind::Ignore, + start_time, + end_time: Utc::now(), + failure: true, + fail_message: e.to_string(), + rel_reach_message_id: message.id.clone(), + }, + &self.funs, + &ctx, + ).await?; + } + return Err(e); } } From 15de3c6dec9a77b04340d74ac9e86f6610aaf926 Mon Sep 17 00:00:00 2001 From: 4t145 Date: Thu, 19 Oct 2023 18:04:19 +0800 Subject: [PATCH 7/7] add more debug info and comments --- support/reach/src/task/message_send_listener.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/support/reach/src/task/message_send_listener.rs b/support/reach/src/task/message_send_listener.rs index 6486b0403..308f7b660 100644 --- a/support/reach/src/task/message_send_listener.rs +++ b/support/reach/src/task/message_send_listener.rs @@ -42,6 +42,7 @@ impl MessageSendListener { &ctx, cfg.invoke.module_urls.get("iam").expect("missing iam base url"), )); + // if not pending status, this task may be excuted by other nodes, just return if !ReachMessageServ::update_status(&message.id, ReachStatusKind::Pending, ReachStatusKind::Sending, &self.funs, &ctx).await? { return Ok(()); } @@ -55,6 +56,8 @@ impl MessageSendListener { continue; }; to.insert(phone); + } else { + log::warn!("[Reach] iam get account info error, account_id: {account_id}") } } match self.channel.send(message.rel_reach_channel, &template, &content_replace, &to).await {