Skip to main content

whatsapp_rust/client/
context_impl.rs

1use crate::client::Client;
2use async_trait::async_trait;
3use std::collections::HashMap;
4use std::sync::Arc;
5use wacore::client::context::{GroupInfo, SendContextResolver};
6use wacore::iq::prekeys::PreKeyFetchReason;
7use wacore::libsignal::protocol::PreKeyBundle;
8use wacore_binary::Jid;
9
10#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
11#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
12impl SendContextResolver for Client {
13    async fn resolve_devices(&self, jids: &[Jid]) -> Result<Vec<Jid>, anyhow::Error> {
14        self.get_user_devices(jids).await
15    }
16
17    async fn fetch_prekeys(
18        &self,
19        jids: &[Jid],
20    ) -> Result<HashMap<Jid, PreKeyBundle>, anyhow::Error> {
21        // The fan-out has its own batch-level handling; it wants the bundles only.
22        self.fetch_pre_keys(jids, None).await.map(|o| o.bundles)
23    }
24
25    async fn fetch_prekeys_for_identity_check(
26        &self,
27        jids: &[Jid],
28    ) -> Result<wacore::prekeys::PreKeyFetchOutcome, anyhow::Error> {
29        self.fetch_pre_keys(jids, Some(PreKeyFetchReason::Identity))
30            .await
31            .map_err(|e| {
32                // Re-wrap server errors as wacore::ServerErrorCode so
33                // encrypt_for_devices can downcast across crate boundaries
34                if let Some(crate::request::IqError::ServerError {
35                    code,
36                    text,
37                    error_type,
38                    backoff,
39                }) = e.downcast_ref::<crate::request::IqError>()
40                {
41                    return anyhow::Error::new(wacore::request::ServerErrorCode {
42                        code: *code,
43                        text: text.clone(),
44                        error_type: error_type.clone(),
45                        backoff: *backoff,
46                    });
47                }
48                e
49            })
50    }
51
52    async fn resolve_group_info(&self, jid: &Jid) -> Result<Arc<GroupInfo>, anyhow::Error> {
53        Ok(self.groups().query_info(jid).await?)
54    }
55
56    async fn get_lid_for_phone(&self, phone_user: &str) -> Option<wacore_binary::CompactString> {
57        self.lid_pn_cache.get_current_lid(phone_user).await
58    }
59
60    fn on_local_identity_change(&self, jid: &Jid) {
61        self.react_to_local_identity_change(jid);
62    }
63
64    async fn lock_device_sessions(
65        &self,
66        device_jids: &[Jid],
67    ) -> wacore::client::context::SessionLockGuard {
68        use wacore::client::context::SessionLockGuard;
69        if device_jids.is_empty() {
70            return SessionLockGuard::none();
71        }
72        // Reuse the DM path's helpers so both lock the identical per-device mutexes.
73        let keys = self.build_session_lock_keys(device_jids).await;
74        SessionLockGuard::hold(Box::new(self.session_guards_for(&keys).await))
75    }
76}