prefer json accept over html

This commit is contained in:
Moon Man 2023-12-31 07:57:13 -05:00
parent 2bfcf63e4a
commit 8dafbfc0d9
1 changed files with 36 additions and 23 deletions

View File

@ -29,13 +29,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")) { console.log("Request headers for object route:", req.headers);
res.redirect(`/${article.slug}.html`);
} if (req.accepts([
else if (req.accepts(["text/plain", "text/markdown"])) { "application/activity+json",
res.redirect(`/${article.slug}.md`); "application/ld+json",
} "json"
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);
@ -44,6 +44,12 @@ 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 if (req.accepts(["text/plain", "text/markdown"])) {
res.redirect(`/${article.slug}.md`);
}
else {
res.redirect(`/${article.slug}.html`);
}
} }
else { else {
res.status(404).end(); res.status(404).end();
@ -62,19 +68,23 @@ app.get(Routes.activity, async (req, res) => {
if (article) { if (article) {
console.log("Request headers for activity route:", req.headers); console.log("Request headers for activity route:", req.headers);
if (req.accepts("html")) { if (req.accepts([
res.redirect(`/${article.slug}.html`); "application/activity+json",
} "application/ld+json",
else if (req.accepts(["text/plain", "text/markdown"])) { "json"
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 if (req.accepts(["text/plain", "text/markdown"])) {
res.redirect(`/${article.slug}.md`);
}
else {
res.redirect(`/${article.slug}.html`);
}
} }
else { else {
res.status(404).end(); res.status(404).end();
@ -96,7 +106,6 @@ app.get(Routes.outbox, async (req: Request<{ actor: string }>, res) => {
else { else {
res.status(404).send("not found"); res.status(404).send("not found");
} }
}); });
app.get(Routes.actor, async (req, res) => { app.get(Routes.actor, async (req, res) => {
@ -104,7 +113,18 @@ app.get(Routes.actor, async (req, res) => {
const actor = await getUserByNickname(nickname); const actor = await getUserByNickname(nickname);
if (actor) { if (actor) {
if (req.accepts("html")) { if (req.accepts([
"application/activity+json",
"application/ld+json",
"json"
])) {
const obj = userToPerson(actor);
const body = JSON.stringify(obj, null, 4);
res.set("Content-Type", ACTIVITYPUB_TYPE);
res.send(body);
}
else {
const posts = (await getByUserId(actor.id)).map((a) => `<li><a href="/${a.slug}.html">${escapeHtml(a.title)}</a></li>`).join(""); const posts = (await getByUserId(actor.id)).map((a) => `<li><a href="/${a.slug}.html">${escapeHtml(a.title)}</a></li>`).join("");
const body = `<!DOCTYPE html> const body = `<!DOCTYPE html>
@ -124,13 +144,6 @@ app.get(Routes.actor, async (req, res) => {
res.set("Content-Type", "text/html; charset=utf-8"); res.set("Content-Type", "text/html; charset=utf-8");
res.send(body); res.send(body);
} }
else {
const obj = userToPerson(actor);
const body = JSON.stringify(obj, null, 4);
res.set("Content-Type", ACTIVITYPUB_TYPE);
res.send(body);
}
} }
else { else {
res.status(404).send("actor not found"); res.status(404).send("actor not found");