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 { Routes } from "./router.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 { handleWebfingerGet } from "./net.js";
|
||||
|
||||
|
@ -22,13 +22,21 @@ app.get(Routes.object, async (req, res) => {
|
|||
const article = await getById(parseInt(req.params.id));
|
||||
|
||||
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);
|
||||
obj["@context"] = CONTEXT;
|
||||
const obj = createArticleObject(article, nickname);
|
||||
obj["@context"] = CONTEXT;
|
||||
|
||||
res.append("Content-Type", ACTIVITYPUB_TYPE);
|
||||
res.send(JSON.stringify(obj, null, 4));
|
||||
res.append("Content-Type", ACTIVITYPUB_TYPE);
|
||||
res.send(JSON.stringify(obj, null, 4));
|
||||
}
|
||||
}
|
||||
else {
|
||||
res.status(404).end();
|
||||
|
@ -45,11 +53,19 @@ app.get(Routes.activity, async (req, res) => {
|
|||
const article = await getById(id);
|
||||
|
||||
if (article) {
|
||||
const user = await getUserById(article.users_id) as User;
|
||||
const activity = createArticleActivity(article, user);
|
||||
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 activity = createArticleActivity(article, user);
|
||||
|
||||
res.append("Content-Type", ACTIVITYPUB_TYPE);
|
||||
res.send(JSON.stringify(activity, null, 4));
|
||||
res.append("Content-Type", ACTIVITYPUB_TYPE);
|
||||
res.send(JSON.stringify(activity, null, 4));
|
||||
}
|
||||
}
|
||||
else {
|
||||
res.status(404).end();
|
||||
|
|
Loading…
Reference in New Issue