diff --git a/ica-rs/src/events.rs b/ica-rs/src/events.rs index 1c1c3fc..b5c4647 100644 --- a/ica-rs/src/events.rs +++ b/ica-rs/src/events.rs @@ -5,7 +5,7 @@ use tracing::{info, warn}; use crate::client::{send_message, IcalinguaStatus}; use crate::data_struct::all_rooms::Room; -use crate::data_struct::messages::NewMessage; +use crate::data_struct::messages::{MessageTrait, NewMessage}; use crate::data_struct::online_data::OnlineData; use crate::{py, VERSION}; @@ -26,19 +26,19 @@ 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 message = NewMessage::new_from_json(value); + let message: NewMessage = serde_json::from_value(value.clone()).unwrap(); // 检测是否在过滤列表内 if IcalinguaStatus::get_config() .filter_list - .contains(&message.sender_id) + .contains(&message.msg.sender_id) { return; } - info!("add_message {}", message.output().cyan()); + info!("add_message {}", message.to_string().cyan()); // info!("add_message {}", format!("{:#?}", message).cyan()); // 就在这里处理掉最基本的消息 // 之后的处理交给插件 - if message.content.eq("/bot-rs") && !message.is_from_self() && !message.is_reply() { + if message.content().eq("/bot-rs") && !message.is_from_self() && !message.is_reply() { let reply = message.reply_with(&format!("ica-async-rs pong v{}", VERSION)); send_message(&client, &reply).await; } @@ -48,6 +48,19 @@ pub async fn add_message(payload: Payload, client: Client) { } } +/// 理论上不会用到 (因为依赖一个客户端去请求) +/// 但反正实际上还是我去请求, 所以只是暂时 +/// 加载一个房间的所有消息 +pub async fn set_messages(payload: Payload, _client: Client) { + if let Payload::Text(values) = payload { + if let Some(value) = values.first() { + let messages: Vec = serde_json::from_value(value.clone()).unwrap(); + let room_id = value["roomId"].as_i64().unwrap(); + info!("set_messages {} len: {}", room_id.to_string().cyan(), messages.len()); + } + } +} + /// 撤回消息 pub async fn delete_message(payload: Payload, client: Client) { if let Payload::Text(values) = payload { @@ -66,10 +79,8 @@ pub async fn update_all_room(payload: Payload, _client: Client) { if let Payload::Text(values) = payload { if let Some(value) = values.first() { if let Some(raw_rooms) = value.as_array() { - let rooms: Vec = raw_rooms - .iter() - .map(|room| Room::new_from_json(room)) - .collect(); + let rooms: Vec = + raw_rooms.iter().map(|room| Room::new_from_json(room)).collect(); unsafe { crate::ClientStatus.update_rooms(rooms.clone()); } @@ -107,6 +118,7 @@ pub async fn any_event(event: Event, payload: Payload, _client: Client) { "addMessage", "deleteMessage", "setAllRooms", + "setMessages", // 也许以后会用到 "messageSuccess", "messageFailed", diff --git a/ica-rs/src/py/call.rs b/ica-rs/src/py/call.rs index 9aa4ef0..0c92f07 100644 --- a/ica-rs/src/py/call.rs +++ b/ica-rs/src/py/call.rs @@ -1,17 +1,15 @@ - - use std::path::PathBuf; -use tracing::{warn, debug}; use pyo3::prelude::*; use rust_socketio::asynchronous::Client; +use tracing::{debug, warn}; -use crate::data_struct::MessageId; use crate::data_struct::messages::NewMessage; +use crate::data_struct::MessageId; use crate::py::{class, verify_plugins, PyStatus}; pub fn get_func<'a>(py_module: &'a PyAny, path: &PathBuf, name: &'a str) -> Option<&'a PyAny> { - // 要处理的情况: + // 要处理的情况: // 1. 有这个函数 // 2. 没有这个函数 // 3. 函数不是 Callable @@ -26,7 +24,7 @@ pub fn get_func<'a>(py_module: &'a PyAny, path: &PathBuf, name: &'a str) -> Opti warn!("function<{}>: {:#?} in {:?} is not callable", name, func, path); None } - }, + } Err(e) => { warn!("failed to get function<{}> from {:?}: {:?}", name, path, e); None diff --git a/ica-rs/src/py/class.rs b/ica-rs/src/py/class.rs index 2f15b20..5732fb4 100644 --- a/ica-rs/src/py/class.rs +++ b/ica-rs/src/py/class.rs @@ -4,7 +4,7 @@ use tokio::runtime::Runtime; use tracing::{debug, info, warn}; use crate::client::send_message; -use crate::data_struct::messages::{NewMessage, ReplyMessage, SendMessage}; +use crate::data_struct::messages::{MessageTrait, NewMessage, ReplyMessage, SendMessage}; use crate::data_struct::MessageId; use crate::ClientStatus; @@ -15,14 +15,10 @@ pub struct IcaStatusPy {} #[pymethods] impl IcaStatusPy { #[new] - pub fn py_new() -> Self { - Self {} - } + pub fn py_new() -> Self { Self {} } #[getter] - pub fn get_login(&self) -> bool { - unsafe { ClientStatus.login } - } + pub fn get_login(&self) -> bool { unsafe { ClientStatus.login } } #[getter] pub fn get_online(&self) -> bool { @@ -106,9 +102,7 @@ impl IcaStatusPy { } impl IcaStatusPy { - pub fn new() -> Self { - Self {} - } + pub fn new() -> Self { Self {} } } #[derive(Clone)] @@ -124,35 +118,21 @@ impl NewMessagePy { SendMessagePy::new(self.msg.reply_with(&content)) } - pub fn __str__(&self) -> String { - format!("{:?}", self.msg) - } + pub fn __str__(&self) -> String { format!("{:?}", self.msg) } #[getter] - pub fn get_id(&self) -> MessageId { - self.msg.msg_id.clone() - } + pub fn get_id(&self) -> MessageId { self.msg.msg_id().clone() } #[getter] - pub fn get_content(&self) -> String { - self.msg.content.clone() - } + pub fn get_content(&self) -> String { self.msg.content().clone() } #[getter] - pub fn get_sender_id(&self) -> i64 { - self.msg.sender_id - } + pub fn get_sender_id(&self) -> i64 { self.msg.sender_id() } #[getter] - pub fn get_is_from_self(&self) -> bool { - self.msg.is_from_self() - } + pub fn get_is_from_self(&self) -> bool { self.msg.is_from_self() } #[getter] - pub fn get_is_reply(&self) -> bool { - self.msg.is_reply() - } + pub fn get_is_reply(&self) -> bool { self.msg.is_reply() } } impl NewMessagePy { - pub fn new(msg: &NewMessage) -> Self { - Self { msg: msg.clone() } - } + pub fn new(msg: &NewMessage) -> Self { Self { msg: msg.clone() } } } #[pyclass] @@ -163,15 +143,11 @@ pub struct ReplyMessagePy { #[pymethods] impl ReplyMessagePy { - pub fn __str__(&self) -> String { - format!("{:?}", self.msg) - } + pub fn __str__(&self) -> String { format!("{:?}", self.msg) } } impl ReplyMessagePy { - pub fn new(msg: ReplyMessage) -> Self { - Self { msg } - } + pub fn new(msg: ReplyMessage) -> Self { Self { msg } } } #[derive(Clone)] @@ -183,29 +159,21 @@ pub struct SendMessagePy { #[pymethods] impl SendMessagePy { - pub fn __str__(&self) -> String { - format!("{:?}", self.msg) - } + pub fn __str__(&self) -> String { format!("{:?}", self.msg) } /// 设置消息内容 - /// 用于链式调用 + /// 用于链式调用 pub fn with_content(&mut self, content: String) -> Self { self.msg.content = content; self.clone() } #[getter] - pub fn get_content(&self) -> String { - self.msg.content.clone() - } + pub fn get_content(&self) -> String { self.msg.content.clone() } #[setter] - pub fn set_content(&mut self, content: String) { - self.msg.content = content; - } + pub fn set_content(&mut self, content: String) { self.msg.content = content; } } impl SendMessagePy { - pub fn new(msg: SendMessage) -> Self { - Self { msg } - } + pub fn new(msg: SendMessage) -> Self { Self { msg } } } #[derive(Clone)] @@ -239,9 +207,7 @@ impl IcaClientPy { } #[getter] - pub fn get_status(&self) -> IcaStatusPy { - IcaStatusPy::new() - } + pub fn get_status(&self) -> IcaStatusPy { IcaStatusPy::new() } pub fn debug(&self, content: String) { debug!("{}", content);