From ca9033a23f8e63b9bb13086304e2d46f60ec193c Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sat, 15 Jun 2024 00:35:21 +0800 Subject: [PATCH] p1 --- .gitignore | 1 + ica-rs/src/data_struct/tailchat/messages.rs | 29 +++++++++++--- ica-rs/src/data_struct/tailchat/status.rs | 22 ++--------- ica-rs/src/ica/events.rs | 4 +- ica-rs/src/main.rs | 1 + ica-rs/src/py/call.rs | 42 +++++---------------- ica-rs/src/py/class/tailchat.rs | 14 +++---- ica-rs/src/tailchat.rs | 14 +++++-- ica-rs/src/tailchat/client.rs | 4 ++ ica-rs/src/tailchat/events.rs | 12 +++--- news.md | 11 ++++++ 11 files changed, 81 insertions(+), 73 deletions(-) diff --git a/.gitignore b/.gitignore index 87efd52..4282a3f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ env* config.toml +.idea *.pyc *__pycache__/ diff --git a/ica-rs/src/data_struct/tailchat/messages.rs b/ica-rs/src/data_struct/tailchat/messages.rs index 6ebd169..284d5f8 100644 --- a/ica-rs/src/data_struct/tailchat/messages.rs +++ b/ica-rs/src/data_struct/tailchat/messages.rs @@ -6,7 +6,7 @@ use serde_json::{json, Value as JsonValue}; use crate::data_struct::tailchat::{ConverseId, GroupId, MessageId, UserId}; #[derive(Serialize, Deserialize, Debug, Clone)] -pub struct ReciveMessage { +pub struct ReceiveMessage { /// 消息ID #[serde(rename = "_id")] pub msg_id: MessageId, @@ -27,7 +27,7 @@ pub struct ReciveMessage { pub has_recall: bool, /// 暂时懒得解析这玩意 /// 准确来说是不确定内容, 毕竟没细看 API - pub meta: JsonValue, + pub meta: Option, /// 也懒得解析这玩意 pub reactions: Vec, /// 创建时间 @@ -38,8 +38,14 @@ pub struct ReciveMessage { pub updated_at: String, } -impl ReciveMessage { - pub fn is_reply(&self) -> bool { self.meta.get("reply").is_some() } +impl ReceiveMessage { + pub fn is_reply(&self) -> bool { + if let Some(meta) = &self.meta { + meta.get("reply").is_some() + } else { + false + } + } /// 创建一个对这条消息的回复 pub fn as_reply(&self) -> SendingMessage { @@ -62,7 +68,7 @@ impl ReciveMessage { } } -impl Display for ReciveMessage { +impl Display for ReceiveMessage { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { // msgid|groupid-converseid|senderid|content write!( @@ -90,6 +96,8 @@ pub struct SendingMessage { /// 消息内容 /// /// 其实还有个 plain, 就是不知道干啥的 + /// + /// [img height=1329 width=1918]{BACKEND}/static/files/6602e20d7b8d10675758e36b/8db505b87bdf9fb309467abcec4d8e2a.png[/img] pub content: String, /// 会话ID #[serde(rename = "converseId")] @@ -99,6 +107,9 @@ pub struct SendingMessage { pub group_id: Option, /// 消息的元数据 pub meta: Option, + /// 额外携带的文件 + #[serde(skip)] + pub file: Option>, } impl SendingMessage { @@ -113,6 +124,7 @@ impl SendingMessage { converse_id, group_id, meta, + file: None, } } pub fn new_without_meta( @@ -125,8 +137,13 @@ impl SendingMessage { converse_id, group_id, meta: None, + file: 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 as_value(&self) -> JsonValue { serde_json::to_value(self).unwrap() } } @@ -143,7 +160,7 @@ pub struct ReplyMeta { } impl ReplyMeta { - pub fn from_recive_message(msg: &ReciveMessage) -> Self { + pub fn from_recive_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 4f9e438..f9c31c7 100644 --- a/ica-rs/src/data_struct/tailchat/status.rs +++ b/ica-rs/src/data_struct/tailchat/status.rs @@ -33,32 +33,18 @@ pub struct UpdateDMConverse { pub updated_at: String, } +#[allow(unused)] pub type Writeable = Arc>; #[derive(Debug, Clone)] pub struct BotStatus { - user_id: Writeable, + user_id: UserId, } impl BotStatus { - pub fn new(user_id: UserId) -> Self { - Self { - user_id: Arc::new(RwLock::new(user_id)), - } - } + pub fn new(user_id: UserId) -> Self { Self { user_id } } - pub async fn get_user_id(&self) -> UserId { self.user_id.read().await.clone() } - - pub fn block_get_user_id(&self) -> UserId { - tokio::task::block_in_place(|| { - let rt = tokio::runtime::Runtime::new().unwrap(); - rt.block_on(self.get_user_id()) - }) - } - - pub async fn set_user_id(&self, user_id: UserId) { - self.user_id.write().await.clone_from(&user_id); - } + pub fn get_user_id(&self) -> UserId { self.user_id.clone() } } #[derive(Debug, Clone)] diff --git a/ica-rs/src/ica/events.rs b/ica-rs/src/ica/events.rs index f573636..b6021f9 100644 --- a/ica-rs/src/ica/events.rs +++ b/ica-rs/src/ica/events.rs @@ -31,7 +31,7 @@ pub async fn add_message(payload: Payload, client: Client) { return; } - info!("add_message {}", message.to_string().cyan()); + event!(Level::INFO, "add_message {}", message.to_string().cyan()); // 就在这里处理掉最基本的消息 // 之后的处理交给插件 if !message.is_from_self() && !message.is_reply() { @@ -68,7 +68,7 @@ pub async fn delete_message(payload: Payload, client: Client) { // 消息 id if let Some(value) = values.first() { if let Some(msg_id) = value.as_str() { - info!("delete_message {}", msg_id.to_string().yellow()); + event!(Level::INFO, "delete_message {}", msg_id.to_string().yellow()); py::call::ica_delete_message_py(msg_id.to_string(), &client).await; } diff --git a/ica-rs/src/main.rs b/ica-rs/src/main.rs index 0643dcb..db11cf3 100644 --- a/ica-rs/src/main.rs +++ b/ica-rs/src/main.rs @@ -28,6 +28,7 @@ pub const VERSION: &str = env!("CARGO_PKG_VERSION"); pub const ICA_VERSION: &str = "1.6.0"; pub const TAILCHAT_VERSION: &str = "1.1.0"; +#[macro_export] macro_rules! async_callback_with_state { ($f:expr, $state:expr) => {{ use futures_util::FutureExt; diff --git a/ica-rs/src/py/call.rs b/ica-rs/src/py/call.rs index 5499419..7859c4a 100644 --- a/ica-rs/src/py/call.rs +++ b/ica-rs/src/py/call.rs @@ -1,9 +1,11 @@ use std::path::PathBuf; +use std::sync::Arc; use pyo3::prelude::*; use rust_socketio::asynchronous::Client; -use tracing::{info, warn}; +use tracing::{event, info, warn, Level}; +use crate::data_struct::tailchat::status::BotStatus; use crate::data_struct::{ica, tailchat}; use crate::error::PyPluginError; use crate::py::{class, PyPlugin, PyStatus}; @@ -106,7 +108,7 @@ macro_rules! call_py_func { $func_name.to_string(), $plugin_path.to_string_lossy().to_string(), ); - warn!("failed to call function<{}>: {:?}", $func_name, e); + event!(Level::WARN, "failed to call function<{}>: {:?}", $func_name, e); } } }) @@ -126,20 +128,6 @@ pub async fn ica_new_message_py(message: &ica::messages::NewMessage, client: &Cl let args = (msg, client); // 甚至实际上压根不需要await这个spawn, 直接让他自己跑就好了(离谱) call_py_func!(args, plugin, path, ICA_NEW_MESSAGE_FUNC, client); - // tokio::spawn(async move { - // Python::with_gil(|py| { - // if let Ok(py_func) = get_func(plugin.py_module.bind(py), ICA_NEW_MESSAGE_FUNC) { - // if let Err(e) = py_func.call1(args) { - // let e = PyPluginError::FuncCallError( - // e, - // ICA_NEW_MESSAGE_FUNC.to_string(), - // path.to_string_lossy().to_string(), - // ); - // warn!("failed to call function<{}>: {:?}", ICA_NEW_MESSAGE_FUNC, e); - // } - // } - // }) - // }); } } @@ -152,29 +140,19 @@ pub async fn ica_delete_message_py(msg_id: ica::MessageId, client: &Client) { let client = class::ica::IcaClientPy::new(client); let args = (msg_id.clone(), client); call_py_func!(args, plugin, path, ICA_DELETE_MESSAGE_FUNC, client); - // tokio::spawn(async move { - // Python::with_gil(|py| { - // if let Ok(py_func) = get_func(plugin.py_module.bind(py), ICA_DELETE_MESSAGE_FUNC) { - // if let Err(e) = py_func.call1(args) { - // let e = PyPluginError::FuncCallError( - // e, - // ICA_DELETE_MESSAGE_FUNC.to_string(), - // path.to_string_lossy().to_string(), - // ); - // warn!("failed to call function<{}>: {:?}", ICA_DELETE_MESSAGE_FUNC, e); - // } - // } - // }) - // }); } } -pub async fn tailchat_new_message_py(message: &tailchat::messages::ReciveMessage, client: &Client) { +pub async fn tailchat_new_message_py( + message: &tailchat::messages::ReceiveMessage, + client: &Client, + status: Arc, +) { verify_plugins(); let plugins = PyStatus::get_files(); for (path, plugin) in plugins.iter() { - let msg = class::tailchat::TailchatReciveMessagePy::from_recive_message(message); + let msg = class::tailchat::TailchatReceiveMessagePy::from_recive_message(message); let client = class::tailchat::TailchatClientPy::new(client); let args = (msg, client); call_py_func!(args, plugin, path, TAILCHAT_NEW_MESSAGE_FUNC, client); diff --git a/ica-rs/src/py/class/tailchat.rs b/ica-rs/src/py/class/tailchat.rs index 978682b..57a4f1e 100644 --- a/ica-rs/src/py/class/tailchat.rs +++ b/ica-rs/src/py/class/tailchat.rs @@ -3,7 +3,7 @@ use pyo3::prelude::*; use rust_socketio::asynchronous::Client; use tracing::{debug, info, warn}; -use crate::data_struct::tailchat::messages::{ReciveMessage, SendingMessage}; +use crate::data_struct::tailchat::messages::{ReceiveMessage, SendingMessage}; use crate::data_struct::tailchat::{ConverseId, GroupId, MessageId, UserId}; use crate::tailchat::client::send_message; @@ -27,13 +27,13 @@ impl TailchatClientPy { pub struct TailchatStatusPy {} #[pyclass] -#[pyo3(name = "TailchatReciveMessage")] -pub struct TailchatReciveMessagePy { - pub message: ReciveMessage, +#[pyo3(name = "TailchatReceiveMessage")] +pub struct TailchatReceiveMessagePy { + pub message: ReceiveMessage, } -impl TailchatReciveMessagePy { - pub fn from_recive_message(msg: &ReciveMessage) -> Self { +impl TailchatReceiveMessagePy { + pub fn from_recive_message(msg: &ReceiveMessage) -> Self { Self { message: msg.clone(), } @@ -76,7 +76,7 @@ impl TailchatClientPy { } #[pymethods] -impl TailchatReciveMessagePy { +impl TailchatReceiveMessagePy { #[getter] pub fn get_is_reply(&self) -> bool { self.message.is_reply() } #[getter] diff --git a/ica-rs/src/tailchat.rs b/ica-rs/src/tailchat.rs index 532a632..becf03a 100644 --- a/ica-rs/src/tailchat.rs +++ b/ica-rs/src/tailchat.rs @@ -1,6 +1,8 @@ pub mod client; pub mod events; +use std::sync::Arc; + use colored::Colorize; use md5::{Digest, Md5}; use reqwest::ClientBuilder as reqwest_ClientBuilder; @@ -11,9 +13,9 @@ use serde_json::{json, Value}; use tracing::{event, span, Level}; use crate::config::TailchatConfig; -use crate::data_struct::tailchat::status::LoginData; +use crate::data_struct::tailchat::status::{BotStatus, LoginData}; use crate::error::{ClientResult, TailchatError}; -use crate::StopGetter; +use crate::{async_callback_with_state, StopGetter}; pub async fn start_tailchat( config: TailchatConfig, @@ -60,11 +62,17 @@ pub async fn start_tailchat( Err(e) => return Err(TailchatError::LoginFailed(e.to_string())), }; + let sharded_status = BotStatus::new(status.user_id.clone()); + let sharded_status = Arc::new(sharded_status); + let socket = ClientBuilder::new(config.host) .auth(json!({"token": status.jwt.clone()})) .transport_type(TransportType::Websocket) .on_any(async_any_callback!(events::any_event)) - .on("notify:chat.message.add", async_callback!(events::on_message)) + .on( + "notify:chat.message.add", + async_callback_with_state!(events::on_message, sharded_status.clone()), + ) .on("notify:chat.message.delete", async_callback!(events::on_msg_delete)) .on( "notify:chat.converse.updateDMConverse", diff --git a/ica-rs/src/tailchat/client.rs b/ica-rs/src/tailchat/client.rs index 5fd363a..3110875 100644 --- a/ica-rs/src/tailchat/client.rs +++ b/ica-rs/src/tailchat/client.rs @@ -8,6 +8,10 @@ use serde_json::{json, Value}; use tracing::{debug, info, warn}; pub async fn send_message(client: &Client, message: &SendingMessage) -> bool { + if message.contain_file() { + // 处理文件 + + } let value: Value = message.as_value(); match client.emit("chat.message.sendMessage", value).await { Ok(_) => { diff --git a/ica-rs/src/tailchat/events.rs b/ica-rs/src/tailchat/events.rs index f10b886..cc1c48e 100644 --- a/ica-rs/src/tailchat/events.rs +++ b/ica-rs/src/tailchat/events.rs @@ -1,10 +1,12 @@ +use std::sync::Arc; + use colored::Colorize; use rust_socketio::asynchronous::Client; use rust_socketio::{Event, Payload}; use tracing::info; -use crate::data_struct::tailchat::messages::ReciveMessage; -use crate::data_struct::tailchat::status::UpdateDMConverse; +use crate::data_struct::tailchat::messages::ReceiveMessage; +use crate::data_struct::tailchat::status::{BotStatus, UpdateDMConverse}; use crate::tailchat::client::{emit_join_room, send_message}; /// 所有 @@ -60,10 +62,10 @@ pub async fn any_event(event: Event, payload: Payload, _client: Client) { } #[allow(clippy::collapsible_if)] -pub async fn on_message(payload: Payload, client: Client) { +pub async fn on_message(payload: Payload, client: Client, status: Arc) { if let Payload::Text(values) = payload { if let Some(value) = values.first() { - let message: ReciveMessage = match serde_json::from_value(value.clone()) { + let message: ReceiveMessage = match serde_json::from_value(value.clone()) { Ok(v) => v, Err(e) => { info!("tailchat_msg {}", value.to_string().red()); @@ -83,7 +85,7 @@ pub async fn on_message(payload: Payload, client: Client) { send_message(&client, &reply).await; } } - crate::py::call::tailchat_new_message_py(&message, &client).await; + crate::py::call::tailchat_new_message_py(&message, &client, status.clone()).await; } } } diff --git a/news.md b/news.md index b074558..a1d89f1 100644 --- a/news.md +++ b/news.md @@ -1,5 +1,16 @@ # 更新日志 +## 0.6.8 + +- 修复了一堆拼写错误 +- 太难绷了 + - `TailchatReciveMessagePy` -> `TailchatReceiveMessagePy` + - `ReciveMessage` -> `ReceiveMessage` +- `ReceiveMessage::meta` + - 从 `JsonValue` 改成 `Option` + - 用来解决发图片的时候没有 `meta` 字段的问题 +- 去除了 + ## 0.6.7 游学回来啦