A full-featured CRM built with React, shadcn/ui, and Supabase.
strip mail signature too
ThieryMichel1f21e7a62a28
diff --git a/supabase/functions/postmark/forwardedParser.test.ts b/supabase/functions/postmark/forwardedParser.test.tsindex 31f9e9ee..ede584e6 100644--- a/supabase/functions/postmark/forwardedParser.test.ts+++ b/supabase/functions/postmark/forwardedParser.test.ts@@ -2,243 +2,314 @@ import { describe, it, expect } from "vitest";import {stripForwardingHeaderBlock,stripSubjectForwardingPrefix,+ stripMailSignature,+ getForwardedMailContent,} from "./forwardedParser";-describe("stripForwardingHeaderBlock", () => {- describe("returns original text when no forwarding separator is found", () => {- it("returns the original text for plain content", () => {- const text = "Hello, this is a regular email body.";- expect(stripForwardingHeaderBlock(text)).toBe(text);+describe("forwardedParser", () => {+ describe("stripForwardingHeaderBlock", () => {+ describe("returns original text when no forwarding separator is found", () => {+ it("returns the original text for plain content", () => {+ const text = "Hello, this is a regular email body.";+ expect(stripForwardingHeaderBlock(text)).toBe(text);+ });++ it("trims whitespace when no separator is found", () => {+ const text = " Hello world ";+ expect(stripForwardingHeaderBlock(text)).toBe("Hello world");+ });++ it("returns original text when separator is not on the first line", () => {+ const text =+ "Some intro line\n---------- Forwarded message ----------\nActual content";+ expect(stripForwardingHeaderBlock(text)).toBe(text.trim());+ });});- it("trims whitespace when no separator is found", () => {- const text = " Hello world ";- expect(stripForwardingHeaderBlock(text)).toBe("Hello world");+ describe("Gmail forwarded separator", () => {+ it("strips the Gmail forwarded header block", () => {+ const text = [+ "---------- Forwarded message ----------",+ "From: Alice <[email protected]>",+ "Date: Mon, 24 Feb 2026 10:00:00 +0000",+ "Subject: Meeting notes",+ "To: Bob <[email protected]>",+ "",+ "Hi Bob,",+ "",+ "Here are the meeting notes.",+ ].join("\n");++ expect(stripForwardingHeaderBlock(text)).toBe(+ "Hi Bob,\n\nHere are the meeting notes.",+ );+ });});- it("returns original text when separator is not on the first line", () => {- const text =- "Some intro line\n---------- Forwarded message ----------\nActual content";- expect(stripForwardingHeaderBlock(text)).toBe(text.trim());+ describe("Apple Mail forwarded separator", () => {+ it("strips the Apple Mail forwarded header block", () => {+ const text = [+ "Begin forwarded message:",+ "From: Alice <[email protected]>",+ "Subject: Hello",+ "Date: 24 Feb 2026",+ "To: Bob <[email protected]>",+ "",+ "This is the original message.",+ ].join("\n");++ expect(stripForwardingHeaderBlock(text)).toBe(+ "This is the original message.",+ );+ });});- });- describe("Gmail forwarded separator", () => {- it("strips the Gmail forwarded header block", () => {- const text = [- "---------- Forwarded message ----------",- "From: Alice <[email protected]>",- "Date: Mon, 24 Feb 2026 10:00:00 +0000",- "Subject: Meeting notes",- "To: Bob <[email protected]>",- "",- "Hi Bob,",- "",- "Here are the meeting notes.",- ].join("\n");+ describe("Outlook / Exchange forwarded separator", () => {+ it("strips the Outlook forwarded header block", () => {+ const text = [+ "-----Original Message-----",+ "From: Alice <[email protected]>",+ "Sent: Monday, February 24, 2026 10:00 AM",+ "To: Bob <[email protected]>",+ "Subject: Project update",+ "",+ "Please find the update below.",+ ].join("\n");++ expect(stripForwardingHeaderBlock(text)).toBe(+ "Please find the update below.",+ );+ });+ });- expect(stripForwardingHeaderBlock(text)).toBe(- "Hi Bob,\n\nHere are the meeting notes.",- );+ describe("French forwarded separators", () => {+ it("strips the 'Message transféré' header block", () => {+ const text = [+ "---------- Message transféré ----------",+ "De : Alice <[email protected]>",+ "Date : 24 févr. 2026",+ "Objet : Réunion",+ "",+ "Bonjour,",+ "",+ "Voici le message.",+ ].join("\n");++ expect(stripForwardingHeaderBlock(text)).toBe(+ "Bonjour,\n\nVoici le message.",+ );+ });++ it("strips the 'Transféré' header block", () => {+ const text = [+ "----- Transféré -----",+ "De : Alice <[email protected]>",+ "",+ "Le message transféré.",+ ].join("\n");++ expect(stripForwardingHeaderBlock(text)).toBe("Le message transféré.");+ });++ it("strips the ASCII-variant 'Message transfere' header block", () => {+ const text = [+ "---------- Message transfere ----------",+ "De : Alice <[email protected]>",+ "",+ "Bonjour.",+ ].join("\n");++ expect(stripForwardingHeaderBlock(text)).toBe("Bonjour.");+ });});- });- describe("Apple Mail forwarded separator", () => {- it("strips the Apple Mail forwarded header block", () => {- const text = [- "Begin forwarded message:",- "From: Alice <[email protected]>",- "Subject: Hello",- "Date: 24 Feb 2026",- "To: Bob <[email protected]>",- "",- "This is the original message.",- ].join("\n");+ describe("German forwarded separator", () => {+ it("strips the 'Weitergeleitete Nachricht' header block", () => {+ const text = [+ "---------- Weitergeleitete Nachricht ----------",+ "Von: Alice <[email protected]>",+ "Datum: 24. Feb. 2026",+ "Betreff: Projekt",+ "",+ "Hallo,",+ "",+ "Hier ist die Nachricht.",+ ].join("\n");++ expect(stripForwardingHeaderBlock(text)).toBe(+ "Hallo,\n\nHier ist die Nachricht.",+ );+ });+ });- expect(stripForwardingHeaderBlock(text)).toBe(- "This is the original message.",- );+ describe("edge cases", () => {+ it("strip everything when there is only one forwarded block and nothing after", () => {+ const text = [+ "---------- Forwarded message ----------",+ "From: Alice <[email protected]>",+ ].join("\n");++ expect(stripForwardingHeaderBlock(text)).toBe("");+ });++ it("preserves multi-paragraph body content", () => {+ const text = [+ "---------- Forwarded message ----------",+ "From: Alice <[email protected]>",+ "",+ "First paragraph.",+ "",+ "Second paragraph.",+ "",+ "Third paragraph.",+ ].join("\n");++ expect(stripForwardingHeaderBlock(text)).toBe(+ "First paragraph.\n\nSecond paragraph.\n\nThird paragraph.",+ );+ });++ it("trims leading and trailing whitespace from the result", () => {+ const text = [+ "---------- Forwarded message ----------",+ "From: Alice",+ "",+ " ",+ " Body content ",+ " ",+ ].join("\n");++ expect(stripForwardingHeaderBlock(text)).toBe("Body content");+ });});});- describe("Outlook / Exchange forwarded separator", () => {- it("strips the Outlook forwarded header block", () => {- const text = [- "-----Original Message-----",- "From: Alice <[email protected]>",- "Sent: Monday, February 24, 2026 10:00 AM",- "To: Bob <[email protected]>",- "Subject: Project update",- "",- "Please find the update below.",- ].join("\n");+ describe("stripSubjectForwardingPrefix", () => {+ it("strips 'Fwd:' prefix", () => {+ expect(stripSubjectForwardingPrefix("Fwd: Hello")).toBe("Hello");+ });- expect(stripForwardingHeaderBlock(text)).toBe(- "Please find the update below.",- );+ it("strips 'FW:' prefix", () => {+ expect(stripSubjectForwardingPrefix("FW: Hello")).toBe("Hello");});- });- describe("French forwarded separators", () => {- it("strips the 'Message transféré' header block", () => {- const text = [- "---------- Message transféré ----------",- "De : Alice <[email protected]>",- "Date : 24 févr. 2026",- "Objet : Réunion",- "",- "Bonjour,",- "",- "Voici le message.",- ].join("\n");+ it("strips 'FWD:' prefix", () => {+ expect(stripSubjectForwardingPrefix("FWD: Hello")).toBe("Hello");+ });- expect(stripForwardingHeaderBlock(text)).toBe(- "Bonjour,\n\nVoici le message.",- );+ it("strips 'Tr:' prefix (French)", () => {+ expect(stripSubjectForwardingPrefix("Tr: Bonjour")).toBe("Bonjour");});- it("strips the 'Transféré' header block", () => {- const text = [- "----- Transféré -----",- "De : Alice <[email protected]>",- "",- "Le message transféré.",- ].join("\n");+ it("strips 'SV:' prefix (Scandinavian)", () => {+ expect(stripSubjectForwardingPrefix("SV: Hei")).toBe("Hei");+ });- expect(stripForwardingHeaderBlock(text)).toBe("Le message transféré.");+ it("strips 'VS:' prefix", () => {+ expect(stripSubjectForwardingPrefix("VS: Hallo")).toBe("Hallo");});- it("strips the ASCII-variant 'Message transfere' header block", () => {- const text = [- "---------- Message transfere ----------",- "De : Alice <[email protected]>",- "",- "Bonjour.",- ].join("\n");+ it("strips 'WG:' prefix (German)", () => {+ expect(stripSubjectForwardingPrefix("WG: Hallo")).toBe("Hallo");+ });- expect(stripForwardingHeaderBlock(text)).toBe("Bonjour.");+ it("is case-insensitive", () => {+ expect(stripSubjectForwardingPrefix("fwd: lowercase")).toBe("lowercase");+ expect(stripSubjectForwardingPrefix("FWD - Mixed")).toBe("Mixed");});- });- describe("German forwarded separator", () => {- it("strips the 'Weitergeleitete Nachricht' header block", () => {- const text = [- "---------- Weitergeleitete Nachricht ----------",- "Von: Alice <[email protected]>",- "Datum: 24. Feb. 2026",- "Betreff: Projekt",- "",- "Hallo,",- "",- "Hier ist die Nachricht.",- ].join("\n");+ it("returns the subject unchanged when no prefix is present", () => {+ expect(stripSubjectForwardingPrefix("Meeting notes")).toBe(+ "Meeting notes",+ );+ });- expect(stripForwardingHeaderBlock(text)).toBe(- "Hallo,\n\nHier ist die Nachricht.",+ it("handles dash separator instead of colon", () => {+ expect(stripSubjectForwardingPrefix("Fwd - My subject")).toBe(+ "My subject",);});++ it("should return original subject if stripping results in empty string", () => {+ const subject = "Fwd: ";+ expect(stripSubjectForwardingPrefix(subject)).toBe(subject);+ });});- describe("edge cases", () => {- it("returns original text when stripping results in an empty body", () => {- const text = [- "---------- Forwarded message ----------",- "From: Alice <[email protected]>",- ].join("\n");+ describe("stripMailSignature", () => {+ it("strips the mail signature when separator is present", () => {+ const text = "Hello,\nThis is the email body.\n-- \nSignature here";+ expect(stripMailSignature(text)).toBe("Hello,\nThis is the email body.");+ });- // No blank line → bodyStartIndex stays 0, slicing from 0 is the whole text.- // In this case, the while loop never finds a blank line and bodyStartIndex = 0.- // The strippedBody will be the original text trimmed.- expect(stripForwardingHeaderBlock(text)).toBe(text.trim());+ it("returns original text when no signature separator is found", () => {+ const text = "Hello,\nThis is the email body.\nBest regards.";+ expect(stripMailSignature(text)).toBe(text);});- it("returns original text when body after header block is empty", () => {- const text = [- "---------- Forwarded message ----------",- "From: Alice <[email protected]>",- "",- " ",- ].join("\n");+ it("handles multiple occurrences of the signature separator", () => {+ const text =+ "Hello,\nThis is the email body.\n-- \nFirst signature\n-- \nSecond signature";+ expect(stripMailSignature(text)).toBe("Hello,\nThis is the email body.");+ });- expect(stripForwardingHeaderBlock(text)).toBe(text.trim());+ it("trims whitespace from the result", () => {+ const text = "Hello,\nThis is the email body.\n-- \nSignature here ";+ expect(stripMailSignature(text)).toBe("Hello,\nThis is the email body.");});+ });- it("preserves multi-paragraph body content", () => {+ describe("getForwardedMailContent", () => {+ it("strips both the forwarding header block and the mail signature", () => {const text = ["---------- Forwarded message ----------",- "From: Alice <[email protected]>",+ "From: Alice <[email protected]>","",- "First paragraph.",- "",- "Second paragraph.",- "",- "Third paragraph.",+ "Hi Bob,",+ "This is the original message.",+ "-- \nAlice's signature",].join("\n");- expect(stripForwardingHeaderBlock(text)).toBe(- "First paragraph.\n\nSecond paragraph.\n\nThird paragraph.",+ expect(getForwardedMailContent(text)).toBe(+ "Hi Bob,\nThis is the original message.",);});- it("trims leading and trailing whitespace from the result", () => {+ it("returns original text when stripping results in empty content", () => {const text = ["---------- Forwarded message ----------",- "From: Alice",+ "From: Alice <[email protected]>","",- " ",- " Body content ",- " ",+ "-- \nAlice's signature",].join("\n");- expect(stripForwardingHeaderBlock(text)).toBe("Body content");+ // Both stripping operations would result in empty content, so original text should be returned.+ expect(getForwardedMailContent(text)).toBe(text.trim());});- });-});--describe("stripSubjectForwardingPrefix", () => {- it("strips 'Fwd:' prefix", () => {- expect(stripSubjectForwardingPrefix("Fwd: Hello")).toBe("Hello");- });-- it("strips 'FW:' prefix", () => {- expect(stripSubjectForwardingPrefix("FW: Hello")).toBe("Hello");- });-- it("strips 'FWD:' prefix", () => {- expect(stripSubjectForwardingPrefix("FWD: Hello")).toBe("Hello");- });-- it("strips 'Tr:' prefix (French)", () => {- expect(stripSubjectForwardingPrefix("Tr: Bonjour")).toBe("Bonjour");- });-- it("strips 'SV:' prefix (Scandinavian)", () => {- expect(stripSubjectForwardingPrefix("SV: Hei")).toBe("Hei");- });-- it("strips 'VS:' prefix", () => {- expect(stripSubjectForwardingPrefix("VS: Hallo")).toBe("Hallo");- });-- it("strips 'WG:' prefix (German)", () => {- expect(stripSubjectForwardingPrefix("WG: Hallo")).toBe("Hallo");- });- it("is case-insensitive", () => {- expect(stripSubjectForwardingPrefix("fwd: lowercase")).toBe("lowercase");- expect(stripSubjectForwardingPrefix("FWD - Mixed")).toBe("Mixed");- });+ it("handles text with no forwarding header and no signature", () => {+ const text = "Hello,\nThis is a simple email body.";+ expect(getForwardedMailContent(text)).toBe(text);+ });- it("returns the subject unchanged when no prefix is present", () => {- expect(stripSubjectForwardingPrefix("Meeting notes")).toBe("Meeting notes");- });+ it("handles text with only a forwarding header", () => {+ const text = [+ "---------- Forwarded message ----------",+ "From: Alice <[email protected]>",+ "",+ "This is the original message.",+ ].join("\n");- it("handles dash separator instead of colon", () => {- expect(stripSubjectForwardingPrefix("Fwd - My subject")).toBe("My subject");- });+ expect(getForwardedMailContent(text)).toBe(+ "This is the original message.",+ );+ });- it("should return original subject if stripping results in empty string", () => {- const subject = "Fwd: ";- expect(stripSubjectForwardingPrefix(subject)).toBe(subject);+ it("handles text with only a mail signature", () => {+ const text = "Hello,\nThis is the email body.\n-- \nBest regards.";+ expect(getForwardedMailContent(text)).toBe(+ "Hello,\nThis is the email body.",+ );+ });});});diff --git a/supabase/functions/postmark/forwardedParser.ts b/supabase/functions/postmark/forwardedParser.tsindex 54eb9734..f2df4ea6 100644--- a/supabase/functions/postmark/forwardedParser.ts+++ b/supabase/functions/postmark/forwardedParser.ts@@ -39,17 +39,12 @@ export const stripForwardingHeaderBlock = (text: string): string => {i++;}- const strippedBody = lines.slice(bodyStartIndex).join("\n").trim();-- if (strippedBody === "") {- // If stripping the header block results in an empty body, return the original text- console.warn(- "Stripping forwarded header block resulted in empty body, returning original text",- );- return text.trim();+ // No blank line found after the header → no body to extract+ if (bodyStartIndex === 0) {+ return "";}- return strippedBody;+ return lines.slice(bodyStartIndex).join("\n").trim();};export const stripSubjectForwardingPrefix = (subject: string): string => {@@ -66,3 +61,23 @@ export const stripSubjectForwardingPrefix = (subject: string): string => {return result;};++export const stripMailSignature = (text: string): string => {+ const signatureSeparatorIndex = text.indexOf("\n-- \n");+ if (signatureSeparatorIndex !== -1) {+ return text.substring(0, signatureSeparatorIndex).trim();+ }+ return text.trim();+};++export const getForwardedMailContent = (body: string): string => {+ const strippedBody = stripForwardingHeaderBlock(stripMailSignature(body));++ if (strippedBody.length === 0) {+ console.warn(+ "Stripping mail signature and forwarded header block resulted in empty note content, returning original body",+ );+ return body.trim();+ }+ return strippedBody;+};diff --git a/supabase/functions/postmark/index.ts b/supabase/functions/postmark/index.tsindex 5a75a6fa..cd5894c7 100644--- a/supabase/functions/postmark/index.ts+++ b/supabase/functions/postmark/index.ts@@ -6,7 +6,7 @@import "jsr:@supabase/functions-js/edge-runtime.d.ts";import { addNoteToContact } from "./addNoteToContact.ts";import {- stripForwardingHeaderBlock,+ getForwardedMailContent,stripSubjectForwardingPrefix,} from "./forwardedParser.ts";import { extractMailContactData } from "./extractMailContactData.ts";@@ -80,7 +80,7 @@ Deno.serve(async (req) => {},];}- TextBody = stripForwardingHeaderBlock(TextBody);+ TextBody = getForwardedMailContent(TextBody);Subject = stripSubjectForwardingPrefix(Subject);}