From 31166bff3b4593bbe165db46ccd1154f8fd3a3e0 Mon Sep 17 00:00:00 2001 From: Moon Man Date: Sun, 31 Dec 2023 11:30:03 -0500 Subject: [PATCH] temp front page --- src/article.ts | 11 +++++++++-- src/index.ts | 27 ++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/article.ts b/src/article.ts index bc1874d..f74ba60 100644 --- a/src/article.ts +++ b/src/article.ts @@ -32,8 +32,8 @@ export const getById = async (articleId: number): Promise
=> { export const getByUserId = async (userId: number): Promise => db
("articles") - .where("users_id", userId) - .orderBy("created_at", "desc"); + .where("users_id", userId) + .orderBy("created_at", "desc"); export const insert = async (userId: number, slug: string, title: string): Promise
=> { const data: Record = { @@ -52,3 +52,10 @@ export const insert = async (userId: number, slug: string, title: string): Promi ...data } as Article)) }; + +export const getAll = async (count: number, lastId = Number.MAX_SAFE_INTEGER) => + db
("articles") + .where("id", "<", lastId) + .orderBy("created_at", "desc") + .limit(count) + ; diff --git a/src/index.ts b/src/index.ts index 86e8f81..5e804f5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,7 @@ import bodyParser from "body-parser"; import { toCollection as getOutbox } from "./outbox.js"; import { getById as getUserById, getNickname, get as getUserByNickname, getId as getUserId, User } from "./user.js"; import { Routes } from "./router.js"; -import { getBySlug as getArticleBySlug, getById, getByUserId } from "./article.js"; +import { getBySlug as getArticleBySlug, getById, getByUserId, getAll as getAllArticles } from "./article.js"; import { userToPerson, TYPE as ACTIVITYPUB_TYPE, handleInboxPost, createArticleObject, CONTEXT, createArticleActivity } from "./activity.js"; import { handleFollowerGet } from "./follower.js"; import { handleWebfingerGet } from "./net.js"; @@ -22,6 +22,31 @@ app.use( bodyParser.text({ type: "*/*" }) ); +/** + * Temporary until better templating is in. + */ +app.get("/", async (req, res) => { + const last = typeof req.query.last === "number" ? parseInt(req.query.last) : Number.MAX_SAFE_INTEGER; + + const articles = await getAllArticles(8, last); + + const renderedArticles = articles.map((a) => ` +
  • + ${escapeHtml(a.title)} - ${a.created_at.toDateString()} +
  • + `).join("\n"); + + const payload = ` + + +

    ${escapeHtml(process.env.blog_title || "")}

    + +
      + ${renderedArticles} +
    + `; +}); + app.post(Routes.inbox, handleInboxPost); app.get(Routes.followers, handleFollowerGet);