From 7bb96b2b861c1357be40c26950e13fa5d65ab09c Mon Sep 17 00:00:00 2001
From: Leandro Facchinetti
Date: Fri, 12 Mar 2021 10:02:31 +0000
Subject: [PATCH]
---
src/index.ts | 96 +++++++++++++++++++++++++++++-----------------------
1 file changed, 53 insertions(+), 43 deletions(-)
diff --git a/src/index.ts b/src/index.ts
index af4b849..54eee1d 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -28,12 +28,48 @@ export default function killTheNewsletter(
const database = new Database(
path.join(rootDirectory, "kill-the-newsletter.db")
);
- database.function("newReference", () =>
+ database.function("newReference", (): string =>
cryptoRandomString({
length: 16,
characters: "abcdefghijklmnopqrstuvwxyz0123456789",
})
);
+ database.function(
+ "entryFeedCreatedTitle",
+ (title: string): string => `“${title}” inbox created`
+ );
+ database.function(
+ "entryFeedCreatedAuthor",
+ (): string => "Kill the Newsletter!"
+ );
+ database.function(
+ "entryFeedCreatedContent",
+ (feedReference: string): HTML => html`
+
+ Sign up for the newsletter with
+ ${feedReference}@${webApplication.get("email host")}
+
+
+ Subscribe to the Atom feed at
+ ${webApplication.get("url")}/feeds/${feedReference}.xml
+
+
+ Don’t share these addresses.
+ They contain an identifier that other people could use to send you spam
+ and to control your newsletter subscriptions.
+
+ Enjoy your readings!
+
+ Create another inbox
+
+ `
+ );
databaseMigrate(database, [
sql`
CREATE TABLE "feeds" (
@@ -54,6 +90,18 @@ export default function killTheNewsletter(
"content" TEXT NOT NULL
);
+ CREATE TRIGGER "entryFeedCreated"
+ AFTER INSERT ON "feeds"
+ BEGIN
+ INSERT INTO "entries" ("feed", "title", "author", "content")
+ VALUES (
+ "NEW"."id",
+ entryFeedCreatedTitle("NEW"."title"),
+ entryFeedCreatedAuthor(),
+ entryFeedCreatedContent("NEW"."reference")
+ );
+ END;
+
CREATE TRIGGER "feedsUpdatedAt"
AFTER INSERT ON "entries"
BEGIN
@@ -287,54 +335,16 @@ export default function killTheNewsletter(
`
)
);
-
const feedId = database.run(
sql`INSERT INTO "feeds" ("title") VALUES (${req.body.name})`
).lastInsertRowid;
- const feed = database.get<{ reference: string }>(
- sql`SELECT "reference" FROM "feeds" WHERE "id" = ${feedId}`
+ const entry = database.get<{ title: string; content: HTML }>(
+ sql`SELECT "title", "content" FROM "entries" WHERE "feed" = ${feedId}`
)!;
-
- const created = html`
-
- Sign up for the newsletter with
- ${feed.reference}@${webApplication.get("email host")}
-
-
- Subscribe to the Atom feed at
- ${webApplication.get("url")}/feeds/${feed.reference}.xml
-
-
- Don’t share these addresses.
- They contain an identifier that other people could use to send you spam
- and to control your newsletter subscriptions.
-
- Enjoy your readings!
-
- Create another inbox
-
- `;
-
- // TODO: Do this entry with a trigger.
- database.run(
- sql`
- INSERT INTO "entries" ("feed", "title", "author", "content")
- VALUES (${feedId}, ${`“${req.body.name}” inbox created`}, ${"Kill the Newsletter!"}, ${created})
- `
- );
-
res.send(
layout(html`
-
- “${req.body.name}” inbox created
- $${created}
-
+ ${entry.title}
+ $${entry.content}
`)
);
});