From a292552a3fbcc9cf5634631fdd7dbc92ff8cfe6f Mon Sep 17 00:00:00 2001 From: Moon Man Date: Sun, 31 Dec 2023 08:08:52 -0500 Subject: [PATCH] better accept handling, maybe --- src/index.ts | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 8b86322..86e8f81 100644 --- a/src/index.ts +++ b/src/index.ts @@ -41,13 +41,16 @@ app.get(Routes.object, async (req, res) => { const obj = createArticleObject(article, nickname); obj["@context"] = CONTEXT; + console.log(`returning object id: ${article.id} as json`); res.append("Content-Type", ACTIVITYPUB_TYPE); res.send(JSON.stringify(obj, null, 4)); } else if (req.accepts(["text/plain", "text/markdown"])) { + console.log(`redirecting object id: ${article.id} to markdown`); res.redirect(`/${article.slug}.md`); } - else { + else if (req.accepts("html")) { + console.log(`redirecting object id: ${article.id} to html`); res.redirect(`/${article.slug}.html`); } } @@ -76,15 +79,21 @@ app.get(Routes.activity, async (req, res) => { const user = await getUserById(article.users_id) as User; const activity = createArticleActivity(article, user); + console.log(`returning activity id: ${id} as json`); res.append("Content-Type", ACTIVITYPUB_TYPE); res.send(JSON.stringify(activity, null, 4)); } else if (req.accepts(["text/plain", "text/markdown"])) { + console.log(`redirecting activity id: ${id} to markdown`); res.redirect(`/${article.slug}.md`); } - else { + else if (req.accepts("html")) { + console.log(`redirecting activity id: ${id} to html`); res.redirect(`/${article.slug}.html`); } + else { + res.status(406).end(); + } } else { res.status(404).end(); @@ -121,10 +130,12 @@ app.get(Routes.actor, async (req, res) => { const obj = userToPerson(actor); const body = JSON.stringify(obj, null, 4); + console.log(`returning actor: ${nickname} as json`); res.set("Content-Type", ACTIVITYPUB_TYPE); res.send(body); } - else { + else if (req.accepts("html")) { + console.log(`returning actor: ${nickname} as html`); const posts = (await getByUserId(actor.id)).map((a) => `
  • ${escapeHtml(a.title)}
  • `).join(""); const body = ` @@ -144,6 +155,9 @@ app.get(Routes.actor, async (req, res) => { res.set("Content-Type", "text/html; charset=utf-8"); res.send(body); } + else { + res.status(406).end(); + } } else { res.status(404).send("actor not found");