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