diff --git a/src/web/inbound/extract.ts b/src/web/inbound/extract.ts index e48ac7be1..71231c53f 100644 --- a/src/web/inbound/extract.ts +++ b/src/web/inbound/extract.ts @@ -14,7 +14,9 @@ function unwrapMessage(message: proto.IMessage | undefined): proto.IMessage | un return normalized as proto.IMessage | undefined; } -function extractContextInfo(message: proto.IMessage | undefined): proto.IContextInfo | undefined { +export function extractContextInfo( + message: proto.IMessage | undefined, +): proto.IContextInfo | undefined { if (!message) return undefined; const contentType = getContentType(message); const candidate = contentType ? (message as Record)[contentType] : undefined; @@ -46,6 +48,22 @@ function extractContextInfo(message: proto.IMessage | undefined): proto.IContext return undefined; } +export interface ForwardingInfo { + isForwarded: boolean; + forwardingScore?: number; +} + +export function extractForwardingInfo(rawMessage: proto.IMessage | undefined): ForwardingInfo { + const message = unwrapMessage(rawMessage); + if (!message) return { isForwarded: false }; + const contextInfo = extractContextInfo(message); + if (!contextInfo) return { isForwarded: false }; + return { + isForwarded: contextInfo.isForwarded ?? false, + forwardingScore: contextInfo.forwardingScore ?? undefined, + }; +} + export function extractMentionedJids(rawMessage: proto.IMessage | undefined): string[] | undefined { const message = unwrapMessage(rawMessage); if (!message) return undefined; diff --git a/src/web/inbound/monitor.ts b/src/web/inbound/monitor.ts index 3633cbce9..280962802 100644 --- a/src/web/inbound/monitor.ts +++ b/src/web/inbound/monitor.ts @@ -13,6 +13,7 @@ import { checkInboundAccessControl } from "./access-control.js"; import { isRecentInboundMessage } from "./dedupe.js"; import { describeReplyContext, + extractForwardingInfo, extractLocationData, extractMediaPlaceholder, extractMentionedJids, @@ -222,6 +223,16 @@ export async function monitorWebInbox(options: { if (!body) continue; } const replyContext = describeReplyContext(msg.message as proto.IMessage | undefined); + const forwardingInfo = extractForwardingInfo(msg.message as proto.IMessage | undefined); + + // Add forwarding tag to body if message is forwarded + if (forwardingInfo.isForwarded) { + const forwardTag = + forwardingInfo.forwardingScore && forwardingInfo.forwardingScore >= 5 + ? "[forwarded many times]" + : "[forwarded]"; + body = `${forwardTag} ${body}`; + } let mediaPath: string | undefined; let mediaType: string | undefined; @@ -290,6 +301,8 @@ export async function monitorWebInbox(options: { groupSubject, groupParticipants, mentionedJids: mentionedJids ?? undefined, + isForwarded: forwardingInfo.isForwarded, + forwardingScore: forwardingInfo.forwardingScore, selfJid, selfE164, location: location ?? undefined, diff --git a/src/web/inbound/types.ts b/src/web/inbound/types.ts index 5f861fcc8..bae783ddd 100644 --- a/src/web/inbound/types.ts +++ b/src/web/inbound/types.ts @@ -29,6 +29,8 @@ export type WebInboundMessage = { groupSubject?: string; groupParticipants?: string[]; mentionedJids?: string[]; + isForwarded?: boolean; + forwardingScore?: number; selfJid?: string | null; selfE164?: string | null; location?: NormalizedLocation;