Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(engine): answer 409 for a wwebjs group write after a teardown

Yudhi Armyndharisca8d0213adbe
diff --git a/src/engine/adapters/wwebjs-client-teardown.spec.ts b/src/engine/adapters/wwebjs-client-teardown.spec.ts
index 9f5e5001f..fa2cf91a4 100644
--- a/src/engine/adapters/wwebjs-client-teardown.spec.ts
+++ b/src/engine/adapters/wwebjs-client-teardown.spec.ts
@@ -140,6 +140,26 @@ const CASES: Case[] = [
},
run: adapter => adapter.revokeGroupInviteCode('[email protected]'),
},
+ {
+ // requireGroupChat is the preamble of every group write, and the write goes out right after it. A
+ // non-idempotent one (an add) would otherwise reach the closing page and fail as a plain 500.
+ name: 'a participant add parked in the group lookup',
+ build: parked => {
+ const addParticipants = jest.fn();
+ const chat = { isGroup: true, addParticipants };
+ return { client: { getChatById: () => parked.park().then(() => chat) }, unreached: addParticipants };
+ },
+ run: adapter => adapter.addParticipants('[email protected]', ['[email protected]']),
+ },
+ {
+ name: 'a group subject change parked in the group lookup',
+ build: parked => {
+ const setSubject = jest.fn();
+ const chat = { isGroup: true, setSubject };
+ return { client: { getChatById: () => parked.park().then(() => chat) }, unreached: setSubject };
+ },
+ run: adapter => adapter.setGroupSubject('[email protected]', 'New subject'),
+ },
{
name: 'a chat label change',
build: parked => {
diff --git a/src/engine/adapters/wwebjs-groups.ts b/src/engine/adapters/wwebjs-groups.ts
index 843667957..c572b8136 100644
--- a/src/engine/adapters/wwebjs-groups.ts
+++ b/src/engine/adapters/wwebjs-groups.ts
@@ -503,7 +503,8 @@ export class WwebjsGroups {
/**
* Resolve a group chat or throw: the shared preamble of the group settings writes. The lookup is a
* read that runs before any write, so a dead page answers 503 with the death signal even for the
- * non-idempotent callers: nothing has been applied yet. `context` names the caller's operation.
+ * non-idempotent callers: nothing has been applied yet. A session that went away during the lookup
+ * answers the not-ready 409 for the same reason. `context` names the caller's operation.
*/
private async requireGroupChat(groupId: string, context: string): Promise<GroupChat> {
this.host.ensureReady();
@@ -513,6 +514,9 @@ export class WwebjsGroups {
if (!chat?.isGroup) {
throw new GroupNotFoundError(groupId);
}
+ // The write goes out on the chat's own client right after this, which the host's getClient never
+ // sees, so a teardown that landed in the lookup must be caught here.
+ this.host.ensureReady();
return chat as unknown as GroupChat;
}