From b1511a972f7bf738f358e9dd487907aa74fbce75 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sat, 15 Jun 2024 01:34:03 +0800 Subject: [PATCH] p3 --- ica-rs/src/data_struct/tailchat/messages.rs | 57 +++++++++++++++++---- ica-rs/src/data_struct/tailchat/status.rs | 15 ++++++ ica-rs/src/ica.rs | 2 +- ica-rs/src/ica/events.rs | 4 +- ica-rs/src/main.rs | 6 +-- ica-rs/src/tailchat.rs | 2 + ica-rs/src/tailchat/client.rs | 2 +- 7 files changed, 73 insertions(+), 15 deletions(-) diff --git a/ica-rs/src/data_struct/tailchat/messages.rs b/ica-rs/src/data_struct/tailchat/messages.rs index deb200f..0ac68f4 100644 --- a/ica-rs/src/data_struct/tailchat/messages.rs +++ b/ica-rs/src/data_struct/tailchat/messages.rs @@ -53,7 +53,7 @@ impl ReceiveMessage { "".to_string(), self.converse_id.clone(), self.group_id.clone(), - Some(ReplyMeta::from_recive_message(self)), + Some(ReplyMeta::from_receive_message(self)), ) } @@ -63,7 +63,7 @@ impl ReceiveMessage { content.to_string(), self.converse_id.clone(), self.group_id.clone(), - Some(ReplyMeta::from_recive_message(self)), + Some(ReplyMeta::from_receive_message(self)), ) } } @@ -79,6 +79,47 @@ impl Display for ReceiveMessage { } } +#[derive(Debug, Clone, Default)] +pub enum SendingFile { + #[default] + None, + /// 需要生成 + /// [img height=1329 width=1918]{BACKEND}/static/files/6602e20d7b8d10675758e36b/8db505b87bdf9fb309467abcec4d8e2a.png[/img] + Image { + file: Vec, + width: u32, + height: u32, + }, + /// [card type=file url={BACKEND}/static/files/6602e20d7b8d10675758e36b/9df28943d17b9713cb0ea9625f37d015.wav]Engine.wav[/card] + File { file: Vec, name: String }, +} + +impl SendingFile { + pub fn is_some(&self) -> bool { !matches!(self, Self::None) } + pub fn is_image(&self) -> bool { matches!(self, Self::Image { .. }) } + pub fn is_file(&self) -> bool { matches!(self, Self::File { .. }) } + // pub fn gen_msg(&self, file_path: &str) -> String { + // match self { + // Self::Image { _file, width, height } => { + // format!( + // "[img height={} width={}]{{BACKEND}}/static/files/{}[/img]", + // height, + // width, + // file_path + // ) + // } + // Self::File { _file, name } => { + // format!( + // "[file name={}]{{BACKEND}}/static/files/{}[/file]", + // name, + // file_path + // ) + // } + // _ => "".to_string(), + // } + // } +} + #[derive(Debug, Clone, Serialize)] /// 将要发送的消息 /// @@ -96,8 +137,6 @@ pub struct SendingMessage { /// 消息内容 /// /// 其实还有个 plain, 就是不知道干啥的 - /// - /// [img height=1329 width=1918]{BACKEND}/static/files/6602e20d7b8d10675758e36b/8db505b87bdf9fb309467abcec4d8e2a.png[/img] pub content: String, /// 会话ID #[serde(rename = "converseId")] @@ -109,7 +148,7 @@ pub struct SendingMessage { pub meta: Option, /// 额外携带的文件 #[serde(skip)] - pub file: Option>, + pub file: SendingFile, } impl SendingMessage { @@ -124,7 +163,7 @@ impl SendingMessage { converse_id, group_id, meta, - file: None, + file: SendingFile::None, } } pub fn new_without_meta( @@ -137,12 +176,12 @@ impl SendingMessage { converse_id, group_id, meta: None, - file: None, + file: SendingFile::None, } } pub fn contain_file(&self) -> bool { self.file.is_some() } - pub fn add_img(&mut self, file: Vec) { self.file = Some(file); } + pub fn add_img(&mut self, file: SendingFile) { self.file = file; } pub fn as_value(&self) -> JsonValue { serde_json::to_value(self).unwrap() } } @@ -160,7 +199,7 @@ pub struct ReplyMeta { } impl ReplyMeta { - pub fn from_recive_message(msg: &ReceiveMessage) -> Self { + pub fn from_receive_message(msg: &ReceiveMessage) -> Self { Self { mentions: vec![msg.sender_id.clone()], reply_id: msg.msg_id.clone(), diff --git a/ica-rs/src/data_struct/tailchat/status.rs b/ica-rs/src/data_struct/tailchat/status.rs index f9c31c7..3ee468c 100644 --- a/ica-rs/src/data_struct/tailchat/status.rs +++ b/ica-rs/src/data_struct/tailchat/status.rs @@ -15,6 +15,21 @@ pub struct LoginData { pub avatar: String, } +impl LoginData { + pub fn update_to_global(&self) { + let status = crate::status::tailchat::MainStatus { + enable: true, + login: true, + user_id: self.user_id.clone(), + nick_name: self.nickname.clone(), + email: self.email.clone(), + jwt_token: self.jwt.clone(), + avatar: self.avatar.clone(), + }; + crate::MainStatus::update_tailchat_status(status); + } +} + #[derive(Debug, Clone, Deserialize, Serialize)] pub struct UpdateDMConverse { /// 会话ID diff --git a/ica-rs/src/ica.rs b/ica-rs/src/ica.rs index 0fb90e0..27923ba 100644 --- a/ica-rs/src/ica.rs +++ b/ica-rs/src/ica.rs @@ -25,7 +25,7 @@ pub async fn start_ica(config: &IcaConfig, stop_reciver: StopGetter) -> ClientRe .on("message", async_callback!(events::connect_callback)) .on("authSucceed", async_callback!(events::connect_callback)) .on("authFailed", async_callback!(events::connect_callback)) - .on("messageSuccess", async_callback!(events::succes_message)) + .on("messageSuccess", async_callback!(events::success_message)) .on("messageFailed", async_callback!(events::failed_message)) .on("onlineData", async_callback!(events::get_online_data)) .on("setAllRooms", async_callback!(events::update_all_room)) diff --git a/ica-rs/src/ica/events.rs b/ica-rs/src/ica/events.rs index b6021f9..a4fa311 100644 --- a/ica-rs/src/ica/events.rs +++ b/ica-rs/src/ica/events.rs @@ -25,6 +25,8 @@ pub async fn get_online_data(payload: Payload, _client: Client) { pub async fn add_message(payload: Payload, client: Client) { if let Payload::Text(values) = payload { if let Some(value) = values.first() { + let span = span!(Level::INFO, "ica add_message"); + let _enter = span.enter(); let message: NewMessage = serde_json::from_value(value.clone()).unwrap(); // 检测是否在过滤列表内 if MainStatus::global_config().ica().filter_list.contains(&message.msg.sender_id) { @@ -88,7 +90,7 @@ pub async fn update_all_room(payload: Payload, _client: Client) { } } -pub async fn succes_message(payload: Payload, _client: Client) { +pub async fn success_message(payload: Payload, _client: Client) { if let Payload::Text(values) = payload { if let Some(value) = values.first() { info!("messageSuccess {}", value.to_string().green()); diff --git a/ica-rs/src/main.rs b/ica-rs/src/main.rs index 5f660df..5e28f34 100644 --- a/ica-rs/src/main.rs +++ b/ica-rs/src/main.rs @@ -12,7 +12,7 @@ mod ica; mod tailchat; use config::BotConfig; -use tracing::{event, info, span, Level}; +use tracing::{event, span, Level}; pub static mut MAIN_STATUS: status::BotStatus = status::BotStatus { config: None, @@ -104,14 +104,14 @@ async fn main() { tokio::time::sleep(Duration::from_secs(2)).await; // 等待一个输入 - info!("Press any key to exit"); + event!(Level::INFO, "Press any key to exit"); let mut input = String::new(); std::io::stdin().read_line(&mut input).unwrap(); ica_send.send(()).ok(); tailchat_send.send(()).ok(); - info!("Disconnected"); + event!(Level::INFO, "Disconnected"); } #[allow(dead_code, unused_variables)] diff --git a/ica-rs/src/tailchat.rs b/ica-rs/src/tailchat.rs index b70b803..ff047d0 100644 --- a/ica-rs/src/tailchat.rs +++ b/ica-rs/src/tailchat.rs @@ -62,6 +62,8 @@ pub async fn start_tailchat( Err(e) => return Err(TailchatError::LoginFailed(e.to_string())), }; + status.update_to_global(); + let sharded_status = BotStatus::new(status.user_id.clone()); let sharded_status = Arc::new(sharded_status); diff --git a/ica-rs/src/tailchat/client.rs b/ica-rs/src/tailchat/client.rs index cf0a49f..0cb6fd4 100644 --- a/ica-rs/src/tailchat/client.rs +++ b/ica-rs/src/tailchat/client.rs @@ -18,7 +18,7 @@ pub async fn send_message(client: &Client, message: &SendingMessage) -> bool { true } Err(e) => { - warn!("send_message faild:{}", format!("{:#?}", e).red()); + warn!("send_message failed:{}", format!("{:#?}", e).red()); false } }