解析 setAllRooms

This commit is contained in:
shenjack 2024-02-20 15:47:59 +08:00
parent c1608a2c84
commit e5f0d1b60e
Signed by: shenjack
GPG Key ID: 7B1134A979775551
4 changed files with 70 additions and 5 deletions

View File

@ -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<String>,
pub download_path: Option<String>,
}
impl Room {
pub fn new_from_json(json: &JsonValue) -> Self {
let inner = serde_json::from_value::<InnerRoom>(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<String>,
#[serde(rename = "downloadPath")]
pub download_path: Option<String>,
}

View File

@ -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<MessageFile> = message["files"]
.as_array()

View File

@ -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<Room> = 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

View File

@ -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()