This commit is contained in:
Leandro Facchinetti 2021-03-11 21:52:54 +00:00
parent 1c9bcb750f
commit dc22ea9d74
1 changed files with 99 additions and 120 deletions

View File

@ -325,17 +325,16 @@ export default function killTheNewsletter(
); );
}); });
webApplication.get<{ feedReference: string }, HTML, {}, {}, {}>( function renderFeed(feedReference: string): HTML | undefined {
"/feeds/:feedReference.xml",
(req, res, next) => {
const feed = database.get<{ const feed = database.get<{
id: number; id: number;
updatedAt: string; updatedAt: string;
title: string; title: string;
}>( }>(
sql`SELECT "id", "updatedAt", "title" FROM "feeds" WHERE "reference" = ${req.params.feedReference}` sql`SELECT "id", "updatedAt", "title" FROM "feeds" WHERE "reference" = ${feedReference}`
); );
if (feed === undefined) return next(); if (feed === undefined) return;
const entries = database.all<{ const entries = database.all<{
createdAt: string; createdAt: string;
reference: string; reference: string;
@ -349,32 +348,26 @@ export default function killTheNewsletter(
WHERE "feed" = ${feed.id} WHERE "feed" = ${feed.id}
` `
); );
res
.contentType("application/atom+xml") return html`
.header("X-Robots-Tag", "noindex")
.send(
html`
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"> <feed xmlns="http://www.w3.org/2005/Atom">
<link <link
rel="self" rel="self"
type="application/atom+xml" type="application/atom+xml"
href="${webApplication.get("url")}/feeds/${req.params href="${webApplication.get("url")}/feeds/${feedReference}.xml"
.feedReference}.xml"
/> />
<link <link
rel="alternate" rel="alternate"
type="text/html" type="text/html"
href="${webApplication.get("url")}/" href="${webApplication.get("url")}/"
/> />
<id>urn:kill-the-newsletter:${req.params.feedReference}</id> <id>urn:kill-the-newsletter:${feedReference}</id>
<title>${feed.title}</title> <title>${feed.title}</title>
<subtitle <subtitle
>Kill the Newsletter! Inbox: >Kill the Newsletter! Inbox:
${req.params.feedReference}@${webApplication.get("email host")} ${feedReference}@${webApplication.get("email host")}
${webApplication.get("url")}/feeds/${feedReference}.xml</subtitle
${webApplication.get("url")}/feeds/${req.params
.feedReference}.xml</subtitle
> >
<updated>${new Date(feed.updatedAt).toISOString()}</updated> <updated>${new Date(feed.updatedAt).toISOString()}</updated>
<author><name>Kill the Newsletter!</name></author> <author><name>Kill the Newsletter!</name></author>
@ -384,9 +377,7 @@ export default function killTheNewsletter(
<id>urn:kill-the-newsletter:${entry.reference}</id> <id>urn:kill-the-newsletter:${entry.reference}</id>
<title>${entry.title}</title> <title>${entry.title}</title>
<author><name>${entry.author}</name></author> <author><name>${entry.author}</name></author>
<updated <updated>${new Date(entry.createdAt).toISOString()}</updated>
>${new Date(entry.createdAt).toISOString()}</updated
>
<link <link
rel="alternate" rel="alternate"
type="text/html" type="text/html"
@ -399,8 +390,18 @@ export default function killTheNewsletter(
` `
)} )}
</feed> </feed>
`.trim() `.trim();
); }
webApplication.get<{ feedReference: string }, HTML, {}, {}, {}>(
"/feeds/:feedReference.xml",
(req, res, next) => {
const feed = renderFeed(req.params.feedReference);
if (feed === undefined) return next();
res
.contentType("application/atom+xml")
.header("X-Robots-Tag", "noindex")
.send(feed);
} }
); );
@ -431,61 +432,40 @@ export default function killTheNewsletter(
const emailApplication = new SMTPServer({ const emailApplication = new SMTPServer({
disabledCommands: ["AUTH", "STARTTLS"], disabledCommands: ["AUTH", "STARTTLS"],
async onData(stream, session, callback) { async onData(stream, session, callback) {
/*
try { try {
const atHost = "@" + webApplication.get("email host");
const email = await mailparser.simpleParser(stream); const email = await mailparser.simpleParser(stream);
const content = const from = email.from?.text ?? "";
const subject = email.subject ?? "";
const body =
typeof email.html === "string" ? email.html : email.textAsHtml ?? ""; typeof email.html === "string" ? email.html : email.textAsHtml ?? "";
for (const address of new Set( for (const address of new Set(
session.envelope.rcptTo.map(({ address }) => address) session.envelope.rcptTo.map(
)) { (smtpServerAddress) => smtpServerAddress.address
const match = address.match(
new RegExp(
`^(?<identifier>\\w+)@${escapeStringRegexp(EMAIL_DOMAIN)}$`
) )
)) {
if (!address.endsWith(atHost)) continue;
const feedReference = address.slice(0, -atHost.length);
const feed = database.get<{ id: number }>(
sql`SELECT "id" FROM "feeds" WHERE "reference" = ${feedReference}`
); );
if (match?.groups === undefined) continue; if (feed === undefined) continue;
const identifier = match.groups.identifier.toLowerCase(); database.run(
const path = feedFilePath(identifier); sql`
let text; INSERT INTO "entries" ("reference", "feed", "title", "author", "content")
try { VALUES (
text = await fs.readFile(path, "utf8"); ${newReference()}, ${feed.id}, ${subject}, ${from}, ${body}
} catch { )
continue; `
}
const feed = new JSDOM(text, { contentType: "text/xml" });
const document = feed.window.document;
const updated = document.querySelector("feed > updated");
if (updated === null) {
console.error(`Field updated not found: ${path}`);
continue;
}
updated.textContent = now();
const renderedEntry = entry(
identifier,
createIdentifier(),
X(email.subject ?? ""),
X(email.from?.text ?? ""),
X(content)
); );
const firstEntry = document.querySelector( // TODO: Do this with a trigger.
"feed > entry:first-of-type" database.run(
sql`UPDATE "feeds" SET "updatedAt" = datetime('now') WHERE "id" = ${feed.id}`
); );
if (firstEntry === null)
document while (renderFeed(feedReference)!.length > 500_00)
.querySelector("feed")! database.run(
.insertAdjacentHTML("beforeend", renderedEntry); sql`DELETE FROM "entries" WHERE "feed" = ${feed.id} ORDER BY "createdAt" ASC LIMIT 1`
else firstEntry.insertAdjacentHTML("beforebegin", renderedEntry);
while (feed.serialize().length > 500_000) {
const lastEntry = document.querySelector(
"feed > entry:last-of-type"
);
if (lastEntry === null) break;
lastEntry.remove();
}
await writeFileAtomic(
path,
html`<?xml version="1.0" encoding="utf-8"?>${feed.serialize()}`.trim()
); );
} }
callback(); callback();
@ -497,7 +477,6 @@ export default function killTheNewsletter(
stream.resume(); stream.resume();
callback(new Error("Failed to receive message. Please try again.")); callback(new Error("Failed to receive message. Please try again."));
} }
*/
}, },
}); });