修复了Message解析replyMessage字段为null时解析失败的问题

This commit is contained in:
shenjack 2024-02-25 01:56:47 +08:00
parent 8448b03d83
commit 5381ef598a
Signed by: shenjack
GPG Key ID: 7B1134A979775551
4 changed files with 42 additions and 37 deletions

View File

@ -17,29 +17,6 @@ pub enum At {
None, None,
} }
impl Serialize for At {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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<D>(deserializer: D) -> Result<At, D::Error>
where
D: serde::de::Deserializer<'de>,
{
let value = JsonValue::deserialize(deserializer)?;
Ok(At::new_from_json(&value))
}
}
impl At { impl At {
/// new_from_json(&message["at"]) /// new_from_json(&message["at"])
pub fn new_from_json(json: &JsonValue) -> Self { pub fn new_from_json(json: &JsonValue) -> Self {
@ -160,11 +137,6 @@ pub struct Message {
impl Message { impl Message {
pub fn new_from_json(json: &JsonValue) -> Self { 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 // 消息 id
let msg_id = json["_id"].as_str().unwrap(); let msg_id = json["_id"].as_str().unwrap();
// 发送者 id (Optional) // 发送者 id (Optional)
@ -195,13 +167,19 @@ impl Message {
} }
// 回复的消息 // 回复的消息
let reply: Option<ReplyMessage> = match json.get("replyMessage") { let reply: Option<ReplyMessage> = match json.get("replyMessage") {
Some(value) => match serde_json::from_value::<ReplyMessage>(value.clone()) { Some(value) => {
if !value.is_null() {
match serde_json::from_value::<ReplyMessage>(value.clone()) {
Ok(reply) => Some(reply), Ok(reply) => Some(reply),
Err(e) => { Err(e) => {
warn!("Failed to parse reply message: {}", e); warn!("Failed to parse reply message: {}", e);
None None
} }
}, }
} else {
None
}
}
None => None, None => None,
}; };
// At // At

View File

@ -1,13 +1,36 @@
use std::fmt::Display; use std::fmt::Display;
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use serde::Deserialize; use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue; use serde_json::Value as JsonValue;
use crate::client::IcalinguaStatus; 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}; use crate::data_struct::{MessageId, UserId};
impl Serialize for At {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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<D>(deserializer: D) -> Result<At, D::Error>
where
D: serde::de::Deserializer<'de>,
{
let value = JsonValue::deserialize(deserializer)?;
Ok(At::new_from_json(&value))
}
}
pub trait MessageTrait { pub trait MessageTrait {
fn is_reply(&self) -> bool; fn is_reply(&self) -> bool;
fn is_from_self(&self) -> bool { fn is_from_self(&self) -> bool {

View File

@ -5,7 +5,7 @@ use tracing::{info, warn};
use crate::client::{send_message, IcalinguaStatus}; use crate::client::{send_message, IcalinguaStatus};
use crate::data_struct::all_rooms::Room; 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::data_struct::online_data::OnlineData;
use crate::{py, VERSION}; 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) { pub async fn set_messages(payload: Payload, _client: Client) {
if let Payload::Text(values) = payload { if let Payload::Text(values) = payload {
if let Some(value) = values.first() { if let Some(value) = values.first() {
let messages: Vec<NewMessage> = serde_json::from_value(value.clone()).unwrap(); println!("{:#?}", value);
let messages: Vec<Message> = serde_json::from_value(value["messages"].clone()).unwrap();
let room_id = value["roomId"].as_i64().unwrap(); let room_id = value["roomId"].as_i64().unwrap();
info!("set_messages {} len: {}", room_id.to_string().cyan(), messages.len()); 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", "messageSuccess",
"messageFailed", "messageFailed",
"setAllChatGroups",
// 忽略的 // 忽略的
"notify", "notify",
"closeLoading", // 发送消息/加载新聊天 有一个 loading "closeLoading", // 发送消息/加载新聊天 有一个 loading

View File

@ -8,6 +8,8 @@
添加了 `rustfmt.toml` 用于格式化代码 添加了 `rustfmt.toml` 用于格式化代码
**注意**: 请在提交代码前使用 `cargo +nightly fmt` 格式化代码 **注意**: 请在提交代码前使用 `cargo +nightly fmt` 格式化代码
修复了 `Message` 解析 `replyMessage` 字段是 如果是 null 则会解析失败的问题
## 0.4.9 ## 0.4.9
修复了 Python 插件运行错误会导致整个程序崩溃的问题 修复了 Python 插件运行错误会导致整个程序崩溃的问题