better handle different accept headers

This commit is contained in:
Moon Man 2023-12-27 16:57:59 -05:00
parent 18173efd42
commit ab02c18b9a
1 changed files with 26 additions and 10 deletions

View File

@ -4,7 +4,7 @@ import { toCollection as getOutbox } from "./outbox.js";
import { getById as getUserById, getNickname, get as getUserByNickname, getId as getUserId, User } from "./user.js"; import { getById as getUserById, getNickname, get as getUserByNickname, getId as getUserId, User } from "./user.js";
import { Routes } from "./router.js"; import { Routes } from "./router.js";
import { getBySlug as getArticleBySlug, getById } from "./article.js"; import { getBySlug as getArticleBySlug, getById } from "./article.js";
import { userToPerson, TYPE as ACTIVITYPUB_TYPE, handleInboxPost, createArticleObject, CONTEXT, createArticleActivity } from "./activity.js"; import { userToPerson, TYPE as ACTIVITYPUB_TYPE, handleInboxPost, createArticleObject, CONTEXT, createArticleActivity, TYPE } from "./activity.js";
import { handleFollowerGet } from "./follower.js"; import { handleFollowerGet } from "./follower.js";
import { handleWebfingerGet } from "./net.js"; import { handleWebfingerGet } from "./net.js";
@ -22,6 +22,13 @@ app.get(Routes.object, async (req, res) => {
const article = await getById(parseInt(req.params.id)); const article = await getById(parseInt(req.params.id));
if (article) { if (article) {
if (req.accepts("html")) {
res.redirect(`/${article.slug}.html`);
}
else if (req.accepts(["text/plain", "text/markdown"])) {
res.redirect(`/${article.slug}.md`);
}
else {
const nickname = await getNickname(article.users_id) as string; const nickname = await getNickname(article.users_id) as string;
const obj = createArticleObject(article, nickname); const obj = createArticleObject(article, nickname);
@ -30,6 +37,7 @@ app.get(Routes.object, async (req, res) => {
res.append("Content-Type", ACTIVITYPUB_TYPE); res.append("Content-Type", ACTIVITYPUB_TYPE);
res.send(JSON.stringify(obj, null, 4)); res.send(JSON.stringify(obj, null, 4));
} }
}
else { else {
res.status(404).end(); res.status(404).end();
} }
@ -45,12 +53,20 @@ app.get(Routes.activity, async (req, res) => {
const article = await getById(id); const article = await getById(id);
if (article) { if (article) {
if (req.accepts("html")) {
res.redirect(`/${article.slug}.html`);
}
else if (req.accepts(["text/plain", "text/markdown"])) {
res.redirect(`/${article.slug}.md`);
}
else {
const user = await getUserById(article.users_id) as User; const user = await getUserById(article.users_id) as User;
const activity = createArticleActivity(article, user); const activity = createArticleActivity(article, user);
res.append("Content-Type", ACTIVITYPUB_TYPE); res.append("Content-Type", ACTIVITYPUB_TYPE);
res.send(JSON.stringify(activity, null, 4)); res.send(JSON.stringify(activity, null, 4));
} }
}
else { else {
res.status(404).end(); res.status(404).end();
} }