Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(message): re-read page rows without the keyset cursor

Yudhi Armyndharis089d27fd61ba
diff --git a/src/modules/message/message-list-media-budget.spec.ts b/src/modules/message/message-list-media-budget.spec.ts
index 726a8e239..02f9f956f 100644
--- a/src/modules/message/message-list-media-budget.spec.ts
+++ b/src/modules/message/message-list-media-budget.spec.ts
@@ -193,6 +193,23 @@ describe('message list reads inline media within the budget', () => {
expect(messages.map(m => m.id)).toEqual(['keep']);
});
+ it('still serves the picked page when the cursor row is deleted before its rows are read', async () => {
+ await seed(Array.from({ length: 5 }, (_, i) => ({ id: `c${i}`, metadata: null })));
+ const getMany = Object.getOwnPropertyDescriptor(SelectQueryBuilder.prototype, 'getMany')!
+ .value as SelectQueryBuilder<Message>['getMany'];
+ jest.spyOn(SelectQueryBuilder.prototype, 'getMany').mockImplementationOnce(async function (
+ this: SelectQueryBuilder<Message>,
+ ) {
+ await repository.delete({ id: 'c4' });
+ return getMany.call(this);
+ });
+
+ const { messages, total } = await service.getMessages(SESSION, { after: 'c4' });
+
+ expect(total).toBe(5);
+ expect(messages.map(m => m.id)).toEqual(['c3', 'c2', 'c1', 'c0']);
+ });
+
it.each([
['chat', { chatId: '[email protected]' }],
['sender', { from: '[email protected]' }],
diff --git a/src/modules/message/message.service.ts b/src/modules/message/message.service.ts
index 73f03f064..4b4fad80b 100644
--- a/src/modules/message/message.service.ts
+++ b/src/modules/message/message.service.ts
@@ -402,6 +402,9 @@ export class MessageService implements PluginMessagePort {
// `total` keeps its documented meaning, rows matching the filters, so count before narrowing.
const total = await query.clone().getCount();
+ // What the page's rows are re-read against. Taken before the cursor: the cursor is only a position the page
+ // ids already encode, and re-applying it would turn a cursor row deleted meanwhile into an empty page.
+ const rowSelection = query.clone();
if (after !== undefined) {
// A selection cursor must name a row inside this same selection, not merely a row from the
@@ -455,7 +458,7 @@ export class MessageService implements PluginMessagePort {
// base64 in metadata.media.data. Spent newest-first in the selected order, so the
// most recently viewed media still arrives inline and the rest keeps its omitted marker.
return {
- messages: await this.readPageSpendingBudget(query, page, inlineMediaBudget),
+ messages: await this.readPageSpendingBudget(rowSelection, page, inlineMediaBudget),
total,
...(unknownTimestampTotal !== undefined ? { unknownTimestampTotal } : {}),
};