This commit is contained in:
Leandro Facchinetti 2021-03-12 10:02:31 +00:00
parent aa2b35f54b
commit 7bb96b2b86
1 changed files with 53 additions and 43 deletions

View File

@ -28,12 +28,48 @@ 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", () => database.function("newReference", (): string =>
cryptoRandomString({ cryptoRandomString({
length: 16, length: 16,
characters: "abcdefghijklmnopqrstuvwxyz0123456789", 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`
<p>
Sign up for the newsletter with<br />
<code class="copyable"
>${feedReference}@${webApplication.get("email host")}</code
>
</p>
<p>
Subscribe to the Atom feed at<br />
<code class="copyable"
>${webApplication.get("url")}/feeds/${feedReference}.xml</code
>
</p>
<p>
<strong>Dont share these addresses.</strong><br />
They contain an identifier that other people could use to send you spam
and to control your newsletter subscriptions.
</p>
<p><strong>Enjoy your readings!</strong></p>
<p>
<a href="${webApplication.get("url")}/"
><strong>Create another inbox</strong></a
>
</p>
`
);
databaseMigrate(database, [ databaseMigrate(database, [
sql` sql`
CREATE TABLE "feeds" ( CREATE TABLE "feeds" (
@ -54,6 +90,18 @@ export default function killTheNewsletter(
"content" TEXT NOT NULL "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" CREATE TRIGGER "feedsUpdatedAt"
AFTER INSERT ON "entries" AFTER INSERT ON "entries"
BEGIN BEGIN
@ -287,54 +335,16 @@ export default function killTheNewsletter(
</p>` </p>`
) )
); );
const feedId = database.run( const feedId = database.run(
sql`INSERT INTO "feeds" ("title") VALUES (${req.body.name})` sql`INSERT INTO "feeds" ("title") VALUES (${req.body.name})`
).lastInsertRowid; ).lastInsertRowid;
const feed = database.get<{ reference: string }>( const entry = database.get<{ title: string; content: HTML }>(
sql`SELECT "reference" FROM "feeds" WHERE "id" = ${feedId}` sql`SELECT "title", "content" FROM "entries" WHERE "feed" = ${feedId}`
)!; )!;
const created = html`
<p>
Sign up for the newsletter with<br />
<code class="copyable"
>${feed.reference}@${webApplication.get("email host")}</code
>
</p>
<p>
Subscribe to the Atom feed at<br />
<code class="copyable"
>${webApplication.get("url")}/feeds/${feed.reference}.xml</code
>
</p>
<p>
<strong>Dont share these addresses.</strong><br />
They contain an identifier that other people could use to send you spam
and to control your newsletter subscriptions.
</p>
<p><strong>Enjoy your readings!</strong></p>
<p>
<a href="${webApplication.get("url")}/"
><strong>Create another inbox</strong></a
>
</p>
`;
// 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( res.send(
layout(html` layout(html`
<p> <p><strong>${entry.title}</strong></p>
<strong>${req.body.name} inbox created</strong> $${entry.content}
$${created}
</p>
`) `)
); );
}); });