better handle different accept headers
This commit is contained in:
parent
18173efd42
commit
ab02c18b9a
36
src/index.ts
36
src/index.ts
|
@ -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,13 +22,21 @@ 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) {
|
||||||
const nickname = await getNickname(article.users_id) as string;
|
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 obj = createArticleObject(article, nickname);
|
const obj = createArticleObject(article, nickname);
|
||||||
obj["@context"] = CONTEXT;
|
obj["@context"] = CONTEXT;
|
||||||
|
|
||||||
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,11 +53,19 @@ app.get(Routes.activity, async (req, res) => {
|
||||||
const article = await getById(id);
|
const article = await getById(id);
|
||||||
|
|
||||||
if (article) {
|
if (article) {
|
||||||
const user = await getUserById(article.users_id) as User;
|
if (req.accepts("html")) {
|
||||||
const activity = createArticleActivity(article, user);
|
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 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();
|
||||||
|
|
Loading…
Reference in New Issue