Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

feat(events): push session.restriction over the socket so the dashboard badge updates live

Yudhi Armyndharise7d1feaf8a5f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index cf6e158f2..70c5a84ce 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- Built under the full TypeScript `strict` family, with per-module test-coverage floors across the codebase.
+- `session.restriction` is now socket-subscribable as well as webhook-delivered, and the dashboard session card picks up a restriction (or its lift) live instead of only on a page reload.
### Fixed
diff --git a/dashboard/src/hooks/useSessionFeed.ts b/dashboard/src/hooks/useSessionFeed.ts
index ea10832ef..be2a4e0c4 100644
--- a/dashboard/src/hooks/useSessionFeed.ts
+++ b/dashboard/src/hooks/useSessionFeed.ts
@@ -13,6 +13,7 @@ export interface UseSessionFeedArgs {
sessionsRef: RefObject<Session[]>;
onQRCode: (event: { sessionId: string; qrCode: string }) => void;
onSessionStatus: (event: { sessionId: string; status: string }) => void;
+ onSessionRestriction?: (event: { sessionId: string }) => void;
}
/**
@@ -26,7 +27,13 @@ export interface UseSessionFeedArgs {
* push touches `sessions`/`selectedSession` state that lives in the page body (see the `useSessionList`
* rejection), so this hook only wires the callbacks through — it never mutates the session list itself.
*/
-export function useSessionFeed({ sessions, sessionsRef, onQRCode, onSessionStatus }: UseSessionFeedArgs): SessionFeed {
+export function useSessionFeed({
+ sessions,
+ sessionsRef,
+ onQRCode,
+ onSessionStatus,
+ onSessionRestriction,
+}: UseSessionFeedArgs): SessionFeed {
// Live session-feed subscription state: wildcard first, per-session fallback for scoped keys.
const feedStateRef = useRef(createSessionFeedState());
// Most recent server error frame; folded into feedStateRef by the effect below (kept as state
@@ -36,6 +43,7 @@ export function useSessionFeed({ sessions, sessionsRef, onQRCode, onSessionStatu
const { isConnected, subscribe } = useWebSocket({
onQRCode,
onSessionStatus,
+ onSessionRestriction,
onServerError: useCallback((frame: { code: string }) => {
setFeedErrorFrame(frame);
}, []),
diff --git a/dashboard/src/hooks/useWebSocket.ts b/dashboard/src/hooks/useWebSocket.ts
index a102468ce..baca6afae 100644
--- a/dashboard/src/hooks/useWebSocket.ts
+++ b/dashboard/src/hooks/useWebSocket.ts
@@ -71,6 +71,12 @@ interface StatusReceivedEvent {
timestamp: string;
}
+/** A restriction imposed or lifted — the dashboard uses it purely as a refetch signal for the badge. */
+interface SessionRestrictionEvent {
+ sessionId: string;
+ timestamp: string;
+}
+
/** Ack frame answering a client `subscribe` request (`{type: 'subscribed'}`). */
interface SubscribedEvent {
sessionId: string;
@@ -92,6 +98,7 @@ interface WebSocketEvents {
onMessageRevoked?: (event: MessageRevokedEvent) => void;
onMessageEdited?: (event: MessageEditedEvent) => void;
onStatusReceived?: (event: StatusReceivedEvent) => void;
+ onSessionRestriction?: (event: SessionRestrictionEvent) => void;
onSubscribed?: (event: SubscribedEvent) => void;
onServerError?: (event: ServerErrorEvent) => void;
}
@@ -272,6 +279,9 @@ export function useWebSocket(events: WebSocketEvents = {}) {
case 'status.received':
events.onStatusReceived?.({ sessionId, timestamp: msg.timestamp });
break;
+ case 'session.restriction':
+ events.onSessionRestriction?.({ sessionId, timestamp: msg.timestamp });
+ break;
case 'message.ack':
events.onMessageAck?.({
sessionId,
diff --git a/dashboard/src/pages/Sessions.tsx b/dashboard/src/pages/Sessions.tsx
index 26cbdf071..f96dabbab 100644
--- a/dashboard/src/pages/Sessions.tsx
+++ b/dashboard/src/pages/Sessions.tsx
@@ -148,6 +148,12 @@ export function Sessions() {
sessions,
sessionsRef,
onQRCode: applyQrPush,
+ // The badge renders from the server projection (`session.restriction`), and a restriction can
+ // arrive with no status transition at all (the Baileys reachout timelock rides a connect
+ // probe) — so the push is purely a refetch signal.
+ onSessionRestriction: useCallback(() => {
+ void fetchSessions();
+ }, [fetchSessions]),
onSessionStatus: useCallback(
(event: { sessionId: string; status: string }) => {
const prev = sessionsRef.current.find(s => s.id === event.sessionId);
diff --git a/dashboard/src/utils/sessionFeedSubscription.ts b/dashboard/src/utils/sessionFeedSubscription.ts
index 9ec0354ea..72678c52e 100644
--- a/dashboard/src/utils/sessionFeedSubscription.ts
+++ b/dashboard/src/utils/sessionFeedSubscription.ts
@@ -6,7 +6,7 @@
// one subscription per visible session. The sessions list endpoint is already scope-filtered
// server-side, so every listed session is joinable.
-export const SESSION_FEED_EVENTS = ['session.status', 'session.qr'] as const;
+export const SESSION_FEED_EVENTS = ['session.status', 'session.qr', 'session.restriction'] as const;
export interface SessionFeedSink {
subscribe(sessionId: string, events: string[]): void;
diff --git a/docs/06-api-specification.md b/docs/06-api-specification.md
index 3b14c64b0..a94bf5850 100644
--- a/docs/06-api-specification.md
+++ b/docs/06-api-specification.md
@@ -5868,6 +5868,7 @@ session.status
session.qr
session.authenticated
session.disconnected
+session.restriction
group.join
group.leave
group.update
@@ -5877,7 +5878,7 @@ status.received
A subscribe request whose `events` array contains no recognized name (after filtering) is rejected with `INVALID_EVENTS`. Unknown names mixed with valid ones are silently dropped; the `subscribed` reply echoes only the accepted events.
-> `message.failed`, `session.reconnect_loop` and `session.restriction` are webhook-only — they are not subscribable on the socket.
+> `message.failed` and `session.reconnect_loop` are webhook-only — they are not subscribable on the socket.
### Wildcards and scoping
diff --git a/src/modules/events/dto/ws-messages.dto.ts b/src/modules/events/dto/ws-messages.dto.ts
index 68dd7e7df..a1443912e 100644
--- a/src/modules/events/dto/ws-messages.dto.ts
+++ b/src/modules/events/dto/ws-messages.dto.ts
@@ -23,6 +23,7 @@ export const SUBSCRIBABLE_EVENTS = [
'session.qr',
'session.authenticated',
'session.disconnected',
+ 'session.restriction',
'group.join',
'group.leave',
'group.update',
diff --git a/src/modules/events/events.gateway.ts b/src/modules/events/events.gateway.ts
index f27d06d1f..cca8b767a 100644
--- a/src/modules/events/events.gateway.ts
+++ b/src/modules/events/events.gateway.ts
@@ -539,6 +539,16 @@ export class EventsGateway implements OnGatewayInit, OnGatewayConnection, OnGate
this.emitToRooms(sessionId, 'session.disconnected', data);
}
+ /**
+ * Emit a restriction change (imposed or lifted), mirroring the `session.restriction` webhook
+ * payload. Needed live because a restriction can arrive with no status transition at all (the
+ * Baileys reachout timelock rides a connect probe) — without this push the dashboard badge only
+ * appeared on a full page reload.
+ */
+ emitSessionRestriction(sessionId: string, data: Record<string, unknown>) {
+ this.emitToRooms(sessionId, 'session.restriction', data);
+ }
+
/**
* The end of a ringing call, one method per outcome.
*
diff --git a/src/modules/session/session-engine-event-wiring.ts b/src/modules/session/session-engine-event-wiring.ts
index 1dc0ddf72..38cad0bc2 100644
--- a/src/modules/session/session-engine-event-wiring.ts
+++ b/src/modules/session/session-engine-event-wiring.ts
@@ -276,13 +276,14 @@ export class SessionEngineEventWiring {
expiresAt: restriction.expiresAt,
action: 'account_restricted',
});
- void host.webhookService.dispatch(id, 'session.restriction', {
- sessionId: id,
+ const payload = {
active: true,
kind: restriction.kind,
code: restriction.code,
expiresAt: restriction.expiresAt ? new Date(restriction.expiresAt).toISOString() : null,
- });
+ };
+ void host.webhookService.dispatch(id, 'session.restriction', { sessionId: id, ...payload });
+ host.eventsGateway.emitSessionRestriction(id, payload);
// Audited, unlike the engine-level connect/disconnect transitions next door: this is rare,
// it is not reconnect noise, and the store that serves it to the API is in memory — so this
// row is the only durable record of when the account was restricted.
diff --git a/src/modules/session/session-engine-lifecycle.service.ts b/src/modules/session/session-engine-lifecycle.service.ts
index 8cb7f42fe..d37c33d67 100644
--- a/src/modules/session/session-engine-lifecycle.service.ts
+++ b/src/modules/session/session-engine-lifecycle.service.ts
@@ -663,6 +663,12 @@ export class SessionEngineLifecycle {
code: lifted.code,
expiresAt: null,
});
+ this.eventsGateway.emitSessionRestriction(id, {
+ active: false,
+ kind: lifted.kind,
+ code: lifted.code,
+ expiresAt: null,
+ });
void this.auditService?.logInfo(AuditAction.SESSION_RESTRICTION_LIFTED, {
sessionId: id,
metadata: { kind: lifted.kind, code: lifted.code },
diff --git a/src/modules/session/session.service.spec.ts b/src/modules/session/session.service.spec.ts
index c998c8719..1b7a78851 100644
--- a/src/modules/session/session.service.spec.ts
+++ b/src/modules/session/session.service.spec.ts
@@ -166,6 +166,7 @@ describe('SessionService', () => {
emitSessionStatus: jest.fn(),
emitSessionAuthenticated: jest.fn(),
emitSessionDisconnected: jest.fn(),
+ emitSessionRestriction: jest.fn(),
emitPresenceUpdate: jest.fn(),
emitCallAccepted: jest.fn(),
emitCallRejected: jest.fn(),
@@ -2239,6 +2240,28 @@ describe('SessionService', () => {
expect(webhookService.dispatch).not.toHaveBeenCalled();
});
+ // A restriction can arrive with no status transition at all (the Baileys reachout timelock
+ // rides a connect probe), so without a live push the dashboard badge only appeared on reload.
+ it('pushes the restriction and its lift to socket subscribers', async () => {
+ const callbacks = await startAndCapture();
+
+ callbacks.onAccountRestriction?.({ ...timelock, expiresAt: Date.UTC(2026, 7, 4, 9) });
+ expect(eventsGateway.emitSessionRestriction).toHaveBeenCalledWith('sess-uuid-1', {
+ active: true,
+ kind: 'reachout_timelock',
+ code: 'BIZ_QUALITY',
+ expiresAt: '2026-08-04T09:00:00.000Z',
+ });
+
+ callbacks.onAccountRestriction?.(null);
+ expect(eventsGateway.emitSessionRestriction).toHaveBeenCalledWith('sess-uuid-1', {
+ active: false,
+ kind: 'reachout_timelock',
+ code: 'BIZ_QUALITY',
+ expiresAt: null,
+ });
+ });
+
it('announces again when the cause changes', async () => {
const callbacks = await startAndCapture();
callbacks.onAccountRestriction?.(timelock);