anbot

78ef64a / anbot.mjs

View file at this commit

diff --git a/anbot.mjs b/anbot.mjs
index 234cb00..867b85a 100644
--- a/anbot.mjs
+++ b/anbot.mjs
@@ -18,7 +18,8 @@ import {
 } from './core.mjs'
 import { pushAuth, pushWithIdentity } from './push-auth.mjs'
 import { receivePush } from './receive.mjs'
-import { publishPost, readFeed } from './social-store.mjs'
+import { receivePost } from './post-http.mjs'
+import { publishPost, readFeed, submitPost } from './social-store.mjs'
 import { pull } from './sync.mjs'
 import { blobPage, commitPage, diffPage, feedPage, indexPage, notFoundPage, treePage } from './webui.mjs'
 
@@ -30,7 +31,8 @@ const help = `Usage:
   node anbot.mjs push    --data DIR --url CLONE_URL [--source CHECKOUT] [--branch BRANCH]
   node anbot.mjs sync    --data DIR --url NODE_URL
   node anbot.mjs identity --data DIR
-  node anbot.mjs post    --data DIR --text TEXT [--reply-to POST_ID]
+  node anbot.mjs post    --data DIR --text TEXT [--reply-to POST_ID] [--url NODE_URL]
+  node anbot.mjs post    --data DIR --id POST_ID --url NODE_URL
 
 The default listen address is 127.0.0.1:8787. The data directory's parent must exist.`
 
@@ -272,6 +274,10 @@ async function runServer(root, identity, host, port, baseUrl, allowlist, pushOri
 
   const server = createServer(async (request, response) => {
     const [pathname, query = ''] = request.url.split('?', 2)
+    if (request.method === 'POST' && pathname === '/posts' && !query) {
+      await receivePost(root, request, response, auth?.isAllowed)
+      return
+    }
     if (request.method === 'GET') {
       if (pathname === '/feed-live.js') {
         response.writeHead(200, {
@@ -459,6 +465,8 @@ async function main() {
   if (command === 'post') {
     options.text = { type: 'string' }
     options['reply-to'] = { type: 'string' }
+    options.id = { type: 'string' }
+    options.url = { type: 'string' }
   }
   if (command === 'sync') {
     options.url = { type: 'string' }
@@ -479,8 +487,23 @@ async function main() {
     return
   }
   if (command === 'post') {
-    requireThat(values.text !== undefined, '--text is required')
-    const id = await publishPost(root, await loadIdentity(root), values.text, values['reply-to'])
+    const retry = values.id !== undefined
+    requireThat(retry ? values.url && values.text === undefined && values['reply-to'] === undefined
+      : values.text !== undefined, 'Supply --text, or --id with --url to retry')
+    const remote = values.url === undefined ? null : new URL(values.url)
+    requireThat(!remote || ((remote.protocol === 'https:' ||
+      (remote.protocol === 'http:' && ['127.0.0.1', 'localhost', '[::1]'].includes(remote.hostname))) &&
+      !remote.username && !remote.password && !remote.search && !remote.hash),
+    'Use an HTTPS node URL or local loopback URL')
+    const identity = await loadIdentity(root)
+    const id = retry ? values.id : await publishPost(root, identity, values.text, values['reply-to'])
+    if (remote) {
+      try {
+        await submitPost(root, identity, id, remote)
+      } catch (error) {
+        throw new Error(`${error.message}. Retry an existing local post with --id ${id} --url ${remote.origin}`)
+      }
+    }
     console.log(JSON.stringify({ id }))
     return
   }

Git ยท Social feed