Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): keep a call rejectable after a failed reject

Yudhi Armyndharise9553402916c
diff --git a/src/engine/adapters/baileys-events.ts b/src/engine/adapters/baileys-events.ts
index 0d9759540..75a0501f8 100644
--- a/src/engine/adapters/baileys-events.ts
+++ b/src/engine/adapters/baileys-events.ts
@@ -1054,25 +1054,35 @@ export class BaileysEvents {
}
/**
- * Reject a currently-ringing call. The entry is evicted on ANY attempt (a rejected/ended call
- * will not become rejectable again); an unknown id or an expired entry maps to CallNotFoundError
- * (HTTP 404). A failure of the library's rejectCall() itself propagates as-is.
+ * Reject a currently-ringing call. The entry is evicted before the attempt, so an outcome that
+ * arrives meanwhile cannot publish a second one, and a rejected call does not become rejectable
+ * again. A failed attempt leaves the call ringing, so its entry is put back for a retry unless the
+ * id rang again meanwhile. An unknown id or an expired entry maps to CallNotFoundError (HTTP 404).
+ * A failure of the library's rejectCall() itself propagates as-is.
*/
async rejectCall(callId: string): Promise<void> {
const entry = this.liveCalls.get(callId);
- this.liveCalls.delete(callId);
if (!entry || entry.expiresAt <= Date.now()) {
+ this.liveCalls.delete(callId);
throw new CallNotFoundError(callId);
}
const sock = this.host.getSocketOrNull();
if (!sock) {
throw new EngineNotReadyError('Cannot reject a call before the engine is initialized.');
}
- await withQueryDeadline(
- sock.rejectCall(callId, entry.callFrom),
- BAILEYS_QUERY_BUDGET_MS,
- 'WhatsApp did not confirm the call rejection in time',
- );
+ this.liveCalls.delete(callId);
+ try {
+ await withQueryDeadline(
+ sock.rejectCall(callId, entry.callFrom),
+ BAILEYS_QUERY_BUDGET_MS,
+ 'WhatsApp did not confirm the call rejection in time',
+ );
+ } catch (err) {
+ // Known limit: an outcome that ended the call during the attempt found no entry and left no
+ // trace, so the handle comes back even then, until its TTL runs out.
+ if (!this.liveCalls.has(callId) && entry.expiresAt > Date.now()) this.liveCalls.set(callId, entry);
+ throw err;
+ }
// A rejection made HERE produces no inbound `reject` signal to observe, so without this the
// one outcome the caller definitely knows about — the one they asked for — was the only one
// never published. Emitted only after the socket accepted it, and the entry is already evicted,
diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.ts
index 83045c83e..bce1abeb1 100644
--- a/src/engine/adapters/baileys.adapter.spec.ts
+++ b/src/engine/adapters/baileys.adapter.spec.ts
@@ -7280,6 +7280,38 @@ describe('BaileysAdapter call outcomes', () => {
expect(onCallOutcome).not.toHaveBeenCalled();
});
+ // A failed attempt leaves the call ringing and answers 503, which invites a retry: the retry must
+ // still find the call rather than answer 404.
+ it('keeps the call rejectable after the socket fails to reject it', async () => {
+ const onCallOutcome = jest.fn();
+ const adapter = await ringing({ onCall: jest.fn(), onCallOutcome });
+ fakeSock.rejectCall.mockRejectedValueOnce(new Error('Connection Closed'));
+
+ await expect(adapter.rejectCall(CALL_ID)).rejects.toThrow('Connection Closed');
+ await adapter.rejectCall(CALL_ID);
+
+ expect(fakeSock.rejectCall).toHaveBeenCalledTimes(2);
+ expect(onCallOutcome).toHaveBeenCalledTimes(1);
+ expect(onCallOutcome).toHaveBeenCalledWith(expect.objectContaining({ callId: CALL_ID, outcome: 'rejected' }));
+ });
+
+ it('does not replace a call that rang again while the failed rejection was pending', async () => {
+ const adapter = await ringing({ onCall: jest.fn(), onCallOutcome: jest.fn() });
+ let fail!: (err: Error) => void;
+ fakeSock.rejectCall.mockReturnValueOnce(new Promise((_, reject) => (fail = reject)));
+
+ const attempt = adapter.rejectCall(CALL_ID);
+ // A new ring under the same id meanwhile owns the handle; the failed attempt must not replace it.
+ fakeSock.fire('call', [
+ { id: CALL_ID, from: '[email protected]', chatId: CALLER, status: 'offer', offline: false, date: new Date() },
+ ]);
+ fail(new Error('Connection Closed'));
+ await expect(attempt).rejects.toThrow('Connection Closed');
+
+ await adapter.rejectCall(CALL_ID);
+ expect(fakeSock.rejectCall).toHaveBeenLastCalledWith(CALL_ID, '[email protected]');
+ });
+
// WhatsApp replays signalling for calls that ended while the session was disconnected. Announcing
// those would report last week's declined call as if it had just happened.
it('drops an offline-replayed outcome', async () => {