feat(signal): add reaction notifications

This commit is contained in:
Peter Steinberger
2026-01-09 23:53:28 +01:00
parent 90d6a55e05
commit 89dc6ebb8b
7 changed files with 266 additions and 17 deletions

View File

@@ -7,7 +7,9 @@ import type { ReplyPayload } from "../auto-reply/types.js";
import type { ClawdbotConfig } from "../config/config.js";
import { loadConfig } from "../config/config.js";
import { resolveStorePath, updateLastRoute } from "../config/sessions.js";
import type { SignalReactionNotificationMode } from "../config/types.js";
import { danger, logVerbose, shouldLogVerbose } from "../globals.js";
import { enqueueSystemEvent } from "../infra/system-events.js";
import { mediaKindFromMime } from "../media/constants.js";
import { saveMediaBuffer } from "../media/store.js";
import { buildPairingReply } from "../pairing/pairing-messages.js";
@@ -50,6 +52,10 @@ type SignalReactionMessage = {
targetAuthorUuid?: string | null;
targetSentTimestamp?: number | null;
isRemove?: boolean | null;
groupInfo?: {
groupId?: string | null;
groupName?: string | null;
} | null;
};
type SignalDataMessage = {
@@ -112,6 +118,66 @@ function normalizeAllowList(raw?: Array<string | number>): string[] {
return (raw ?? []).map((entry) => String(entry).trim()).filter(Boolean);
}
type SignalReactionTarget = {
kind: "phone" | "uuid";
id: string;
display: string;
};
function resolveSignalReactionTarget(
reaction: SignalReactionMessage,
): SignalReactionTarget | null {
const uuid = reaction.targetAuthorUuid?.trim();
if (uuid) {
return { kind: "uuid", id: uuid, display: `uuid:${uuid}` };
}
const author = reaction.targetAuthor?.trim();
if (!author) return null;
const normalized = normalizeE164(author);
return { kind: "phone", id: normalized, display: normalized };
}
function shouldEmitSignalReactionNotification(params: {
mode?: SignalReactionNotificationMode;
account?: string | null;
target?: SignalReactionTarget | null;
sender?: ReturnType<typeof resolveSignalSender> | null;
allowlist?: string[];
}) {
const { mode, account, target, sender, allowlist } = params;
const effectiveMode = mode ?? "own";
if (effectiveMode === "off") return false;
if (effectiveMode === "own") {
const accountId = account?.trim();
if (!accountId || !target) return false;
if (target.kind === "uuid") {
return accountId === target.id || accountId === `uuid:${target.id}`;
}
return normalizeE164(accountId) === target.id;
}
if (effectiveMode === "allowlist") {
if (!sender || !allowlist || allowlist.length === 0) return false;
return isSignalSenderAllowed(sender, allowlist);
}
return true;
}
function buildSignalReactionSystemEventText(params: {
emojiLabel: string;
actorLabel: string;
messageId: string;
targetLabel?: string;
groupLabel?: string;
}) {
const base = `Signal reaction added: ${params.emojiLabel} by ${params.actorLabel} msg ${params.messageId}`;
const withTarget = params.targetLabel
? `${base} from ${params.targetLabel}`
: base;
return params.groupLabel
? `${withTarget} in ${params.groupLabel}`
: withTarget;
}
async function waitForSignalDaemonReady(params: {
baseUrl: string;
abortSignal?: AbortSignal;
@@ -253,6 +319,10 @@ export async function monitorSignalProvider(
: []),
);
const groupPolicy = accountInfo.config.groupPolicy ?? "open";
const reactionMode = accountInfo.config.reactionNotifications ?? "own";
const reactionAllowlist = normalizeAllowList(
accountInfo.config.reactionAllowlist,
);
const mediaMaxBytes =
(opts.mediaMaxMb ?? accountInfo.config.mediaMaxMb ?? 8) * 1024 * 1024;
const ignoreAttachments =
@@ -315,23 +385,6 @@ export async function monitorSignalProvider(
if (!envelope) return;
if (envelope.syncMessage) return;
const dataMessage =
envelope.dataMessage ?? envelope.editMessage?.dataMessage;
if (envelope.reactionMessage && !dataMessage) {
const reaction = envelope.reactionMessage;
if (reaction.isRemove) return; // Ignore reaction removals
const emoji = reaction.emoji ?? "unknown";
const sender = resolveSignalSender(envelope);
if (!sender) return;
const senderDisplay = formatSignalSenderDisplay(sender);
const senderName = envelope.sourceName ?? senderDisplay;
logVerbose(`signal reaction: ${emoji} from ${senderName}`);
// Skip processing reactions as messages for now - just log them
// Future: could dispatch as a notification or store for context
return;
}
if (!dataMessage) return;
const sender = resolveSignalSender(envelope);
if (!sender) return;
if (account && sender.kind === "phone") {
@@ -339,6 +392,69 @@ export async function monitorSignalProvider(
return;
}
}
const dataMessage =
envelope.dataMessage ?? envelope.editMessage?.dataMessage;
if (envelope.reactionMessage && !dataMessage) {
const reaction = envelope.reactionMessage;
if (reaction.isRemove) return; // Ignore reaction removals
const emojiLabel = reaction.emoji?.trim() || "emoji";
const senderDisplay = formatSignalSenderDisplay(sender);
const senderName = envelope.sourceName ?? senderDisplay;
logVerbose(`signal reaction: ${emojiLabel} from ${senderName}`);
const target = resolveSignalReactionTarget(reaction);
const shouldNotify = shouldEmitSignalReactionNotification({
mode: reactionMode,
account,
target,
sender,
allowlist: reactionAllowlist,
});
if (!shouldNotify) return;
const groupId = reaction.groupInfo?.groupId ?? undefined;
const groupName = reaction.groupInfo?.groupName ?? undefined;
const isGroup = Boolean(groupId);
const senderPeerId = resolveSignalPeerId(sender);
const route = resolveAgentRoute({
cfg,
provider: "signal",
accountId: accountInfo.accountId,
peer: {
kind: isGroup ? "group" : "dm",
id: isGroup ? (groupId ?? "unknown") : senderPeerId,
},
});
const groupLabel = isGroup
? `${groupName ?? "Signal Group"} id:${groupId}`
: undefined;
const messageId = reaction.targetSentTimestamp
? String(reaction.targetSentTimestamp)
: "unknown";
const text = buildSignalReactionSystemEventText({
emojiLabel,
actorLabel: senderName,
messageId,
targetLabel: target?.display,
groupLabel,
});
const senderId = formatSignalSenderId(sender);
const contextKey = [
"signal",
"reaction",
"added",
messageId,
senderId,
emojiLabel,
groupId ?? "",
]
.filter(Boolean)
.join(":");
enqueueSystemEvent(text, {
sessionKey: route.sessionKey,
contextKey,
});
return;
}
if (!dataMessage) return;
const senderDisplay = formatSignalSenderDisplay(sender);
const senderRecipient = resolveSignalRecipient(sender);
const senderPeerId = resolveSignalPeerId(sender);