diff --git a/ica-rs/src/data_struct/all_rooms.rs b/ica-rs/src/data_struct/all_rooms.rs index 90a00c5..7a44d8f 100644 --- a/ica-rs/src/data_struct/all_rooms.rs +++ b/ica-rs/src/data_struct/all_rooms.rs @@ -1,6 +1,7 @@ use crate::data_struct::messages::{At, LastMessage}; use crate::data_struct::RoomId; +use serde::{Deserialize, Serialize}; use serde_json::Value as JsonValue; /// export default interface Room { @@ -28,9 +29,54 @@ pub struct Room { pub utime: i64, /// 我严重怀疑是脱裤子放屁 /// 历史遗留啊,那没事了() - pub users: JsonValue, + // pub users: JsonValue, pub at: At, pub last_message: LastMessage, - pub auto_donwload: String, - pub download_path: String, + pub auto_download: Option, + pub download_path: Option, +} + +impl Room { + pub fn new_from_json(json: &JsonValue) -> Self { + let inner = serde_json::from_value::(json.clone()).unwrap(); + let at = At::new_from_json(&json["at"]); + Self { + room_id: inner.room_id, + room_name: inner.room_name, + index: inner.index, + unread_count: inner.unread_count, + priority: inner.priority, + utime: inner.utime, + // users: inner.users, + at, + last_message: inner.last_message, + auto_download: inner.auto_download, + download_path: inner.download_path, + } + } +} + +#[derive(Debug, Clone, Deserialize, Serialize)] +struct InnerRoom { + #[serde(rename = "roomId")] + pub room_id: RoomId, + #[serde(rename = "roomName")] + pub room_name: String, + #[serde(rename = "index")] + pub index: i64, + #[serde(rename = "unreadCount")] + pub unread_count: u64, + #[serde(rename = "priority")] + pub priority: u8, + #[serde(rename = "utime")] + pub utime: i64, + #[serde(rename = "users")] + pub users: JsonValue, + // 忽略 at + #[serde(rename = "lastMessage")] + pub last_message: LastMessage, + #[serde(rename = "autoDownload")] + pub auto_download: Option, + #[serde(rename = "downloadPath")] + pub download_path: Option, } diff --git a/ica-rs/src/data_struct/messages.rs b/ica-rs/src/data_struct/messages.rs index 52231c0..e9d07c4 100644 --- a/ica-rs/src/data_struct/messages.rs +++ b/ica-rs/src/data_struct/messages.rs @@ -153,7 +153,7 @@ impl NewMessage { .map(|t| NaiveDateTime::from_timestamp_micros(t).unwrap_or(current)) .unwrap_or(current); // 身份 - let role = message["role"].as_str().unwrap(); + let role = message["role"].as_str().unwrap_or("unknown"); // 文件 let files: Vec = message["files"] .as_array() diff --git a/ica-rs/src/events.rs b/ica-rs/src/events.rs index 0b28f39..1fcf19a 100644 --- a/ica-rs/src/events.rs +++ b/ica-rs/src/events.rs @@ -4,8 +4,10 @@ use tracing::{info, warn}; use crate::data_struct::messages::NewMessage; use crate::data_struct::online_data::OnlineData; +use crate::data_struct::all_rooms::Room; use crate::py; +/// 获取在线数据 pub fn get_online_data(payload: Payload, _client: RawClient) { if let Payload::Text(values) = payload { if let Some(value) = values.first() { @@ -21,6 +23,7 @@ pub fn get_online_data(payload: Payload, _client: RawClient) { } } +/// 接收消息 pub fn add_message(payload: Payload, _client: RawClient) { if let Payload::Text(values) = payload { if let Some(value) = values.first() { @@ -42,6 +45,21 @@ pub fn delete_message(payload: Payload, _client: RawClient) { } } +pub fn update_all_room(payload: Payload, _client: RawClient) { + 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(); + info!("update_all_room {}", format!("{:#?}", rooms).purple()); + } + } + } +} + +/// 所有 pub fn any_event(event: Event, payload: Payload, _client: RawClient) { let handled = vec![ // 真正处理过的 @@ -52,7 +70,7 @@ pub fn any_event(event: Event, payload: Payload, _client: RawClient) { "onlineData", "addMessage", "deleteMessage", - // "setAllRooms", + "setAllRooms", // 忽略的 "notify", "closeLoading", // 发送消息/加载新聊天 有一个 loading diff --git a/ica-rs/src/main.rs b/ica-rs/src/main.rs index 21e1da5..61c2f14 100644 --- a/ica-rs/src/main.rs +++ b/ica-rs/src/main.rs @@ -35,6 +35,7 @@ fn main() { .on("authSucceed", events::connect_callback) .on("authFailed", events::connect_callback) .on("onlineData", events::get_online_data) + .on("setAllRooms", events::update_all_room) .on("addMessage", events::add_message) .on("deleteMessage", events::delete_message) .connect()