Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(message): stop rewriting unchanged metadata on send

Yudhi Armyndharisaa048d4cc241
diff --git a/src/modules/message/message-send.service.spec.ts b/src/modules/message/message-send.service.spec.ts
index 11485f051..3fd7441c1 100644
--- a/src/modules/message/message-send.service.spec.ts
+++ b/src/modules/message/message-send.service.spec.ts
@@ -744,6 +744,18 @@ describe('MessageSendService', () => {
);
});
+ // The payload is stored once, by the PENDING insert. Writing it again on every SENT update would
+ // double the WAL and the table growth of a base64 send for no change in what the row holds.
+ it('marks the row SENT without writing the media payload a second time', async () => {
+ await service.sendImage('sess-1', { chatId: '[email protected]', base64: 'QUJD', mimetype: 'image/png' });
+
+ const update = repository.update as jest.Mock<unknown, [unknown, Record<string, unknown>]>;
+ expect(update).toHaveBeenCalledTimes(1);
+ const [, patch] = update.mock.calls[0];
+ expect(patch).toMatchObject({ status: MessageStatus.SENT, waMessageId: 'wa-msg-1' });
+ expect(patch).not.toHaveProperty('metadata');
+ });
+
it('threads media mentions into the MediaInput (#530)', async () => {
await service.sendImage('sess-1', {
chatId: '[email protected]',
diff --git a/src/modules/message/message-send.service.ts b/src/modules/message/message-send.service.ts
index 42f4c3073..668921cea 100644
--- a/src/modules/message/message-send.service.ts
+++ b/src/modules/message/message-send.service.ts
@@ -636,7 +636,13 @@ export class MessageSendService {
button: { id: finalDto.buttonId, text: result.body },
};
}
- return this.persistSentState(message, result, result.body ? { body: result.body } : undefined);
+ return this.persistSentState(
+ message,
+ result,
+ result.body
+ ? { body: result.body, metadata: message.metadata as QueryDeepPartialEntity<Record<string, unknown>> }
+ : undefined,
+ );
}
async forward(
@@ -859,7 +865,9 @@ export class MessageSendService {
result: MessageResult,
// Columns a sender changed in memory after its PENDING insert. The write below is an UPDATE of
// fixed columns, so anything not listed here (the resolved button label, for one) is never stored.
- changed: Pick<QueryDeepPartialEntity<Message>, 'body'> = {},
+ // `metadata` is listed only when it changed: the insert already stored it, and a base64 media
+ // payload inside it would otherwise be written a second time on every send.
+ changed: Pick<QueryDeepPartialEntity<Message>, 'body' | 'metadata'> = {},
): Promise<MessageResponseDto> {
// A send whose engine couldn't read the sent message's id back reports an empty id — a forward that
// can't recover the copy, or a WhatsApp Web build that renamed the id field out from under the
@@ -879,7 +887,6 @@ export class MessageSendService {
waMessageId: message.waMessageId,
status: message.status,
timestamp: message.timestamp,
- metadata: message.metadata as QueryDeepPartialEntity<Record<string, unknown>>,
},
);
if (!updated.affected) return { messageId: result.id, timestamp: result.timestamp };