Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): address review on @mention resolution
Tanmay Chachra86f53559f4c1
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 0870f7207..561eb02f2 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -25,7 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Fixed-- The dashboard Chats thread renders an `@<digits>` mention as `@FirstName` when that participant has posted in the loaded thread; an unmatched mention is left as WhatsApp sent it.+- The dashboard Chats thread renders an `@<digits>` mention as `@FirstName` when that participant has posted in the loaded thread and their id matches the digits in the body. This covers whatsapp-web.js, where the author id and the mention carry the same digits. On Baileys the author is normalized to the phone number when the lid mapping is known while the body keeps the lid digits, so those mentions stay as WhatsApp sent them, as does any mention of someone who has not posted. The name is reduced to letters, digits and spaces before it is inserted, so a push name cannot become a link.- Baileys sessions list the saved address book again after a process restart. WhatsApp sends neither history nor an app-state snapshot to a device that has synced once, and the contact store is in memory, so a restarted gateway answered `GET /api/sessions/:sessionId/contacts` with only the peers it had seen since. A reconnect that finds the store empty now re-pulls the snapshot of the app-state collection that carries saved contacts. The list is also the address book only: a peer known by pushname alone is no longer listed, while `GET .../contacts/:contactId` still resolves it with `isMyContact: false`, and a chat title from history sync no longer overwrites a saved name. Thanks @gabrielmmoraes1999.- whatsapp-web.js sessions send media again on the WhatsApp Web builds rolled out on 2026-09-17: the library spread the media model into the outgoing message after its id, and the model's private `__x_id` clobbered it, so every image, video, audio, document, sticker and status media send failed with `Data passed to getter must include an id property` while text kept working. An install-time patch carries upstream's one-line fix until a whatsapp-web.js release ships it ([#1643](https://github.com/rmyndharis/OpenWA/issues/1643), [#1636](https://github.com/rmyndharis/OpenWA/issues/1636)). Thanks @15874611923 and @Magnarks for the reports.- An ingress route can declare `dedupOn: "body"` to key provider retries on the raw body instead of the delivery-id header. A provider that mints a fresh delivery id on every retry attempt, as supabase/auth does inside its hook retry loop, was never deduplicated when an ack was lost, so the contact received the message twice; routes that do not declare it keep the header ([#1641](https://github.com/rmyndharis/OpenWA/issues/1641)).diff --git a/dashboard/src/components/chats/ChatThread.tsx b/dashboard/src/components/chats/ChatThread.tsxindex 278f078a1..8eb29c952 100644--- a/dashboard/src/components/chats/ChatThread.tsx+++ b/dashboard/src/components/chats/ChatThread.tsx@@ -2,7 +2,13 @@ import { useCallback, useEffect, useMemo, useState, type RefObject } from 'reactimport { useTranslation } from 'react-i18next';import { AlertCircle, ChevronDown, CornerUpLeft, Loader2, MessageSquare, Smile, Trash2 } from 'lucide-react';import { sessionApi, type Chat } from '../../services/api';-import { buildMentionNameMap, getMediaSrc, resolveMentions, senderKey, type ChatMessageView } from '../../utils/chatMessages';+import {+ buildMentionNameMap,+ getMediaSrc,+ resolveMentions,+ senderKey,+ type ChatMessageView,+} from '../../utils/chatMessages';import { shouldFetchOlderMessages } from '../../utils/scrollDecision';import MessageBody from './MessageBody';diff --git a/dashboard/src/utils/chatMessages.test.ts b/dashboard/src/utils/chatMessages.test.tsindex b32ff0142..f85c8cf77 100644--- a/dashboard/src/utils/chatMessages.test.ts+++ b/dashboard/src/utils/chatMessages.test.ts@@ -442,26 +442,26 @@ test('mergeReactionSnapshot stays undefined when neither side knows anything', (assert.equal(mergeReactionSnapshot(undefined, undefined), undefined);});-test('buildMentionNameMap keys on the author JID\'s local part, stripped of a :device suffix', () => {+test("buildMentionNameMap keys on the author JID's local part, stripped of a :device suffix", () => {const map = buildMentionNameMap([msg({ author: '166868170059932@lid', chatName: 'Sneha Desai' }),- msg({ author: '[email protected]:7', chatName: 'Group Admin' }),+ msg({ author: '628111:[email protected]', chatName: 'Group Admin' }),]);assert.equal(map.get('166868170059932'), 'Sneha Desai');assert.equal(map.get('628111'), 'Group Admin');});test('buildMentionNameMap skips a row with no author or no resolved name', () => {- const map = buildMentionNameMap([msg({ author: undefined, chatName: 'Sneha Desai' }), msg({ author: '[email protected]', chatName: undefined })]);+ const map = buildMentionNameMap([+ msg({ author: undefined, chatName: 'Sneha Desai' }),+ msg({ author: '[email protected]', chatName: undefined }),+ ]);assert.equal(map.size, 0);});test('resolveMentions replaces a matched @<digits> token with @<FirstName>', () => {const names = buildMentionNameMap([msg({ author: '166868170059932@lid', chatName: 'Sneha Desai' })]);- assert.equal(- resolveMentions('Hi @166868170059932, any update?', names),- 'Hi @Sneha, any update?',- );+ assert.equal(resolveMentions('Hi @166868170059932, any update?', names), 'Hi @Sneha, any update?');});test('resolveMentions leaves an unmatched @<digits> token exactly as WhatsApp sent it', () => {@@ -477,3 +477,45 @@ test('resolveMentions does not touch a short @-token that is not a real mentiontest('resolveMentions is a no-op with an empty name map (skips the regex pass entirely)', () => {assert.equal(resolveMentions('Hi @166868170059932', new Map()), 'Hi @166868170059932');});++test('buildMentionNameMap only keys a device-suffixed author when the :device part is stripped', () => {+ // Without .split(':')[0] the local part is '628111:7', fails /^\d+$/ and the entry is dropped.+ assert.equal(+ buildMentionNameMap([msg({ author: '628111:[email protected]', chatName: 'Ravi' })]).get('628111'),+ 'Ravi',+ );+});++test('a push name cannot become a link: link characters are stripped before it is spliced in', () => {+ const names = buildMentionNameMap([msg({ author: '[email protected]', chatName: 'bit.ly/free' })]);+ assert.equal(resolveMentions('hi @6281112345', names), 'hi @bitlyfree');+});++test('a push name with WhatsApp formatting characters is reduced to plain text', () => {+ const names = buildMentionNameMap([msg({ author: '[email protected]', chatName: '*_Ravi_* ~x~' })]);+ assert.equal(resolveMentions('@6281112345', names), '@Ravi');+});++test('resolveMentions needs a left boundary: it does not fire inside an email address', () => {+ const names = buildMentionNameMap([msg({ author: '[email protected]', chatName: 'Ravi' })]);+ assert.equal(resolveMentions('mail [email protected] now', names), 'mail [email protected] now');+ assert.equal(resolveMentions('(@12345678) and, @12345678.', names), '(@Ravi) and, @Ravi.');+ assert.equal(resolveMentions('@12345678', names), '@Ravi');+});++test('a blank push name is skipped, so the mention stays as WhatsApp sent it instead of a bare @', () => {+ const names = buildMentionNameMap([+ msg({ author: '[email protected]', chatName: ' ' }),+ msg({ author: '[email protected]', chatName: '///' }),+ ]);+ assert.equal(names.size, 0);+ assert.equal(resolveMentions('@6281112345 @6281112346', names), '@6281112345 @6281112346');+});++test('a later usable name for the same participant is still picked up after a blank one', () => {+ const names = buildMentionNameMap([+ msg({ author: '[email protected]', chatName: ' ' }),+ msg({ author: '[email protected]', chatName: 'Ravi Kumar' }),+ ]);+ assert.equal(resolveMentions('@6281112345', names), '@Ravi');+});diff --git a/dashboard/src/utils/chatMessages.ts b/dashboard/src/utils/chatMessages.tsindex c4b649fe2..0aebfe6a7 100644--- a/dashboard/src/utils/chatMessages.ts+++ b/dashboard/src/utils/chatMessages.ts@@ -124,11 +124,25 @@ export function buildMentionNameMap(messages: Pick<ChatMessage, 'author' | 'chatfor (const m of messages) {if (!m.author || !m.chatName) continue;const local = m.author.split('@')[0].split(':')[0];- if (/^\d+$/.test(local) && !map.has(local)) map.set(local, m.chatName);+ const name = safeMentionName(m.chatName);+ if (name && /^\d+$/.test(local) && !map.has(local)) map.set(local, name);}return map;}+/**+ * A push name is set freely by any WhatsApp user and is spliced into text that MessageBody later+ * linkifies, so keep only letters, digits, marks and spaces: `bit.ly/free` becomes `bitlyfree`, which can+ * never form a link or WhatsApp formatting. Returns '' when nothing usable is left, so the caller keeps+ * the original `@<digits>` instead of rendering a bare `@`.+ */+function safeMentionName(name: string): string {+ return name+ .replace(/[^\p{L}\p{N}\p{M} ]/gu, '')+ .replace(/\s+/g, ' ')+ .trim();+}+/*** Replace "@<digits>" mention tokens with "@<FirstName>" wherever the digits match a known* participant (see buildMentionNameMap). An unmatched token is left exactly as WhatsApp sent it —@@ -138,9 +152,10 @@ export function buildMentionNameMap(messages: Pick<ChatMessage, 'author' | 'chat*/export function resolveMentions(text: string, names: Map<string, string>): string {if (names.size === 0 || !text.includes('@')) return text;- return text.replace(/@(\d{7,})/g, (full: string, digits: string) => {- const name = names.get(digits);- return name ? `@${name.split(' ')[0]}` : full;+ // The left boundary keeps this off the middle of a word: `[email protected]` is not a mention.+ return text.replace(/(?<![\p{L}\p{N}])@(\d{7,})/gu, (full: string, digits: string) => {+ const first = names.get(digits)?.split(' ')[0];+ return first ? `@${first}` : full;});}