Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): remember a missing twin chat-state row

Yudhi Armyndharis69c1244be3dc
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 0d4dc6d16..40e9781a3 100644
--- a/src/engine/adapters/baileys-chat-state-store.service.spec.ts
+++ b/src/engine/adapters/baileys-chat-state-store.service.spec.ts
@@ -603,6 +603,23 @@ describe('ChatStateStoreService', () => {
expect(repo.rows.get(KEY('s', PHONE))).toMatchObject({ pinned: true, muteEndTime: -1, archived: true });
});
+ it('queries a twin with no row once, not on every fold', async () => {
+ const repo = makeRepo();
+ const svc = svcWith(repo);
+ for (let i = 0; i < 3; i++) await svc.fold('s', PHONE, [LID], { archived: false }, false);
+ expect(repo.findOne).toHaveBeenCalledWith({ where: { sessionId: 's', chatId: LID } });
+ expect(repo.findOne.mock.calls.filter(([o]) => o.where.chatId === LID)).toHaveLength(1);
+ });
+
+ it('does not query a twin again once its row was folded away', async () => {
+ const repo = makeRepo([{ sessionId: 's', chatId: LID, pinned: true, updatedAt: new Date(1000) }]);
+ const svc = svcWith(repo);
+ await svc.fold('s', PHONE, [LID], { archived: false }, false);
+ expect([...repo.rows.keys()]).toEqual([KEY('s', PHONE)]);
+ await svc.fold('s', PHONE, [LID], { archived: false }, false);
+ expect(repo.findOne.mock.calls.filter(([o]) => o.where.chatId === LID)).toHaveLength(1);
+ });
+
it('applies only the patch and keeps the twin when a row cannot be read', async () => {
const repo = coldNewerPhone();
const svc = svcWith(repo);
diff --git a/src/engine/adapters/baileys-chat-state-store.service.ts b/src/engine/adapters/baileys-chat-state-store.service.ts
index d9229338f..c12044100 100644
--- a/src/engine/adapters/baileys-chat-state-store.service.ts
+++ b/src/engine/adapters/baileys-chat-state-store.service.ts
@@ -276,6 +276,8 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {
const cached = this.states.get(k);
if (cached || this.completeSessions.has(sessionId) || this.absent.has(k)) return cached;
const row = await this.repo.findOne({ where: { sessionId, chatId } });
+ // Recorded like a listing's miss, or every fold of a lid-addressed chat queries its twin again.
+ if (!row && !this.states.has(k)) this.markAbsent(k);
return row ? fromRow(row) : undefined;
}
@@ -409,6 +411,7 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {
this.writes.enqueue(k, async () => {
try {
await this.repo.delete({ sessionId, chatId });
+ this.markAbsent(k);
} catch (err) {
this.logger.warn(
`Failed to forget chat state for ${chatId}: ${err instanceof Error ? err.message : String(err)}`,