whatsapp_rust/handlers/
message.rs1use super::traits::StanzaHandler;
2use crate::client::Client;
3use async_trait::async_trait;
4use log::warn;
5use std::sync::Arc;
6use wacore_binary::node::Node;
7
8#[derive(Default)]
16pub struct MessageHandler;
17
18impl MessageHandler {
19 pub fn new() -> Self {
20 Self
21 }
22}
23
24#[async_trait]
25impl StanzaHandler for MessageHandler {
26 fn tag(&self) -> &'static str {
27 "message"
28 }
29
30 async fn handle(&self, client: Arc<Client>, node: &Node, _cancelled: &mut bool) -> bool {
31 let client_clone = client.clone();
32 let node_arc = Arc::new(node.clone());
33
34 tokio::spawn(async move {
35 let info = match client_clone.parse_message_info(&node_arc).await {
36 Ok(info) => info,
37 Err(e) => {
38 warn!(
39 "Could not parse message info to acquire lock; dropping message. Error: {e:?}"
40 );
41 return;
42 }
43 };
44 let chat_jid = info.source.chat;
45
46 let mutex_arc = client_clone
47 .chat_locks
48 .entry(chat_jid)
49 .or_insert_with(|| Arc::new(tokio::sync::Mutex::new(())))
50 .clone();
51
52 let _lock_guard = mutex_arc.lock().await;
53
54 client_clone.handle_encrypted_message(node_arc).await;
55 });
56
57 true
58 }
59}