Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): keep the quoted message when replying with an attachment
Yudhi Armyndharis0a0a7f567ccf
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex b49b8e703..893dd8611 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0- The JavaScript, Python, Go and Java SDKs omitted `contact`, `call` and `ephemeralDuration` from their chat-history message type, so a typed client had to cast to read three fields the endpoint returns; all four now mirror the engine payload.- `POST /api/infra/import-data` bound its body to an inline type, which erases at runtime, so on the replace-all restore the global ValidationPipe's `whitelist` and `forbidNonWhitelisted` never ran: a body carrying no `tables` failed as a 500 from inside the import, and a misspelled key was accepted in silence. It now takes a DTO — a missing `tables` answers 400 naming the field, an unknown key is refused, and `force`/`stopOrphans` accept only a real boolean or an exact `'true'`/`'false'`, so an ambiguous spelling cannot open the orphan-engine escape.- The engine parity gate took an adapter file's engine from its filename prefix and credited the rest to whatsapp-web.js, so a `501` added to one of the five unprefixed modules would be blamed on the wrong engine — and the invariant would then demand that the engine which really refuses be marked `supported`. Those files are now attributed explicitly, and one that is neither prefixed nor attributed fails the spec by name.+- Replying with an attachment in the dashboard composer silently dropped the quote: the media branch never carried the quoted id, the optimistic bubble's metadata took the same either/or, and the reply banner was cleared regardless — so it looked exactly like a quoted reply had been sent. Both payloads now carry the quote, and the two builders moved to a tested utility module.### Documentationdiff --git a/dashboard/src/components/chats/ChatComposer.tsx b/dashboard/src/components/chats/ChatComposer.tsxindex 2de44b983..f6814ffda 100644--- a/dashboard/src/components/chats/ChatComposer.tsx+++ b/dashboard/src/components/chats/ChatComposer.tsx@@ -5,6 +5,7 @@ import { Loader2, Paperclip, Send, Smile, X } from 'lucide-react';import { messageApi, type Chat, type MessageType } from '../../services/api';import { mergeOrAppend, type ChatMessageView } from '../../utils/chatMessages';import { promoteChatWithSnippet } from '../../utils/chatList';+import { buildMediaSendPayload, buildOptimisticMetadata, quotedIdOf } from '../../utils/composerSend';import { messagesQueryKey, useChatMessagesActions } from '../../hooks/useChatMessages';import { useRole } from '../../hooks/useRole';import { useToast } from '../../hooks/useToast';@@ -175,22 +176,7 @@ function ChatComposer({direction: 'outgoing',status: 'pending',createdAt: new Date().toISOString(),- metadata: attachment- ? {- media: {- mimetype: attachment.mimetype,- filename: attachment.filename,- data: attachment.base64,- },- }- : replyingTo- ? {- quotedMessage: {- id: replyingTo.waMessageId || replyingTo.id,- body: replyingTo.type !== 'text' ? `[${replyingTo.type}]` : replyingTo.body,- },- }- : undefined,+ metadata: buildOptimisticMetadata(attachment, replyingTo),};appendMessage(selectedSessionId, activeChat.id, tempMessage);@@ -211,16 +197,18 @@ function ChatComposer({else if (mime.startsWith('video/')) mediaType = 'video';else if (mime.startsWith('audio/')) mediaType = 'audio';- result = await messageApi.sendMedia(selectedSessionId, activeChat.id, mediaType, {- base64: currentAttachment.base64,- mimetype: currentAttachment.mimetype,- filename: currentAttachment.filename,- caption: mediaType !== 'audio' ? textToSend : undefined,- });+ // A reply that carries an attachment takes this branch, so the quote has to travel with the+ // media — the `else if` below never sees it.+ result = await messageApi.sendMedia(+ selectedSessionId,+ activeChat.id,+ mediaType,+ buildMediaSendPayload(currentAttachment, mediaType !== 'audio' ? textToSend : undefined, currentReplyingTo),+ );} else if (currentReplyingTo) {result = await messageApi.reply(selectedSessionId, {chatId: activeChat.id,- quotedMessageId: currentReplyingTo.waMessageId || currentReplyingTo.id,+ quotedMessageId: quotedIdOf(currentReplyingTo)!,text: textToSend,});} else {diff --git a/dashboard/src/services/api.ts b/dashboard/src/services/api.tsindex bc6ca6721..ababf8575 100644--- a/dashboard/src/services/api.ts+++ b/dashboard/src/services/api.ts@@ -332,6 +332,8 @@ export interface SendMediaPayload {mimetype?: string;filename?: string;caption?: string;+ /** Quote an earlier message, making the media send a reply. Omit for an ordinary send. */+ quotedMessageId?: string;}// Payloads below mirror the backend DTOs in src/modules/message/dto (raw bodies, no envelope).diff --git a/dashboard/src/utils/composerSend.test.ts b/dashboard/src/utils/composerSend.test.tsnew file mode 100644index 000000000..994ff0ade--- /dev/null+++ b/dashboard/src/utils/composerSend.test.ts@@ -0,0 +1,72 @@+import { test } from 'node:test';+import assert from 'node:assert/strict';+import { quotedIdOf, buildMediaSendPayload, buildOptimisticMetadata } from './composerSend.ts';++/**+ * Replying with an attachment used to drop the quote silently.+ *+ * The composer treated "has an attachment" and "is replying" as mutually exclusive — the media+ * branch never carried the quoted id, and the optimistic bubble's metadata took the same either/or.+ * Because the reply banner is cleared before the send either way, the UI looked exactly like a+ * successful quoted reply. Nothing failed, so nothing surfaced it.+ *+ * These are the two payloads that were wrong, extracted so they can be asserted directly. Rendering+ * the composer would also work — `pages/Chats.test.ts` and two siblings do render real components+ * under jsdom — but that path costs a jsdom install and `createElement` scaffolding (the runner+ * cannot parse JSX, so `.test.tsx` is not an option), and these are pure payload builders with no+ * DOM in them. Most of this project's suites test `utils/` for the same reason.+ */++const REPLY = { id: 'local-1', waMessageId: '[email protected]_3EB0', type: 'text', body: 'the original' };+const ATTACHMENT = { base64: 'AAAA', mimetype: 'image/png', filename: 'a.png' };++test('quotedIdOf prefers the WhatsApp id over the local one', () => {+ assert.equal(quotedIdOf(REPLY), '[email protected]_3EB0');+});++test('quotedIdOf falls back to the local id before the echo assigns a WA id', () => {+ assert.equal(quotedIdOf({ ...REPLY, waMessageId: undefined }), 'local-1');+});++test('quotedIdOf yields nothing when not replying', () => {+ assert.equal(quotedIdOf(null), undefined);+});++test('a media send while replying carries the quoted id', () => {+ const payload = buildMediaSendPayload(ATTACHMENT, 'look at this', REPLY);++ assert.equal(payload.quotedMessageId, '[email protected]_3EB0');+ // The attachment must survive intact — the quote is added, not substituted.+ assert.equal(payload.base64, 'AAAA');+ assert.equal(payload.mimetype, 'image/png');+ assert.equal(payload.filename, 'a.png');+ assert.equal(payload.caption, 'look at this');+});++// Known-negative control: without it an implementation that always emitted the key would satisfy+// the assertion above while sending `quotedMessageId: undefined` on every ordinary media send.+test('a media send that is not a reply carries no quote key at all', () => {+ const payload = buildMediaSendPayload(ATTACHMENT, 'look at this', null);++ assert.equal('quotedMessageId' in payload, false);+});++test('the optimistic bubble shows BOTH the media and the quote', () => {+ const metadata = buildOptimisticMetadata(ATTACHMENT, REPLY);++ assert.equal(metadata?.media?.data, 'AAAA');+ assert.equal(metadata?.quotedMessage?.id, '[email protected]_3EB0');+ assert.equal(metadata?.quotedMessage?.body, 'the original');+});++// A non-text quoted message has no useful body to preview, so the type stands in for it — the+// behaviour the composer already had for text-only replies, preserved now that media can coexist.+test('a quoted non-text message previews as its type', () => {+ const metadata = buildOptimisticMetadata(null, { ...REPLY, type: 'image', body: '' });++ assert.equal(metadata?.quotedMessage?.body, '[image]');+});++test('no attachment and no reply produces no metadata', () => {+ assert.equal(buildOptimisticMetadata(null, null), undefined);+});diff --git a/dashboard/src/utils/composerSend.ts b/dashboard/src/utils/composerSend.tsnew file mode 100644index 000000000..93c298b88--- /dev/null+++ b/dashboard/src/utils/composerSend.ts@@ -0,0 +1,84 @@+/**+ * Payload builders for the chat composer's send path.+ *+ * These live here rather than inline in `ChatComposer` because the composer treated "has an+ * attachment" and "is replying" as mutually exclusive, and the resulting bug — replying with an+ * attachment silently dropped the quote — had no assertion anywhere. As pure payload builders they+ * are cheaper to test here than through a rendered composer, which is the shape most of this+ * project's suites already take.+ */++/** The subset of a message the composer needs in order to quote it. */+export interface QuotableMessage {+ id: string;+ waMessageId?: string | null;+ type?: string;+ body?: string;+}++/** The subset of a staged attachment the composer needs in order to send it. */+export interface ComposerAttachment {+ base64: string;+ mimetype: string;+ filename?: string;+}++export interface OptimisticMetadata {+ media?: { mimetype: string; filename?: string; data: string };+ quotedMessage?: { id: string; body: string };+}++/**+ * The id to quote. The WhatsApp id wins, but a message that has only just been sent optimistically+ * has no WA id yet, so the local id is the fallback — replying to your own just-sent message is an+ * ordinary thing to do and must not silently lose its quote.+ */+export function quotedIdOf(replyingTo: QuotableMessage | null | undefined): string | undefined {+ if (!replyingTo) return undefined;+ return replyingTo.waMessageId || replyingTo.id;+}++/**+ * Body text for the quoted-message preview. A non-text message has no meaningful body to show, so+ * its type stands in — matching what the composer already displayed for text-only replies.+ */+function quotedPreviewBody(replyingTo: QuotableMessage): string {+ return replyingTo.type && replyingTo.type !== 'text' ? `[${replyingTo.type}]` : (replyingTo.body ?? '');+}++/**+ * Body for a media send. The quote key is omitted entirely rather than set to `undefined` when the+ * composer is not replying: the API rejects unknown/empty fields, and an always-present key would+ * also make every ordinary media send look like a failed reply in a request log.+ */+export function buildMediaSendPayload(+ attachment: ComposerAttachment,+ caption: string | undefined,+ replyingTo: QuotableMessage | null | undefined,+): { base64: string; mimetype: string; filename?: string; caption?: string; quotedMessageId?: string } {+ const quotedMessageId = quotedIdOf(replyingTo);+ return {+ base64: attachment.base64,+ mimetype: attachment.mimetype,+ filename: attachment.filename,+ caption,+ ...(quotedMessageId ? { quotedMessageId } : {}),+ };+}++/**+ * Metadata for the optimistic bubble. Media and quote are independent: an attachment sent as a+ * reply has both, which is precisely the combination the previous either/or could not express.+ */+export function buildOptimisticMetadata(+ attachment: ComposerAttachment | null | undefined,+ replyingTo: QuotableMessage | null | undefined,+): OptimisticMetadata | undefined {+ if (!attachment && !replyingTo) return undefined;+ return {+ ...(attachment+ ? { media: { mimetype: attachment.mimetype, filename: attachment.filename, data: attachment.base64 } }+ : {}),+ ...(replyingTo ? { quotedMessage: { id: quotedIdOf(replyingTo)!, body: quotedPreviewBody(replyingTo) } } : {}),+ };+}