Skip to content

Commit d012a00

Browse files
committed
fix(whatsapp): make inbound diagnostics policy-aware
1 parent 5774f2a commit d012a00

3 files changed

Lines changed: 67 additions & 7 deletions

File tree

extensions/whatsapp/src/auto-reply/monitor.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
WHATSAPP_WATCHDOG_TIMEOUT_ERROR,
2323
type ManagedWhatsAppListener,
2424
} from "../connection-controller.js";
25+
import { resolveWhatsAppInboundPolicy } from "../inbound-policy.js";
2526
import { attachWebInboxToSocket, type WhatsAppGroupMetadataCache } from "../inbound/monitor.js";
2627
import {
2728
newConnectionId,
@@ -542,7 +543,18 @@ export async function monitorWebChannel(
542543
);
543544
});
544545

545-
whatsappLog.info(formatWhatsAppInboundListeningLog(account));
546+
const inboundPolicy = resolveWhatsAppInboundPolicy({
547+
cfg,
548+
accountId: account.accountId,
549+
selfE164: selfE164 ?? null,
550+
});
551+
whatsappLog.info(
552+
formatWhatsAppInboundListeningLog({
553+
groups: inboundPolicy.account.groups,
554+
groupPolicy: inboundPolicy.groupPolicy,
555+
hasGroupAllowFrom: inboundPolicy.groupAllowFrom.length > 0,
556+
}),
557+
);
546558
if (process.stdout.isTTY || process.stderr.isTTY) {
547559
whatsappLog.raw("Ctrl+C to stop.");
548560
}

extensions/whatsapp/src/auto-reply/monitor/listener-log.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
export function formatWhatsAppInboundListeningLog(account: {
22
groups?: Record<string, unknown>;
3+
groupPolicy: "open" | "allowlist" | "disabled";
4+
hasGroupAllowFrom: boolean;
35
}): string {
6+
if (account.groupPolicy === "disabled") {
7+
return "Listening for WhatsApp inbound messages (DM + groups disabled by groupPolicy).";
8+
}
9+
if (account.groupPolicy === "allowlist" && !account.hasGroupAllowFrom) {
10+
return "Listening for WhatsApp inbound messages (DM + group inbound blocked by empty groupPolicy allowlist).";
11+
}
12+
413
const groups = account.groups ?? {};
514
if (Object.keys(groups).length === 0) {
6-
return "Listening for WhatsApp inbound messages (DM + all groups; no group allowlist configured).";
15+
const suffix =
16+
account.groupPolicy === "allowlist"
17+
? "sender allowlist configured"
18+
: "no group allowlist configured";
19+
return `Listening for WhatsApp inbound messages (DM + all groups; ${suffix}).`;
720
}
821
if (Object.hasOwn(groups, "*")) {
922
return "Listening for WhatsApp inbound messages (DM + all groups; wildcard configured).";

extensions/whatsapp/src/auto-reply/web-auto-reply-monitor.test.ts

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,50 @@ function makeInboundCfg(messagePrefix = "") {
120120

121121
describe("WhatsApp listener diagnostics", () => {
122122
it("describes WhatsApp inbound listener scope without implying DM-only routing", () => {
123-
expect(formatWhatsAppInboundListeningLog({})).toBe(
123+
expect(
124+
formatWhatsAppInboundListeningLog({
125+
groupPolicy: "open",
126+
hasGroupAllowFrom: false,
127+
}),
128+
).toBe(
124129
"Listening for WhatsApp inbound messages (DM + all groups; no group allowlist configured).",
125130
);
126-
expect(formatWhatsAppInboundListeningLog({ groups: { "123@g.us": {}, "*": {} } })).toBe(
127-
"Listening for WhatsApp inbound messages (DM + all groups; wildcard configured).",
131+
expect(
132+
formatWhatsAppInboundListeningLog({
133+
groupPolicy: "disabled",
134+
hasGroupAllowFrom: true,
135+
}),
136+
).toBe("Listening for WhatsApp inbound messages (DM + groups disabled by groupPolicy).");
137+
expect(
138+
formatWhatsAppInboundListeningLog({
139+
groupPolicy: "allowlist",
140+
hasGroupAllowFrom: false,
141+
}),
142+
).toBe(
143+
"Listening for WhatsApp inbound messages (DM + group inbound blocked by empty groupPolicy allowlist).",
128144
);
129-
expect(formatWhatsAppInboundListeningLog({ groups: { "123@g.us": {}, "456@g.us": {} } })).toBe(
130-
"Listening for WhatsApp inbound messages (DM + 2 configured groups).",
145+
expect(
146+
formatWhatsAppInboundListeningLog({
147+
groupPolicy: "allowlist",
148+
hasGroupAllowFrom: true,
149+
}),
150+
).toBe(
151+
"Listening for WhatsApp inbound messages (DM + all groups; sender allowlist configured).",
131152
);
153+
expect(
154+
formatWhatsAppInboundListeningLog({
155+
groups: { "123@g.us": {}, "*": {} },
156+
groupPolicy: "allowlist",
157+
hasGroupAllowFrom: true,
158+
}),
159+
).toBe("Listening for WhatsApp inbound messages (DM + all groups; wildcard configured).");
160+
expect(
161+
formatWhatsAppInboundListeningLog({
162+
groups: { "123@g.us": {}, "456@g.us": {} },
163+
groupPolicy: "allowlist",
164+
hasGroupAllowFrom: true,
165+
}),
166+
).toBe("Listening for WhatsApp inbound messages (DM + 2 configured groups).");
132167
});
133168
});
134169

0 commit comments

Comments
 (0)