@@ -9,6 +9,7 @@ use tokio::runtime;
99use tokio:: sync:: watch;
1010use tokio:: time:: { Duration , interval} ;
1111use wacore:: types:: events:: Event ;
12+ use waproto:: whatsapp:: sync_action_value:: ArchiveChatAction ;
1213use whatsapp_rust:: Client ;
1314use whatsapp_rust:: bot:: Bot ;
1415use whatsapp_rust:: store:: Backend ;
@@ -19,9 +20,10 @@ use tracing::{debug, error, info, warn};
1920use super :: tryx_client:: TryxClient ;
2021use crate :: log:: init_logging;
2122use crate :: backend:: { SqliteBackend , BackendBase } ;
22- use crate :: events:: types:: { EvConnected , EvLoggedOut , EvMessage , EvPairingQrCode } ;
23+ use crate :: events:: types:: { EvArchiveUpdate , EvConnected , EvLoggedOut , EvMessage , EvPairingQrCode } ;
2324use crate :: exceptions:: UnsupportedBackend ;
2425use 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 _ => {
0 commit comments