mirror of
https://github.com/juherr/kill-the-news.git
synced 2026-06-20 22:03:48 +00:00
chore: apply Prettier formatting to entire codebase
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
+47
-41
@@ -1,4 +1,4 @@
|
||||
import { EmailData } from '../types';
|
||||
import { EmailData } from "../types";
|
||||
|
||||
/**
|
||||
* Simple email parser specialized for ForwardEmail.net's webhook format
|
||||
@@ -14,59 +14,62 @@ export class EmailParser {
|
||||
const match = emailAddress.match(/^([a-z]+\.[a-z]+\.\d{2})@/i);
|
||||
return match ? match[1] : null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Parse email data from ForwardEmail.net's webhook payload
|
||||
* @param payload ForwardEmail.net webhook payload
|
||||
*/
|
||||
static parseForwardEmailPayload(payload: any): EmailData {
|
||||
if (!payload) {
|
||||
throw new Error('Missing or invalid webhook payload');
|
||||
throw new Error("Missing or invalid webhook payload");
|
||||
}
|
||||
|
||||
|
||||
// Extract the "to" address
|
||||
const toAddress = payload.recipients?.[0] || '';
|
||||
|
||||
const toAddress = payload.recipients?.[0] || "";
|
||||
|
||||
// Extract the sender information using ForwardEmail's structure
|
||||
const fromAddress = payload.from?.text ||
|
||||
(payload.from?.value?.[0]?.address ?
|
||||
`${payload.from.value[0].name || ''} <${payload.from.value[0].address}>` :
|
||||
'Unknown Sender');
|
||||
|
||||
const fromAddress =
|
||||
payload.from?.text ||
|
||||
(payload.from?.value?.[0]?.address
|
||||
? `${payload.from.value[0].name || ""} <${payload.from.value[0].address}>`
|
||||
: "Unknown Sender");
|
||||
|
||||
// Extract subject
|
||||
let subject = payload.subject || 'No Subject';
|
||||
let subject = payload.subject || "No Subject";
|
||||
// Decode any encoded words in the subject
|
||||
subject = this.decodeEncodedWords(subject);
|
||||
|
||||
|
||||
// Get content, preferring HTML over plain text
|
||||
const content = payload.html || payload.text || '';
|
||||
|
||||
const content = payload.html || payload.text || "";
|
||||
|
||||
// Create simple email data object
|
||||
return {
|
||||
subject,
|
||||
from: fromAddress,
|
||||
content,
|
||||
receivedAt: payload.date ? new Date(payload.date).getTime() : Date.now(),
|
||||
headers: this.extractHeaders(payload)
|
||||
headers: this.extractHeaders(payload),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Extract headers from ForwardEmail payload
|
||||
*/
|
||||
private static extractHeaders(payload: any): Record<string, string> {
|
||||
const headers: Record<string, string> = {};
|
||||
|
||||
|
||||
// Extract headers from headerLines if available
|
||||
if (payload.headerLines && Array.isArray(payload.headerLines)) {
|
||||
payload.headerLines.forEach((h: {key: string; line: string}) => {
|
||||
payload.headerLines.forEach((h: { key: string; line: string }) => {
|
||||
const key = h.key.toLowerCase();
|
||||
const value = h.line.replace(new RegExp(`^${h.key}:\\s*`, 'i'), '').trim();
|
||||
const value = h.line
|
||||
.replace(new RegExp(`^${h.key}:\\s*`, "i"), "")
|
||||
.trim();
|
||||
headers[key] = value;
|
||||
});
|
||||
}
|
||||
// Or from headers string if provided
|
||||
else if (typeof payload.headers === 'string') {
|
||||
else if (typeof payload.headers === "string") {
|
||||
payload.headers.split(/\r?\n/).forEach((line: string) => {
|
||||
const match = line.match(/^([^:]+):\s*(.*)$/);
|
||||
if (match) {
|
||||
@@ -74,35 +77,38 @@ export class EmailParser {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
return headers;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Decode RFC 2047 encoded words in headers
|
||||
* @param text Text that may contain encoded words like =?UTF-8?Q?Hello_World?=
|
||||
*/
|
||||
static decodeEncodedWords(text: string): string {
|
||||
if (!text) return '';
|
||||
|
||||
if (!text) return "";
|
||||
|
||||
// Simple RFC 2047 encoded-word decoder
|
||||
return text.replace(/=\?([^?]+)\?([BQ])\?([^?]+)\?=/gi, (_, charset, encoding, text) => {
|
||||
if (encoding.toUpperCase() === 'B') {
|
||||
// Base64 encoding
|
||||
try {
|
||||
const decoded = atob(text);
|
||||
return decoded;
|
||||
} catch (e) {
|
||||
return text;
|
||||
return text.replace(
|
||||
/=\?([^?]+)\?([BQ])\?([^?]+)\?=/gi,
|
||||
(_, charset, encoding, text) => {
|
||||
if (encoding.toUpperCase() === "B") {
|
||||
// Base64 encoding
|
||||
try {
|
||||
const decoded = atob(text);
|
||||
return decoded;
|
||||
} catch (e) {
|
||||
return text;
|
||||
}
|
||||
} else if (encoding.toUpperCase() === "Q") {
|
||||
// Quoted-printable encoding
|
||||
return this.decodeQuotedPrintable(text.replace(/_/g, " "));
|
||||
}
|
||||
} else if (encoding.toUpperCase() === 'Q') {
|
||||
// Quoted-printable encoding
|
||||
return this.decodeQuotedPrintable(text.replace(/_/g, ' '));
|
||||
}
|
||||
return text;
|
||||
});
|
||||
return text;
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Decode quoted-printable encoded text
|
||||
* @param text Quoted-printable encoded text
|
||||
@@ -112,4 +118,4 @@ export class EmailParser {
|
||||
return String.fromCharCode(parseInt(hex, 16));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user