Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(message): store the quoted text for every quoting send, not just a reply

Yudhi Armyndharisd99cc552050a
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d4b4d0874..96d592744 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -48,6 +48,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- A QR code that arrives while the dashboard is asking the server about a disconnect no longer has its modal closed underneath it. The handler blanks the displayed code, re-reads the session list, and closed the modal when the answer said no engine was left; a reconnect completing inside that window had already pushed a fresh, scannable code.
- Escape dismisses the Chats emoji picker instead of closing the whole conversation behind it.
- A click on a WhatsApp Business prompt quotes the prompt in the stored reply, so the dashboard renders the question above the answer instead of an empty quote box.
+- A send that quotes a message stores the quoted text, whatever the send is. Only replies did: an image, video, document, location, contact, poll or text sent with `quotedMessageId` stored the quoted id with an empty body, and the dashboard drew a blank quote box above it.
- A template prompt that numbers only some of its buttons no longer answers the bot with an index belonging to a different button. A template that carries its own numbering is answered with it, one that carries none is answered by position, and a choice left unnumbered inside a numbered template is answered by id alone, with no index at all. Such a choice is still offered in `buttons[]` and still accepted by the click route.
- A proxy password no longer reaches the log. A failed SOCKS connect carries the whole proxy config as the error's only property, and `BAILEYS_LOG_LEVEL=debug` wrote it to stdout verbatim.
- 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. Each session now re-pulls the snapshot of the app-state collection that carries saved contacts, once per engine start. That also repairs a PARTIAL address book, which is the usual shape: during the initial sync the library's event buffer folds the saved-contact updates into the history batch it is already holding, where their names are stripped as chat titles, so the store keeps plenty of contacts with the saved ones missing. 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.
diff --git a/src/modules/message/message-send.service.spec.ts b/src/modules/message/message-send.service.spec.ts
index a3ef86904..2eb33e03f 100644
--- a/src/modules/message/message-send.service.spec.ts
+++ b/src/modules/message/message-send.service.spec.ts
@@ -938,6 +938,47 @@ describe('MessageSendService', () => {
expect(mockEngine.sendTextMessage).toHaveBeenCalledWith('[email protected]', 'hi');
});
+
+ it('stores the quoted message body, not an empty quote box', async () => {
+ // Every sender but reply and click-button routes the quoted id through the shared persist,
+ // which hardcoded an empty body, so the dashboard drew a blank quote above a quoted image,
+ // location, contact or poll.
+ (repository.findOne as jest.Mock).mockResolvedValueOnce({ id: 'row-9', body: 'the original text' });
+
+ await service.sendImage('sess-1', {
+ chatId: '[email protected]',
+ url: 'https://example.com/a.png',
+ quotedMessageId: 'wa-quoted-9',
+ });
+
+ expect(repository.findOne).toHaveBeenCalledWith({
+ where: { sessionId: 'sess-1', waMessageId: 'wa-quoted-9' },
+ });
+ expect(repository.create).toHaveBeenCalledWith(
+ expect.objectContaining({
+ metadata: expect.objectContaining({
+ quotedMessage: { id: 'wa-quoted-9', body: 'the original text' },
+ }) as unknown,
+ }),
+ );
+ });
+
+ it('still stores the quote when the quoted row cannot be read', async () => {
+ (repository.findOne as jest.Mock).mockRejectedValueOnce(new Error('database is locked'));
+
+ await service.sendLocation('sess-1', {
+ chatId: '[email protected]',
+ latitude: 1,
+ longitude: 2,
+ quotedMessageId: 'wa-quoted-9',
+ });
+
+ expect(repository.create).toHaveBeenCalledWith(
+ expect.objectContaining({
+ metadata: expect.objectContaining({ quotedMessage: { id: 'wa-quoted-9', body: '' } }) as unknown,
+ }),
+ );
+ });
});
// ── reply / forward ───────────────────────────────────────────────
@@ -1044,6 +1085,11 @@ describe('MessageSendService', () => {
buttonId: 'yes',
});
+ // The lookup is scoped to the session: without it, one session's prompt body could be quoted
+ // into another session's outgoing row.
+ expect(repository.findOne).toHaveBeenCalledWith({
+ where: { sessionId: 'sess-1', waMessageId: 'PROMPT-1' },
+ });
expect(repository.create).toHaveBeenCalledWith(
expect.objectContaining({
metadata: expect.objectContaining({
diff --git a/src/modules/message/message-send.service.ts b/src/modules/message/message-send.service.ts
index 236fefca4..9e1a46c17 100644
--- a/src/modules/message/message-send.service.ts
+++ b/src/modules/message/message-send.service.ts
@@ -483,15 +483,7 @@ export class MessageSendService {
const engine = this.getEngine(sessionId);
// Resolve the quoted message body (best-effort) so the dashboard can render the reply preview.
- let quotedBody = '';
- try {
- const quoted = await this.messageRepository.findOne({
- where: { sessionId, waMessageId: finalDto.quotedMessageId },
- });
- quotedBody = quoted?.body || '';
- } catch (err) {
- this.logger.warn(`Failed to resolve quoted message ${finalDto.quotedMessageId}`, { error: String(err) });
- }
+ const quotedBody = await this.resolveQuotedBody(sessionId, finalDto.quotedMessageId);
// Save message as pending BEFORE sending
const message = await this.saveOutgoingMessage(sessionId, {
@@ -526,15 +518,7 @@ export class MessageSendService {
// A click IS a reply to the prompt, so resolve the prompt's body the way reply() does: the
// dashboard renders the quote box from this field, and a hardcoded empty string left every
// answered prompt showing an empty quote above the choice the user tapped.
- let promptBody = '';
- try {
- const prompt = await this.messageRepository.findOne({
- where: { sessionId, waMessageId: finalDto.messageId },
- });
- promptBody = prompt?.body || '';
- } catch (err) {
- this.logger.warn(`Failed to resolve prompt message ${finalDto.messageId}`, { error: String(err) });
- }
+ const promptBody = await this.resolveQuotedBody(sessionId, finalDto.messageId);
const message = await this.saveOutgoingMessage(sessionId, {
chatId: finalDto.chatId,
@@ -589,6 +573,23 @@ export class MessageSendService {
return this.persistSentState(message, result);
}
+ /**
+ * The body of the message a send is quoting, for the dashboard's quote preview. Best-effort in
+ * every direction: an id that names nothing stored (evicted, or sent from the phone before this
+ * gateway saw the chat) and a read that fails both answer '', because a preview is never worth
+ * failing a send over. The row is looked up by engine id within the session, so one session
+ * cannot read another's message.
+ */
+ private async resolveQuotedBody(sessionId: string, quotedMessageId: string): Promise<string> {
+ try {
+ const quoted = await this.messageRepository.findOne({ where: { sessionId, waMessageId: quotedMessageId } });
+ return quoted?.body || '';
+ } catch (err) {
+ this.logger.warn(`Failed to resolve quoted message ${quotedMessageId}`, { error: String(err) });
+ return '';
+ }
+ }
+
/**
* Save outgoing message to database.
* When called before sending, creates a record with PENDING status; bulk send reuses this after a
@@ -602,6 +603,13 @@ export class MessageSendService {
*/
async saveOutgoingMessage(sessionId: string, data: SaveOutgoingMessageData): Promise<Message> {
const session = await this.sessionService.findOne(sessionId);
+ // Resolved here rather than in each sender: reply and click-button build their own
+ // `metadata.quotedMessage` and never reach this branch, so every OTHER quoting sender (media,
+ // location, contact, poll, quoted text) used to persist an id with an empty body and the
+ // dashboard drew a blank quote box above the message.
+ const quotedMessage = data.quotedMessageId
+ ? { id: data.quotedMessageId, body: await this.resolveQuotedBody(sessionId, data.quotedMessageId) }
+ : undefined;
const message = this.messageRepository.create({
sessionId,
// An engine that sent a message but could not read its id back reports an empty id (see the
@@ -619,9 +627,7 @@ export class MessageSendService {
direction: MessageDirection.OUTGOING,
timestamp: data.timestamp,
status: data.status ?? MessageStatus.PENDING,
- metadata: data.quotedMessageId
- ? { ...data.metadata, quotedMessage: { id: data.quotedMessageId, body: '' } }
- : data.metadata,
+ metadata: quotedMessage ? { ...data.metadata, quotedMessage } : data.metadata,
});
const saved = await this.messageRepository.save(message).catch(async (err: unknown) => {
const waMessageId = message.waMessageId;