communicate: WhatsApp notifications are posted under the canonical conversation id
- Commit
047d7f9c2b60345af730c1da3a53c5444f9defff- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 04:41 UTC (5 days ago)
- Parents
80562df
WhatsApp client events carry prefixed ids (wa:<number>:<device>@s.whatsapp.net) and every other surface canonicalises them to the plain JID that Room rows, thread remoteIds and conversation lookups are keyed by. showIncomingNotification skipped that canonicalisation: the notification posted under shortcutId WhatsApp:wa:1555... while ConversationReadEffects cancels WhatsApp:1555..., so reading the thread never cleared the shade; the tap extras carried remoteId=wa:... so the opened conversation queried zero rows, could not send a read receipt, and would build a malformed send JID. The processor's canonicalisation is now a shared canonicalWhatsAppChatJid(), and the notification target is built through whatsAppNotificationTarget (canonical remoteId, display address derived from it, group detection on it). Four new tests pin prefix/device stripping, the 1:1 target, the shortcut-id match with the opened thread and the group target. Unread accounting, drafts, dedup and the other lines' notification ids were audited and cleared. Verified: :communicate:testDevUnitTest 1465 tests — the 2 failures are pre-existing and unrelated (a no-network host failure and a WebRTC test), both passing in isolation; lint passes.
Diff
communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/WhatsAppEventProcessor.kt
diff --git a/communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/WhatsAppEventProcessor.kt b/communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/WhatsAppEventProcessor.ktindex 16da2b5e94..44c8d15849 100644--- a/communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/WhatsAppEventProcessor.kt+++ b/communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/WhatsAppEventProcessor.kt@@ -223,12 +223,7 @@ class WhatsAppEventProcessor(private val db: WhatsAppDatabase) { } /** Canonical chat JID: strip the "wa:" source prefix and any :device suffix (user:5@srv -> user@srv). */- private fun chatJid(raw: String): String {- val s = raw.removePrefix("wa:")- val at = s.indexOf('@')- if (at <= 0) return s- return s.substring(0, at).substringBefore(':') + s.substring(at)- }+ private fun chatJid(raw: String): String = canonicalWhatsAppChatJid(raw) /** * Extract the group's participant display names from a [WhatsAppEvent.ConversationUpdate]'s@@ -272,3 +267,18 @@ class WhatsAppEventProcessor(private val db: WhatsAppDatabase) { messages.updateServiceData(messageId, transform(current).serialize()) } }++/**+ * Canonical chat JID for a raw WhatsApp conversation id: strip the "wa:"+ * source prefix the client puts on event conversation ids and any :device+ * suffix in the user part (`wa:user:5@srv` -> `user@srv`). This is the form+ * cached messages/conversations are stored under, so every other surface+ * (thread list, conversation screen, notification shortcut/tap target) must+ * use it too — a raw `wa:`-prefixed id matches no stored conversation.+ */+internal fun canonicalWhatsAppChatJid(raw: String): String {+ val s = raw.removePrefix("wa:")+ val at = s.indexOf('@')+ if (at <= 0) return s+ return s.substring(0, at).substringBefore(':') + s.substring(at)+}
communicate/src/main/java/com/vayunmathur/communicate/telephony/WhatsAppSyncService.kt
diff --git a/communicate/src/main/java/com/vayunmathur/communicate/telephony/WhatsAppSyncService.kt b/communicate/src/main/java/com/vayunmathur/communicate/telephony/WhatsAppSyncService.ktindex 5b0046e461..89206b50e6 100644--- a/communicate/src/main/java/com/vayunmathur/communicate/telephony/WhatsAppSyncService.kt+++ b/communicate/src/main/java/com/vayunmathur/communicate/telephony/WhatsAppSyncService.kt@@ -20,6 +20,7 @@ import com.vayunmathur.communicate.data.whatsapp.WhatsAppDatabase import com.vayunmathur.communicate.data.whatsapp.WhatsAppEvent import com.vayunmathur.communicate.data.whatsapp.WhatsAppEventProcessor import com.vayunmathur.communicate.data.whatsapp.WhatsAppLineSession+import com.vayunmathur.communicate.data.whatsapp.canonicalWhatsAppChatJid import com.vayunmathur.communicate.notifications.ConversationSpace import com.vayunmathur.communicate.notifications.ConversationTarget import com.vayunmathur.library.util.ensureNotificationChannel@@ -144,15 +145,7 @@ class WhatsAppSyncService : Service() { } private fun showIncomingNotification(event: WhatsAppEvent.IncomingMessage) {- val isGroup = event.conversationId.endsWith("@g.us")- val target = ConversationTarget(- line = CommunicateLine.WhatsApp,- address = event.peerPhone ?: event.conversationId,- remoteId = event.conversationId,- isGroup = isGroup,- title = if (isGroup) event.peerName else null,- personName = event.senderName ?: event.peerName ?: event.peerPhone ?: "WhatsApp",- )+ val target = whatsAppNotificationTarget(event) ConversationSpace.notifyIncoming( context = this, target = target,@@ -202,3 +195,35 @@ class WhatsAppSyncService : Service() { } } }++/**+ * Build the notification target for an inbound WhatsApp message.+ *+ * Event conversation ids carry the client's `wa:` source prefix (and may+ * carry a :device suffix), but cached conversations — and therefore thread+ * `remoteId`s, shortcut ids and the conversation screen's Room lookups —+ * are keyed by the canonical JID ([canonicalWhatsAppChatJid]). Using the+ * raw event id here posted the notification under a different shortcut id+ * than opening the thread cancels, and its tap intent opened an empty+ * conversation that matched no stored messages.+ */+internal fun whatsAppNotificationTarget(event: WhatsAppEvent.IncomingMessage): ConversationTarget {+ val jid = canonicalWhatsAppChatJid(event.conversationId)+ val isGroup = jid.endsWith("@g.us")+ val localPart = jid.substringBefore("@").substringBefore(":").substringBefore(".")+ val address = event.peerPhone ?: if (isGroup) {+ jid+ } else if (localPart.isNotEmpty() && localPart.all { it.isDigit() }) {+ "+$localPart"+ } else {+ localPart.ifEmpty { jid }+ }+ return ConversationTarget(+ line = CommunicateLine.WhatsApp,+ address = address,+ remoteId = jid,+ isGroup = isGroup,+ title = if (isGroup) event.peerName else null,+ personName = event.senderName ?: event.peerName ?: event.peerPhone ?: "WhatsApp",+ )+}
communicate/src/test/java/com/vayunmathur/communicate/telephony/WhatsAppNotificationTargetTest.kt
diff --git a/communicate/src/test/java/com/vayunmathur/communicate/telephony/WhatsAppNotificationTargetTest.kt b/communicate/src/test/java/com/vayunmathur/communicate/telephony/WhatsAppNotificationTargetTest.ktnew file mode 100644index 0000000000..29572a7ea1--- /dev/null+++ b/communicate/src/test/java/com/vayunmathur/communicate/telephony/WhatsAppNotificationTargetTest.kt@@ -0,0 +1,78 @@+package com.vayunmathur.communicate.telephony++import com.vayunmathur.communicate.data.CommunicateLine+import com.vayunmathur.communicate.data.whatsapp.WhatsAppEvent+import com.vayunmathur.communicate.data.whatsapp.canonicalWhatsAppChatJid+import com.vayunmathur.communicate.notifications.ConversationSpace+import com.vayunmathur.communicate.notifications.ConversationTarget+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFalse+import kotlin.test.assertTrue+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner++/**+ * Regression: WhatsApp client events name conversations `wa:<jid>` (the+ * processor strips that to the canonical JID the Room cache is keyed by),+ * but the notification target used the raw event id. Its shortcut id then+ * differed from the one the open conversation cancels, and its tap intent+ * carried a remoteId that matches no stored conversation.+ */+@RunWith(RobolectricTestRunner::class)+class WhatsAppNotificationTargetTest {++ private fun incoming(conversationId: String, peerName: String? = "Pat") = WhatsAppEvent.IncomingMessage(+ conversationId = conversationId,+ messageId = "m1",+ body = "hello",+ peerName = peerName,+ peerPhone = null,+ timestamp = 1_700_000_000_000L,+ senderName = "Pat",+ )++ @Test+ fun canonicalJidStripsPrefixAndDeviceSuffix() {+ assertEquals("[email protected]", canonicalWhatsAppChatJid("wa:15551234567:[email protected]"))+ assertEquals("[email protected]", canonicalWhatsAppChatJid("[email protected]"))+ assertEquals("[email protected]", canonicalWhatsAppChatJid("wa:[email protected]"))+ }++ @Test+ fun notificationTargetUsesCanonicalJidForOneToOne() {+ val target = whatsAppNotificationTarget(incoming("wa:15551234567:[email protected]"))++ assertEquals(CommunicateLine.WhatsApp, target.line)+ assertEquals("[email protected]", target.remoteId)+ assertEquals("+15551234567", target.address)+ assertFalse(target.isGroup)+ assertEquals("WhatsApp:[email protected]", ConversationSpace.shortcutId(target))+ }++ @Test+ fun notificationTargetMatchesShortcutCancelledWhenThreadOpens() {+ val target = whatsAppNotificationTarget(incoming("wa:[email protected]"))+ // The conversation screen cancels by the thread's remoteId, which is+ // the canonical JID from the Room-backed thread list.+ val openedThread = ConversationTarget(+ line = CommunicateLine.WhatsApp,+ address = "+15551234567",+ remoteId = "[email protected]",+ personName = "+15551234567",+ )++ assertEquals(ConversationSpace.shortcutId(openedThread), ConversationSpace.shortcutId(target))+ }++ @Test+ fun notificationTargetUsesCanonicalJidForGroup() {+ val target = whatsAppNotificationTarget(incoming("wa:[email protected]", peerName = "Club"))++ assertEquals("[email protected]", target.remoteId)+ assertEquals("[email protected]", target.address)+ assertTrue(target.isGroup)+ assertEquals("Club", target.title)+ assertEquals("WhatsApp:[email protected]", ConversationSpace.shortcutId(target))+ }+}