temporary quality of life stuff prepping for html support
This commit is contained in:
parent
ab02c18b9a
commit
c3d57ccbe8
38
src/index.ts
38
src/index.ts
|
@ -3,11 +3,18 @@ import bodyParser from "body-parser";
|
|||
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, TYPE } from "./activity.js";
|
||||
import { getBySlug as getArticleBySlug, getById, getByUserId } from "./article.js";
|
||||
import { userToPerson, TYPE as ACTIVITYPUB_TYPE, handleInboxPost, createArticleObject, CONTEXT, createArticleActivity } from "./activity.js";
|
||||
import { handleFollowerGet } from "./follower.js";
|
||||
import { handleWebfingerGet } from "./net.js";
|
||||
|
||||
const escapeHtml = (unsafe: string) => unsafe
|
||||
.replaceAll("&", "&")
|
||||
.replaceAll("<", "<")
|
||||
.replaceAll(">", ">")
|
||||
.replaceAll("\"", """)
|
||||
.replace("'", "'");
|
||||
|
||||
const port = parseInt(process.env.port || "8080");
|
||||
const app = express();
|
||||
|
||||
|
@ -95,18 +102,33 @@ app.get(Routes.actor, async (req, res) => {
|
|||
const actor = await getUserByNickname(nickname);
|
||||
|
||||
if (actor) {
|
||||
const accept = req.headers["accept"] || "";
|
||||
if (accept.includes(ACTIVITYPUB_TYPE)) {
|
||||
if (req.accepts("html")) {
|
||||
const posts = (await getByUserId(actor.id)).map((a) => `<li><a href="/${a.slug}.html">${escapeHtml(a.title)}</a></li>`).join("");
|
||||
|
||||
const body = `<!DOCTYPE html>
|
||||
<html>
|
||||
username: ${escapeHtml(actor.nickname)}
|
||||
<br>
|
||||
Name: ${escapeHtml(actor.name)}
|
||||
<p>
|
||||
Bio: ${escapeHtml(actor.bio)}
|
||||
<p>
|
||||
Posts:<br>
|
||||
<ul>
|
||||
${posts}
|
||||
</ul>
|
||||
`;
|
||||
|
||||
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 {
|
||||
// TODO: html version.
|
||||
res.status(403).send("todo");
|
||||
}
|
||||
}
|
||||
else {
|
||||
res.status(404).send("actor not found");
|
||||
|
|
Loading…
Reference in New Issue