refactor: normalize inbound context
This commit is contained in:
@@ -1,28 +1,21 @@
|
||||
import type { MsgContext } from "../templating.js";
|
||||
import { normalizeChatType } from "../../channels/chat-type.js";
|
||||
|
||||
export function formatInboundBodyWithSenderMeta(params: { body: string; ctx: MsgContext }): string {
|
||||
const body = params.body;
|
||||
if (!body.trim()) return body;
|
||||
const chatType = params.ctx.ChatType?.trim().toLowerCase();
|
||||
const chatType = normalizeChatType(params.ctx.ChatType);
|
||||
if (!chatType || chatType === "direct") return body;
|
||||
if (hasSenderMetaLine(body)) return body;
|
||||
|
||||
const senderLabel = formatSenderLabel(params.ctx);
|
||||
if (!senderLabel) return body;
|
||||
|
||||
const lineBreak = resolveBodyLineBreak(body);
|
||||
return `${body}${lineBreak}[from: ${senderLabel}]`;
|
||||
}
|
||||
|
||||
function resolveBodyLineBreak(body: string): string {
|
||||
const hasEscaped = body.includes("\\n");
|
||||
const hasNewline = body.includes("\n");
|
||||
if (hasEscaped && !hasNewline) return "\\n";
|
||||
return "\n";
|
||||
return `${body}\n[from: ${senderLabel}]`;
|
||||
}
|
||||
|
||||
function hasSenderMetaLine(body: string): boolean {
|
||||
return /(^|\n|\\n)\[from:/i.test(body);
|
||||
return /(^|\n)\[from:/i.test(body);
|
||||
}
|
||||
|
||||
function formatSenderLabel(ctx: MsgContext): string | null {
|
||||
|
||||
Reference in New Issue
Block a user