-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
helpers: apply sanitizations to email; add removeLabels
- Loading branch information
Showing
2 changed files
with
75 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
function getHeaderValue(email: string, header: string) { | ||
const headerStartIndex = email.indexOf(`${header}: `) + header.length + 2; | ||
const headerEndIndex = email.indexOf("\n", headerStartIndex); | ||
const headerValue = email.substring(headerStartIndex, headerEndIndex); | ||
|
||
return headerValue; | ||
} | ||
|
||
function setHeaderValue(email: string, header: string, value: string) { | ||
return email.replace(getHeaderValue(email, header), value); | ||
} | ||
|
||
|
||
// Google sets their own Message-ID and put the original one in X-Google-Original-Message-ID | ||
// when ARC forwarding | ||
function revertGoogleMessageId(email: string): string { | ||
// (Optional check) This only happens when google does ARC | ||
if (!email.includes("ARC-Authentication-Results")) { | ||
return email; | ||
} | ||
|
||
const googleReplacedMessageId = getHeaderValue( | ||
email, | ||
"X-Google-Original-Message-ID" | ||
); | ||
|
||
if (googleReplacedMessageId) { | ||
return setHeaderValue(email, "Message-ID", googleReplacedMessageId); | ||
} | ||
|
||
return email; | ||
} | ||
|
||
// Remove labels inserted to Subject - `[ListName] Newsletter 2024` to `Newsletter 2024` | ||
function removeLabels(email: string): string { | ||
// Replace Subject: [label] with Subject: | ||
const sanitized = email.replace(/Subject: \[.*\]/, "Subject:"); | ||
return sanitized; | ||
} | ||
|
||
|
||
const sanitizers = [ | ||
revertGoogleMessageId, | ||
removeLabels, | ||
]; | ||
|
||
|
||
export default sanitizers; |