fix: reduce redundant envelope formatting for iMessage and Signal
This commit is contained in:
committed by
Peter Steinberger
parent
64d21f5ea8
commit
d0bc08a934
@@ -33,7 +33,8 @@ function hasSenderMetaLine(body: string, ctx: MsgContext): boolean {
|
|||||||
if (candidates.length === 0) return false;
|
if (candidates.length === 0) return false;
|
||||||
return candidates.some((candidate) => {
|
return candidates.some((candidate) => {
|
||||||
const escaped = escapeRegExp(candidate);
|
const escaped = escapeRegExp(candidate);
|
||||||
const pattern = new RegExp(`(^|\\n)${escaped}:\\s`, "i");
|
// Check for sender at start of line OR after envelope header bracket
|
||||||
|
const pattern = new RegExp(`(^|\\n|\\]\\s*)${escaped}:\\s`, "i");
|
||||||
return pattern.test(body);
|
return pattern.test(body);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -381,8 +381,9 @@ describe("monitorIMessageProvider", () => {
|
|||||||
expect(replyMock).toHaveBeenCalledOnce();
|
expect(replyMock).toHaveBeenCalledOnce();
|
||||||
const ctx = replyMock.mock.calls[0]?.[0] as { Body?: string; ChatType?: string };
|
const ctx = replyMock.mock.calls[0]?.[0] as { Body?: string; ChatType?: string };
|
||||||
expect(ctx.ChatType).toBe("group");
|
expect(ctx.ChatType).toBe("group");
|
||||||
expect(String(ctx.Body ?? "")).toContain("[from:");
|
// Sender should appear as prefix in group messages (no redundant [from:] suffix)
|
||||||
expect(String(ctx.Body ?? "")).toContain("+15550002222");
|
expect(String(ctx.Body ?? "")).toContain("+15550002222:");
|
||||||
|
expect(String(ctx.Body ?? "")).not.toContain("[from:");
|
||||||
|
|
||||||
expect(sendMock).toHaveBeenCalledWith(
|
expect(sendMock).toHaveBeenCalledWith(
|
||||||
"chat_id:42",
|
"chat_id:42",
|
||||||
|
|||||||
@@ -165,16 +165,8 @@ export async function monitorIMessageProvider(opts: MonitorIMessageOpts = {}): P
|
|||||||
async function handleMessageNow(message: IMessagePayload) {
|
async function handleMessageNow(message: IMessagePayload) {
|
||||||
const senderRaw = message.sender ?? "";
|
const senderRaw = message.sender ?? "";
|
||||||
const sender = senderRaw.trim();
|
const sender = senderRaw.trim();
|
||||||
if (!sender) {
|
if (!sender) return;
|
||||||
logVerbose(`imessage: skipping message (no sender), chat_id=${message.chat_id}`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const senderNormalized = normalizeIMessageHandle(sender);
|
const senderNormalized = normalizeIMessageHandle(sender);
|
||||||
if (!senderNormalized) {
|
|
||||||
logVerbose(
|
|
||||||
`imessage: sender normalized to empty, raw="${sender}", chat_id=${message.chat_id}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (message.is_from_me) return;
|
if (message.is_from_me) return;
|
||||||
|
|
||||||
const chatId = message.chat_id ?? undefined;
|
const chatId = message.chat_id ?? undefined;
|
||||||
@@ -391,14 +383,13 @@ export async function monitorIMessageProvider(opts: MonitorIMessageOpts = {}): P
|
|||||||
}
|
}
|
||||||
|
|
||||||
const chatTarget = formatIMessageChatTarget(chatId);
|
const chatTarget = formatIMessageChatTarget(chatId);
|
||||||
|
// For groups: use chat name or just "Group" (channel "iMessage" is already shown)
|
||||||
|
// For DMs: show sender, only add id: suffix if raw differs from normalized
|
||||||
const fromLabel = isGroup
|
const fromLabel = isGroup
|
||||||
? `${message.chat_name || "iMessage Group"} id:${chatId ?? "unknown"}`
|
? `${message.chat_name || "Group"} id:${chatId ?? "unknown"}`
|
||||||
: `${senderNormalized} id:${sender}`;
|
: senderNormalized === sender
|
||||||
if (isGroup && !senderNormalized) {
|
? senderNormalized
|
||||||
logVerbose(
|
: `${senderNormalized} id:${sender}`;
|
||||||
`imessage: group message missing normalized sender, raw="${sender}", chat_id=${chatId}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const body = formatInboundEnvelope({
|
const body = formatInboundEnvelope({
|
||||||
channel: "iMessage",
|
channel: "iMessage",
|
||||||
from: fromLabel,
|
from: fromLabel,
|
||||||
|
|||||||
@@ -60,7 +60,9 @@ describe("signal createSignalEventHandler inbound contract", () => {
|
|||||||
|
|
||||||
expect(capturedCtx).toBeTruthy();
|
expect(capturedCtx).toBeTruthy();
|
||||||
expectInboundContextContract(capturedCtx!);
|
expectInboundContextContract(capturedCtx!);
|
||||||
expect(String(capturedCtx?.Body ?? "")).toContain("[from:");
|
// Sender should appear as prefix in group messages (no redundant [from:] suffix)
|
||||||
expect(String(capturedCtx?.Body ?? "")).toContain("Alice");
|
expect(String(capturedCtx?.Body ?? "")).toContain("Alice");
|
||||||
|
expect(String(capturedCtx?.Body ?? "")).toMatch(/Alice.*:/);
|
||||||
|
expect(String(capturedCtx?.Body ?? "")).not.toContain("[from:");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -65,9 +65,13 @@ export function createSignalEventHandler(deps: SignalEventHandlerDeps) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
async function handleSignalInboundMessage(entry: SignalInboundEntry) {
|
async function handleSignalInboundMessage(entry: SignalInboundEntry) {
|
||||||
|
// For groups: use group name or just "Group" (channel "Signal" is already shown)
|
||||||
|
// For DMs: show sender, only add id: suffix if display differs from name
|
||||||
const fromLabel = entry.isGroup
|
const fromLabel = entry.isGroup
|
||||||
? `${entry.groupName ?? "Signal Group"} id:${entry.groupId}`
|
? `${entry.groupName || "Group"} id:${entry.groupId}`
|
||||||
: `${entry.senderName} id:${entry.senderDisplay}`;
|
: entry.senderName === entry.senderDisplay
|
||||||
|
? entry.senderName
|
||||||
|
: `${entry.senderName} id:${entry.senderDisplay}`;
|
||||||
const body = formatInboundEnvelope({
|
const body = formatInboundEnvelope({
|
||||||
channel: "Signal",
|
channel: "Signal",
|
||||||
from: fromLabel,
|
from: fromLabel,
|
||||||
|
|||||||
Reference in New Issue
Block a user