Skip to content

Commit

Permalink
return snippet metadata for bots
Browse files Browse the repository at this point in the history
  • Loading branch information
echo8 committed Dec 22, 2024
1 parent 1be6251 commit 652d3b6
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 9 deletions.
163 changes: 163 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"kysely": "^0.27.4",
"multiformats": "^13.3.1",
"pino": "^9.5.0",
"uhtml": "^4.7.0",
"zod": "^3.23.8"
},
"devDependencies": {
Expand Down
45 changes: 36 additions & 9 deletions src/server/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { Agent } from "@atproto/api";
import { AppContext, Session, SnippetType, SnippetSchema } from "./types";
import { createTRPCContext } from "./context";
import { isDid } from "@atproto/did";
import { page, html } from "./util/view";
import { env } from "./util/env";

const logger = pino({ name: "router" });

Expand Down Expand Up @@ -45,15 +47,40 @@ export const createExpressRouter = (ctx: AppContext) => {
router.get(
"/bot/user/:handleOrDid/snippet/:rkey",
expressHandler(async (req, res) => {
logger.info(
{
handleOrDid: req.params["handleOrDid"],
rkey: req.params["rkey"],
userAgent: req.headers["user-agent"],
},
"bot request"
);
return res.status(200).send();
const handleOrDid = req.params["handleOrDid"];
const rkey = req.params["rkey"];
const did = isDid(handleOrDid)
? handleOrDid
: await ctx.didService.resolveHandleToDid(handleOrDid);
if (did) {
const snippet = await ctx.snippetService.get(did, rkey);
if (snippet) {
const url = `${env.PUBLIC_URL}/user/${snippet.authorDid}/snippet/${snippet.rkey}`;
return res.type("html").send(
page(
html`<html>
<head>
<title>${snippet.title}</title>
<meta property="og:title" content="${snippet.title}" />
<meta
property="og:description"
content="${snippet.description}"
/>
<meta property="og:type" content="website" />
<meta property="og:url" content="${url}" />
<meta property="og:site_name" content="Pastesphere" />
<meta name="description" content="${snippet.description}" />
<link rel="canonical" href="${url}" />
</head>
<body>
Snippet metadata for bots.
</body>
</html>`
)
);
}
}
return res.status(404).send();
})
);

Expand Down
12 changes: 12 additions & 0 deletions src/server/src/util/view.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @ts-ignore
import ssr from 'uhtml/ssr'
import type initSSR from 'uhtml/types/init-ssr'
import type { Hole } from 'uhtml/types/keyed'

export type { Hole }

export const { html }: ReturnType<typeof initSSR> = ssr()

export function page(hole: Hole) {
return `<!DOCTYPE html>\n${hole.toDOM().toString()}`
}

0 comments on commit 652d3b6

Please sign in to comment.