重命名一波

This commit is contained in:
shenjack 2024-02-20 17:34:43 +08:00
parent 198839c65f
commit 4819880e2c
Signed by: shenjack
GPG Key ID: 7B1134A979775551
2 changed files with 48 additions and 14 deletions

View File

@ -3,7 +3,7 @@ use crate::data_struct::{MessageId, RoomId, UserId};
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue; use serde_json::{json, Value as JsonValue};
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum At { pub enum At {
@ -41,7 +41,7 @@ pub struct LastMessage {
} }
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ReplyedMessage { pub struct ReplyMessage {
#[serde(rename = "_id")] #[serde(rename = "_id")]
pub msg_id: String, pub msg_id: String,
pub content: String, pub content: String,
@ -72,7 +72,7 @@ pub struct NewMessage {
/// 文件 /// 文件
pub files: Vec<MessageFile>, pub files: Vec<MessageFile>,
/// 回复的消息 /// 回复的消息
pub reply: Option<ReplyedMessage>, pub reply: Option<ReplyMessage>,
/// At /// At
pub at: At, pub at: At,
/// 是否已撤回 /// 是否已撤回
@ -136,8 +136,8 @@ impl NewMessage {
} }
} }
// 回复的消息 // 回复的消息
let reply: Option<ReplyedMessage> = match message.get("replyMessage") { let reply: Option<ReplyMessage> = match message.get("replyMessage") {
Some(value) => serde_json::from_value::<ReplyedMessage>(value.clone()).ok(), Some(value) => serde_json::from_value::<ReplyMessage>(value.clone()).ok(),
None => None, None => None,
}; };
// At // 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 { pub fn is_reply(&self) -> bool {
self.reply.is_some() self.reply.is_some()
} }
/// 获取回复 /// 获取回复
pub fn get_reply(&self) -> Option<&ReplyedMessage> { pub fn get_reply(&self) -> Option<&ReplyMessage> {
self.reply.as_ref() 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() self.reply.as_mut()
} }
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[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<ReplyMessage>,
#[serde(rename = "at")]
pub at: JsonValue,
}
impl SendMessage {
pub fn new(content: String, room_id: RoomId, reply_to: Option<ReplyMessage>) -> Self {
Self {
content,
room_id,
reply_to,
at: json!([]),
}
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {

View File

@ -1,16 +1,14 @@
pub mod class; pub mod class;
use pyo3::{prelude::*, types::IntoPyDict}; use pyo3::{prelude::*, types::{IntoPyDict, PyDict}};
use tracing::{debug, info}; use tracing::{debug, info};
#[pyclass]
#[pyo3(name = "BotStatus")]
pub struct BotStatusPy {}
pub fn run() { pub fn run() {
Python::with_gil(|py| { Python::with_gil(|py| {
let bot_status = BotStatusPy {}; let bot_status = class::IcaStatusPy::new();
let _bot_status = PyCell::new(py, bot_status).unwrap(); let _bot_status: &PyCell<_> = PyCell::new(py, bot_status).unwrap();
let locals = [("state", _bot_status)].into_py_dict(py); let locals = [("state", _bot_status)].into_py_dict(py);
py.run("print(state)", None, Some(locals)).unwrap(); py.run("print(state)", None, Some(locals)).unwrap();
}); });