diff --git a/src/client/lid_pn.rs b/src/client/lid_pn.rs index b3de0e9a8..0497bfef0 100644 --- a/src/client/lid_pn.rs +++ b/src/client/lid_pn.rs @@ -238,24 +238,18 @@ impl Client { } } - /// Get the phone number (user part) for a given LID. - /// Looks up the LID-PN mapping from the in-memory cache. + /// Look up the LID↔phone mapping for a JID. /// - /// # Arguments - /// - /// * `lid` - The LID user part (e.g., "100000012345678") or full JID (e.g., "100000012345678@lid") - /// - /// # Returns - /// - /// The phone number user part if a mapping exists, None otherwise. - pub async fn get_phone_number_from_lid(&self, lid: &str) -> Option { - // Handle both full JID (e.g., "100000012345678@lid") and user part only - let lid_user = if lid.contains('@') { - lid.split('@').next().unwrap_or(lid) + /// Routes automatically: LID JIDs search by LID, PN JIDs search by phone. + /// Returns `None` for non-user JIDs (groups, newsletters, etc.). + pub async fn get_lid_pn_entry(&self, jid: &Jid) -> Option { + if jid.is_lid() { + self.lid_pn_cache.get_entry_by_lid(&jid.user).await + } else if jid.is_pn() { + self.lid_pn_cache.get_entry_by_phone(&jid.user).await } else { - lid - }; - self.lid_pn_cache.get_phone_number(lid_user).await + None + } } } @@ -307,4 +301,40 @@ mod tests { assert_eq!(resolved, pn_jid); } + + #[tokio::test] + async fn test_get_lid_pn_entry_from_pn() { + let client: Arc = create_test_client().await; + let pn = "55999999999"; + let lid = "100000012345678"; + + assert!(client.get_lid_pn_entry(&Jid::pn(pn)).await.is_none()); + + client + .add_lid_pn_mapping(lid, pn, LearningSource::Usync) + .await + .unwrap(); + + let entry = client.get_lid_pn_entry(&Jid::pn(pn)).await.unwrap(); + assert_eq!(entry.lid, lid); + assert_eq!(entry.phone_number, pn); + } + + #[tokio::test] + async fn test_get_lid_pn_entry_from_lid() { + let client: Arc = create_test_client().await; + let pn = "55999999999"; + let lid = "100000012345678"; + + assert!(client.get_lid_pn_entry(&Jid::lid(lid)).await.is_none()); + + client + .add_lid_pn_mapping(lid, pn, LearningSource::Usync) + .await + .unwrap(); + + let entry = client.get_lid_pn_entry(&Jid::lid(lid)).await.unwrap(); + assert_eq!(entry.lid, lid); + assert_eq!(entry.phone_number, pn); + } } diff --git a/src/features/groups.rs b/src/features/groups.rs index f0e3a33d6..3e5f6ad23 100644 --- a/src/features/groups.rs +++ b/src/features/groups.rs @@ -189,14 +189,14 @@ impl<'a> Groups<'a> { for participant in options.participants { let resolved = if participant.jid.is_lid() && participant.phone_number.is_none() { - let phone_number = self + let entry = self .client - .get_phone_number_from_lid(&participant.jid.user) + .get_lid_pn_entry(&participant.jid) .await .ok_or_else(|| { anyhow::anyhow!("Missing phone number mapping for LID {}", participant.jid) })?; - participant.with_phone_number(Jid::pn(phone_number)) + participant.with_phone_number(Jid::pn(entry.phone_number)) } else { participant };