Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): drop a deleted chat from the chat-state cache at once

Yudhi Armyndharis0280beab1de4
diff --git a/src/engine/adapters/baileys-chat-state-store.service.spec.ts b/src/engine/adapters/baileys-chat-state-store.service.spec.ts
index 40e9781a3..be46609bc 100644
--- a/src/engine/adapters/baileys-chat-state-store.service.spec.ts
+++ b/src/engine/adapters/baileys-chat-state-store.service.spec.ts
@@ -426,6 +426,24 @@ describe('ChatStateStoreService', () => {
expect(svc.get('t', 'c')).toEqual(expect.objectContaining({ pinned: true }));
});
+ it('drops a forgotten chat from the listing before its row delete settles', async () => {
+ const repo = makeRepo([{ sessionId: 's', chatId: 'c', pinned: true }]);
+ const svc = svcWith(repo);
+ svc.get('s', 'c');
+ await tick();
+ expect(svc.chatIds('s')).toEqual(['c']);
+ let release!: () => void;
+ repo.delete.mockImplementationOnce(() => new Promise(resolve => (release = () => resolve(undefined))));
+ const done = svc.forget('s', ['c']);
+ expect(svc.chatIds('s')).toEqual([]);
+ expect(svc.get('s', 'c')).toBeUndefined();
+ await tick(); // a read in the window must not warm the row back from the table
+ expect(svc.chatIds('s')).toEqual([]);
+ release();
+ await done;
+ expect(svc.chatIds('s')).toEqual([]);
+ });
+
it('forget swallows a repo error', async () => {
const repo = makeRepo();
repo.delete.mockRejectedValueOnce(new Error('SQLITE_BUSY'));
diff --git a/src/engine/adapters/baileys-chat-state-store.service.ts b/src/engine/adapters/baileys-chat-state-store.service.ts
index c12044100..271efc4c7 100644
--- a/src/engine/adapters/baileys-chat-state-store.service.ts
+++ b/src/engine/adapters/baileys-chat-state-store.service.ts
@@ -402,11 +402,17 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {
}
}
- /** Queued behind each chat's pending writes, so a patch still in flight cannot re-create the row. */
+ /**
+ * Queued behind each chat's pending writes, so a patch still in flight cannot re-create the row. The
+ * cache drops the chat at once and knows it absent, so a listing in the meantime neither shows it nor
+ * warms it back from the table; the drop repeats after the delete for a patch queued ahead of it.
+ */
async forget(sessionId: string, chatIds: string[]): Promise<void> {
await Promise.all(
chatIds.map(chatId => {
const k = this.key(sessionId, chatId);
+ this.states.delete(k);
+ this.markAbsent(k);
return new Promise<void>(resolve =>
this.writes.enqueue(k, async () => {
try {