feat: move group mention gating to provider groups

This commit is contained in:
Peter Steinberger
2026-01-02 22:23:00 +01:00
parent e93102b276
commit 5cf1a9535e
27 changed files with 613 additions and 50 deletions

View File

@@ -59,10 +59,10 @@ async function waitForSubscribe() {
beforeEach(() => {
config = {
imessage: {},
imessage: { groups: { "*": { requireMention: true } } },
session: { mainKey: "main" },
routing: {
groupChat: { mentionPatterns: ["@clawd"], requireMention: true },
groupChat: { mentionPatterns: ["@clawd"] },
allowFrom: [],
},
};
@@ -106,6 +106,35 @@ describe("monitorIMessageProvider", () => {
expect(sendMock).not.toHaveBeenCalled();
});
it("allows group messages when imessage groups default disables mention gating", async () => {
config = {
...config,
imessage: { groups: { "*": { requireMention: false } } },
};
const run = monitorIMessageProvider();
await waitForSubscribe();
notificationHandler?.({
method: "message",
params: {
message: {
id: 11,
chat_id: 123,
sender: "+15550001111",
is_from_me: false,
text: "hello group",
is_group: true,
},
},
});
await flush();
closeResolve?.();
await run;
expect(replyMock).toHaveBeenCalled();
});
it("delivers group replies when mentioned", async () => {
replyMock.mockResolvedValueOnce({ text: "yo" });
const run = monitorIMessageProvider();

View File

@@ -79,10 +79,22 @@ function resolveMentionRegexes(cfg: ReturnType<typeof loadConfig>): RegExp[] {
);
}
function resolveRequireMention(opts: MonitorIMessageOpts): boolean {
const cfg = loadConfig();
function resolveGroupRequireMention(
cfg: ReturnType<typeof loadConfig>,
opts: MonitorIMessageOpts,
chatId?: number | null,
): boolean {
if (typeof opts.requireMention === "boolean") return opts.requireMention;
return cfg.routing?.groupChat?.requireMention ?? true;
const groupId = chatId != null ? String(chatId) : undefined;
if (groupId) {
const groupConfig = cfg.imessage?.groups?.[groupId];
if (typeof groupConfig?.requireMention === "boolean") {
return groupConfig.requireMention;
}
}
const groupDefault = cfg.imessage?.groups?.["*"]?.requireMention;
if (typeof groupDefault === "boolean") return groupDefault;
return true;
}
function isMentioned(text: string, regexes: RegExp[]): boolean {
@@ -133,7 +145,6 @@ export async function monitorIMessageProvider(
const cfg = loadConfig();
const allowFrom = resolveAllowFrom(opts);
const mentionRegexes = resolveMentionRegexes(cfg);
const requireMention = resolveRequireMention(opts);
const includeAttachments =
opts.includeAttachments ?? cfg.imessage?.includeAttachments ?? false;
const mediaMaxBytes =
@@ -170,6 +181,7 @@ export async function monitorIMessageProvider(
const messageText = (message.text ?? "").trim();
const mentioned = isGroup ? isMentioned(messageText, mentionRegexes) : true;
const requireMention = resolveGroupRequireMention(cfg, opts, chatId);
if (isGroup && requireMention && !mentioned) {
logVerbose(`imessage: skipping group message (no mention)`);
return;