0.4.11 发release吗?

This commit is contained in:
shenjack 2024-02-25 12:01:36 +08:00
parent 9f5956e77a
commit 0ef4aeb4f3
Signed by: shenjack
GPG Key ID: 7B1134A979775551
8 changed files with 187 additions and 100 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "ica-rs" name = "ica-rs"
version = "0.4.10" version = "0.4.11"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -1,6 +1,6 @@
# Python 兼容版本 3.8+ # Python 兼容版本 3.8+
from typing import Optional, Callable from typing import Callable
""" """
pub type RoomId = i64; pub type RoomId = i64;
@ -14,6 +14,10 @@ MessageId = str
class IcaStatus: class IcaStatus:
"""
ica状态信息
此类并不存储信息, 所有方法都是实时获取
"""
@property @property
def login(self) -> bool: def login(self) -> bool:
... ...
@ -21,25 +25,25 @@ class IcaStatus:
def online(self) -> bool: def online(self) -> bool:
... ...
@property @property
def self_id(self) -> Optional[UserId]: def self_id(self) -> UserId:
... ...
@property @property
def nick_name(self) -> Optional[str]: def nick_name(self) -> str:
... ...
@property @property
def ica_version(self) -> Optional[str]: def ica_version(self) -> str:
... ...
@property @property
def os_info(self) -> Optional[str]: def os_info(self) -> str:
... ...
@property @property
def resident_set_size(self) -> Optional[str]: def resident_set_size(self) -> str:
... ...
@property @property
def head_used(self) -> Optional[str]: def head_used(self) -> str:
... ...
@property @property
def load_average(self) -> Optional[str]: def load_average(self) -> str:
... ...
@ -62,9 +66,16 @@ class SendMessage:
return self return self
class DeleteMessage:
def __str__(self):
...
class NewMessage: class NewMessage:
def reply_with(self, message: str) -> SendMessage: def reply_with(self, message: str) -> SendMessage:
... ...
def as_deleted(self) -> DeleteMessage:
...
def __str__(self) -> str: def __str__(self) -> str:
... ...
@property @property
@ -94,6 +105,8 @@ class IcaClient:
""" """
def send_message(self, message: SendMessage) -> bool: def send_message(self, message: SendMessage) -> bool:
... ...
def delete_message(self, message: DeleteMessage) -> bool:
...
@property @property
def status() -> IcaStatus: def status() -> IcaStatus:

View File

@ -1,6 +1,8 @@
use crate::config::IcaConfig; use crate::config::IcaConfig;
use crate::data_struct::messages::SendMessage; use crate::data_struct::all_rooms::Room;
use crate::data_struct::{all_rooms::Room, online_data::OnlineData}; use crate::data_struct::messages::{DeleteMessage, SendMessage};
use crate::data_struct::online_data::OnlineData;
use crate::data_struct::RoomId;
use crate::ClientStatus; use crate::ClientStatus;
use colored::Colorize; use colored::Colorize;
@ -24,10 +26,31 @@ pub async fn send_message(client: &Client, message: &SendMessage) -> bool {
} }
} }
} }
/// "安全" 的 删除一条消息
pub async fn delete_message(client: &Client, message: &DeleteMessage) -> bool {
let value = message.as_value();
match client.emit("deleteMessage", value).await {
Ok(_) => {
debug!("delete_message {}", format!("{:#?}", message).yellow());
true
}
Err(e) => {
warn!("delete_message faild:{}", format!("{:#?}", e).red());
false
}
}
}
/// "安全" 的 获取历史消息
/// ```typescript
/// async fetchHistory(messageId: string, roomId: number, currentLoadedMessagesCount: number)
/// ```
pub async fn fetch_history(client: &Client, roomd_id: RoomId) -> bool { false }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct IcalinguaStatus { pub struct IcalinguaStatus {
pub login: bool, pub login: bool,
/// currentLoadedMessagesCount
pub current_loaded_messages_count: u64,
pub online_data: Option<OnlineData>, pub online_data: Option<OnlineData>,
pub rooms: Option<Vec<Room>>, pub rooms: Option<Vec<Room>>,
pub config: Option<IcaConfig>, pub config: Option<IcaConfig>,
@ -37,26 +60,58 @@ impl IcalinguaStatus {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
login: false, login: false,
current_loaded_messages_count: 0,
online_data: None, online_data: None,
rooms: None, rooms: None,
config: Some(IcaConfig::new_from_cli()), config: Some(IcaConfig::new_from_cli()),
} }
} }
pub fn update_online_data(&mut self, online_data: OnlineData) { #[inline]
self.online_data = Some(online_data); pub fn update_online_data(online_data: OnlineData) {
unsafe {
ClientStatus.online_data = Some(online_data);
}
} }
#[inline]
pub fn update_rooms(&mut self, rooms: Vec<Room>) { self.rooms = Some(rooms); } pub fn update_rooms(rooms: Vec<Room>) {
unsafe {
pub fn update_login_status(&mut self, login: bool) { self.login = login; } ClientStatus.rooms = Some(rooms);
}
pub fn update_config(&mut self, config: IcaConfig) { self.config = Some(config); } }
#[inline]
pub fn update_login_status(login: bool) {
unsafe {
ClientStatus.login = login;
}
}
#[inline]
pub fn update_config(config: IcaConfig) {
unsafe {
ClientStatus.config = Some(config);
}
}
#[inline]
pub fn update_loaded_messages_count(count: u64) {
unsafe {
ClientStatus.current_loaded_messages_count = count;
}
}
#[inline]
pub fn get_login_status() -> bool { unsafe { ClientStatus.login } }
#[inline]
pub fn get_rooms() -> &'static Vec<Room> {
unsafe { ClientStatus.rooms.as_ref().expect("rooms should be set") }
}
#[inline]
pub fn get_loaded_messages_count() -> u64 {
unsafe { ClientStatus.current_loaded_messages_count }
}
#[inline]
pub fn get_online_data() -> &'static OnlineData { pub fn get_online_data() -> &'static OnlineData {
unsafe { ClientStatus.online_data.as_ref().expect("online_data should be set") } unsafe { ClientStatus.online_data.as_ref().expect("online_data should be set") }
} }
#[inline]
pub fn get_config() -> &'static IcaConfig { pub fn get_config() -> &'static IcaConfig {
unsafe { ClientStatus.config.as_ref().expect("config should be set") } unsafe { ClientStatus.config.as_ref().expect("config should be set") }
} }

View File

@ -277,6 +277,14 @@ impl NewMessage {
pub fn reply_with(&self, content: &String) -> SendMessage { pub fn reply_with(&self, content: &String) -> SendMessage {
SendMessage::new(content.clone(), self.room_id, Some(self.msg.as_reply())) SendMessage::new(content.clone(), self.room_id, Some(self.msg.as_reply()))
} }
/// 作为被删除的消息
pub fn as_deleted(&self) -> DeleteMessage {
DeleteMessage {
room_id: self.room_id,
message_id: self.msg.msg_id.clone(),
}
}
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
@ -303,6 +311,25 @@ impl SendMessage {
pub fn as_value(&self) -> JsonValue { serde_json::to_value(self).unwrap() } pub fn as_value(&self) -> JsonValue { serde_json::to_value(self).unwrap() }
} }
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeleteMessage {
#[serde(rename = "roomId")]
pub room_id: RoomId,
#[serde(rename = "messageId")]
pub message_id: MessageId,
}
impl DeleteMessage {
pub fn new(room_id: RoomId, message_id: MessageId) -> Self {
Self {
room_id,
message_id,
}
}
pub fn as_value(&self) -> JsonValue { serde_json::to_value(self).unwrap() }
}
// #[cfg(test)] // #[cfg(test)]
// mod test { // mod test {
// use serde_json::json; // use serde_json::json;

View File

@ -15,9 +15,7 @@ pub async fn get_online_data(payload: Payload, _client: Client) {
if let Some(value) = values.first() { if let Some(value) = values.first() {
let online_data = OnlineData::new_from_json(value); let online_data = OnlineData::new_from_json(value);
info!("update_online_data {}", format!("{:?}", online_data).cyan()); info!("update_online_data {}", format!("{:?}", online_data).cyan());
unsafe { IcalinguaStatus::update_online_data(online_data);
crate::ClientStatus.update_online_data(online_data);
}
} }
} }
} }
@ -78,9 +76,7 @@ pub async fn update_all_room(payload: Payload, _client: Client) {
if let Some(raw_rooms) = value.as_array() { if let Some(raw_rooms) = value.as_array() {
let rooms: Vec<Room> = let rooms: Vec<Room> =
raw_rooms.iter().map(|room| Room::new_from_json(room)).collect(); raw_rooms.iter().map(|room| Room::new_from_json(room)).collect();
unsafe { IcalinguaStatus::update_rooms(rooms.clone());
crate::ClientStatus.update_rooms(rooms.clone());
}
info!("update_all_room {}", rooms.len()); info!("update_all_room {}", rooms.len());
} }
} }

View File

@ -14,6 +14,7 @@ mod py;
#[allow(non_upper_case_globals)] #[allow(non_upper_case_globals)]
pub static mut ClientStatus: client::IcalinguaStatus = client::IcalinguaStatus { pub static mut ClientStatus: client::IcalinguaStatus = client::IcalinguaStatus {
login: false, login: false,
current_loaded_messages_count: 0,
online_data: None, online_data: None,
rooms: None, rooms: None,
config: None, config: None,
@ -41,9 +42,7 @@ async fn main() {
// 从命令行获取 host 和 key // 从命令行获取 host 和 key
// 从命令行获取配置文件路径 // 从命令行获取配置文件路径
let ica_config = config::IcaConfig::new_from_cli(); let ica_config = config::IcaConfig::new_from_cli();
unsafe { client::IcalinguaStatus::update_config(ica_config.clone());
ClientStatus.update_config(ica_config.clone());
}
py::init_py(&ica_config); py::init_py(&ica_config);
let socket = ClientBuilder::new(ica_config.host.clone()) let socket = ClientBuilder::new(ica_config.host.clone())

View File

@ -3,8 +3,10 @@ use rust_socketio::asynchronous::Client;
use tokio::runtime::Runtime; use tokio::runtime::Runtime;
use tracing::{debug, info, warn}; use tracing::{debug, info, warn};
use crate::client::send_message; use crate::client::{delete_message, send_message, IcalinguaStatus};
use crate::data_struct::messages::{MessageTrait, NewMessage, ReplyMessage, SendMessage}; use crate::data_struct::messages::{
DeleteMessage, MessageTrait, NewMessage, ReplyMessage, SendMessage,
};
use crate::data_struct::MessageId; use crate::data_struct::MessageId;
use crate::ClientStatus; use crate::ClientStatus;
@ -16,88 +18,39 @@ pub struct IcaStatusPy {}
impl IcaStatusPy { impl IcaStatusPy {
#[new] #[new]
pub fn py_new() -> Self { Self {} } pub fn py_new() -> Self { Self {} }
#[getter] #[getter]
pub fn get_login(&self) -> bool { unsafe { ClientStatus.login } } pub fn get_login(&self) -> bool { unsafe { ClientStatus.login } }
#[getter] #[getter]
pub fn get_online(&self) -> bool { pub fn get_online(&self) -> bool { IcalinguaStatus::get_online_data().online }
unsafe { #[getter]
match ClientStatus.online_data.as_ref() { pub fn get_self_id(&self) -> i64 { IcalinguaStatus::get_online_data().qqid }
Some(data) => data.online, #[getter]
None => false, pub fn get_nick_name(&self) -> String { IcalinguaStatus::get_online_data().nick.clone() }
} #[getter]
} pub fn get_loaded_messages_count(&self) -> u64 { IcalinguaStatus::get_loaded_messages_count() }
#[getter]
pub fn get_ica_version(&self) -> String {
IcalinguaStatus::get_online_data().icalingua_info.ica_version.clone()
} }
#[getter] #[getter]
pub fn get_self_id(&self) -> Option<i64> { pub fn get_os_info(&self) -> String {
unsafe { IcalinguaStatus::get_online_data().icalingua_info.os_info.clone()
match ClientStatus.online_data.as_ref() {
Some(data) => Some(data.qqid),
None => None,
}
}
} }
#[getter] #[getter]
pub fn get_nick_name(&self) -> Option<String> { pub fn get_resident_set_size(&self) -> String {
unsafe { IcalinguaStatus::get_online_data().icalingua_info.resident_set_size.clone()
match ClientStatus.online_data.as_ref() {
Some(data) => Some(data.nick.clone()),
None => None,
}
}
} }
#[getter] #[getter]
pub fn get_ica_version(&self) -> Option<String> { pub fn get_heap_used(&self) -> String {
unsafe { IcalinguaStatus::get_online_data().icalingua_info.heap_used.clone()
match ClientStatus.online_data.as_ref() {
Some(data) => Some(data.icalingua_info.ica_version.clone()),
None => None,
}
}
} }
#[getter] #[getter]
pub fn get_os_info(&self) -> Option<String> { pub fn get_load(&self) -> String {
unsafe { IcalinguaStatus::get_online_data().icalingua_info.load.clone()
match ClientStatus.online_data.as_ref() {
Some(data) => Some(data.icalingua_info.os_info.clone()),
None => None,
}
}
}
#[getter]
pub fn get_resident_set_size(&self) -> Option<String> {
unsafe {
match ClientStatus.online_data.as_ref() {
Some(data) => Some(data.icalingua_info.resident_set_size.clone()),
None => None,
}
}
}
#[getter]
pub fn get_heap_used(&self) -> Option<String> {
unsafe {
match ClientStatus.online_data.as_ref() {
Some(data) => Some(data.icalingua_info.heap_used.clone()),
None => None,
}
}
}
#[getter]
pub fn get_load(&self) -> Option<String> {
unsafe {
match ClientStatus.online_data.as_ref() {
Some(data) => Some(data.icalingua_info.load.clone()),
None => None,
}
}
} }
} }
@ -117,7 +70,7 @@ impl NewMessagePy {
pub fn reply_with(&self, content: String) -> SendMessagePy { pub fn reply_with(&self, content: String) -> SendMessagePy {
SendMessagePy::new(self.msg.reply_with(&content)) SendMessagePy::new(self.msg.reply_with(&content))
} }
pub fn as_deleted(&self) -> DeleteMessagePy { DeleteMessagePy::new(self.msg.as_deleted()) }
pub fn __str__(&self) -> String { format!("{:?}", self.msg) } pub fn __str__(&self) -> String { format!("{:?}", self.msg) }
#[getter] #[getter]
pub fn get_id(&self) -> MessageId { self.msg.msg_id().clone() } pub fn get_id(&self) -> MessageId { self.msg.msg_id().clone() }
@ -176,6 +129,22 @@ impl SendMessagePy {
pub fn new(msg: SendMessage) -> Self { Self { msg } } pub fn new(msg: SendMessage) -> Self { Self { msg } }
} }
#[derive(Clone)]
#[pyclass]
#[pyo3(name = "DeleteMessage")]
pub struct DeleteMessagePy {
pub msg: DeleteMessage,
}
#[pymethods]
impl DeleteMessagePy {
pub fn __str__(&self) -> String { format!("{:?}", self.msg) }
}
impl DeleteMessagePy {
pub fn new(msg: DeleteMessage) -> Self { Self { msg } }
}
#[derive(Clone)] #[derive(Clone)]
#[pyclass] #[pyclass]
#[pyo3(name = "IcaClient")] #[pyo3(name = "IcaClient")]
@ -192,6 +161,13 @@ impl IcaClientPy {
}) })
} }
pub fn delete_message(&self, message: DeleteMessagePy) -> bool {
tokio::task::block_in_place(|| {
let rt = Runtime::new().unwrap();
rt.block_on(delete_message(&self.client, &message.msg))
})
}
/// 仅作占位 /// 仅作占位
/// (因为目前来说, rust调用 Python端没法启动一个异步运行时 /// (因为目前来说, rust调用 Python端没法启动一个异步运行时
/// 所以只能 tokio::task::block_in_place 转换成同步调用) /// 所以只能 tokio::task::block_in_place 转换成同步调用)

21
news.md
View File

@ -1,5 +1,26 @@
# 更新日志 # 更新日志
## 0.4.11
这几天就是在刷版本号的感觉
- 添加
- `DeleteMessage` 用于删除消息
- `NewMessage.as_delete` 用于将消息转换为删除消息
- `client::delete_message` 用于删除消息
- `client::fetch_history` 用于获取历史消息 TODO
- `py::class::DeleteMessagePy` 用于删除消息 的 Python 侧 API
- `py::class::IcaClientPy.delete_message` 用于删除消息 的 Python 侧 API
- `IcalinguaStatus.current_loaded_messages_count`
- 用于以后加载信息计数
- 修改
- `py::class::IcaStatusPy`
- 大部分方法从手动 `unsafe` + `Option`
- 改成直接调用 `IcalinguaStatus` 的方法
- `IcalinguaStatus`
- 所有方法均改成 直接对着 `IcalinguaStatus` 的方法调用
- 补全没有的方法
## 0.4.10 ## 0.4.10
好家伙, 我感觉都快能叫 0.5 了 好家伙, 我感觉都快能叫 0.5 了