This commit is contained in:
Leandro Facchinetti 2021-03-12 09:11:16 +00:00
parent 6cb6eba93f
commit aa2b35f54b
1 changed files with 20 additions and 15 deletions

View File

@ -28,20 +28,26 @@ export default function killTheNewsletter(
const database = new Database( const database = new Database(
path.join(rootDirectory, "kill-the-newsletter.db") path.join(rootDirectory, "kill-the-newsletter.db")
); );
database.function("newReference", () =>
cryptoRandomString({
length: 16,
characters: "abcdefghijklmnopqrstuvwxyz0123456789",
})
);
databaseMigrate(database, [ databaseMigrate(database, [
sql` sql`
CREATE TABLE "feeds" ( CREATE TABLE "feeds" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT, "id" INTEGER PRIMARY KEY AUTOINCREMENT,
"createdAt" TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, "createdAt" TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, "updatedAt" TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
"reference" TEXT NOT NULL UNIQUE, "reference" TEXT NOT NULL UNIQUE DEFAULT (newReference()),
"title" TEXT NOT NULL "title" TEXT NOT NULL
); );
CREATE TABLE "entries" ( CREATE TABLE "entries" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT, "id" INTEGER PRIMARY KEY AUTOINCREMENT,
"createdAt" TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, "createdAt" TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
"reference" TEXT NOT NULL UNIQUE, "reference" TEXT NOT NULL UNIQUE DEFAULT (newReference()),
"feed" INTEGER NOT NULL REFERENCES "feeds", "feed" INTEGER NOT NULL REFERENCES "feeds",
"title" TEXT NOT NULL, "title" TEXT NOT NULL,
"author" TEXT NOT NULL, "author" TEXT NOT NULL,
@ -282,19 +288,24 @@ export default function killTheNewsletter(
) )
); );
const reference = newReference(); 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 created = html` const created = html`
<p> <p>
Sign up for the newsletter with<br /> Sign up for the newsletter with<br />
<code class="copyable" <code class="copyable"
>${reference}@${webApplication.get("email host")}</code >${feed.reference}@${webApplication.get("email host")}</code
> >
</p> </p>
<p> <p>
Subscribe to the Atom feed at<br /> Subscribe to the Atom feed at<br />
<code class="copyable" <code class="copyable"
>${webApplication.get("url")}/feeds/${reference}.xml</code >${webApplication.get("url")}/feeds/${feed.reference}.xml</code
> >
</p> </p>
<p> <p>
@ -310,15 +321,11 @@ export default function killTheNewsletter(
</p> </p>
`; `;
// TODO: Add references with a default value with is a call to a JavaScript function.
const feedId = database.run(
sql`INSERT INTO "feeds" ("reference", "title") VALUES (${reference}, ${req.body.name})`
).lastInsertRowid;
// TODO: Do this entry with a trigger. // TODO: Do this entry with a trigger.
database.run( database.run(
sql` sql`
INSERT INTO "entries" ("reference", "feed", "title", "author", "content") INSERT INTO "entries" ("feed", "title", "author", "content")
VALUES (${newReference()}, ${feedId}, ${`${req.body.name}” inbox created`}, ${"Kill the Newsletter!"}, ${created}) VALUES (${feedId}, ${`${req.body.name}” inbox created`}, ${"Kill the Newsletter!"}, ${created})
` `
); );
@ -459,10 +466,8 @@ export default function killTheNewsletter(
if (feed === undefined) continue; if (feed === undefined) continue;
database.run( database.run(
sql` sql`
INSERT INTO "entries" ("reference", "feed", "title", "author", "content") INSERT INTO "entries" ("feed", "title", "author", "content")
VALUES ( VALUES (${feed.id}, ${subject}, ${from}, ${body})
${newReference()}, ${feed.id}, ${subject}, ${from}, ${body}
)
` `
); );
while (renderFeed(feedReference)!.length > 500_000) while (renderFeed(feedReference)!.length > 500_000)