Skip to content

Commit a6fcba9

Browse files
committed
feat: add prost dependency and enhance message handling in types
1 parent 82054d5 commit a6fcba9

3 files changed

Lines changed: 99 additions & 14 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ waproto = "0.3"
1919
tokio = { version = "1.48", features = ["macros", "rt-multi-thread"] }
2020
async-trait = "0.1.89"
2121
pyo3-async-runtimes = { version = "0.28.0", features = ["attributes", "async-std-runtime"] }
22+
prost = "0.14.3"

src/types.rs

Lines changed: 97 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use std::sync::Arc;
22

3-
use pyo3::{PyErr, PyResult, Python, exceptions::{PyException, PyRuntimeError}, ffi::PyObject, pyclass, pymethods, types::PyDateTime};
3+
use pyo3::{PyErr, PyResult, Python, exceptions::{PyException, PyRuntimeError}, ffi::PyObject, pyclass, pymethods, types::{PyBytes, PyDateTime}};
44
use whatsapp_rust::{Jid as WhatsAppJID};
5-
use wacore::types::message::{EditAttribute, MessageInfo as WhatsAppMessageInfo, MessageSource as WhatsAppMessageSource, MsgBotInfo as WhatsAppMsgBotInfo, BotEditType};
5+
use wacore::types::message::{BotEditType, EditAttribute, MessageInfo as WhatsAppMessageInfo, MessageSource as WhatsAppMessageSource, MsgBotInfo as WhatsAppMsgBotInfo, MsgMetaInfo as WhatsappMsgMetaInfo};
6+
use prost::Message;
67
#[pyclass]
78
pub struct JID {
89
inner: Arc<WhatsAppJID>,
@@ -80,16 +81,7 @@ impl MessageSource {
8081
}
8182

8283

83-
#[pyclass]
84-
struct MessageInfo {
85-
inner: Arc<WhatsAppMessageInfo>,
86-
#[pyo3(get)]
87-
id: String,
88-
#[pyo3(get)]
89-
r#type: String,
90-
#[pyo3(get)]
91-
push_name: String,
92-
}
84+
9385
#[pyclass]
9486
struct MsgBotInfo {
9587
inner: Arc<WhatsAppMsgBotInfo>,
@@ -98,6 +90,7 @@ struct MsgBotInfo {
9890
}
9991
#[pymethods]
10092
impl MsgBotInfo {
93+
#[getter]
10194
fn edit_type(&self) -> Option<&str> {
10295
self.inner.edit_type.as_ref().map(|edit_type| match edit_type {
10396
BotEditType::First => "First",
@@ -120,6 +113,61 @@ impl MsgBotInfo {
120113
}
121114
}
122115
}
116+
#[pyclass]
117+
struct MsgMetaInfo {
118+
inner: Arc<WhatsappMsgMetaInfo>,
119+
#[pyo3(get)]
120+
target_id: Option<String>,
121+
target_sender: Option<JID>,
122+
#[pyo3(get)]
123+
deprecated_lid_session: Option<bool>,
124+
#[pyo3(get)]
125+
thread_message_id: Option<String>,
126+
}
127+
128+
#[pymethods]
129+
impl MsgMetaInfo {
130+
#[getter]
131+
fn target_id(&self) -> Option<&str> {
132+
self.inner.target_id.as_ref().map(|s| s.as_str())
133+
}
134+
#[getter]
135+
fn target_sender(&self) -> Option<JID> {
136+
self.inner.target_sender.as_ref().map(|jid| JID { inner: Arc::new(jid.clone()) })
137+
}
138+
#[getter]
139+
fn deprecated_lid_session(&self) -> Option<bool> {
140+
self.inner.deprecated_lid_session
141+
}
142+
#[getter]
143+
fn thread_message_id(&self) -> Option<&str> {
144+
self.inner.thread_message_id.as_ref().map(|s| s.as_str())
145+
}
146+
#[getter]
147+
fn thread_message_sender_jid(&self) -> Option<JID> {
148+
self.inner.thread_message_sender_jid.as_ref().map(|jid| JID { inner: Arc::new(jid.clone()) })
149+
}
150+
}
151+
152+
#[pyclass]
153+
struct MessageInfo {
154+
inner: Arc<WhatsAppMessageInfo>,
155+
#[pyo3(get)]
156+
id: String,
157+
#[pyo3(get)]
158+
r#type: String,
159+
#[pyo3(get)]
160+
push_name: String,
161+
}
162+
163+
#[pyclass]
164+
struct DeviceSentMeta {
165+
#[pyo3(get)]
166+
destination_jid: String,
167+
#[pyo3(get)]
168+
phash: String,
169+
}
170+
123171
#[pymethods]
124172
impl MessageInfo {
125173
#[getter]
@@ -174,7 +222,42 @@ impl MessageInfo {
174222
}
175223
}
176224
#[getter]
177-
fn meta_info(&self) -> Option<&str> {
178-
self.inner.meta_info.as_ref().map(|_| "MetaInfo")
225+
fn meta_info(&self) -> MsgMetaInfo{
226+
MsgMetaInfo {
227+
inner: Arc::new(self.inner.meta_info.clone()),
228+
target_id: match self.inner.meta_info.target_id {
229+
Some(ref s) => Some(s.clone()),
230+
None => None,
231+
},
232+
target_sender: match self.inner.meta_info.target_sender {
233+
Some(ref jid) => Some(JID { inner: Arc::new(jid.clone()) }),
234+
None => None,
235+
},
236+
deprecated_lid_session: self.inner.meta_info.deprecated_lid_session,
237+
thread_message_id: match self.inner.meta_info.thread_message_id {
238+
Some(ref s) => Some(s.clone()),
239+
None => None,
240+
},
241+
}
242+
}
243+
#[getter]
244+
fn verified_name(&self, py: Python) -> Option<pyo3::Py<PyBytes>> {
245+
match self.inner.verified_name {
246+
Some(ref name) => {
247+
let mut buffer = Vec::new();
248+
name.encode(&mut buffer);
249+
let py_bytes = PyBytes::new(py, &buffer);
250+
Some(py_bytes.into())
251+
252+
},
253+
None => None, // Placeholder, as VerifiedNameCertificate is not yet implemented
254+
}
255+
}
256+
#[getter]
257+
fn device_sent_meta(&self) -> Option<DeviceSentMeta> {
258+
self.inner.device_sent_meta.as_ref().map(|meta| DeviceSentMeta {
259+
destination_jid: meta.destination_jid.clone(),
260+
phash: meta.phash.clone(),
261+
})
179262
}
180263
}

0 commit comments

Comments
 (0)