From 6799b5fa42c85a0a1ec16a0171e3f56548a37262 Mon Sep 17 00:00:00 2001 From: Leandro Facchinetti Date: Sat, 13 Mar 2021 09:28:00 +0000 Subject: [PATCH] --- src/index.ts | 149 ++++++++++++++++++++++++++------------------------- 1 file changed, 75 insertions(+), 74 deletions(-) diff --git a/src/index.ts b/src/index.ts index e68f1da..4bf13fb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,81 +28,25 @@ export default function killTheNewsletter( const database = new Database( path.join(rootDirectory, "kill-the-newsletter.db") ); - database.function("newRandomReference", (): string => - cryptoRandomString({ - length: 16, - characters: "abcdefghijklmnopqrstuvwxyz0123456789", - }) - ); - database.function( - "welcomeEntryTitle", - (title: string): string => `“${title}” inbox created` - ); - database.function( - "welcomeEntryContent", - (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" ( "id" INTEGER PRIMARY KEY AUTOINCREMENT, "createdAt" TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, "updatedAt" TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, - "reference" TEXT NOT NULL UNIQUE DEFAULT (newRandomReference()), + "reference" TEXT NOT NULL UNIQUE, "title" TEXT NOT NULL ); CREATE TABLE "entries" ( "id" INTEGER PRIMARY KEY AUTOINCREMENT, "createdAt" TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, - "reference" TEXT NOT NULL UNIQUE DEFAULT (newRandomReference()), + "reference" TEXT NOT NULL UNIQUE, "feed" INTEGER NOT NULL REFERENCES "feeds", "title" TEXT NOT NULL, "author" TEXT NOT NULL, "content" TEXT NOT NULL ); - - CREATE TRIGGER "welcomeEntry" - AFTER INSERT ON "feeds" - BEGIN - INSERT INTO "entries" ("feed", "title", "author", "content") - VALUES ( - "NEW"."id", - welcomeEntryTitle("NEW"."title"), - 'Kill the Newsletter!', - welcomeEntryContent("NEW"."reference") - ); - END; - - CREATE TRIGGER "entriesInsertImpliesFeedsUpdatedAt" - AFTER INSERT ON "entries" - BEGIN - UPDATE "feeds" SET "updatedAt" = CURRENT_TIMESTAMP WHERE "id" = "NEW"."feed"; - END; `, ]); @@ -323,26 +267,67 @@ export default function killTheNewsletter( ) return res.status(400).send( layout( - html`

- Error: Missing newsletter name. - Try again. -

` + html` +

+ Error: Missing newsletter name. + Try again. +

+ ` ) ); - const feedId = database.run( - sql`INSERT INTO "feeds" ("title") VALUES (${req.body.name})` - ).lastInsertRowid; - const entry = database.get<{ title: string; content: HTML }>( - sql`SELECT "title", "content" FROM "entries" WHERE "feed" = ${feedId}` - )!; + const feedReference = newReference(); + const welcomeTitle = `“${req.body.name}” inbox created`; + const welcomeContent = 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 +

+ `; + + database.executeTransaction(() => { + const feedId = database.run( + sql`INSERT INTO "feeds" ("reference", "title") VALUES (${feedReference}, ${req.body.name})` + ).lastInsertRowid; + database.run( + sql` + INSERT INTO "entries" ("reference", "feed", "title", "author", "content") + VALUES ( + ${newReference()} + ${feedId}, + ${welcomeTitle}, + 'Kill the Newsletter!', + ${welcomeContent} + ) + ` + ); + }); res.send( layout(html` -

${entry.title}

- $${entry.content} +

${welcomeTitle}

+ $${welcomeContent} `) ); }); @@ -475,10 +460,19 @@ export default function killTheNewsletter( if (feed === undefined) continue; database.run( sql` - INSERT INTO "entries" ("feed", "title", "author", "content") - VALUES (${feed.id}, ${subject}, ${from}, ${body}) + INSERT INTO "entries" ("reference", "feed", "title", "author", "content") + VALUES ( + ${newReference()}, + ${feed.id}, + ${subject}, + ${from}, + ${body} + ) ` ); + database.run( + sql`UPDATE "feeds" SET "updatedAt" = CURRENT_TIMESTAMP WHERE "id" = ${feed.id}` + ); while (renderFeed(feedReference)!.length > 500_000) database.run( sql`DELETE FROM "entries" WHERE "feed" = ${feed.id} ORDER BY "createdAt" ASC LIMIT 1` @@ -497,6 +491,13 @@ export default function killTheNewsletter( }, }); + function newReference(): string { + return cryptoRandomString({ + length: 16, + characters: "abcdefghijklmnopqrstuvwxyz0123456789", + }); + } + return { webApplication, emailApplication }; }