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));
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 {
console.log("Request headers for object route:", req.headers);
if (req.accepts([
"application/activity+json",
"application/ld+json",
"json"
])) {
const nickname = await getNickname(article.users_id) as string;
const obj = createArticleObject(article, nickname);
@ -44,6 +44,12 @@ app.get(Routes.object, async (req, res) => {
res.append("Content-Type", ACTIVITYPUB_TYPE);
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 {
res.status(404).end();
@ -62,19 +68,23 @@ app.get(Routes.activity, async (req, res) => {
if (article) {
console.log("Request headers for activity route:", req.headers);
if (req.accepts("html")) {
res.redirect(`/${article.slug}.html`);
}
else if (req.accepts(["text/plain", "text/markdown"])) {
res.redirect(`/${article.slug}.md`);
}
else {
if (req.accepts([
"application/activity+json",
"application/ld+json",
"json"
])) {
const user = await getUserById(article.users_id) as User;
const activity = createArticleActivity(article, user);
res.append("Content-Type", ACTIVITYPUB_TYPE);
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 {
res.status(404).end();
@ -96,7 +106,6 @@ app.get(Routes.outbox, async (req: Request<{ actor: string }>, res) => {
else {
res.status(404).send("not found");
}
});
app.get(Routes.actor, async (req, res) => {
@ -104,7 +113,18 @@ app.get(Routes.actor, async (req, res) => {
const actor = await getUserByNickname(nickname);
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 body = `<!DOCTYPE html>
@ -124,13 +144,6 @@ app.get(Routes.actor, async (req, res) => {
res.set("Content-Type", "text/html; charset=utf-8");
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 {
res.status(404).send("actor not found");