Skip to content

Commit

Permalink
feat: impl multi_msg element parse
Browse files Browse the repository at this point in the history
  • Loading branch information
pk5ls20 committed Feb 5, 2025
1 parent 665ccc5 commit 786630b
Show file tree
Hide file tree
Showing 13 changed files with 378 additions and 8 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
| | | | | Markdown | 🔴 | Cookies | 🔴 | FriendRequest | 🔴 |
| | | | | MarketFace | 🟡[^1] | Send Message | 🔴 | ~~FriendTyping~~ | 🔴 |
| | | | | Mention | 🔴 | | | ~~FriendVoiceCall~~ | 🔴 |
| | | | | MultiMsg | 🔴 | | | | |
| | | | | MultiMsg | 🟡[^1] | | | | |
| | | | | Poke | 🔴 | | | | |
| | | | | Record | 🔴 | | | | |
| | | | | SpecialPoke | 🔴 | | | | |
Expand Down
11 changes: 11 additions & 0 deletions examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions mania/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ elliptic-curve = "0.13.8"
log = "0.4.25"
rand_chacha = "0.3.1"
num_enum = "0.7.3"
quick-xml = { version = "0.37.2", features = ["serialize"] }

[build-dependencies]
prost-build = "0.13.4"
Expand Down
19 changes: 19 additions & 0 deletions mania/src/core/business/messaging_logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,25 @@ async fn messaging_logic_incoming(
}
}
}
Entity::MultiMsg(ref mut multi) => match multi.chains.is_empty() {
true => {
let msg = handle
.multi_msg_download(chain.uid.clone(), multi.res_id.clone())
.await;
match msg {
Ok(Some(chains)) => {
multi.chains = chains;
}
Ok(None) => {
tracing::warn!("No chains found in MultiMsgDownloadEvent");
}
Err(e) => {
tracing::error!("Failed to download MultiMsg: {:?}", e);
}
}
}
false => {}
},
_ => {}
}
}
Expand Down
4 changes: 4 additions & 0 deletions mania/src/core/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ pub fn downcast_event<T: ServerEvent + 'static>(event: &impl AsRef<dyn ServerEve
event.as_ref().as_any().downcast_ref::<T>()
}

pub fn downcast_mut_event<T: ServerEvent + 'static>(event: &mut dyn ServerEvent) -> Option<&mut T> {
event.as_any_mut().downcast_mut::<T>()
}

#[derive(Debug, Error)]
pub enum EventError {
#[error("unsupported event, commend: {0}")]
Expand Down
1 change: 1 addition & 0 deletions mania/src/core/event/message/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod image_c2c_download;
pub mod image_group_download;
pub mod multi_msg_download;
pub mod push_msg;
66 changes: 66 additions & 0 deletions mania/src/core/event/message/multi_msg_download.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use crate::core::event::prelude::*;
use crate::core::protos::message::{
LongMsgResult, LongMsgSettings, LongMsgUid, RecvLongMsgInfo, RecvLongMsgReq, RecvLongMsgResp,
};
use crate::message::chain::MessageChain;
use crate::message::packer::MessagePacker;
use crate::utility::compress::gzip;
use mania_macros::{commend, ServerEvent};

#[commend("trpc.group.long_msg_interface.MsgService.SsoRecvLongMsg")]
#[derive(Debug, ServerEvent, Default)]
pub struct MultiMsgDownloadEvent {
pub uid: Option<String>,
pub res_id: Option<String>,
pub chains: Option<Vec<MessageChain>>,
}

impl ClientEvent for MultiMsgDownloadEvent {
fn build(&self, _: &Context) -> BinaryPacket {
let packet = RecvLongMsgReq {
info: Some(RecvLongMsgInfo {
uid: Some(LongMsgUid {
uid: self.uid.clone(),
}),
res_id: self.res_id.clone(),
acquire: true,
}),
settings: Some(LongMsgSettings {
field1: 2,
field2: 0,
field3: 0,
field4: 0,
}),
};
BinaryPacket(packet.encode_to_vec().into())
}

fn parse(packet: Bytes, _: &Context) -> Result<Box<dyn ServerEvent>, EventError> {
let packet = RecvLongMsgResp::decode(packet)?;
let inflate = gzip::decompress(&packet.result.expect("missing RecvLongMsgInfo").payload)
.ok_or(EventError::OtherError(
"Failed to decompress long message".to_string(),
))?;
let result = LongMsgResult::decode(Bytes::from(inflate))?;
let main = result
.action
.into_iter()
.find(|a| a.action_command == "MultiMsg")
.ok_or(EventError::OtherError(
"Failed to find MultiMsg command".to_string(),
))?;
let chains = main
.action_data
.ok_or(EventError::OtherError(
"Failed to find action_data".to_string(),
))?
.msg_body
.into_iter()
.map(MessagePacker::parse_fake_chain)
.collect::<Result<Vec<MessageChain>, String>>()
.map_err(EventError::OtherError)?;
Ok(Box::new(dda!(MultiMsgDownloadEvent {
chains: Some(chains),
})))
}
}
18 changes: 17 additions & 1 deletion mania/src/core/operation/system.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::core::business::BusinessHandle;
use crate::core::event::downcast_event;
use crate::core::event::message::image_c2c_download::ImageC2CDownloadEvent;
use crate::core::event::message::image_group_download::ImageGroupDownloadEvent;
use crate::core::event::message::multi_msg_download::MultiMsgDownloadEvent;
use crate::core::event::system::fetch_rkey::FetchRKeyEvent;
use crate::core::event::{downcast_event, downcast_mut_event};
use crate::core::protos::service::oidb::IndexNode;
use crate::dda;
use crate::message::chain::MessageChain;
use std::sync::Arc;

impl BusinessHandle {
Expand Down Expand Up @@ -49,4 +51,18 @@ impl BusinessHandle {
let event: &ImageC2CDownloadEvent = downcast_event(&res).unwrap();
Ok(event.image_url.clone())
}

pub async fn multi_msg_download(
self: &Arc<Self>,
uid: String,
res_id: String,
) -> crate::Result<Option<Vec<MessageChain>>> {
let mut fetch_event = dda!(MultiMsgDownloadEvent {
uid: Some(uid),
res_id: Some(res_id),
});
let mut res = self.send_event(&mut fetch_event).await?;
let event: &mut MultiMsgDownloadEvent = downcast_mut_event(&mut *res).unwrap();
Ok(event.chains.take())
}
}
4 changes: 4 additions & 0 deletions mania/src/message/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ impl Display for MessageChain {
}

impl MessageChain {
pub fn is_group(&self) -> bool {
matches!(self.typ, MessageType::Group(_))
}

pub(crate) fn friend(friend_uin: u32, friend_uid: String, self_uid: String) -> Self {
dda!(Self {
typ: MessageType::Friend(FriendMessageUniqueElem::default()),
Expand Down
9 changes: 6 additions & 3 deletions mania/src/message/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod image;
pub mod json;
pub mod light_app;
pub mod market_face;
pub mod multi_msg;
pub mod text;

pub use face::FaceEntity as Face;
Expand All @@ -12,6 +13,7 @@ pub use image::ImageEntity as Image;
pub use json::JsonEntity as Json;
pub use light_app::LightAppEntity as LightApp;
pub use market_face::MarketFaceEntity as MarketFace;
pub use multi_msg::MultiMsgEntity as MultiMsg;
pub use text::TextEntity as Text;

use crate::core::protos::message::Elem;
Expand All @@ -36,6 +38,7 @@ pub enum Entity {
Forward(forward::ForwardEntity),
MarketFace(market_face::MarketFaceEntity),
LightApp(light_app::LightAppEntity),
MultiMsg(multi_msg::MultiMsgEntity),
}

macro_rules! impl_entity_show {
Expand Down Expand Up @@ -97,9 +100,9 @@ macro_rules! impl_entity_unpack {
}
}

impl_entity_show!(Text, Json, Image, Face, Forward, MarketFace, LightApp);
impl_entity_pack!(Text, Json, Image, Face, Forward, MarketFace, LightApp);
impl_entity_unpack!(Text, Json, Image, Face, Forward, MarketFace, LightApp);
impl_entity_show!(Text, Json, Image, Face, Forward, MarketFace, LightApp, MultiMsg);
impl_entity_pack!(Text, Json, Image, Face, Forward, MarketFace, LightApp, MultiMsg);
impl_entity_unpack!(Text, Json, Image, Face, Forward, MarketFace, LightApp, MultiMsg);

impl Entity {
pub fn from_elems(elems: &[Elem]) -> Vec<Self> {
Expand Down
Loading

0 comments on commit 786630b

Please sign in to comment.