Skip to content

Commit a8ef461

Browse files
committed
feat: enhance JID struct with conversion implementations and update MessageSource getters
1 parent 83ef1f2 commit a8ef461

2 files changed

Lines changed: 51 additions & 20 deletions

File tree

src/clients/tryx.rs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use tokio::runtime;
99
use tokio::sync::watch;
1010
use tokio::time::{Duration, interval};
1111
use wacore::types::events::Event;
12+
use waproto::whatsapp::sync_action_value::ArchiveChatAction;
1213
use whatsapp_rust::Client;
1314
use whatsapp_rust::bot::Bot;
1415
use whatsapp_rust::store::Backend;
@@ -19,9 +20,10 @@ use tracing::{debug, error, info, warn};
1920
use super::tryx_client::TryxClient;
2021
use crate::log::init_logging;
2122
use crate::backend::{SqliteBackend, BackendBase};
22-
use crate::events::types::{EvConnected, EvLoggedOut,EvMessage, EvPairingQrCode};
23+
use crate::events::types::{EvArchiveUpdate, EvConnected, EvLoggedOut, EvMessage, EvPairingQrCode};
2324
use crate::exceptions::UnsupportedBackend;
2425
use crate::events::dispatcher::Dispatcher;
26+
use crate::types::JID;
2527

2628

2729
#[pyclass]
@@ -386,16 +388,12 @@ impl Tryx {
386388
}
387389
}
388390
Event::Message(msg, info) => {
389-
debug!(message_id = %info.id, "received message event");
390-
info!(handlers = message_callbacks.len(), message_id = %info.id, "dispatching message handlers");
391-
392-
for (idx, callback) in message_callbacks.iter().enumerate() {
393-
debug!(handler_index = idx, message_id = %info.id, "calling message Python callback");
391+
let payload = Python::attach(|py| Py::new(py, EvMessage::new(msg, info))).map_err(|e| e).unwrap();
392+
for callback in message_callbacks.iter() {
394393
let locals = locals.clone();
395394
let py_future = Python::attach(|py| -> PyResult<_> {
396-
let payload = Py::new(py, EvMessage::new(msg.clone(), info.clone()))?;
397395
let client_obj = tryx_client.clone_ref(py);
398-
let awaitable = callback.bind(py).call1((client_obj, payload))?;
396+
let awaitable = callback.bind(py).call1((client_obj, payload.clone_ref(py)))?;
399397
let fut: Pin<Box<dyn Future<Output = PyResult<Py<PyAny>>> + Send>> = match &locals {
400398
Some(locals) => {
401399
let fut = into_future_with_locals(locals, awaitable)?;
@@ -412,42 +410,58 @@ impl Tryx {
412410
match py_future {
413411
Ok(py_future) => {
414412
if let Err(err) = py_future.await {
415-
error!(handler_index = idx, message_id = %info.id, error = %err, "message callback failed");
416413
Python::attach(|py| err.print(py));
417414
} else {
418-
debug!(handler_index = idx, message_id = %info.id, "message callback finished");
415+
debug!( "message callback finished");
419416
}
420417
}
421418
Err(err) => {
422-
error!(handler_index = idx, message_id = %info.id, error = %err, "failed to schedule message callback");
419+
error!("failed to schedule message callback");
423420
Python::attach(|py| err.print(py));
424421
}
425422
}
426423
}
427424
}
428425
Event::Connected(_) => {
429-
for (idx, callback) in connected_callbacks.iter().enumerate() {
430-
debug!(handler_index = idx, "calling connected event handler");
426+
let payload = Python::attach(|py| pyo3::Py::new(py, EvConnected{})).map_err(|e| e).unwrap();
427+
for callback in connected_callbacks.iter() {
428+
debug!("calling connected event handler");
431429
let _ = Python::attach(|py| -> PyResult<_> {
432-
let awaitable = callback.bind(py).call1((EvConnected{},))?;
430+
let awaitable = callback.bind(py).call1((payload.clone_ref(py),))?;
433431
let fut = into_future(awaitable)?;
434432
Ok(fut)
435433
});
436434
}
437435
}
438436
Event::LoggedOut(logout) => {
439-
for (idx, callback) in logout_callbacks.iter().enumerate() {
440-
debug!(handler_index = idx, "calling logged out event handler");
437+
let payload = Python::attach(|py| pyo3::Py::new(py, EvLoggedOut::new(logout))).map_err(|e| e).unwrap();
438+
for callback in logout_callbacks.iter() {
439+
debug!("calling logged out event handler");
441440
let _ = Python::attach(|py| -> PyResult<_> {
442-
let awaitable = callback.bind(py).call1((EvLoggedOut::new(logout.clone()),))?;
441+
let awaitable = callback.bind(py).call1((payload.clone_ref(py),))?;
443442
let fut = into_future(awaitable)?;
444443
Ok(fut)
445444
});
446445
}
447446

448447
}
449448
Event::ArchiveUpdate(archived) => {
450-
debug!("received archive update event for jid {}", archived.jid);
449+
450+
let payload = Python::attach(|py| pyo3::Py::new(py, EvArchiveUpdate::new(
451+
archived.jid.into(),
452+
archived.timestamp,
453+
Arc::from(archived.action.clone()),
454+
archived.from_full_sync,
455+
))).map_err(|e| e).unwrap();
456+
for callback in archive_update_callbacks.iter() {
457+
debug!("calling archive update event handler");
458+
let _ = Python::attach(|py| -> PyResult<_> {
459+
let awaitable = callback.bind(py).call1((payload.clone_ref(py),))?;
460+
let fut = into_future(awaitable)?;
461+
Ok(fut)
462+
});
463+
}
464+
// debug!("received archive update event for jid {}", archived.jid);
451465

452466
}
453467
_ => {

src/types.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@ use prost::Message;
1111
pub struct JID {
1212
inner: Arc<WhatsAppJID>,
1313
}
14+
impl From<WhatsAppJID> for JID {
15+
fn from(jid: WhatsAppJID) -> Self {
16+
JID { inner: Arc::new(jid) }
17+
}
18+
}
19+
20+
impl From<JID> for WhatsAppJID {
21+
fn from(jid: JID) -> Self {
22+
(*jid.inner).clone()
23+
}
24+
}
25+
26+
impl From<Arc<WhatsAppJID>> for JID {
27+
fn from(jid: Arc<WhatsAppJID>) -> Self {
28+
JID { inner: jid }
29+
}
30+
}
1431

1532
impl JID {
1633
pub fn as_whatsapp_jid(&self) -> WhatsAppJID {
@@ -49,11 +66,11 @@ struct MessageSource {
4966
impl MessageSource {
5067
#[getter]
5168
fn chat(&self) -> JID {
52-
JID { inner: self.chat.clone() }
69+
self.chat.clone().into()
5370
}
5471
#[getter]
5572
fn sender(&self) -> JID {
56-
JID { inner: self.sender.clone() }
73+
self.sender.clone().into()
5774
}
5875
#[getter]
5976
fn is_from_me(&self) -> bool {

0 commit comments

Comments
 (0)