Add support for alternate URL

This commit is contained in:
Johan Holmerin 2020-07-14 19:42:41 +02:00
parent 0fd156cec5
commit 62dee956d7
3 changed files with 32 additions and 6 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
/node_modules /node_modules
/static/feeds/* /static/feeds/*
!/static/feeds/.gitkeep !/static/feeds/.gitkeep
/static/alternate/*
!/static/alternate/.gitkeep

View File

@ -34,6 +34,7 @@ export const webServer = express()
try { try {
const { name } = req.body; const { name } = req.body;
const identifier = createIdentifier(); const identifier = createIdentifier();
await writeFileAtomic(alternatePath(identifier), created(identifier));
await writeFileAtomic(feedPath(identifier), feed(X(name), identifier)); await writeFileAtomic(feedPath(identifier), feed(X(name), identifier));
res.send( res.send(
layout(` layout(`
@ -61,10 +62,15 @@ export const emailServer = new SMTPServer({
async onData(stream, session, callback) { async onData(stream, session, callback) {
try { try {
const email = await mailparser.simpleParser(stream); const email = await mailparser.simpleParser(stream);
const identifier = createIdentifier();
const content =
typeof email.html === "string" ? email.html : email.textAsHtml ?? "";
await writeFileAtomic(alternatePath(identifier), content);
const newEntry = entry( const newEntry = entry(
X(email.subject ?? ""), X(email.subject ?? ""),
X(email.from?.text ?? ""), X(email.from?.text ?? ""),
X(typeof email.html === "string" ? email.html : email.textAsHtml ?? "") X(content),
identifier
); );
for (const { address } of session.envelope.rcptTo) { for (const { address } of session.envelope.rcptTo) {
const match = address.match( const match = address.match(
@ -154,7 +160,9 @@ function feed(name: string, identifier: string): string {
<link rel="self" type="application/atom+xml" href="${feedURL( <link rel="self" type="application/atom+xml" href="${feedURL(
identifier identifier
)}"/> )}"/>
<link rel="alternate" type="text/html" href="${BASE_URL}/alternate"/> <link rel="alternate" type="text/html" href="${alternateURL(
identifier
)}"/>
<id>${urn(identifier)}</id> <id>${urn(identifier)}</id>
<title>${name}</title> <title>${name}</title>
<subtitle>Kill the Newsletter! Inbox: ${feedEmail( <subtitle>Kill the Newsletter! Inbox: ${feedEmail(
@ -165,20 +173,28 @@ function feed(name: string, identifier: string): string {
${entry( ${entry(
`${name}” Inbox Created`, `${name}” Inbox Created`,
"Kill the Newsletter!", "Kill the Newsletter!",
X(created(identifier)) X(created(identifier)),
identifier
)} )}
</feed> </feed>
`; `;
} }
function entry(title: string, author: string, content: string): string { function entry(
title: string,
author: string,
content: string,
identifier: string
): string {
return ` return `
<entry> <entry>
<id>${urn(createIdentifier())}</id> <id>${urn(identifier)}</id>
<title>${title}</title> <title>${title}</title>
<author><name>${author}</name></author> <author><name>${author}</name></author>
<updated>${now()}</updated> <updated>${now()}</updated>
<link rel="alternate" type="text/html" href="${BASE_URL}/alternate"/> <link rel="alternate" type="text/html" href="${alternateURL(
identifier
)}"/>
<content type="html">${content}</content> <content type="html">${content}</content>
</entry> </entry>
`.trim(); `.trim();
@ -207,6 +223,14 @@ function feedEmail(identifier: string): string {
return `${identifier}@${EMAIL_DOMAIN}`; return `${identifier}@${EMAIL_DOMAIN}`;
} }
function alternatePath(identifier: string): string {
return `static/alternate/${identifier}.html`;
}
function alternateURL(identifier: string): string {
return `${BASE_URL}/alternate/${identifier}.html`;
}
function urn(identifier: string): string { function urn(identifier: string): string {
return `urn:kill-the-newsletter:${identifier}`; return `urn:kill-the-newsletter:${identifier}`;
} }

View File