fedipod 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (276) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +123 -0
  3. package/architecture.svg +89 -0
  4. package/bin/fedipod.mjs +2303 -0
  5. package/cli.md +243 -0
  6. package/groups.md +85 -0
  7. package/gui.md +99 -0
  8. package/lib/account.mjs +87 -0
  9. package/lib/admin.mjs +1606 -0
  10. package/lib/atproto.mjs +310 -0
  11. package/lib/bskyfeed.mjs +166 -0
  12. package/lib/bskygroup.mjs +162 -0
  13. package/lib/c2s.mjs +305 -0
  14. package/lib/certs.mjs +265 -0
  15. package/lib/deliver.mjs +249 -0
  16. package/lib/directory.mjs +90 -0
  17. package/lib/embed.mjs +214 -0
  18. package/lib/export-collections.mjs +161 -0
  19. package/lib/front-core.mjs +322 -0
  20. package/lib/gateway-core.mjs +115 -0
  21. package/lib/guard.mjs +213 -0
  22. package/lib/home.mjs +161 -0
  23. package/lib/httpsig.mjs +109 -0
  24. package/lib/import.mjs +357 -0
  25. package/lib/intake.mjs +1710 -0
  26. package/lib/keys.mjs +139 -0
  27. package/lib/lease.mjs +190 -0
  28. package/lib/localapi.mjs +87 -0
  29. package/lib/mastoapi.mjs +1588 -0
  30. package/lib/migrate.mjs +127 -0
  31. package/lib/oidc-auth.mjs +53 -0
  32. package/lib/podrdf.mjs +132 -0
  33. package/lib/ports.mjs +27 -0
  34. package/lib/publisher.mjs +1003 -0
  35. package/lib/remote.mjs +303 -0
  36. package/lib/safefetch.mjs +235 -0
  37. package/lib/setup.mjs +262 -0
  38. package/lib/social.mjs +505 -0
  39. package/lib/storage.mjs +170 -0
  40. package/lib/store.mjs +470 -0
  41. package/lib/streaming.mjs +154 -0
  42. package/lib/tagfeed.mjs +158 -0
  43. package/lib/ua.mjs +29 -0
  44. package/lib/update.mjs +91 -0
  45. package/lib/webpush.mjs +98 -0
  46. package/lib/wire.mjs +762 -0
  47. package/package.json +60 -0
  48. package/phanpy/dist/404.html +40 -0
  49. package/phanpy/dist/_headers +2 -0
  50. package/phanpy/dist/apple-touch-icon.png +0 -0
  51. package/phanpy/dist/assets/ICONS-DIPJeU0C.js +2 -0
  52. package/phanpy/dist/assets/ICONS-DIPJeU0C.js.map +1 -0
  53. package/phanpy/dist/assets/ScheduledAtField-D-Abfo4J.js +183 -0
  54. package/phanpy/dist/assets/ScheduledAtField-D-Abfo4J.js.map +1 -0
  55. package/phanpy/dist/assets/Temml-CCjoacWt.woff2 +0 -0
  56. package/phanpy/dist/assets/__vite-browser-external-C7iut881.js +2 -0
  57. package/phanpy/dist/assets/__vite-browser-external-C7iut881.js.map +1 -0
  58. package/phanpy/dist/assets/boosts-carousel-BiOaNdMT.jpg +0 -0
  59. package/phanpy/dist/assets/catch-up-O5q5C75Z.png +0 -0
  60. package/phanpy/dist/assets/chunk-aKtaBQYM.js +1 -0
  61. package/phanpy/dist/assets/compose-CFEx7wb4.js +2 -0
  62. package/phanpy/dist/assets/compose-CFEx7wb4.js.map +1 -0
  63. package/phanpy/dist/assets/compose-ZJN5xSb4.js +35 -0
  64. package/phanpy/dist/assets/compose-ZJN5xSb4.js.map +1 -0
  65. package/phanpy/dist/assets/debug.module-YFQgwnIr.js +52 -0
  66. package/phanpy/dist/assets/debug.module-YFQgwnIr.js.map +1 -0
  67. package/phanpy/dist/assets/dom-p1YcJ5Rw.js +2 -0
  68. package/phanpy/dist/assets/dom-p1YcJ5Rw.js.map +1 -0
  69. package/phanpy/dist/assets/home-mobile-dark@2x-Dc-J8-SM.png +0 -0
  70. package/phanpy/dist/assets/home-mobile-light@2x-Bd3fod8m.png +0 -0
  71. package/phanpy/dist/assets/home-tablet-dark@2x-BgcNI8le.png +0 -0
  72. package/phanpy/dist/assets/home-tablet-light@2x-BW2RV9T6.png +0 -0
  73. package/phanpy/dist/assets/icons/add-circle-line-WD0mwORD.js +1 -0
  74. package/phanpy/dist/assets/icons/alert-line-CpFUMf9s.js +1 -0
  75. package/phanpy/dist/assets/icons/android-2-line-Bzxnrd3S.js +1 -0
  76. package/phanpy/dist/assets/icons/announcement-line-5nwaJFJm.js +1 -0
  77. package/phanpy/dist/assets/icons/arrow-down-circle-line-a7Id9-Iv.js +1 -0
  78. package/phanpy/dist/assets/icons/arrow-down-line-B8uzXzq6.js +1 -0
  79. package/phanpy/dist/assets/icons/arrow-left-line-CGrGT9C-.js +1 -0
  80. package/phanpy/dist/assets/icons/arrow-right-line-CE-HERfX.js +1 -0
  81. package/phanpy/dist/assets/icons/arrow-to-up-line-CvdQjmcy.js +1 -0
  82. package/phanpy/dist/assets/icons/arrow-up-circle-line-DEparSNF.js +1 -0
  83. package/phanpy/dist/assets/icons/arrow-up-line-C7yBeugc.js +1 -0
  84. package/phanpy/dist/assets/icons/arrows-right-line-BtbXmJGP.js +1 -0
  85. package/phanpy/dist/assets/icons/at-line-CF66Bznk.js +1 -0
  86. package/phanpy/dist/assets/icons/attachment-line-BY8It-l9.js +1 -0
  87. package/phanpy/dist/assets/icons/blockquote-line-BS6pU_hm.js +1 -0
  88. package/phanpy/dist/assets/icons/board-line-C0UWXyA6.js +1 -0
  89. package/phanpy/dist/assets/icons/bookmark-line-1CtX5JLF.js +1 -0
  90. package/phanpy/dist/assets/icons/building-5-line-Dd59MzbO.js +1 -0
  91. package/phanpy/dist/assets/icons/bus-2-line-6_dvA0GG.js +1 -0
  92. package/phanpy/dist/assets/icons/calendar-day-line-Crpl_fmu.js +1 -0
  93. package/phanpy/dist/assets/icons/calendar-month-line-vommts2C.js +1 -0
  94. package/phanpy/dist/assets/icons/calendar-time-add-line-BU_DTlQk.js +1 -0
  95. package/phanpy/dist/assets/icons/camera-line-Cv69MEei.js +1 -0
  96. package/phanpy/dist/assets/icons/celebrate-line-B8DvIoi0.js +1 -0
  97. package/phanpy/dist/assets/icons/chart-bar-line-FK-QOmtF.js +1 -0
  98. package/phanpy/dist/assets/icons/chart-line-line-BGcJAFWw.js +1 -0
  99. package/phanpy/dist/assets/icons/chat-3-line-YFK2-hwz.js +1 -0
  100. package/phanpy/dist/assets/icons/check-circle-line-DsAxFr5-.js +1 -0
  101. package/phanpy/dist/assets/icons/clipboard-line-BBp364gJ.js +1 -0
  102. package/phanpy/dist/assets/icons/close-circle-line-CICdiuGz.js +1 -0
  103. package/phanpy/dist/assets/icons/close-line-DYrFWcjv.js +1 -0
  104. package/phanpy/dist/assets/icons/cloud-line-tiQAbah8.js +1 -0
  105. package/phanpy/dist/assets/icons/code-line-fAl8iiV3.js +1 -0
  106. package/phanpy/dist/assets/icons/comment-2-line-CR3g7WeB.js +1 -0
  107. package/phanpy/dist/assets/icons/copy-2-line-CLW69yF5.js +1 -0
  108. package/phanpy/dist/assets/icons/delete-2-line-B8xmbr4n.js +1 -0
  109. package/phanpy/dist/assets/icons/document-3-line-DBGNhAg2.js +1 -0
  110. package/phanpy/dist/assets/icons/document-line-C7mb9sb7.js +1 -0
  111. package/phanpy/dist/assets/icons/down-line-BGqSSThd.js +1 -0
  112. package/phanpy/dist/assets/icons/edit-4-line-DcgGaEhx.js +1 -0
  113. package/phanpy/dist/assets/icons/emoji-2-line-D8YgnM-y.js +1 -0
  114. package/phanpy/dist/assets/icons/exit-line-Dc07BS-x.js +1 -0
  115. package/phanpy/dist/assets/icons/external-link-line-BvpSkmjO.js +1 -0
  116. package/phanpy/dist/assets/icons/eye-2-line-CiWXx-f_.js +1 -0
  117. package/phanpy/dist/assets/icons/eye-close-line-BgTaSJ4P.js +1 -0
  118. package/phanpy/dist/assets/icons/filter-2-line-B_mR2usy.js +1 -0
  119. package/phanpy/dist/assets/icons/filter-line-C_nm-3cU.js +1 -0
  120. package/phanpy/dist/assets/icons/flag-1-line-C2HB05I0.js +1 -0
  121. package/phanpy/dist/assets/icons/forbid-circle-line-D3LTT9yd.js +1 -0
  122. package/phanpy/dist/assets/icons/formula-line-DLrt2d6j.js +1 -0
  123. package/phanpy/dist/assets/icons/grid-line-CgUmEikf.js +1 -0
  124. package/phanpy/dist/assets/icons/group-line-C5fnX0wU.js +1 -0
  125. package/phanpy/dist/assets/icons/hand-finger-2-line-BGgxbuL9.js +1 -0
  126. package/phanpy/dist/assets/icons/hashtag-line-CyTEEXBb.js +1 -0
  127. package/phanpy/dist/assets/icons/heart-crack-line-BZddbO2C.js +1 -0
  128. package/phanpy/dist/assets/icons/heart-line-CJ7K47J4.js +1 -0
  129. package/phanpy/dist/assets/icons/history-2-line-kWWvFzqv.js +1 -0
  130. package/phanpy/dist/assets/icons/history-line-bGQiwbTH.js +1 -0
  131. package/phanpy/dist/assets/icons/home-3-line-fRU1zeLG.js +1 -0
  132. package/phanpy/dist/assets/icons/information-line-iNk6-sOY.js +1 -0
  133. package/phanpy/dist/assets/icons/keyboard-line-DsCyuQNz.js +1 -0
  134. package/phanpy/dist/assets/icons/layout-4-line-CZkOX52Z.js +1 -0
  135. package/phanpy/dist/assets/icons/layout-5-line-B_znt5wO.js +1 -0
  136. package/phanpy/dist/assets/icons/left-line-eUw0tya4.js +1 -0
  137. package/phanpy/dist/assets/icons/lightning-line-DaKoDdix.js +1 -0
  138. package/phanpy/dist/assets/icons/link-2-line-DCqLmMA7.js +1 -0
  139. package/phanpy/dist/assets/icons/list-check-line-BAxosBh9.js +1 -0
  140. package/phanpy/dist/assets/icons/lock-line-CPoaXeUW.js +1 -0
  141. package/phanpy/dist/assets/icons/mail-line-B0P8aa1L.js +1 -0
  142. package/phanpy/dist/assets/icons/moon-line-CEqNAToP.js +1 -0
  143. package/phanpy/dist/assets/icons/more-1-fill-Dw2e5xab.js +1 -0
  144. package/phanpy/dist/assets/icons/more-3-line-BIiVjYTz.js +1 -0
  145. package/phanpy/dist/assets/icons/notification-line-BZZq2Gtu.js +1 -0
  146. package/phanpy/dist/assets/icons/pencil-line-CKh8-A1v.js +1 -0
  147. package/phanpy/dist/assets/icons/photo-album-line-Bnfg2u40.js +1 -0
  148. package/phanpy/dist/assets/icons/pin-line-DpTolyhs.js +1 -0
  149. package/phanpy/dist/assets/icons/play-fill-B0-G_8lX.js +1 -0
  150. package/phanpy/dist/assets/icons/qrcode-2-line-B6SPCaHn.js +1 -0
  151. package/phanpy/dist/assets/icons/quill-pen-line-B6LUrnCP.js +1 -0
  152. package/phanpy/dist/assets/icons/quote-left-fill-hQZ1cj_6.js +1 -0
  153. package/phanpy/dist/assets/icons/radar-line-CKyroTth.js +1 -0
  154. package/phanpy/dist/assets/icons/react-line-CoIKhhtK.js +1 -0
  155. package/phanpy/dist/assets/icons/refresh-2-line-D0mTF97S.js +1 -0
  156. package/phanpy/dist/assets/icons/right-line-B3KWJaqc.js +1 -0
  157. package/phanpy/dist/assets/icons/rocket-line-4KZl537D.js +1 -0
  158. package/phanpy/dist/assets/icons/round-fill-C69NTrZ0.js +1 -0
  159. package/phanpy/dist/assets/icons/round-line-B9k_1uo-.js +1 -0
  160. package/phanpy/dist/assets/icons/route-line-B28ReHcE.js +1 -0
  161. package/phanpy/dist/assets/icons/rows-4-line-CrBniagM.js +1 -0
  162. package/phanpy/dist/assets/icons/scan-line-DSf74YdP.js +1 -0
  163. package/phanpy/dist/assets/icons/search-2-line-B6geIU1z.js +1 -0
  164. package/phanpy/dist/assets/icons/settings-3-line-azJwK47w.js +1 -0
  165. package/phanpy/dist/assets/icons/settings-6-line-C_5zc5YR.js +1 -0
  166. package/phanpy/dist/assets/icons/share-2-line-D5MxWjrm.js +1 -0
  167. package/phanpy/dist/assets/icons/share-forward-line-Cp61fZzZ.js +1 -0
  168. package/phanpy/dist/assets/icons/sparkles-2-line-DYYyXTaW.js +1 -0
  169. package/phanpy/dist/assets/icons/sparkles-line-C2hY1s6I.js +1 -0
  170. package/phanpy/dist/assets/icons/time-line-BF3eet1v.js +1 -0
  171. package/phanpy/dist/assets/icons/transfer-4-line-oisfvTBw.js +1 -0
  172. package/phanpy/dist/assets/icons/translate-line-BFWol2Ce.js +1 -0
  173. package/phanpy/dist/assets/icons/unlock-line-X_x5vwv3.js +1 -0
  174. package/phanpy/dist/assets/icons/upload-3-line-DgzwUJeW.js +1 -0
  175. package/phanpy/dist/assets/icons/user-4-line-BVSynZCp.js +1 -0
  176. package/phanpy/dist/assets/icons/user-add-2-line-BlGS5Q8q.js +1 -0
  177. package/phanpy/dist/assets/icons/user-add-line-BbXdHG4z.js +1 -0
  178. package/phanpy/dist/assets/icons/user-edit-line-BFAHOV0X.js +1 -0
  179. package/phanpy/dist/assets/icons/user-follow-line-BXux0wSd.js +1 -0
  180. package/phanpy/dist/assets/icons/user-star-line-DdYlpHLU.js +1 -0
  181. package/phanpy/dist/assets/icons/user-warning-line-Cj43j0_E.js +1 -0
  182. package/phanpy/dist/assets/icons/user-x-line-DIDgNlPH.js +1 -0
  183. package/phanpy/dist/assets/icons/volume-line-Bs390KQt.js +1 -0
  184. package/phanpy/dist/assets/icons/volume-mute-line-DzlzIqFZ.js +1 -0
  185. package/phanpy/dist/assets/icons/walk-line-yNXfB-md.js +1 -0
  186. package/phanpy/dist/assets/icons/world-2-line-CenkGIwj.js +1 -0
  187. package/phanpy/dist/assets/icons/zoom-in-line-CaENUphf.js +1 -0
  188. package/phanpy/dist/assets/icons/zoom-out-line-BDNk0EWo.js +1 -0
  189. package/phanpy/dist/assets/instances-BNPptnn-.json +333 -0
  190. package/phanpy/dist/assets/locales/ar-SA-CfheRbCu.js +1 -0
  191. package/phanpy/dist/assets/locales/ca-ES-DHRW86Tz.js +1 -0
  192. package/phanpy/dist/assets/locales/cs-CZ-CKoqEuA1.js +1 -0
  193. package/phanpy/dist/assets/locales/de-DE-ByRPMRFy.js +1 -0
  194. package/phanpy/dist/assets/locales/eo-UY-CPC7wLCS.js +1 -0
  195. package/phanpy/dist/assets/locales/es-ES-dJa0Fd9t.js +1 -0
  196. package/phanpy/dist/assets/locales/eu-ES-C3djlPR2.js +1 -0
  197. package/phanpy/dist/assets/locales/fa-IR-DTT490Ck.js +1 -0
  198. package/phanpy/dist/assets/locales/fi-FI-B23MCPuz.js +1 -0
  199. package/phanpy/dist/assets/locales/fr-FR-BcaQsO5C.js +1 -0
  200. package/phanpy/dist/assets/locales/gl-ES-BR-CVKBN.js +1 -0
  201. package/phanpy/dist/assets/locales/he-IL-CfheRbCu.js +1 -0
  202. package/phanpy/dist/assets/locales/hu-HU-CfheRbCu.js +1 -0
  203. package/phanpy/dist/assets/locales/it-IT-C1136UFV.js +1 -0
  204. package/phanpy/dist/assets/locales/ja-JP-CRZmRAhD.js +1 -0
  205. package/phanpy/dist/assets/locales/kab-Cz8xH_TT.js +1 -0
  206. package/phanpy/dist/assets/locales/ko-KR-CG9UiY4o.js +1 -0
  207. package/phanpy/dist/assets/locales/lt-LT-CJcI6sHZ.js +1 -0
  208. package/phanpy/dist/assets/locales/nb-NO-CfheRbCu.js +1 -0
  209. package/phanpy/dist/assets/locales/nl-NL-DxN4Yc1t.js +1 -0
  210. package/phanpy/dist/assets/locales/oc-FR-CfheRbCu.js +1 -0
  211. package/phanpy/dist/assets/locales/pl-PL-DkwGXf6P.js +1 -0
  212. package/phanpy/dist/assets/locales/pseudo-LOCALE-CQ4XxkQ0.js +1 -0
  213. package/phanpy/dist/assets/locales/pt-BR-nGL0HdFS.js +1 -0
  214. package/phanpy/dist/assets/locales/pt-PT-Cp17aj2z.js +1 -0
  215. package/phanpy/dist/assets/locales/ru-RU-0Y8cIoaK.js +1 -0
  216. package/phanpy/dist/assets/locales/th-TH-CfheRbCu.js +1 -0
  217. package/phanpy/dist/assets/locales/tok-CfheRbCu.js +1 -0
  218. package/phanpy/dist/assets/locales/tr-TR-CfheRbCu.js +1 -0
  219. package/phanpy/dist/assets/locales/uk-UA-C5E3dFBf.js +1 -0
  220. package/phanpy/dist/assets/locales/zh-CN-BHJi7_et.js +1 -0
  221. package/phanpy/dist/assets/locales/zh-TW-CfheRbCu.js +1 -0
  222. package/phanpy/dist/assets/main-BdqNbG-a.js +26 -0
  223. package/phanpy/dist/assets/main-BdqNbG-a.js.map +1 -0
  224. package/phanpy/dist/assets/mock-home-DGhR__He.js +2 -0
  225. package/phanpy/dist/assets/mock-home-DGhR__He.js.map +1 -0
  226. package/phanpy/dist/assets/multi-column-ROck0NVt.jpg +0 -0
  227. package/phanpy/dist/assets/multi-hashtag-timeline-DH5INVHi.jpg +0 -0
  228. package/phanpy/dist/assets/nested-comments-thread-hv59kZLc.jpg +0 -0
  229. package/phanpy/dist/assets/polyfill-force-H8sUkLGp.js +2 -0
  230. package/phanpy/dist/assets/polyfill-force-H8sUkLGp.js.map +1 -0
  231. package/phanpy/dist/assets/pwa-viewport-ogKbF51K.js +20 -0
  232. package/phanpy/dist/assets/pwa-viewport-ogKbF51K.js.map +1 -0
  233. package/phanpy/dist/assets/style-B1iLPTPR.css +2 -0
  234. package/phanpy/dist/assets/temml-DuyIQMei.js +97 -0
  235. package/phanpy/dist/assets/temml-DuyIQMei.js.map +1 -0
  236. package/phanpy/dist/assets/tinyld.light.node-DtbBhLYC.js +2 -0
  237. package/phanpy/dist/assets/tinyld.light.node-DtbBhLYC.js.map +1 -0
  238. package/phanpy/dist/assets/year-in-posts-B2lywqdx.png +0 -0
  239. package/phanpy/dist/assets/year-in-posts-DJv_star.js +3 -0
  240. package/phanpy/dist/assets/year-in-posts-DJv_star.js.map +1 -0
  241. package/phanpy/dist/compose/index.html +20 -0
  242. package/phanpy/dist/favicon.ico +0 -0
  243. package/phanpy/dist/index.html +73 -0
  244. package/phanpy/dist/logo-192.png +0 -0
  245. package/phanpy/dist/logo-512.png +0 -0
  246. package/phanpy/dist/logo-badge-72.png +0 -0
  247. package/phanpy/dist/logo-maskable-512.png +0 -0
  248. package/phanpy/dist/logo-monochrome-512.png +0 -0
  249. package/phanpy/dist/logo-monochrome-maskable-512.png +0 -0
  250. package/phanpy/dist/manifest.webmanifest +1 -0
  251. package/phanpy/dist/og-image-2.jpg +0 -0
  252. package/phanpy/dist/og-image.png +0 -0
  253. package/phanpy/dist/robots.txt +2 -0
  254. package/phanpy/dist/sw.js +25 -0
  255. package/phanpy/dist/version.json +1 -0
  256. package/run-agent.mjs +805 -0
  257. package/scripts/build-dist.mjs +37 -0
  258. package/scripts/verify-ui.mjs +60 -0
  259. package/ui/README.md +23 -0
  260. package/vendor/gate.cjs +132 -0
  261. package/vendor/idp-grant.cjs +394 -0
  262. package/vendor/jfetch.cjs +16 -0
  263. package/web/admin/admin.js +958 -0
  264. package/web/admin/bar.css +64 -0
  265. package/web/admin/bar.js +61 -0
  266. package/web/admin/client/client.js +66 -0
  267. package/web/admin/client/index.html +42 -0
  268. package/web/admin/index.html +493 -0
  269. package/web/admin/setup/index.html +172 -0
  270. package/web/admin/setup/setup.js +268 -0
  271. package/web/admin/window.css +55 -0
  272. package/web/admin/window.js +136 -0
  273. package/web/front/install.sh +69 -0
  274. package/web/front/new-account.html +424 -0
  275. package/web/front/run.html +138 -0
  276. package/web/front/solid-client-authn.bundle.js +2 -0
@@ -0,0 +1,1003 @@
1
+ // publisher.mjs — builds/maintains the actor's public face on the remote pod
2
+ // (webfinger, actor doc, collections, notes) and mirrors truth into the local
3
+ // pod. The remote /ap/ tree is disposable: publishProfile() rebuilds it.
4
+
5
+ import crypto from 'node:crypto';
6
+ import fs from 'node:fs';
7
+ import path from 'node:path';
8
+ import { fileURLToPath } from 'node:url';
9
+ import * as wire from './wire.mjs';
10
+ import { USER_AGENT } from './ua.mjs';
11
+ import { HTTP_TIMEOUT_MS } from './safefetch.mjs';
12
+
13
+ const ACCEPT_AP = 'application/activity+json, application/ld+json; profile="https://www.w3.org/ns/activitystreams"';
14
+ // The default for publishCollections: the whole public surface, ACLs included.
15
+ // A caller that knows what it changed narrows it; a caller that says nothing
16
+ // still gets everything, so a missed call site degrades to the old cost rather
17
+ // than silently publishing nothing.
18
+ const REBUILD_MAX_PER_RUN = 200;
19
+ const ALL_COLLECTIONS = { followers: true, following: true, outbox: true, acls: true,
20
+ pending: true, blocked: true };
21
+ const AGENT_VERSION = JSON.parse(fs.readFileSync(
22
+ path.join(path.dirname(fileURLToPath(import.meta.url)), '../package.json'), 'utf8')).version;
23
+
24
+ export class Publisher {
25
+ constructor({ config, remote, local, store, deliverer, publicKeyPem, log = console.log,
26
+ probeFetch = null, resolveMention = null, privateOnPod = true,
27
+ }) {
28
+ this.config = config;
29
+ this.remote = remote;
30
+ this.local = local;
31
+ this.store = store;
32
+ this.deliverer = deliverer;
33
+ this.publicKeyPem = publicKeyPem;
34
+ // A fronted identity (config.gateway.frontActor) advertises its ids on a
35
+ // shared domain; the map tells RemotePod where each writes on the pod.
36
+ const publicBase = config.gateway?.frontActor
37
+ ? config.gateway.frontActor.replace(/ap\/actor\/?$/, '') : null;
38
+ this.urls = wire.apUrls(config.remotePod, config.root, { publicBase });
39
+ if (this.urls.toPod && this.remote?.setUrlMap) this.remote.setUrlMap(this.urls.toPod);
40
+ // Credential-free by design — it asks what a stranger sees — but routed
41
+ // through the pod's own cooldown and accounting, because it is still a
42
+ // socket opened to that pod. Tests inject their own.
43
+ this.probeFetch = probeFetch || ((u, i) => this.remote.probe(u, i));
44
+ this.resolveMention = resolveMention;
45
+ this.privateOnPod = privateOnPod;
46
+ this.log = log;
47
+ }
48
+
49
+ // Idempotent: (re)write webfinger + actor + collections + container ACLs.
50
+ //
51
+ // ~34 pod requests, so it does not run when it would rewrite the same bytes.
52
+ // Every document below is derived from the actor doc, the handle, the host,
53
+ // whether the actor is quiesced (which decides the inbox ACL) and the agent
54
+ // version (which rides in nodeinfo) — so if none of those moved, there is
55
+ // nothing to say. Phanpy's editor submits the whole form on every save, so
56
+ // "saved without changing anything" is the common case, not a rare one.
57
+ //
58
+ // `force` is for the callers that publish precisely BECAUSE the pod does not
59
+ // have what the digest says it has: the repair path, and the explicit
60
+ // republish button. Without it, an actor lost from the pod would match the
61
+ // digest, be skipped, and leave the agent reporting success while nobody can
62
+ // resolve it.
63
+ async publishProfile({ force = false } = {}) {
64
+ const { urls } = this;
65
+ const host = new URL(urls.base).host;
66
+
67
+ // The owner-only collections (FEP-4ccd, FEP-c648) are advertised only
68
+ // where "owner-only" is real — the pod must provably enforce the private
69
+ // container's ACL, the same bar private posts clear.
70
+ const priv = await this.privateReady() === true;
71
+ const moderators = (this.config.moderators || []).length ? urls.moderators : null;
72
+ // An advertised inbox gateway (config.gateway.mode past 'off') becomes the
73
+ // actor's inbox; deliveries reach the pod inbox through it. Absent → today.
74
+ const gw = this.config.gateway;
75
+ const gwActive = gw && gw.url && gw.mode && gw.mode !== 'off';
76
+ const actorDoc = wire.actorDoc({
77
+ urls, handle: this.config.handle, name: this.config.name, publicKeyPem: this.publicKeyPem,
78
+ movedTo: this.config.movedTo || null, kind: this.config.kind,
79
+ approveJoins: !!this.config.approveJoins,
80
+ summary: this.config.summary || null, icon: this.config.icon || null,
81
+ image: this.config.image || null, fields: this.config.fields || [],
82
+ webId: this.remote.webId || null,
83
+ aliases: this.config.aliases || [],
84
+ moderators,
85
+ pendingFollowers: priv ? urls.pendingFollowers : null,
86
+ pendingFollowing: priv ? urls.pendingFollowing : null,
87
+ blocked: priv ? urls.blocked : null,
88
+ inbox: gwActive ? gw.url : null,
89
+ });
90
+ const surface = crypto.createHash('sha256').update(JSON.stringify({
91
+ actor: actorDoc, handle: this.config.handle, host,
92
+ quiesced: !!this.config.quiescedAt, version: AGENT_VERSION,
93
+ moderators: this.config.moderators || [],
94
+ })).digest('hex').slice(0, 32);
95
+ if (!force && this.store.read('published.json', {}).surfaceDigest === surface) {
96
+ this.log('profile unchanged — nothing republished');
97
+ return { unreachable: [], updated: 0, skipped: true };
98
+ }
99
+
100
+ await this.remote.putJson(urls.webfinger,
101
+ wire.jrd({ handle: this.config.handle, host, actor: urls.actor }), 'application/jrd+json');
102
+ await this.remote.setAcl(urls.webfinger, ['Read']);
103
+
104
+ const hostMetaUrl = urls.base + '.well-known/host-meta';
105
+ await this.remote.put(hostMetaUrl, wire.hostMeta(urls.base), 'application/xrd+xml');
106
+ await this.remote.setAcl(hostMetaUrl, ['Read']);
107
+
108
+ // NodeInfo: pointer at the protocol-required root, document in our tree.
109
+ const nodeinfoDocUrl = urls.home + 'ap/nodeinfo-2.0';
110
+ const localPosts = this.store.getStatuses().filter(s => s.kind === 'post').length;
111
+ await this.remote.putJson(urls.base + '.well-known/nodeinfo',
112
+ wire.nodeinfoPointer(nodeinfoDocUrl), 'application/json');
113
+ await this.remote.setAcl(urls.base + '.well-known/nodeinfo', ['Read']);
114
+ await this.remote.putJson(nodeinfoDocUrl,
115
+ wire.nodeinfoDoc({ version: AGENT_VERSION, localPosts }), 'application/json');
116
+ await this.remote.setAcl(nodeinfoDocUrl, ['Read']);
117
+
118
+ const actor = actorDoc; // built above, for the digest
119
+ await this.remote.putJson(urls.actor, actor);
120
+ await this.remote.setAcl(urls.actor, ['Read']);
121
+
122
+ // FEP-1b12: the moderator roster, public — it is what a recipient
123
+ // validates a group's announced moderation against.
124
+ if (moderators) {
125
+ await this.remote.putJson(urls.moderators,
126
+ wire.orderedCollection(urls.moderators, this.config.moderators));
127
+ await this.remote.setAcl(urls.moderators, ['Read']);
128
+ }
129
+ // The gateway policy doc: the PUBLIC data a keyless gateway reads to decide
130
+ // what concerns this identity. Written only while a gateway is advertised.
131
+ if (gwActive) await this.publishGatewayPolicy();
132
+
133
+ // The human half: a page a browser can open and follow from. The actor
134
+ // document is for servers; this is the address you hand to a person.
135
+ await this.remote.put(urls.profileHtml, wire.profilePageHtml({
136
+ name: this.config.name || this.config.handle,
137
+ address: wire.webfingerHost(urls.base) ? `@${this.config.handle}@${host}` : urls.actor,
138
+ summary: this.config.summary ? wire.contentHtml(this.config.summary) : null,
139
+ icon: this.config.icon || null,
140
+ kind: this.config.kind,
141
+ }), 'text/html');
142
+ await this.remote.setAcl(urls.profileHtml, ['Read']);
143
+
144
+ // WebID → actor: the profile card lists the actor as a foaf:account.
145
+ // Best-effort — a profile that cannot be read or edited does not stop the
146
+ // publish, it is logged and the rest of the surface still goes up.
147
+ try {
148
+ const wrote = await this.remote.linkAccountInProfile({
149
+ actorUrl: urls.actor,
150
+ accountName: `@${this.config.handle}@${host}`,
151
+ kind: this.config.kind,
152
+ });
153
+ if (wrote) this.log('WebID profile now lists the actor as a foaf:account');
154
+ } catch (e) {
155
+ this.log(`WebID profile not updated with the actor link: ${e.message}`);
156
+ }
157
+
158
+ // inbox: public may only Append; owner (the agent) reads + drains. A
159
+ // quiesced actor keeps its name resolving but takes no more mail, so a
160
+ // republish must not re-open the door.
161
+ await this.remote.putJson(urls.inbox + '.keep', { keep: true }, 'application/json');
162
+ if (this.config.quiescedAt) {
163
+ await this.remote.setAcl(urls.inbox, []);
164
+ this.log('inbox left closed — this actor is quiesced');
165
+ } else {
166
+ await this.remote.setAcl(urls.inbox, ['Append']);
167
+ }
168
+
169
+ // notes live under a public-Read container (acl:default covers new notes).
170
+ await this.remote.putJson(urls.notes + '.keep', { keep: true }, 'application/json');
171
+ await this.remote.setAcl(urls.notes, ['Read']);
172
+
173
+ await this.publishCollections({ ...ALL_COLLECTIONS, force });
174
+ const updated = await this.announceProfileChange(actor);
175
+ await this.local.writeSettings({ handle: this.config.handle, actorUrl: urls.actor });
176
+ await this.ensurePrivateAcls();
177
+ const unreachable = await this.verifyPublicSurface();
178
+ // Last, and merged: announceProfileChange writes this document too.
179
+ //
180
+ // Only when the surface came back readable. Recording it regardless meant a
181
+ // publish that half-landed still matched the digest, so the NEXT save — the
182
+ // one the operator makes because the first did not work — was skipped as a
183
+ // no-op and reported success. The digest is a record of what is up there,
184
+ // and an unreachable document is not up there.
185
+ if (!unreachable.length) {
186
+ this.store.write('published.json',
187
+ { ...this.store.read('published.json', {}), surfaceDigest: surface });
188
+ }
189
+ this.log(wire.webfingerHost(urls.base)
190
+ ? `profile published: @${this.config.handle}@${host} → ${urls.actor}`
191
+ : `profile published → ${urls.actor} — NOT discoverable as @${this.config.handle}@${host}: `
192
+ + 'this pod is a path on a shared host, and WebFinger is only answered at a host root');
193
+ return { unreachable, updated };
194
+ }
195
+
196
+ // Fires when the document differs from the one last published — including
197
+ // the first time, when there is nothing to differ from.
198
+ //
199
+ // Every call to publishProfile is already a DELIBERATE republish: setup, a
200
+ // rename, an edit through the client or /config, `describe`, a key rotation,
201
+ // or an actor document found missing from the pod. Starting the agent does not
202
+ // call it. So the digest is not there to survive restarts — it is there for
203
+ // the republish that changes nothing, which /config does whenever you save a
204
+ // field the actor document does not carry.
205
+ //
206
+ // A silent first publish was considered and is WRONG here: it would spend a
207
+ // real edit doing nothing but recording a digest, and that edit is exactly the
208
+ // one whose invisibility this fixes.
209
+ async announceProfileChange(actor) {
210
+ const digest = crypto.createHash('sha256').update(JSON.stringify(actor)).digest('hex').slice(0, 32);
211
+ const seen = this.store.read('published.json', {});
212
+ if (seen.actorDigest === digest) return 0;
213
+ this.store.write('published.json', { ...seen, actorDigest: digest, at: new Date().toISOString() });
214
+ const inboxes = [...new Set(this.store.getContacts().followers
215
+ .map(f => f.sharedInbox || f.inbox).filter(Boolean))];
216
+ if (!inboxes.length) return 0;
217
+ await this.deliverer.deliverToAll(inboxes,
218
+ wire.updateActorActivity({ urls: this.urls, actor, serial: Date.now() }));
219
+ this.log(`profile changed — Update delivered to ${inboxes.length} inbox(es)`);
220
+ return inboxes.length;
221
+ }
222
+
223
+ // The mirror of ensurePrivateAcls, and the check this project lacked: these
224
+ // documents MUST be readable by strangers or no server can see the actor.
225
+ // A publish that dies half-way, or an ACL write that is accepted without
226
+ // taking effect, is otherwise indistinguishable from success — the agent
227
+ // reports itself configured and federating while nobody can find it.
228
+ async verifyPublicSurface() {
229
+ const { urls } = this;
230
+ const targets = [
231
+ ['webfinger', urls.webfinger],
232
+ ['host-meta', urls.base + '.well-known/host-meta'],
233
+ ['actor', urls.actor],
234
+ ['notes', urls.notes],
235
+ ['followers', urls.followers],
236
+ ['following', urls.following],
237
+ ['outbox', urls.outbox],
238
+ ];
239
+ const unreachable = [];
240
+ for (const [name, url] of targets) {
241
+ if (!await this.publiclyReadable(url)) unreachable.push(name);
242
+ }
243
+ if (unreachable.length) {
244
+ this.log(`FEDERATION: ${unreachable.join(', ')} not readable without credentials — `
245
+ + 'other servers cannot resolve or fetch this actor');
246
+ }
247
+ return unreachable;
248
+ }
249
+
250
+ // An ACL write that silently failed — or was changed afterwards by anything
251
+ // else touching the pod — leaves the private trees, signing keys included,
252
+ // world-readable. bootstrap writes them once at setup and never returns, so
253
+ // this runs on every connect: probe UNauthenticated, rewrite whatever
254
+ // answers, and say so loudly if the rewrite does not take.
255
+ async ensurePrivateAcls() {
256
+ // Only trees that are actually ON the pod. With the default local
257
+ // privateRoot the fediverse tree lives on disk, so probing it was a request
258
+ // that could only ever 404, on every start.
259
+ const onPod = [this.urls.home, this.urls.state,
260
+ ...(this.privateOnPod === false ? [] : [this.urls.fediverse])];
261
+ for (const url of onPod) {
262
+ if (!await this.publiclyReadable(url)) continue;
263
+ this.log(`${url} was readable without credentials — rewriting its ACL`);
264
+ try {
265
+ await this.remote.setAcl(url, []);
266
+ } catch (e) {
267
+ this.log(`SECURITY: ${url} is public and its ACL could not be rewritten: ${e.message}`);
268
+ continue;
269
+ }
270
+ if (await this.publiclyReadable(url)) {
271
+ this.log(`SECURITY: ${url} is STILL readable without credentials — check the pod's ACLs`);
272
+ }
273
+ }
274
+ }
275
+
276
+ // Deliberately credential-free: this asks what a stranger would see.
277
+ // accept: */* matters — asking for turtle makes the server answer 501 on the
278
+ // JSON documents (webfinger, actor), which reads as "unreachable" when the
279
+ // world can in fact see them perfectly well.
280
+ async publiclyReadable(url) {
281
+ try {
282
+ const res = await this.probeFetch(url, {
283
+ headers: { accept: '*/*', 'user-agent': USER_AGENT },
284
+ signal: AbortSignal.timeout(HTTP_TIMEOUT_MS),
285
+ });
286
+ return res.status < 400;
287
+ } catch { return false; } // unreachable is not a finding here
288
+ }
289
+
290
+ // Retire this identity for good: tell everyone who follows us to drop the
291
+ // account, then leave a Tombstone where the actor was. The inbox stays
292
+ // publicly Append-able on purpose — closing it would make deliveries 401,
293
+ // which Mastodon treats as failure and retries, the opposite of the point.
294
+ // A Delete stops well-behaved servers; anything that keeps delivering gets a
295
+ // cheap 201 into a container we no longer read.
296
+ async retireActor() {
297
+ const { urls } = this;
298
+ const contacts = this.store.getContacts();
299
+ const inboxes = [...new Set(contacts.followers.map(f => f.sharedInbox || f.inbox).filter(Boolean))];
300
+ const deletedAt = new Date().toISOString();
301
+ await this.deliverer.deliverToAll(inboxes, wire.deleteActorActivity(urls, Date.parse(deletedAt)));
302
+ await this.remote.putJson(urls.actor, wire.tombstoneDoc(urls, deletedAt, this.config.kind));
303
+ await this.remote.setAcl(urls.actor, ['Read']); // a Tombstone must stay fetchable
304
+ this.store.setConfig({ ...this.store.getConfig(), retiredAt: deletedAt });
305
+ await this.store.flush();
306
+ this.log(`retired: Delete sent to ${inboxes.length} inbox(es), actor replaced with a Tombstone`);
307
+ return { inboxes: inboxes.length, deletedAt };
308
+ }
309
+
310
+ // Stop accepting mail without giving up the name: deliveries get an immediate
311
+ // 401 rather than a 201 into storage nobody will ever drain. WebFinger,
312
+ // host-meta and the actor stay published, so the handle still resolves.
313
+ async closeInbox() {
314
+ await this.remote.setAcl(this.urls.inbox, []);
315
+ const at = new Date().toISOString();
316
+ this.store.setConfig({ ...this.store.getConfig(), quiescedAt: at });
317
+ await this.store.flush();
318
+ this.log(`inbox closed — @${this.config.handle} still resolves but accepts nothing`);
319
+ return at;
320
+ }
321
+
322
+ // Undo closeInbox: mail flows again and the actor is no longer quiesced.
323
+ async openInbox() {
324
+ await this.remote.setAcl(this.urls.inbox, ['Append']);
325
+ const { quiescedAt, ...rest } = this.store.getConfig() || {};
326
+ this.store.setConfig(rest);
327
+ this.config.quiescedAt = undefined;
328
+ await this.store.flush();
329
+ this.log(`inbox re-opened — @${this.config.handle} is taking mail again`);
330
+ }
331
+
332
+ // Lock the inbox to a gateway: the public loses Append, so nothing reaches
333
+ // the pod except through the gateway's verify-at-the-door. Reversible with
334
+ // openInbox (public-Append) — the one-call rollback.
335
+ async lockInboxToGateway(gatewayWebId) {
336
+ await this.remote.setAcl(this.urls.inbox, [], { appendAgents: [gatewayWebId] });
337
+ this.log(`inbox locked to gateway ${gatewayWebId} — public delivery is refused`);
338
+ }
339
+
340
+ // Tell the fediverse the account lives somewhere else now. Well-behaved
341
+ // servers migrate their followers to the target and stop delivering here.
342
+ async publishMove(target) {
343
+ const { urls } = this;
344
+ const contacts = this.store.getContacts();
345
+ const inboxes = [...new Set(contacts.followers.map(f => f.sharedInbox || f.inbox).filter(Boolean))];
346
+ const at = new Date().toISOString();
347
+ await this.deliverer.deliverToAll(inboxes, wire.moveActivity(urls, target, Date.parse(at)));
348
+ this.config.movedTo = target; // so the republish below carries it
349
+ this.store.setConfig({ ...this.store.getConfig(), movedTo: target, movedAt: at });
350
+ await this.remote.putJson(urls.actor, wire.actorDoc({
351
+ urls, handle: this.config.handle, name: this.config.name,
352
+ publicKeyPem: this.publicKeyPem, movedTo: target, kind: this.config.kind,
353
+ approveJoins: !!this.config.approveJoins,
354
+ summary: this.config.summary || null, icon: this.config.icon || null,
355
+ image: this.config.image || null, fields: this.config.fields || [],
356
+ webId: this.remote.webId || null,
357
+ aliases: this.config.aliases || [],
358
+ }));
359
+ await this.remote.setAcl(urls.actor, ['Read']);
360
+ await this.store.flush();
361
+ this.log(`moved to ${target}: Move sent to ${inboxes.length} inbox(es), actor now advertises movedTo`);
362
+ return { inboxes: inboxes.length, target, movedAt: at };
363
+ }
364
+
365
+ // Anyone the pod says follows us that we have no record of, and never
366
+ // deliberately removed. In the steady state there is nobody: the pod's list is
367
+ // written from this one. They appear when the local half is BEHIND the pod —
368
+ // a restored backup, a home copied off a dead machine — and republishing
369
+ // blindly would delete them from the wire and, worse, stop delivering to them.
370
+ //
371
+ // Removals are what makes this safe to do unconditionally: `dropFollower`
372
+ // records every unfollow, ejection and account deletion, so a returning name
373
+ // is either genuinely still a follower or genuinely a mistake we made.
374
+ async reconcileFollowers(contacts) {
375
+ let published = [];
376
+ try {
377
+ published = (await this.readPublishedFollowers()) || []; // walks pages
378
+ } catch { return 0; } // no list to reconcile against
379
+
380
+ const known = new Set(contacts.followers.map(f => f.actor));
381
+ const removed = new Set((contacts.removedFollowers || []).map(r => r.actor));
382
+ const missing = published.filter(a => typeof a === 'string' && !known.has(a) && !removed.has(a));
383
+ if (!missing.length) return 0;
384
+
385
+ // An inbox is what delivery needs, and only the actor document has it — so
386
+ // recovering a follower costs one fetch each. Capped: a list this wrong is
387
+ // a restore, and the rest catch up on the next publish.
388
+ let recovered = 0;
389
+ for (const actor of missing.slice(0, 200)) {
390
+ try {
391
+ const res = await this.deliverer.signedFetch(actor, { headers: { accept: ACCEPT_AP } });
392
+ if (!res.ok) continue;
393
+ const doc = await res.json();
394
+ if (!doc?.inbox) continue;
395
+ contacts.followers.push({
396
+ actor, inbox: doc.inbox, sharedInbox: doc.endpoints?.sharedInbox || null, recovered: true,
397
+ // Said explicitly, because onUndo reads it: the pod publishes WHO
398
+ // follows, never the id of the Follow that did it, so a recovered
399
+ // record has nothing an Undo can be matched against and must not be
400
+ // evictable by one naming anything at all.
401
+ followId: null,
402
+ });
403
+ recovered++;
404
+ } catch { /* unreachable now; it will be there next time */ }
405
+ }
406
+ if (recovered) {
407
+ this.store.setContacts(contacts);
408
+ this.log(`reconciled ${recovered} follower(s) the pod knew about and this machine did not `
409
+ + '— a restored or copied state was behind');
410
+ }
411
+ return recovered;
412
+ }
413
+
414
+ // The same argument as reconcileFollowers, for the other published list. It
415
+ // matters more: the outbox is the INDEX a statuses rebuild reads, so a
416
+ // republish from a restored-and-behind machine would destroy the record of
417
+ // everything this actor ever posted — and destroy it before anyone noticed
418
+ // there was anything to recover.
419
+ //
420
+ // Safe for the same reason: `unrecordOutbox` leaves a tombstone, so an entry
421
+ // the pod still carries is one this machine has not heard of, never one it
422
+ // deliberately took back.
423
+ async reconcileOutbox(outbox) {
424
+ let published = [];
425
+ try {
426
+ published = await this.readPublishedOutbox() || [];
427
+ } catch { return 0; }
428
+ if (!Array.isArray(published) || !published.length) return 0;
429
+
430
+ const idOf = (i) => (typeof i === 'string' ? i : i?.id || null);
431
+ const known = new Set(outbox.map(idOf).filter(Boolean));
432
+ const removed = new Set((this.store.read('outbox-removed.json', [])).map(r => r.id));
433
+ const missing = published.filter((i) => {
434
+ const id = idOf(i);
435
+ return id && !known.has(id) && !removed.has(id);
436
+ });
437
+ if (!missing.length) return 0;
438
+ // Newest first, like recordOutbox leaves it; the pod's copy is already in
439
+ // that order, so appending the tail is enough to keep both sorted.
440
+ outbox.push(...missing);
441
+ this.store.write('outbox.json', outbox);
442
+ this.log(`reconciled ${missing.length} outbox entr(ies) the pod carried and this machine did not`);
443
+ return missing.length;
444
+ }
445
+
446
+ // Recover this actor's own posts from the pod's public face. The private half
447
+ // lives on this machine now, so a restored backup or a replaced machine loses
448
+ // statuses.json while the pod still serves every note. Followers already come
449
+ // back; this is the other half of the same gap.
450
+ //
451
+ // The INDEX is ap/outbox, not the ap/notes/ listing, and that difference is
452
+ // the safety argument. Deleting a post rewrites the outbox in one PUT, so an
453
+ // entry still there is a post that still stands. The note DOCUMENT can outlive
454
+ // its own deletion — deleteNote's `remote.delete` is a request that can fail —
455
+ // so walking the container can bring back something its author took down.
456
+ // `fromNotes` is for when you would rather have that than lose the post; it is
457
+ // not the default, and it says so where it is offered.
458
+ //
459
+ // MERGE ONLY. A status this machine already holds is left exactly as it is:
460
+ // it carries local facts — favourited, reblogged, the activities an Undo has
461
+ // to name — that the pod knows nothing about. That is also what makes the
462
+ // failure modes harmless: a listing that fails returns nothing, and nothing
463
+ // is what an empty listing recovers.
464
+ async rebuildStatuses({ fromNotes = false } = {}) {
465
+ const { urls } = this;
466
+ const ids = new Set();
467
+ const boosts = [];
468
+ let indexed = false;
469
+
470
+ const published = await this.readPublishedOutbox().catch(() => null);
471
+ if (published) {
472
+ indexed = true;
473
+ for (const item of published) {
474
+ if (typeof item === 'string') { if (item.startsWith(urls.notes)) ids.add(item); }
475
+ else if (item?.type === 'Announce') boosts.push(item);
476
+ }
477
+ }
478
+ if (fromNotes) {
479
+ // Three documents are published per post — the note, `-create` and
480
+ // `-replies` — plus a `.keep`. Which is which is settled by reading the
481
+ // document below, not by its name: a slug is a date and eight hex
482
+ // characters and says nothing about what it holds.
483
+ for (const child of await this.remote.listContainer(urls.notes).catch(() => [])) {
484
+ if (/(-create|-replies|\/\.keep)$/.test(child.url)) continue;
485
+ ids.add(child.url);
486
+ }
487
+ indexed = true;
488
+ }
489
+ if (!indexed) return { indexed: 0, recovered: 0, reblogs: 0, landed: false, why: 'the pod would not answer for its outbox' };
490
+
491
+ const statuses = this.store.getStatuses();
492
+ const have = new Set(statuses.map(s => s.noteId));
493
+ const removed = new Set(this.store.read('outbox-removed.json', []).map(r => r.id));
494
+ const recovered = [];
495
+ // Capped per run: a long-lived actor's rebuild is otherwise one pod request
496
+ // per post it has ever made, in one burst. What is left is picked up by
497
+ // running it again — the merge is idempotent, so that is safe to repeat.
498
+ let budget = REBUILD_MAX_PER_RUN;
499
+ for (const id of ids) {
500
+ if (budget <= 0) { this.log(`rebuild: stopping at ${REBUILD_MAX_PER_RUN} this run — run it again for the rest`); break; }
501
+ if (have.has(id) || removed.has(id)) continue;
502
+ budget--;
503
+ const note = await this.remote.getJson(id).catch(() => null);
504
+ if (note?.type !== 'Note' || note.id !== id || note.attributedTo !== urls.actor) continue;
505
+ const attachments = wire.attachmentsOf(note);
506
+ const mentions = (Array.isArray(note.tag) ? note.tag : [])
507
+ .filter(t => t?.type === 'Mention' && t.href)
508
+ .map(t => ({ href: t.href, name: t.name }));
509
+ recovered.push({
510
+ noteId: note.id, actor: urls.actor, content: note.content || '',
511
+ published: note.published || null,
512
+ ...(note.inReplyTo ? { inReplyTo: note.inReplyTo } : {}),
513
+ kind: 'post', slug: note.id.slice(urls.notes.length),
514
+ ...(attachments.length ? { attachments } : {}),
515
+ ...(mentions.length ? { mentions } : {}),
516
+ recovered: true,
517
+ });
518
+ }
519
+
520
+ // A boost is only recoverable for a post we can name — the Announce carries
521
+ // the activity a later Undo needs, but not the boosted post's text, which
522
+ // belongs to whoever wrote it.
523
+ const merged = [...statuses, ...recovered];
524
+ let reblogs = 0;
525
+ for (const act of boosts) {
526
+ const object = typeof act.object === 'string' ? act.object : act.object?.id;
527
+ const s = object && merged.find(x => x.noteId === object);
528
+ if (!s || s.reblogged) continue;
529
+ s.reblogged = true;
530
+ s.announceActivity = act;
531
+ reblogs++;
532
+ }
533
+ if (!recovered.length && !reblogs) return { indexed: ids.size, recovered: 0, reblogs: 0, landed: true };
534
+
535
+ // Written whole and sorted, the way backfillStatuses does it: addStatus
536
+ // unshifts and fires the streaming event, so a loop of it would arrive
537
+ // backwards and push every recovered post at connected clients as new.
538
+ merged.sort((a, b) => String(b.published || '').localeCompare(String(a.published || '')));
539
+ const kept = merged.slice(0, 1000);
540
+ this.store.write('statuses.json', kept);
541
+ const landed = await this.store.commit();
542
+
543
+ // The RDF mirror is the other thing a lost machine took with it, and it is
544
+ // what backfillStatuses reads on a fresh state. Best effort: a post in
545
+ // statuses.json is already the recovery that matters.
546
+ let rdf = 0;
547
+ for (const s of recovered) {
548
+ try {
549
+ await this.local.writeNote('posts', s.slug, {
550
+ noteId: s.noteId, actor: s.actor, published: s.published,
551
+ content: s.content, inReplyTo: s.inReplyTo, attachments: s.attachments,
552
+ });
553
+ rdf++;
554
+ } catch (e) { this.log(`rebuild: RDF for ${s.slug} not written (${e.message})`); }
555
+ }
556
+ this.log(`rebuilt ${recovered.length} post(s) and ${reblogs} boost(s) from the pod`
557
+ + `${landed ? '' : ' — THE STATE WRITE DID NOT LAND'}`);
558
+ return {
559
+ indexed: ids.size, recovered: recovered.length, reblogs, rdf, landed,
560
+ dropped: Math.max(0, merged.length - kept.length),
561
+ };
562
+ }
563
+
564
+ // Publish the collections a change actually TOUCHED.
565
+ //
566
+ // Publishing all three on every follower event cost nine pod requests where
567
+ // two do: two reconcile reads, three collection PUTs, and three ACL PUTs
568
+ // whose bodies are a pure function of the WebID and the target URL and so
569
+ // are byte-identical to the ones written at setup. A new follower does not
570
+ // change what this actor follows, and it does not change the outbox.
571
+ //
572
+ // `acls` is true only on the default path, which is publishProfile: that is
573
+ // where the public surface is built, and where verifyPublicSurface already
574
+ // checks the world can read it.
575
+ //
576
+ // Reconciliation stays welded to the collection it guards. It is what stops a
577
+ // restored-and-behind machine publishing a short list over the pod's longer
578
+ // one — erasing followers it would then stop delivering to, and erasing the
579
+ // outbox that `rebuild` reads as its index — so a narrowed publish still runs
580
+ // the one belonging to whatever it is about to overwrite.
581
+ // Publish only the pages that actually changed.
582
+ //
583
+ // `known` is what we last wrote, so a post rewrites the newest page and the
584
+ // head and nothing else — where the flat collection rewrote the actor's whole
585
+ // history on every post. A page gets its ACL when it is first created; the
586
+ // container above it is owner-only, so it cannot be inherited.
587
+ // `force` is for the caller that publishes BECAUSE the pod does not have what
588
+ // the digests say it has. Without it a repair republish rewrote the head and
589
+ // skipped every page — the digests still matched the local record — so the
590
+ // head advertised a `first:` that 404s, readPublishedOutbox came back empty,
591
+ // rebuildStatuses recovered nothing, and the whole thing logged success. That
592
+ // happens to an actor with one post as surely as one with five thousand.
593
+ async publishOutbox(outbox, { acls = false, force = false } = {}) {
594
+ const { urls } = this;
595
+ const seen = this.store.read('published.json', {});
596
+ const { pages, index } = wire.outboxPaging(outbox, seen.outboxIndex || []);
597
+ const before = force ? {} : (seen.outboxPages || {});
598
+ const after = {};
599
+ let wrote = 0;
600
+
601
+ for (let i = 0; i < pages.length; i++) {
602
+ const n = i + 1; // 1 = oldest
603
+ const doc = wire.outboxPage(urls.outbox, n, pages[i]);
604
+ const digest = crypto.createHash('sha256').update(JSON.stringify(doc)).digest('hex').slice(0, 16);
605
+ after[n] = digest;
606
+ if (before[n] === digest) continue; // sealed and unchanged
607
+ await this.remote.putJson(wire.outboxPageId(urls.outbox, n), doc);
608
+ if (!before[n] || acls) await this.remote.setAcl(wire.outboxPageId(urls.outbox, n), ['Read']);
609
+ wrote++;
610
+ }
611
+ // Pages above the new count are orphans: the outbox shrank past them, and
612
+ // left where they were they keep serving activities that have been taken
613
+ // back. The head no longer points at them, so nothing walks to them — but
614
+ // the URL is guessable and public.
615
+ const stale = Object.keys(seen.outboxPages || {}).map(Number)
616
+ .filter(n => Number.isFinite(n) && n > pages.length);
617
+ for (const n of stale) {
618
+ await this.remote.delete(wire.outboxPageId(urls.outbox, n)).catch(() => {});
619
+ }
620
+ // The head carries totalItems, so it moves whenever the outbox does. Four
621
+ // lines, and constant however much you have posted.
622
+ await this.remote.putJson(urls.outbox,
623
+ wire.outboxHead(urls.outbox, outbox.length, pages.length));
624
+ if (acls) await this.remote.setAcl(urls.outbox, ['Read']);
625
+ this.store.write('published.json',
626
+ { ...this.store.read('published.json', {}), outboxPages: after, outboxIndex: index });
627
+ return wrote;
628
+ }
629
+
630
+ // Every activity in the published outbox, walking the pages. Also understands
631
+ // the flat collection this used to write, so an actor published before paging
632
+ // is still readable — which matters because rebuild reads this to recover
633
+ // posts a lost machine no longer has.
634
+ async readPublishedOutbox() {
635
+ const head = await this.remote.getJson(this.urls.outbox).catch(() => null);
636
+ if (!head) return null;
637
+ if (Array.isArray(head.orderedItems) && head.orderedItems.length) return head.orderedItems;
638
+ const items = [];
639
+ let next = head.first;
640
+ const seen = new Set();
641
+ while (next && !seen.has(next) && items.length < 10_000) {
642
+ seen.add(next);
643
+ const page = await this.remote.getJson(next).catch(() => null);
644
+ if (!page) break;
645
+ items.push(...(page.orderedItems || []));
646
+ next = page.next;
647
+ }
648
+ return items;
649
+ }
650
+
651
+ // The followers collection, paged like the outbox: a head that carries only
652
+ // the count and the page bounds, and page documents holding the actor IRIs —
653
+ // so a remote server reads a small head and walks pages instead of pulling one
654
+ // document that grows without limit. Regenerated from the in-memory follow
655
+ // graph: a follow extends the newest page and an unfollow leaves its page one
656
+ // short (wire.pageItems), so only the pages that changed are rewritten.
657
+ async publishFollowers(actors, { acls = false, force = false } = {}) {
658
+ const { urls } = this;
659
+ const seen = this.store.read('published.json', {});
660
+ const { pages, index } = wire.followersPaging(actors, seen.followersIndex || []);
661
+ const before = force ? {} : (seen.followersPages || {});
662
+ const after = {};
663
+
664
+ for (let i = 0; i < pages.length; i++) {
665
+ const n = i + 1;
666
+ const doc = wire.followersPage(urls.followers, n, pages[i], pages.length);
667
+ const digest = crypto.createHash('sha256').update(JSON.stringify(doc)).digest('hex').slice(0, 16);
668
+ after[n] = digest;
669
+ if (before[n] === digest) continue; // unchanged page
670
+ await this.remote.putJson(wire.followersPageId(urls.followers, n), doc);
671
+ if (!before[n] || acls) await this.remote.setAcl(wire.followersPageId(urls.followers, n), ['Read']);
672
+ }
673
+ // Pages the collection shrank past would keep serving names that no longer
674
+ // follow; the head stops pointing at them but the URL is guessable.
675
+ const stale = Object.keys(seen.followersPages || {}).map(Number)
676
+ .filter(n => Number.isFinite(n) && n > pages.length);
677
+ for (const n of stale) {
678
+ await this.remote.delete(wire.followersPageId(urls.followers, n)).catch(() => {});
679
+ }
680
+ await this.remote.putJson(urls.followers,
681
+ wire.followersHead(urls.followers, actors.length, pages.length));
682
+ if (acls) await this.remote.setAcl(urls.followers, ['Read']);
683
+ this.store.write('published.json',
684
+ { ...this.store.read('published.json', {}), followersPages: after, followersIndex: index });
685
+ }
686
+
687
+ // Every actor in the published followers collection, walking pages. Also reads
688
+ // the flat collection this used to write, so an actor published before paging
689
+ // still reconciles.
690
+ async readPublishedFollowers() {
691
+ const head = await this.remote.getJson(this.urls.followers).catch(() => null);
692
+ if (!head) return null;
693
+ if (Array.isArray(head.orderedItems) && head.orderedItems.length) return head.orderedItems;
694
+ if (Array.isArray(head.items) && head.items.length) return head.items;
695
+ const items = [];
696
+ let next = head.first;
697
+ const seen = new Set();
698
+ while (next && !seen.has(next) && items.length < 100_000) {
699
+ seen.add(next);
700
+ const page = await this.remote.getJson(next).catch(() => null);
701
+ if (!page) break;
702
+ items.push(...(page.orderedItems || []));
703
+ next = page.next;
704
+ }
705
+ return items;
706
+ }
707
+
708
+ async publishCollections(which = ALL_COLLECTIONS) {
709
+ const { urls } = this;
710
+ const contacts = this.store.getContacts();
711
+ if (which.followers) {
712
+ // Reconcile walks the published pages, so — like the outbox — pay for it
713
+ // only when the local record of what is up there is missing (a restore or
714
+ // a copied machine), not on every ordinary save.
715
+ const knownF = this.store.read('published.json', {}).followersIndex;
716
+ if (which.force || !Array.isArray(knownF)) await this.reconcileFollowers(contacts);
717
+ // Bluesky-only members are not AP actors; the published collection
718
+ // lists only what a remote server could dereference.
719
+ const actors = contacts.followers.filter(f => !f.bsky).map(f => f.actor);
720
+ await this.publishFollowers(actors, { acls: which.acls, force: which.force });
721
+ }
722
+ if (which.following) {
723
+ await this.remote.putJson(urls.following,
724
+ wire.orderedCollection(urls.following, contacts.following.filter(f => f.accepted).map(f => f.actor)));
725
+ if (which.acls) await this.remote.setAcl(urls.following, ['Read']);
726
+ }
727
+ if (which.pending) await this.publishPending();
728
+ if (which.blocked) await this.publishBlocked();
729
+ if (which.outbox) {
730
+ const outbox = this.store.read('outbox.json', []);
731
+ // Reconcile reads every published page, which is the expensive part of a
732
+ // profile save. It is worth paying only when we are about to write pages
733
+ // we did not write: on a repair, or on a machine whose state no longer
734
+ // records what it put up there — which is exactly the restored backup the
735
+ // reconcile exists for. With an intact record our copy IS what the pod
736
+ // has, and re-reading it to confirm that is a page walk for nothing.
737
+ const known = this.store.read('published.json', {}).outboxIndex;
738
+ if (which.force || !Array.isArray(known)) await this.reconcileOutbox(outbox);
739
+ await this.publishOutbox(outbox, { acls: which.acls, force: which.force });
740
+ }
741
+ if (which.followers || which.following) await this.local.writeContacts(contacts);
742
+ }
743
+
744
+ // FEP-4ccd: the follows in limbo, as owner-only collections of the Follow
745
+ // activities themselves. Inside the private container so its ACL is
746
+ // inherited — and published only where that ACL provably holds, the same
747
+ // bar private posts clear. A Bluesky-only request has no Follow activity a
748
+ // remote server could ever act on, so it is not listed.
749
+ async publishPending() {
750
+ if (await this.privateReady() !== true) return;
751
+ const { urls } = this;
752
+ const contacts = this.store.getContacts();
753
+ await this.remote.putJson(urls.pendingFollowers, wire.orderedCollection(
754
+ urls.pendingFollowers,
755
+ this.store.getRequests().filter(r => r.activity && !r.bsky).map(r => r.activity)));
756
+ await this.remote.putJson(urls.pendingFollowing, wire.orderedCollection(
757
+ urls.pendingFollowing,
758
+ contacts.following.filter(f => !f.accepted && f.followActivity)
759
+ .map(f => f.followActivity).reverse()));
760
+ }
761
+
762
+ // The gateway policy: a small PUBLIC document a keyless inbox gateway reads
763
+ // to decide, at the edge, what to forward and what to drop. It carries only
764
+ // public facts — the actor/followers URLs, the REAL pod inbox to forward to,
765
+ // the accepted-following list (already public) and a mirror of the blocklist.
766
+ // Publishing the blocklist here makes it public; that is a deliberate part of
767
+ // running a gateway, surfaced to the operator in the admin UI.
768
+ async publishGatewayPolicy() {
769
+ const { urls } = this;
770
+ const contacts = this.store.getContacts();
771
+ const bl = this.store.getBlocklist();
772
+ await this.remote.putJson(urls.home + 'ap/gateway-policy.json', {
773
+ v: 1,
774
+ actorUrl: urls.actor,
775
+ followersUrl: urls.followers,
776
+ followingUrl: urls.following,
777
+ inboxUrl: urls.inbox, // the pod inbox the gateway forwards to
778
+ notesPrefix: urls.notes,
779
+ kind: this.config.kind || 'person',
780
+ following: contacts.following.filter(f => f.accepted && !f.bsky).map(f => f.actor),
781
+ blocklist: { domains: bl.domains || [], actors: bl.actors || [] },
782
+ }, 'application/json');
783
+ await this.remote.setAcl(urls.home + 'ap/gateway-policy.json', ['Read']);
784
+ }
785
+
786
+ // FEP-c648: the blocked actors, as an owner-only collection. Actors only —
787
+ // domain blocks are ours, and the FEP does not carry them.
788
+ async publishBlocked() {
789
+ if (await this.privateReady() !== true) return;
790
+ const { urls } = this;
791
+ const actors = [...(this.store.getBlocklist().actors || [])].reverse();
792
+ await this.remote.putJson(urls.blocked, wire.orderedCollection(urls.blocked, actors));
793
+ }
794
+
795
+ // The outbox is the public record of everything this actor has said, boosts
796
+ // included. A Create goes in as its note id, which dereferences; an Announce
797
+ // has only a fragment id, so the activity itself goes in the collection —
798
+ // legal AS2, and what Mastodon serves.
799
+ async recordOutbox(item) {
800
+ const outbox = this.store.read('outbox.json', []);
801
+ outbox.unshift(item);
802
+ this.store.write('outbox.json', outbox);
803
+ await this.publishOutbox(outbox);
804
+ }
805
+
806
+ // Taking something out of the outbox is a DECISION — a post deleted, a boost
807
+ // undone. It leaves a mark for the same reason dropFollower does: the pod's
808
+ // copy is rewritten right after, but a rewrite that fails would otherwise let
809
+ // the next reconcile put the entry back. Bounded, like the follower one.
810
+ async unrecordOutbox(matches) {
811
+ const before = this.store.read('outbox.json', []);
812
+ const outbox = before.filter(i => !matches(i));
813
+ const gone = before.filter(i => matches(i))
814
+ .map(i => (typeof i === 'string' ? i : i?.id)).filter(Boolean);
815
+ if (gone.length) {
816
+ const marks = this.store.read('outbox-removed.json', []).filter(r => !gone.includes(r.id));
817
+ const at = new Date().toISOString();
818
+ this.store.write('outbox-removed.json',
819
+ [...marks, ...gone.map(id => ({ id, at }))].slice(-500));
820
+ }
821
+ this.store.write('outbox.json', outbox);
822
+ await this.publishOutbox(outbox);
823
+ }
824
+
825
+ // Media container on the remote pod — public-Read like notes, created lazily
826
+ // at first upload (idempotent; the flag only saves round-trips).
827
+ async ensureMediaContainer() {
828
+ if (this._mediaReady) return;
829
+ await this.remote.putJson(this.urls.media + '.keep', { keep: true }, 'application/json');
830
+ await this.remote.setAcl(this.urls.media, ['Read']);
831
+ this._mediaReady = true;
832
+ }
833
+
834
+ // The owner-only container that followers-only and direct posts live in.
835
+ // Its ACL is set once; every note under it inherits.
836
+ async ensurePrivateContainer() {
837
+ if (this._privateContainer) return;
838
+ await this.remote.putJson(this.urls.privateNotes + '.keep', { keep: true }, 'application/json');
839
+ await this.remote.setAcl(this.urls.privateNotes, []);
840
+ this._privateContainer = true;
841
+ }
842
+
843
+ // Whether the pod actually enforces that ACL: a bare, unauthenticated read
844
+ // of the private container's canary must be refused. Returns true, or the
845
+ // reason private posts stay off. A definite answer is cached for the run; a
846
+ // network failure is not, so a pod that was briefly unreachable is asked
847
+ // again rather than refused forever.
848
+ async privateReady() {
849
+ if (this._privateVerdict !== undefined) return this._privateVerdict;
850
+ try {
851
+ await this.ensurePrivateContainer();
852
+ // The probe is credential-free and bypasses RemotePod's url map, so a
853
+ // fronted identity must map the advertised private container back to the
854
+ // pod itself — the ACL we are testing is the pod's.
855
+ const pn = this.urls.toPod ? this.urls.toPod(this.urls.privateNotes) : this.urls.privateNotes;
856
+ const r = await (this.probeFetch || fetch)(pn + '.keep', { redirect: 'manual' });
857
+ this._privateVerdict = (r.status === 401 || r.status === 403) ? true
858
+ : `this pod serves private documents to strangers (HTTP ${r.status}) — private posts stay off it`;
859
+ return this._privateVerdict;
860
+ } catch (e) {
861
+ return `could not verify that the pod protects private posts (${e.message})`;
862
+ }
863
+ }
864
+
865
+ // Compose → wire note on remote pod + RDF truth locally + deliver Create.
866
+ async publishNote(content, { inReplyTo, attachments, visibility = 'public', spoilerText = null } = {}) {
867
+ const { urls } = this;
868
+ const priv = visibility === 'private' || visibility === 'direct';
869
+ if (priv) {
870
+ const ready = await this.privateReady();
871
+ if (ready !== true) throw new Error(ready);
872
+ }
873
+ const published = new Date().toISOString();
874
+ const slug = published.slice(0, 10) + '-' + crypto.randomBytes(4).toString('hex');
875
+ // A mention nobody can resolve stays plain text rather than failing the post.
876
+ // The reply's own text decides who is mentioned: trim a handle out and that
877
+ // person is not notified, which is what every fediverse client leads people
878
+ // to expect. A Group named in the parent is the one thing carried forward
879
+ // regardless — drop it and the group stops carrying the thread.
880
+ const inText = new Set(wire.mentionsIn(content));
881
+ const carried = inReplyTo
882
+ ? (this.store.getStatuses().find(s => s.noteId === inReplyTo)?.mentions || [])
883
+ .map(m => String(m.name || '').replace(/^@/, '')).filter(Boolean)
884
+ : [];
885
+ const mentions = [];
886
+ for (const handle of [...new Set([...inText, ...carried])]) {
887
+ if (!this.resolveMention) break;
888
+ const doc = await this.resolveMention(handle).catch(() => null);
889
+ if (!doc?.id) { this.log(`mention @${handle} did not resolve — left as text`); continue; }
890
+ if (!inText.has(handle) && doc.type !== 'Group') continue; // author trimmed them out
891
+ mentions.push({ handle, actor: doc.id, page: doc.url || null, inbox: doc.endpoints?.sharedInbox || doc.inbox });
892
+ }
893
+ const note = wire.noteDoc({ urls, slug, content, published, inReplyTo, attachments, mentions,
894
+ visibility, summary: spoilerText, container: priv ? urls.privateNotes : urls.notes });
895
+
896
+ await this.remote.putJson(note.id, note);
897
+ // Empty, but present: a dangling `replies` that 404s is worse than none.
898
+ await this.remote.putJson(wire.repliesId(note.id), wire.collection(wire.repliesId(note.id), []));
899
+ // The outbox is the PUBLIC index; a private or direct post is not in it.
900
+ if (!priv) await this.recordOutbox(note.id);
901
+
902
+ await this.local.writeNote('posts', slug, {
903
+ noteId: note.id, actor: urls.actor, published, content: note.content, inReplyTo, attachments,
904
+ });
905
+ this.store.addStatus({
906
+ noteId: note.id, actor: urls.actor, content: note.content, published, inReplyTo,
907
+ kind: 'post', slug, text: content, visibility,
908
+ ...(spoilerText ? { spoiler: spoilerText } : {}),
909
+ ...(attachments?.length ? { attachments } : {}),
910
+ ...(note.tag?.length ? { mentions: note.tag.map(t => ({ href: t.href, name: t.name })) } : {}),
911
+ });
912
+
913
+ const create = wire.createActivity(note, urls);
914
+ // Published as its own document: a group that carries this post wraps the
915
+ // whole activity, and the receiving server resolves it by fetching this id.
916
+ // It inherits the notes container's public-Read acl:default.
917
+ await this.remote.putJson(create.id, create);
918
+ const contacts = this.store.getContacts();
919
+ // A direct post goes to the people it names and to nobody else.
920
+ const inboxes = [...new Set([
921
+ ...(visibility === 'direct' ? [] : contacts.followers.map(f => f.sharedInbox || f.inbox)),
922
+ ...mentions.map(m => m.inbox),
923
+ ].filter(Boolean))];
924
+ await this.deliverer.deliverToAll(inboxes, create);
925
+ this.log(`note published: ${note.id} → ${inboxes.length} inbox(es)`);
926
+
927
+ // Bluesky mirror: PUBLIC posts only — unlisted, followers-only and direct
928
+ // are never carried off the fediverse. A mirror failure never fails the
929
+ // post; it is logged and recorded on the status for the admin page.
930
+ if (visibility === 'public' && this.atproto?.connected()
931
+ && this.store.getConfig()?.atproto?.crossPost) {
932
+ try {
933
+ const mirror = await this.atproto.crossPost(
934
+ { text: content, published, attachments }, { noteUrl: note.id });
935
+ this.store.updateStatus(note.id, { atproto: mirror });
936
+ this.log(`cross-posted to bluesky: ${mirror.uri}${mirror.truncated ? ' (truncated, links back)' : ''}`);
937
+ } catch (e) {
938
+ this.store.updateStatus(note.id, { atproto: { error: e.message } });
939
+ this.log(`bluesky cross-post failed: ${e.message}`);
940
+ }
941
+ }
942
+ return note;
943
+ }
944
+
945
+ // The pinned posts, as the actor's featured collection — the one document a
946
+ // remote server reads when it shows this profile's pins.
947
+ async publishFeatured() {
948
+ const ids = this.store.getStatuses().filter(s => s.kind === 'post' && s.pinned).map(s => s.noteId);
949
+ await this.remote.putJson(this.urls.featured, wire.orderedCollection(this.urls.featured, ids));
950
+ await this.remote.setAcl(this.urls.featured, ['Read']);
951
+ return ids.length;
952
+ }
953
+
954
+ // An edit keeps the note's id, slug and published time; `updated` is the
955
+ // edit's own stamp. The pod documents are overwritten in place — the Create
956
+ // too, so a group's Announce resolves to the edited text — and an Update
957
+ // goes everywhere the Create went.
958
+ async updateNote(s, { content, spoilerText = null, attachments = null } = {}) {
959
+ const { urls } = this;
960
+ const updated = new Date().toISOString();
961
+ const inText = new Set(wire.mentionsIn(content));
962
+ const mentions = [];
963
+ for (const handle of inText) {
964
+ if (!this.resolveMention) break;
965
+ const doc = await this.resolveMention(handle).catch(() => null);
966
+ if (!doc?.id) { this.log(`mention @${handle} did not resolve — left as text`); continue; }
967
+ mentions.push({ handle, actor: doc.id, page: doc.url || null, inbox: doc.endpoints?.sharedInbox || doc.inbox });
968
+ }
969
+ const atts = attachments ?? s.attachments ?? [];
970
+ // The note stays in the container its visibility put it in; a recovered
971
+ // post may carry no slug, but the note id already contains it.
972
+ const container = String(s.noteId).startsWith(urls.privateNotes) ? urls.privateNotes : urls.notes;
973
+ const slug = s.slug || String(s.noteId).slice(container.length);
974
+ const note = wire.noteDoc({
975
+ urls, slug, content, published: s.published, inReplyTo: s.inReplyTo,
976
+ attachments: atts, mentions, visibility: s.visibility || 'public',
977
+ summary: spoilerText, updated, container,
978
+ });
979
+ await this.remote.putJson(note.id, note);
980
+ await this.remote.putJson(wire.createActivityId(note.id), wire.createActivity(note, urls));
981
+ if (s.slug) {
982
+ await this.local.writeNote('posts', s.slug, {
983
+ noteId: note.id, actor: urls.actor, published: s.published, content: note.content,
984
+ inReplyTo: s.inReplyTo, attachments: atts,
985
+ });
986
+ }
987
+ const patched = this.store.updateStatus(s.noteId, {
988
+ content: note.content, text: content, editedAt: updated,
989
+ spoiler: spoilerText || undefined,
990
+ attachments: atts.length ? atts : undefined,
991
+ mentions: note.tag?.length ? note.tag.map(t => ({ href: t.href, name: t.name })) : undefined,
992
+ });
993
+ const update = wire.updateActivity(note, urls);
994
+ const contacts = this.store.getContacts();
995
+ const inboxes = [...new Set([
996
+ ...(s.visibility === 'direct' ? [] : contacts.followers.map(f => f.sharedInbox || f.inbox)),
997
+ ...mentions.map(m => m.inbox),
998
+ ].filter(Boolean))];
999
+ await this.deliverer.deliverToAll(inboxes, update);
1000
+ this.log(`note edited: ${note.id} → ${inboxes.length} inbox(es)`);
1001
+ return patched;
1002
+ }
1003
+ }