better accept handling, maybe

This commit is contained in:
Moon Man 2023-12-31 08:08:52 -05:00
parent 8dafbfc0d9
commit a292552a3f
1 changed files with 17 additions and 3 deletions

View File

@ -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) => `<li><a href="/${a.slug}.html">${escapeHtml(a.title)}</a></li>`).join("");
const body = `<!DOCTYPE html>
@ -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");