修复了Message解析replyMessage字段为null时解析失败的问题
This commit is contained in:
parent
8448b03d83
commit
5381ef598a
@ -17,29 +17,6 @@ pub enum At {
|
||||
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 {
|
||||
/// 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<ReplyMessage> = match json.get("replyMessage") {
|
||||
Some(value) => match serde_json::from_value::<ReplyMessage>(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::<ReplyMessage>(value.clone()) {
|
||||
Ok(reply) => Some(reply),
|
||||
Err(e) => {
|
||||
warn!("Failed to parse reply message: {}", e);
|
||||
None
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
},
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
// At
|
||||
|
@ -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<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 {
|
||||
fn is_reply(&self) -> bool;
|
||||
fn is_from_self(&self) -> bool {
|
||||
|
@ -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<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();
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user