From 4819880e2c10c0dfb3a0adf8add2e1163c6a59a1 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Tue, 20 Feb 2024 17:34:43 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=E4=B8=80=E6=B3=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ica-rs/src/data_struct/messages.rs | 52 +++++++++++++++++++++++++----- ica-rs/src/py/mod.rs | 10 +++--- 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/ica-rs/src/data_struct/messages.rs b/ica-rs/src/data_struct/messages.rs index 8c7635c..bb6e078 100644 --- a/ica-rs/src/data_struct/messages.rs +++ b/ica-rs/src/data_struct/messages.rs @@ -3,7 +3,7 @@ use crate::data_struct::{MessageId, RoomId, UserId}; use chrono::NaiveDateTime; use serde::{Deserialize, Serialize}; -use serde_json::Value as JsonValue; +use serde_json::{json, Value as JsonValue}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum At { @@ -41,7 +41,7 @@ pub struct LastMessage { } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] -pub struct ReplyedMessage { +pub struct ReplyMessage { #[serde(rename = "_id")] pub msg_id: String, pub content: String, @@ -72,7 +72,7 @@ pub struct NewMessage { /// 文件 pub files: Vec, /// 回复的消息 - pub reply: Option, + pub reply: Option, /// At pub at: At, /// 是否已撤回 @@ -136,8 +136,8 @@ impl NewMessage { } } // 回复的消息 - let reply: Option = match message.get("replyMessage") { - Some(value) => serde_json::from_value::(value.clone()).ok(), + let reply: Option = match message.get("replyMessage") { + Some(value) => serde_json::from_value::(value.clone()).ok(), None => None, }; // At @@ -193,23 +193,59 @@ impl NewMessage { } } + /// 作为回复消息使用 + pub fn as_reply(&self) -> ReplyMessage { + ReplyMessage { + // 虽然其实只要这一条就行 + msg_id: self.msg_id.clone(), + // 但是懒得动上面的了, 就这样吧 + content: self.content.clone(), + files: json!([]), + sender_name: self.sender_name.clone(), + } + } + + /// 创建一条对这条消息的回复 + pub fn reply_with(&self, content: &String) -> SendMessage { + SendMessage::new(content.clone(), self.room_id, Some(self.as_reply())) + } + /// 是否是回复 pub fn is_reply(&self) -> bool { self.reply.is_some() } /// 获取回复 - pub fn get_reply(&self) -> Option<&ReplyedMessage> { + pub fn get_reply(&self) -> Option<&ReplyMessage> { self.reply.as_ref() } - pub fn get_reply_mut(&mut self) -> Option<&mut ReplyedMessage> { + pub fn get_reply_mut(&mut self) -> Option<&mut ReplyMessage> { self.reply.as_mut() } } #[derive(Debug, Clone, Serialize, Deserialize)] -pub struct SendMessage {} +pub struct SendMessage { + pub content: String, + #[serde(rename = "roomId")] + pub room_id: RoomId, + #[serde(rename = "replyMessage")] + pub reply_to: Option, + #[serde(rename = "at")] + pub at: JsonValue, +} + +impl SendMessage { + pub fn new(content: String, room_id: RoomId, reply_to: Option) -> Self { + Self { + content, + room_id, + reply_to, + at: json!([]), + } + } +} #[cfg(test)] mod test { diff --git a/ica-rs/src/py/mod.rs b/ica-rs/src/py/mod.rs index b17d6d5..c8c2dab 100644 --- a/ica-rs/src/py/mod.rs +++ b/ica-rs/src/py/mod.rs @@ -1,16 +1,14 @@ pub mod class; -use pyo3::{prelude::*, types::IntoPyDict}; +use pyo3::{prelude::*, types::{IntoPyDict, PyDict}}; use tracing::{debug, info}; -#[pyclass] -#[pyo3(name = "BotStatus")] -pub struct BotStatusPy {} pub fn run() { Python::with_gil(|py| { - let bot_status = BotStatusPy {}; - let _bot_status = PyCell::new(py, bot_status).unwrap(); + let bot_status = class::IcaStatusPy::new(); + let _bot_status: &PyCell<_> = PyCell::new(py, bot_status).unwrap(); + let locals = [("state", _bot_status)].into_py_dict(py); py.run("print(state)", None, Some(locals)).unwrap(); });