diff --git a/ica-rs/src/data_struct/messages.rs b/ica-rs/src/data_struct/messages.rs index 54b30fa..28ab063 100644 --- a/ica-rs/src/data_struct/messages.rs +++ b/ica-rs/src/data_struct/messages.rs @@ -17,29 +17,6 @@ pub enum At { None, } -impl Serialize for At { - fn serialize(&self, serializer: S) -> Result - where - S: serde::ser::Serializer, - { - match self { - At::All => serializer.serialize_str("all"), - At::Bool(b) => serializer.serialize_bool(*b), - At::None => serializer.serialize_none(), - } - } -} - -impl<'de> Deserialize<'de> for At { - fn deserialize(deserializer: D) -> Result - where - D: serde::de::Deserializer<'de>, - { - let value = JsonValue::deserialize(deserializer)?; - Ok(At::new_from_json(&value)) - } -} - impl At { /// new_from_json(&message["at"]) pub fn new_from_json(json: &JsonValue) -> Self { @@ -160,11 +137,6 @@ pub struct Message { impl Message { pub fn new_from_json(json: &JsonValue) -> Self { - // // room id 还是必定有的 - // 但是他现在是 Message 了, Message 没有 room_id - // let room_id = json["roomId"].as_i64().unwrap(); - // message 本体也是 - // let json = json.get("message").unwrap(); // 消息 id let msg_id = json["_id"].as_str().unwrap(); // 发送者 id (Optional) @@ -195,13 +167,19 @@ impl Message { } // 回复的消息 let reply: Option = match json.get("replyMessage") { - Some(value) => match serde_json::from_value::(value.clone()) { - Ok(reply) => Some(reply), - Err(e) => { - warn!("Failed to parse reply message: {}", e); + Some(value) => { + if !value.is_null() { + match serde_json::from_value::(value.clone()) { + Ok(reply) => Some(reply), + Err(e) => { + warn!("Failed to parse reply message: {}", e); + None + } + } + } else { None } - }, + } None => None, }; // At diff --git a/ica-rs/src/data_struct/messages/msg_trait.rs b/ica-rs/src/data_struct/messages/msg_trait.rs index ab5d2ff..1efcd23 100644 --- a/ica-rs/src/data_struct/messages/msg_trait.rs +++ b/ica-rs/src/data_struct/messages/msg_trait.rs @@ -1,13 +1,36 @@ use std::fmt::Display; use chrono::NaiveDateTime; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use serde_json::Value as JsonValue; use crate::client::IcalinguaStatus; -use crate::data_struct::messages::{Message, NewMessage}; +use crate::data_struct::messages::{At, Message, NewMessage}; use crate::data_struct::{MessageId, UserId}; +impl Serialize for At { + fn serialize(&self, serializer: S) -> Result + where + S: serde::ser::Serializer, + { + match self { + At::All => serializer.serialize_str("all"), + At::Bool(b) => serializer.serialize_bool(*b), + At::None => serializer.serialize_none(), + } + } +} + +impl<'de> Deserialize<'de> for At { + fn deserialize(deserializer: D) -> Result + where + D: serde::de::Deserializer<'de>, + { + let value = JsonValue::deserialize(deserializer)?; + Ok(At::new_from_json(&value)) + } +} + pub trait MessageTrait { fn is_reply(&self) -> bool; fn is_from_self(&self) -> bool { diff --git a/ica-rs/src/events.rs b/ica-rs/src/events.rs index fd4e77b..b6574a9 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::{MessageTrait, NewMessage}; +use crate::data_struct::messages::{Message, MessageTrait, NewMessage}; use crate::data_struct::online_data::OnlineData; use crate::{py, VERSION}; @@ -51,7 +51,8 @@ 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(); + println!("{:#?}", value); + let messages: Vec = serde_json::from_value(value["messages"].clone()).unwrap(); let room_id = value["roomId"].as_i64().unwrap(); info!("set_messages {} len: {}", room_id.to_string().cyan(), messages.len()); } @@ -119,6 +120,7 @@ pub async fn any_event(event: Event, payload: Payload, _client: Client) { // 也许以后会用到 "messageSuccess", "messageFailed", + "setAllChatGroups", // 忽略的 "notify", "closeLoading", // 发送消息/加载新聊天 有一个 loading diff --git a/news.md b/news.md index 820af6a..ee28c5e 100644 --- a/news.md +++ b/news.md @@ -8,6 +8,8 @@ 添加了 `rustfmt.toml` 用于格式化代码 **注意**: 请在提交代码前使用 `cargo +nightly fmt` 格式化代码 +修复了 `Message` 解析 `replyMessage` 字段是 如果是 null 则会解析失败的问题 + ## 0.4.9 修复了 Python 插件运行错误会导致整个程序崩溃的问题