Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): drop mention delimiters that arrive in raw message text

Yudhi Armyndharisdbc119faa162
diff --git a/dashboard/src/pages/Chats.tsx b/dashboard/src/pages/Chats.tsx
index eae65fe05..631abd5d9 100644
--- a/dashboard/src/pages/Chats.tsx
+++ b/dashboard/src/pages/Chats.tsx
@@ -29,6 +29,7 @@ import {
byMessageId,
getMediaSrc,
liveMessageMetadata,
+ stripMentionDelimiters,
type ChatMessageView,
type MessageMedia,
} from '../utils/chatMessages';
@@ -1032,7 +1033,7 @@ export function Chats() {
(channelMessages.data ?? []).map(m => (
<div key={m.id} className="message-bubble incoming">
{m.hasMedia && m.mediaUrl && <img className="channel-media" src={m.mediaUrl} alt="" />}
- {m.body && <MessageBody text={m.body} className="message-text" />}
+ {m.body && <MessageBody text={stripMentionDelimiters(m.body)} className="message-text" />}
<span className="message-time">{formatChatTime(m.timestamp)}</span>
</div>
))
@@ -1078,7 +1079,9 @@ export function Chats() {
type={item.type === 'video' ? 'video' : item.type === 'voice' ? 'audio' : 'image'}
/>
)}
- {item.caption && <MessageBody text={item.caption} className="message-text" />}
+ {item.caption && (
+ <MessageBody text={stripMentionDelimiters(item.caption)} className="message-text" />
+ )}
<span className="message-time">
{formatChatTime(Math.floor(new Date(item.timestamp).getTime() / 1000))}
</span>
diff --git a/dashboard/src/utils/chatMessages.test.ts b/dashboard/src/utils/chatMessages.test.ts
index a73271608..7c97ca15c 100644
--- a/dashboard/src/utils/chatMessages.test.ts
+++ b/dashboard/src/utils/chatMessages.test.ts
@@ -3,6 +3,7 @@ import assert from 'node:assert/strict';
import {
buildMentionNameMap,
resolveMentions,
+ stripMentionDelimiters,
mapEngineHistoryMessage,
mergeChatMessages,
mergeReactionSnapshot,
@@ -618,3 +619,11 @@ test('resolveMentions does not fire after "_" or "(" inside a URL, only when the
assert.equal(resolveMentions('hi _(@6281234567)_', names), `hi _(${wrap('@Ann')})_`);
assert.equal(resolveMentions('`x`*@6281234567*', names), `\`x\`*${wrap('@Ann')}*`);
});
+
+test('a span delimiter already in a raw body is dropped, so it can never parse as a mention', () => {
+ const raw = `see ${MENTION_OPEN}*not bold* https://x.example${MENTION_CLOSE} end`;
+ assert.equal(stripMentionDelimiters(raw), 'see *not bold* https://x.example end');
+ assert.equal(resolveMentions(raw, new Map()), 'see *not bold* https://x.example end');
+ const names = buildMentionNameMap([msg({ author: '[email protected]', chatName: 'Ann' })]);
+ assert.equal(resolveMentions(`${MENTION_OPEN} *hi* @6281112345`, names), ` *hi* ${wrap('@Ann')}`);
+});
diff --git a/dashboard/src/utils/chatMessages.ts b/dashboard/src/utils/chatMessages.ts
index 8de53ba03..b194c53cf 100644
--- a/dashboard/src/utils/chatMessages.ts
+++ b/dashboard/src/utils/chatMessages.ts
@@ -138,6 +138,13 @@ export function buildMentionNameMap(messages: Pick<ChatMessage, 'author' | 'chat
const MENTION_DELIMITERS = new RegExp(`[${MENTION_OPEN}${MENTION_CLOSE}]`, 'g');
+/**
+ * Drop any span delimiter already present in raw message text. Only resolveMentions may place one:
+ * parseMessageBody reads every MENTION_OPEN...MENTION_CLOSE pair as a mention, so a body that
+ * carries them itself would lose its formatting and links inside the pair.
+ */
+export const stripMentionDelimiters = (text: string): string => text.replace(MENTION_DELIMITERS, '');
+
// Characters that render as nothing yet survive `.trim()`: format controls (zero-width space, soft
// hyphen, word joiner, bidi marks), combining marks, the Hangul fillers and the blank braille
// pattern. A push name made only of these would render as a bare "@".
@@ -175,7 +182,8 @@ const CODE_SEGMENT = /(```[\s\S]*?```|`[^`]*`)/;
* so a URL or an inline-code span with the same digits is left untouched (this runs on the raw
* body, before parseMessageBody splits out code spans).
*/
-export function resolveMentions(text: string, names: Map<string, string>): string {
+export function resolveMentions(raw: string, names: Map<string, string>): string {
+ const text = stripMentionDelimiters(raw);
if (names.size === 0 || !text.includes('@')) return text;
return text
.split(CODE_SEGMENT)
diff --git a/dashboard/src/utils/messageFormatter.ts b/dashboard/src/utils/messageFormatter.ts
index fa2a68893..7359c63cc 100644
--- a/dashboard/src/utils/messageFormatter.ts
+++ b/dashboard/src/utils/messageFormatter.ts
@@ -26,8 +26,9 @@ const FORMATS: Record<string, 'bold' | 'italic' | 'strike'> = {
const BOUNDARY_CHAR = /^[\s.,;:!?()[\]{}'"<>]$/;
-// Private-use-area delimiters resolveMentions wraps a resolved name in. WhatsApp message text is
-// never PUA, so these can't collide with real content the way a printable marker could.
+// Private-use-area delimiters resolveMentions wraps a resolved name in. Raw text is stripped of
+// both before it gets here (chatMessages.ts#stripMentionDelimiters), so a pair in the input is
+// always one resolveMentions placed.
export const MENTION_OPEN = '';
export const MENTION_CLOSE = '';