Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(session): announce a restriction imposed again after one lapsed

Yudhi Armyndharisb8c458eee8b8
diff --git a/src/modules/session/session-restriction-store.service.spec.ts b/src/modules/session/session-restriction-store.service.spec.ts
index e02d6b7ba..d4737365c 100644
--- a/src/modules/session/session-restriction-store.service.spec.ts
+++ b/src/modules/session/session-restriction-store.service.spec.ts
@@ -62,6 +62,21 @@ describe('SessionRestrictionStore', () => {
expect(store.set('s1', timelock({ expiresAt: refreshed }))).toBe(false);
expect(store.get('s1')?.expiresAt).toBe(refreshed);
});
+
+ // The lapsed one already reads as no restriction, so the new one flips the session to restricted.
+ it('is news when the same restriction is imposed again after the last one lapsed', () => {
+ store.set('s1', timelock({ expiresAt: Date.now() - 1000 }));
+
+ expect(store.set('s1', timelock({ expiresAt: Date.now() + 60_000 }))).toBe(true);
+ });
+
+ // A clock ahead of WhatsApp's sees the re-read timelock as already over on every connect.
+ it('is not news when the engine re-reports a restriction that has already lapsed', () => {
+ const lapsed = Date.now() - 1000;
+ expect(store.set('s1', timelock({ expiresAt: lapsed }))).toBe(true);
+
+ expect(store.set('s1', timelock({ expiresAt: lapsed }))).toBe(false);
+ });
});
describe('expiry', () => {
diff --git a/src/modules/session/session-restriction-store.service.ts b/src/modules/session/session-restriction-store.service.ts
index f10fb6ac5..824c2869b 100644
--- a/src/modules/session/session-restriction-store.service.ts
+++ b/src/modules/session/session-restriction-store.service.ts
@@ -48,10 +48,14 @@ export class SessionRestrictionStore {
* connect, so an un-deduped webhook would fire on a loop for one unchanged fact.
*
* The stored entry is refreshed either way, so a re-report with a later `expiresAt` updates what
- * the API serves without being announced as a new restriction.
+ * the API serves without being announced as a new restriction. Compared against what is in force:
+ * a same-cause restriction imposed after the last one lapsed is a new onset, not a re-report. A
+ * report that is itself already over is compared against the stored entry, so a clock running
+ * ahead of WhatsApp's does not announce the same lapsed timelock on every connect.
*/
set(sessionId: string, restriction: AccountRestriction): boolean {
- const previous = this.restrictions.get(sessionId);
+ const lapsed = restriction.expiresAt != null && restriction.expiresAt <= Date.now();
+ const previous = lapsed ? this.restrictions.get(sessionId) : this.inForce(sessionId);
this.restrictions.set(sessionId, restriction);
this.publishCount();
return previous?.kind !== restriction.kind || previous?.code !== restriction.code;