anbot

0431c76 / anbot.mjs

View file at this commit

diff --git a/anbot.mjs b/anbot.mjs
index 867b85a..dc1bb09 100644
--- a/anbot.mjs
+++ b/anbot.mjs
@@ -20,6 +20,7 @@ import { pushAuth, pushWithIdentity } from './push-auth.mjs'
 import { receivePush } from './receive.mjs'
 import { receivePost } from './post-http.mjs'
 import { publishPost, readFeed, submitPost } from './social-store.mjs'
+import { verifyPost } from './social.mjs'
 import { pull } from './sync.mjs'
 import { blobPage, commitPage, diffPage, feedPage, indexPage, notFoundPage, treePage } from './webui.mjs'
 
@@ -262,6 +263,38 @@ async function serveFeed(root, query, response) {
   serveHtml(response, missing ? 404 : 200, feedPage({ posts: matching, author, post, missing }))
 }
 
+async function serveRawPost(root, query, response) {
+  const id = new URLSearchParams(query).get('post')
+  try {
+    recordPath(id)
+  } catch {
+    response.writeHead(404).end('Post not found\n')
+    return
+  }
+  try {
+    const stored = await createFsStore(root).getPost(id)
+    let record
+    if (stored) {
+      record = parseJson(stored)
+      const post = await verifyPost(record)
+      requireThat(post.id === id, 'Stored post does not match its record ID')
+    } else {
+      record = (await readPushAnnouncements(root)).find(post => post.id === id)?.record
+    }
+    if (!record) {
+      response.writeHead(404).end('Post not found\n')
+      return
+    }
+    response.writeHead(200, {
+      'Content-Type': 'application/json; charset=utf-8', 'Cache-Control': 'no-store',
+      'X-Content-Type-Options': 'nosniff'
+    }).end(JSON.stringify({ id: record.id, signature: record.signature, content: record.content }, null, 2) + '\n')
+  } catch (error) {
+    console.error(error.message)
+    response.writeHead(503).end('Post unavailable\n')
+  }
+}
+
 async function runServer(root, identity, host, port, baseUrl, allowlist, pushOrigin) {
   const verified = new Map()
   const { repoId } = await selectedRepository(root, verified)
@@ -279,6 +312,10 @@ async function runServer(root, identity, host, port, baseUrl, allowlist, pushOri
       return
     }
     if (request.method === 'GET') {
+      if (pathname === '/raw') {
+        await serveRawPost(root, query, response)
+        return
+      }
       if (pathname === '/feed-live.js') {
         response.writeHead(200, {
           'Content-Type': 'text/javascript; charset=utf-8', 'Cache-Control': 'no-cache'

Git ยท Social feed