fedipod-server 0.11.0 → 0.12.1

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 (142) hide show
  1. package/README.md +15 -6
  2. package/dist/handler.d.ts +6 -0
  3. package/dist/handler.js +23 -10
  4. package/dist/handler.jsonld +4 -0
  5. package/dist/store-pod.js +18 -4
  6. package/lib/{c2s.mjs → client/c2s.mjs} +8 -3
  7. package/lib/{localapi.mjs → client/localapi.mjs} +2 -2
  8. package/lib/client/masto/accounts.mjs +264 -0
  9. package/lib/client/masto/body.mjs +69 -0
  10. package/lib/client/masto/index.mjs +183 -0
  11. package/lib/client/masto/instance.mjs +104 -0
  12. package/lib/client/masto/media.mjs +133 -0
  13. package/lib/client/masto/oauth.mjs +599 -0
  14. package/lib/client/masto/render.mjs +459 -0
  15. package/lib/client/masto/statuses.mjs +331 -0
  16. package/lib/client/masto/timelines.mjs +316 -0
  17. package/lib/{streaming.mjs → client/streaming.mjs} +1 -1
  18. package/lib/{acctfeed.mjs → connections/acctfeed.mjs} +1 -1
  19. package/lib/{atproto.mjs → connections/atproto.mjs} +15 -16
  20. package/lib/{bskygroup.mjs → connections/bskygroup.mjs} +1 -1
  21. package/lib/{fediacct.mjs → connections/fediacct.mjs} +31 -35
  22. package/lib/{import.mjs → connections/import.mjs} +1 -1
  23. package/lib/{tagfeed.mjs → connections/tagfeed.mjs} +3 -3
  24. package/lib/connections/vault.mjs +114 -0
  25. package/lib/core/as2.mjs +124 -0
  26. package/lib/core/contexts/activitystreams.json +379 -0
  27. package/lib/core/contexts/did-v1.json +57 -0
  28. package/lib/core/contexts/fep-5711.json +36 -0
  29. package/lib/core/contexts/gotosocial.json +86 -0
  30. package/lib/core/contexts/identity-v1.json +152 -0
  31. package/lib/core/contexts/index.mjs +45 -0
  32. package/lib/core/contexts/join-lemmy.json +33 -0
  33. package/lib/core/contexts/joinmastodon.json +28 -0
  34. package/lib/core/contexts/map.json +16 -0
  35. package/lib/core/contexts/miscellany.json +19 -0
  36. package/lib/core/contexts/schemaorg.json +8845 -0
  37. package/lib/core/contexts/security-data-integrity-v1.json +78 -0
  38. package/lib/core/contexts/security-data-integrity-v2.json +81 -0
  39. package/lib/core/contexts/security-multikey-v1.json +35 -0
  40. package/lib/core/contexts/security-v1.json +74 -0
  41. package/lib/core/contexts/webfinger.json +10 -0
  42. package/lib/{deliver.mjs → core/deliver.mjs} +2 -2
  43. package/lib/core/intake/activities.mjs +437 -0
  44. package/lib/core/intake/activity.mjs +240 -0
  45. package/lib/core/intake/channel.mjs +144 -0
  46. package/lib/core/intake/group.mjs +222 -0
  47. package/lib/core/intake/index.mjs +629 -0
  48. package/lib/core/intake/notes.mjs +288 -0
  49. package/lib/core/intake/verify.mjs +141 -0
  50. package/lib/{keys.mjs → core/keys.mjs} +1 -1
  51. package/lib/core/publisher/collections.mjs +229 -0
  52. package/lib/core/publisher/index.mjs +421 -0
  53. package/lib/core/publisher/notes.mjs +188 -0
  54. package/lib/core/publisher/questions.mjs +233 -0
  55. package/lib/core/publisher/restore.mjs +196 -0
  56. package/lib/core/shapes/activitystreams.ttl +129 -0
  57. package/lib/core/shapes/index.mjs +107 -0
  58. package/lib/core/shapes/shapes-text.mjs +13 -0
  59. package/lib/{social.mjs → core/social.mjs} +2 -2
  60. package/lib/{store.mjs → core/store.mjs} +4 -0
  61. package/lib/{wire.mjs → core/wire.mjs} +2 -2
  62. package/lib/device/admin/index.mjs +13 -0
  63. package/lib/device/admin/origins.mjs +35 -0
  64. package/lib/device/admin/routes/connections.mjs +144 -0
  65. package/lib/device/admin/routes/gateway.mjs +199 -0
  66. package/lib/device/admin/routes/lifecycle.mjs +191 -0
  67. package/lib/device/admin/routes/owner.mjs +322 -0
  68. package/lib/device/admin/routes/setup.mjs +393 -0
  69. package/lib/device/admin/routes/social.mjs +188 -0
  70. package/lib/device/admin/server.mjs +95 -0
  71. package/lib/device/admin/static.mjs +244 -0
  72. package/lib/device/admin/surface.mjs +274 -0
  73. package/lib/device/cli/commands/account.mjs +586 -0
  74. package/lib/device/cli/commands/run.mjs +278 -0
  75. package/lib/device/cli/commands/service.mjs +221 -0
  76. package/lib/device/cli/commands/setup.mjs +410 -0
  77. package/lib/device/cli/commands/state.mjs +559 -0
  78. package/lib/device/cli/context.mjs +288 -0
  79. package/lib/{migrate.mjs → device/migrate.mjs} +1 -1
  80. package/lib/{remote.mjs → device/remote.mjs} +3 -3
  81. package/lib/{setup.mjs → device/setup.mjs} +3 -3
  82. package/lib/{update.mjs → device/update.mjs} +1 -1
  83. package/lib/{directory.mjs → gateway/directory.mjs} +1 -1
  84. package/lib/{front-core.mjs → gateway/front-core.mjs} +3 -3
  85. package/lib/{gateway-core.mjs → gateway/gateway-core.mjs} +1 -1
  86. package/lib/{httpsig.mjs → gateway/httpsig.mjs} +1 -1
  87. package/lib/{embed.mjs → server/embed.mjs} +149 -22
  88. package/lib/{links.mjs → shared/links.mjs} +1 -1
  89. package/lib/{ua.mjs → shared/ua.mjs} +1 -1
  90. package/package.json +1 -1
  91. package/run-agent.mjs +33 -25
  92. package/web/admin/actors.js +145 -0
  93. package/web/admin/common.js +23 -0
  94. package/web/admin/connections.js +112 -0
  95. package/web/admin/gateway.js +111 -0
  96. package/web/admin/group.js +258 -0
  97. package/web/admin/index.html +7 -1
  98. package/web/admin/record.js +378 -0
  99. package/web/admin/setup/index.html +1 -0
  100. package/web/admin/setup/setup.js +2 -13
  101. package/web/admin/upkeep.js +170 -0
  102. package/web/app/README.md +6 -6
  103. package/web/app/admin-facade.mjs +3 -3
  104. package/web/app/agent.mjs +12 -12
  105. package/web/app/atproto-browser.mjs +1 -1
  106. package/web/app/deliver-relay.mjs +1 -1
  107. package/web/app/dist/sw.js +21684 -5415
  108. package/web/app/dist/sw.js.map +4 -4
  109. package/web/app/fediacct-browser.mjs +1 -1
  110. package/web/app/shims/shapes-text.mjs +8 -0
  111. package/web/app/site/admin/actors.js +145 -0
  112. package/web/app/site/admin/common.js +23 -0
  113. package/web/app/site/admin/connections.js +112 -0
  114. package/web/app/site/admin/gateway.js +111 -0
  115. package/web/app/site/admin/group.js +258 -0
  116. package/web/app/site/admin/index.html +7 -1
  117. package/web/app/site/admin/record.js +378 -0
  118. package/web/app/site/admin/setup/index.html +1 -0
  119. package/web/app/site/admin/setup/setup.js +2 -13
  120. package/web/app/site/admin/upkeep.js +170 -0
  121. package/web/app/site/sw.js +21684 -5415
  122. package/web/app/sw-src.mjs +17 -2
  123. package/lib/admin.mjs +0 -1913
  124. package/lib/intake.mjs +0 -1981
  125. package/lib/mastoapi.mjs +0 -2284
  126. package/lib/publisher.mjs +0 -1192
  127. package/web/admin/admin.js +0 -1181
  128. package/web/app/site/admin/admin.js +0 -1181
  129. /package/lib/{oidc-auth.mjs → client/oidc-auth.mjs} +0 -0
  130. /package/lib/{webpush.mjs → client/webpush.mjs} +0 -0
  131. /package/lib/{bskyfeed.mjs → connections/bskyfeed.mjs} +0 -0
  132. /package/lib/{lease.mjs → core/lease.mjs} +0 -0
  133. /package/lib/{polls.mjs → core/polls.mjs} +0 -0
  134. /package/lib/{proof.mjs → core/proof.mjs} +0 -0
  135. /package/lib/{storage.mjs → core/storage.mjs} +0 -0
  136. /package/lib/{account.mjs → device/account.mjs} +0 -0
  137. /package/lib/{certs.mjs → device/certs.mjs} +0 -0
  138. /package/lib/{export-collections.mjs → device/export-collections.mjs} +0 -0
  139. /package/lib/{home.mjs → device/home.mjs} +0 -0
  140. /package/lib/{ports.mjs → device/ports.mjs} +0 -0
  141. /package/lib/{guard.mjs → shared/guard.mjs} +0 -0
  142. /package/lib/{safefetch.mjs → shared/safefetch.mjs} +0 -0
package/lib/publisher.mjs DELETED
@@ -1,1192 +0,0 @@
1
- // publisher.mjs — builds/maintains the actor's public face on the remote pod:
2
- // webfinger, actor doc, collections, notes. The /ap/ tree is disposable:
3
- // 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 * as polls from './polls.mjs';
11
- import { USER_AGENT } from './ua.mjs';
12
- import { HTTP_TIMEOUT_MS } from './safefetch.mjs';
13
- import * as containers from './pod/containers.mjs';
14
- import * as discovery from './pod/discovery.mjs';
15
- import * as podActor from './pod/actor.mjs';
16
- import * as podInbox from './pod/inbox.mjs';
17
- import * as podOutbox from './pod/outbox.mjs';
18
- import * as podFollowers from './pod/followers.mjs';
19
- import * as podFollowing from './pod/following.mjs';
20
- import * as podFeatured from './pod/featured.mjs';
21
- import * as podPrivate from './pod/private.mjs';
22
- import * as podPolicy from './pod/policy.mjs';
23
- import * as podNotes from './pod/notes.mjs';
24
- import * as podMedia from './pod/media.mjs';
25
-
26
- const ACCEPT_AP = 'application/activity+json, application/ld+json; profile="https://www.w3.org/ns/activitystreams"';
27
- // The default for publishCollections: the whole public surface, ACLs included.
28
- // A caller that knows what it changed narrows it; a caller that says nothing
29
- // still gets everything, so a missed call site degrades to the old cost rather
30
- // than silently publishing nothing.
31
- const REBUILD_MAX_PER_RUN = 200;
32
- // How long a poll gathers votes before its Question is rewritten. Every vote
33
- // changes a number other servers re-read, and rewriting per vote would make a
34
- // busy poll a steady write stream against the pod. A burst costs one rewrite
35
- // and one Update instead.
36
- const POLL_REWRITE_MS = 10_000;
37
- const ALL_COLLECTIONS = { followers: true, following: true, outbox: true, acls: true,
38
- pending: true, blocked: true };
39
- const AGENT_VERSION = JSON.parse(fs.readFileSync(
40
- path.join(path.dirname(fileURLToPath(import.meta.url)), '../package.json'), 'utf8')).version;
41
-
42
- export class Publisher {
43
- constructor({ config, remote, store, deliverer, publicKeyPem, assertionKey = null, log = console.log,
44
- probeFetch = null, resolveMention = null, clientOrigin = null,
45
- }) {
46
- this.config = config;
47
- this.remote = remote;
48
- this.store = store;
49
- this.deliverer = deliverer;
50
- this.publicKeyPem = publicKeyPem;
51
- this.assertionKey = assertionKey; // Ed25519 public half, multibase; null = no proofs
52
- // Where this identity's client surface answers, when that is an address a
53
- // stranger can reach. Null on a laptop, where the surface is on loopback
54
- // and advertising it to the world would name somewhere nobody can go.
55
- this.clientOrigin = clientOrigin;
56
- // A fronted identity (config.gateway.frontActor) advertises its ids on a
57
- // shared domain; the map tells RemotePod where each writes on the pod.
58
- const publicBase = config.gateway?.frontActor
59
- ? config.gateway.frontActor.replace(/ap\/actor\/?$/, '') : null;
60
- this.urls = wire.apUrls(config.remotePod, config.root, { publicBase });
61
- if (this.urls.toPod && this.remote?.setUrlMap) this.remote.setUrlMap(this.urls.toPod);
62
- // Credential-free by design — it asks what a stranger sees — but routed
63
- // through the pod's own cooldown and accounting, because it is still a
64
- // socket opened to that pod. Tests inject their own.
65
- this.probeFetch = probeFetch || ((u, i) => this.remote.probe(u, i));
66
- this.resolveMention = resolveMention;
67
- // Per-poll rewrite windows, keyed by question id. See POLL_REWRITE_MS.
68
- this.pollTimers = new Map();
69
- this.log = log;
70
- }
71
-
72
- // Idempotent: (re)write webfinger + actor + collections + container ACLs.
73
- //
74
- // ~34 pod requests, so it does not run when it would rewrite the same bytes.
75
- // Every document below is derived from the actor doc, the handle, the host,
76
- // whether the actor is quiesced (which decides the inbox ACL) and the agent
77
- // version (which rides in nodeinfo) — so if none of those moved, there is
78
- // nothing to say. Phanpy's editor submits the whole form on every save, so
79
- // "saved without changing anything" is the common case, not a rare one.
80
- //
81
- // `force` is for the callers that publish precisely BECAUSE the pod does not
82
- // have what the digest says it has: the repair path, and the explicit
83
- // republish button. Without it, an actor lost from the pod would match the
84
- // digest, be skipped, and leave the agent reporting success while nobody can
85
- // resolve it.
86
- async publishProfile({ force = false } = {}) {
87
- const { urls } = this;
88
- const host = new URL(urls.base).host;
89
-
90
- // The owner-only collections (FEP-4ccd, FEP-c648) are advertised only
91
- // where "owner-only" is real — the pod must provably enforce the private
92
- // container's ACL, the same bar private posts clear.
93
- const priv = await this.privateReady() === true;
94
- const moderators = (this.config.moderators || []).length ? urls.moderators : null;
95
- // An advertised inbox gateway (config.gateway.mode past 'off') becomes the
96
- // actor's inbox; deliveries reach the pod inbox through it. Absent → today.
97
- const gw = this.config.gateway;
98
- const gwActive = gw && gw.url && gw.mode && gw.mode !== 'off';
99
- const actorDoc = wire.actorDoc({
100
- urls, handle: wire.publicHandle(this.config), name: this.config.name, publicKeyPem: this.publicKeyPem,
101
- movedTo: this.config.movedTo || null, kind: this.config.kind,
102
- approveJoins: wire.followsNeedApproval(this.config),
103
- assertionKey: this.assertionKey,
104
- summary: this.config.summary || null, icon: this.config.icon || null,
105
- image: this.config.image || null, fields: this.config.fields || [],
106
- webId: this.remote.webId || null,
107
- aliases: this.config.aliases || [],
108
- moderators,
109
- pendingFollowers: priv ? urls.pendingFollowers : null,
110
- pendingFollowing: priv ? urls.pendingFollowing : null,
111
- blocked: priv ? urls.blocked : null,
112
- inbox: gwActive ? gw.url : null,
113
- // The agent's own outbox endpoint, where it is reachable: a client
114
- // following the actor must arrive somewhere that will take a write.
115
- outbox: this.clientOrigin ? `${this.clientOrigin}ap/outbox` : null,
116
- // How a client-to-server client finds the way in with nothing configured
117
- // by hand. Advertised only where the surface is publicly reachable.
118
- oauthAuthorize: this.clientOrigin ? `${this.clientOrigin}oauth/authorize` : null,
119
- oauthToken: this.clientOrigin ? `${this.clientOrigin}oauth/token` : null,
120
- });
121
- const surface = crypto.createHash('sha256').update(JSON.stringify({
122
- actor: actorDoc, handle: this.config.handle, host,
123
- quiesced: !!this.config.quiescedAt, version: AGENT_VERSION,
124
- moderators: this.config.moderators || [],
125
- })).digest('hex').slice(0, 32);
126
- if (!force && this.store.read('published.json', {}).surfaceDigest === surface) {
127
- this.log('profile unchanged — nothing republished');
128
- return { unreachable: [], updated: 0, skipped: true };
129
- }
130
-
131
- await discovery.writeWebfinger(this.remote, urls,
132
- wire.jrd({ handle: this.config.handle, host, actor: urls.actor }));
133
- await discovery.writeHostMeta(this.remote, urls, wire.hostMeta(urls.base));
134
-
135
- const nodeinfoDocUrl = urls.home + 'ap/nodeinfo-2.0';
136
- const localPosts = this.store.getStatuses().filter(s => s.kind === 'post').length;
137
- await discovery.writeNodeinfo(this.remote, urls, {
138
- pointer: wire.nodeinfoPointer(nodeinfoDocUrl),
139
- doc: wire.nodeinfoDoc({ version: AGENT_VERSION, localPosts }),
140
- });
141
-
142
- const actor = actorDoc; // built above, for the digest
143
- await podActor.write(this.remote, urls, actor);
144
-
145
- // FEP-1b12: the moderator roster, public — it is what a recipient
146
- // validates a group's announced moderation against.
147
- if (moderators) {
148
- await podFeatured.writeModerators(this.remote, urls,
149
- wire.orderedCollection(urls.moderators, this.config.moderators));
150
- }
151
- // The gateway policy doc: the PUBLIC data a keyless gateway reads to decide
152
- // what concerns this identity. Written only while a gateway is advertised.
153
- if (gwActive) await this.publishGatewayPolicy();
154
-
155
- // The human half: a page a browser can open and follow from. The actor
156
- // document is for servers; this is the address you hand to a person.
157
- await podActor.writeProfilePage(this.remote, urls, wire.profilePageHtml({
158
- name: this.config.name || this.config.handle,
159
- address: wire.webfingerHost(urls.base) ? `@${this.config.handle}@${host}` : urls.actor,
160
- summary: this.config.summary ? wire.contentHtml(this.config.summary) : null,
161
- icon: this.config.icon || null,
162
- kind: this.config.kind,
163
- }));
164
-
165
- // WebID → actor: the profile card lists the actor as a foaf:account.
166
- // Best-effort — a profile that cannot be read or edited does not stop the
167
- // publish, it is logged and the rest of the surface still goes up.
168
- try {
169
- const wrote = await podActor.linkInWebIdProfile(this.remote, {
170
- actorUrl: urls.actor,
171
- accountName: `@${this.config.handle}@${host}`,
172
- kind: this.config.kind,
173
- });
174
- if (wrote) this.log('WebID profile now lists the actor as a foaf:account');
175
- } catch (e) {
176
- this.log(`WebID profile not updated with the actor link: ${e.message}`);
177
- }
178
-
179
- // inbox: public may only Append; owner (the agent) reads + drains. A
180
- // quiesced actor keeps its name resolving but takes no more mail, so a
181
- // republish must not re-open the door.
182
- await podInbox.writeKeep(this.remote, urls);
183
- await podInbox.setPosture(this.remote, urls, this.config.quiescedAt ? 'closed' : 'open');
184
- if (this.config.quiescedAt) this.log('inbox left closed — this actor is quiesced');
185
-
186
- // notes live under a public-Read container (acl:default covers new notes).
187
- await podNotes.provisionContainer(this.remote, urls);
188
-
189
- await this.publishCollections({ ...ALL_COLLECTIONS, force });
190
- const updated = await this.announceProfileChange(actor, { force });
191
- await this.ensurePrivateAcls();
192
- const unreachable = await this.verifyPublicSurface();
193
- // Last, and merged: announceProfileChange writes this document too.
194
- //
195
- // Only when the surface came back readable. Recording it regardless meant a
196
- // publish that half-landed still matched the digest, so the NEXT save — the
197
- // one the operator makes because the first did not work — was skipped as a
198
- // no-op and reported success. The digest is a record of what is up there,
199
- // and an unreachable document is not up there.
200
- if (!unreachable.length) {
201
- this.store.write('published.json',
202
- { ...this.store.read('published.json', {}), surfaceDigest: surface });
203
- }
204
- // Named as the fediverse sees it: a fronted actor's name and host are the
205
- // front's, not the pod's.
206
- const pubName = wire.publicHandle(this.config);
207
- const pubHost = this.config.gateway?.frontActor ? new URL(this.config.gateway.frontActor).host : host;
208
- this.log(this.config.gateway?.frontActor || wire.webfingerHost(urls.base)
209
- ? `profile published: @${pubName}@${pubHost} → ${urls.actor}`
210
- : `profile published → ${urls.actor} — NOT discoverable as @${pubName}@${pubHost}: `
211
- + 'this pod is a path on a shared host, and WebFinger is only answered at a host root');
212
- return { unreachable, updated };
213
- }
214
-
215
- // Fires when the document differs from the one last published — including
216
- // the first time, when there is nothing to differ from.
217
- //
218
- // Every call to publishProfile is already a DELIBERATE republish: setup, a
219
- // rename, an edit through the client or /config, `describe`, a key rotation,
220
- // or an actor document found missing from the pod. Starting the agent does not
221
- // call it. So the digest is not there to survive restarts — it is there for
222
- // the republish that changes nothing, which /config does whenever you save a
223
- // field the actor document does not carry.
224
- //
225
- // A silent first publish was considered and is WRONG here: it would spend a
226
- // real edit doing nothing but recording a digest, and that edit is exactly the
227
- // one whose invisibility this fixes.
228
- async announceProfileChange(actor, { force = false } = {}) {
229
- const digest = crypto.createHash('sha256').update(JSON.stringify(actor)).digest('hex').slice(0, 32);
230
- const seen = this.store.read('published.json', {});
231
- // A forced republish is the operator saying TELL THE WORLD — the digest
232
- // gate is for silent no-op saves, not for that.
233
- if (seen.actorDigest === digest && !force) return 0;
234
- this.store.write('published.json', { ...seen, actorDigest: digest, at: new Date().toISOString() });
235
- const inboxes = [...new Set(this.store.getContacts().followers
236
- .map(f => f.sharedInbox || f.inbox).filter(Boolean))];
237
- if (!inboxes.length) return 0;
238
- await this.deliverer.deliverToAll(inboxes,
239
- wire.updateActorActivity({ urls: this.urls, actor, serial: Date.now() }));
240
- this.log(`profile changed — Update delivered to ${inboxes.length} inbox(es)`);
241
- return inboxes.length;
242
- }
243
-
244
- // The mirror of ensurePrivateAcls, and the check this project lacked: these
245
- // documents MUST be readable by strangers or no server can see the actor.
246
- // A publish that dies half-way, or an ACL write that is accepted without
247
- // taking effect, is otherwise indistinguishable from success — the agent
248
- // reports itself configured and federating while nobody can find it.
249
- async verifyPublicSurface() {
250
- const { urls } = this;
251
- const targets = [
252
- ['webfinger', urls.webfinger],
253
- ['host-meta', urls.base + '.well-known/host-meta'],
254
- ['actor', urls.actor],
255
- ['notes', urls.notes],
256
- ['followers', urls.followers],
257
- ['following', urls.following],
258
- ['outbox', urls.outbox],
259
- ];
260
- const unreachable = [];
261
- for (const [name, url] of targets) {
262
- if (!await this.publiclyReadable(url)) unreachable.push(name);
263
- }
264
- if (unreachable.length) {
265
- this.log(`FEDERATION: ${unreachable.join(', ')} not readable without credentials — `
266
- + 'other servers cannot resolve or fetch this actor');
267
- }
268
- return unreachable;
269
- }
270
-
271
- // An ACL write that silently failed — or was changed afterwards by anything
272
- // else touching the pod — leaves the private trees, signing keys included,
273
- // world-readable. bootstrap writes them once at setup and never returns, so
274
- // this runs on every connect: probe UNauthenticated, rewrite whatever
275
- // answers, and say so loudly if the rewrite does not take.
276
- async ensurePrivateAcls() {
277
- const findings = await containers.repairPrivateAcls(this.remote,
278
- [this.urls.home, this.urls.state],
279
- { isPublic: (u) => this.publiclyReadable(u) });
280
- // What a finding MEANS is ours to say; the library just reports.
281
- for (const f of findings) {
282
- this.log(`${f.url} was readable without credentials — rewriting its ACL`);
283
- if (f.error) { this.log(`SECURITY: ${f.url} is public and its ACL could not be rewritten: ${f.error}`); continue; }
284
- if (f.stillPublic) this.log(`SECURITY: ${f.url} is STILL readable without credentials — check the pod's ACLs`);
285
- }
286
- }
287
-
288
- // Deliberately credential-free: this asks what a stranger would see.
289
- // accept: */* matters — asking for turtle makes the server answer 501 on the
290
- // JSON documents (webfinger, actor), which reads as "unreachable" when the
291
- // world can in fact see them perfectly well.
292
- publiclyReadable(url) {
293
- return containers.probePublicReadability(this.probeFetch, url,
294
- { headers: { 'user-agent': USER_AGENT }, timeoutMs: HTTP_TIMEOUT_MS });
295
- }
296
-
297
- // Retire this identity for good: tell everyone who follows us to drop the
298
- // account, then leave a Tombstone where the actor was. The inbox stays
299
- // publicly Append-able on purpose — closing it would make deliveries 401,
300
- // which Mastodon treats as failure and retries, the opposite of the point.
301
- // A Delete stops well-behaved servers; anything that keeps delivering gets a
302
- // cheap 201 into a container we no longer read.
303
- async retireActor() {
304
- const { urls } = this;
305
- const contacts = this.store.getContacts();
306
- const inboxes = [...new Set(contacts.followers.map(f => f.sharedInbox || f.inbox).filter(Boolean))];
307
- const deletedAt = new Date().toISOString();
308
- await this.deliverer.deliverToAll(inboxes, wire.deleteActorActivity(urls, Date.parse(deletedAt)));
309
- await podActor.writeTombstone(this.remote, urls, wire.tombstoneDoc(urls, deletedAt, this.config.kind));
310
- this.store.setConfig({ ...this.store.getConfig(), retiredAt: deletedAt });
311
- await this.store.flush();
312
- this.log(`retired: Delete sent to ${inboxes.length} inbox(es), actor replaced with a Tombstone`);
313
- return { inboxes: inboxes.length, deletedAt };
314
- }
315
-
316
- // Stop accepting mail without giving up the name: deliveries get an immediate
317
- // 401 rather than a 201 into storage nobody will ever drain. WebFinger,
318
- // host-meta and the actor stay published, so the handle still resolves.
319
- async closeInbox() {
320
- await podInbox.setPosture(this.remote, this.urls, 'closed');
321
- const at = new Date().toISOString();
322
- this.store.setConfig({ ...this.store.getConfig(), quiescedAt: at });
323
- await this.store.flush();
324
- this.log(`inbox closed — @${this.config.handle} still resolves but accepts nothing`);
325
- return at;
326
- }
327
-
328
- // Undo closeInbox: mail flows again and the actor is no longer quiesced.
329
- async openInbox() {
330
- await podInbox.setPosture(this.remote, this.urls, 'open');
331
- const { quiescedAt, ...rest } = this.store.getConfig() || {};
332
- this.store.setConfig(rest);
333
- this.config.quiescedAt = undefined;
334
- await this.store.flush();
335
- this.log(`inbox re-opened — @${this.config.handle} is taking mail again`);
336
- }
337
-
338
- // Lock the inbox to a gateway: the public loses Append, so nothing reaches
339
- // the pod except through the gateway's verify-at-the-door. Reversible with
340
- // openInbox (public-Append) — the one-call rollback.
341
- async lockInboxToGateway(gatewayWebId) {
342
- await podInbox.setPosture(this.remote, this.urls, { gatewayWebId });
343
- this.log(`inbox locked to gateway ${gatewayWebId} — public delivery is refused`);
344
- }
345
-
346
- // Tell the fediverse the account lives somewhere else now. Well-behaved
347
- // servers migrate their followers to the target and stop delivering here.
348
- async publishMove(target) {
349
- const { urls } = this;
350
- const contacts = this.store.getContacts();
351
- const inboxes = [...new Set(contacts.followers.map(f => f.sharedInbox || f.inbox).filter(Boolean))];
352
- const at = new Date().toISOString();
353
- await this.deliverer.deliverToAll(inboxes, wire.moveActivity(urls, target, Date.parse(at)));
354
- this.config.movedTo = target; // so the republish below carries it
355
- this.store.setConfig({ ...this.store.getConfig(), movedTo: target, movedAt: at });
356
- await podActor.writeMoved(this.remote, urls, wire.actorDoc({
357
- urls, handle: wire.publicHandle(this.config), name: this.config.name,
358
- publicKeyPem: this.publicKeyPem, movedTo: target, kind: this.config.kind,
359
- approveJoins: wire.followsNeedApproval(this.config),
360
- assertionKey: this.assertionKey,
361
- summary: this.config.summary || null, icon: this.config.icon || null,
362
- image: this.config.image || null, fields: this.config.fields || [],
363
- webId: this.remote.webId || null,
364
- aliases: this.config.aliases || [],
365
- }));
366
- await this.store.flush();
367
- this.log(`moved to ${target}: Move sent to ${inboxes.length} inbox(es), actor now advertises movedTo`);
368
- return { inboxes: inboxes.length, target, movedAt: at };
369
- }
370
-
371
- // Anyone the pod says follows us that we have no record of, and never
372
- // deliberately removed. In the steady state there is nobody: the pod's list is
373
- // written from this one. They appear when the local half is BEHIND the pod —
374
- // a restored backup, a home copied off a dead machine — and republishing
375
- // blindly would delete them from the wire and, worse, stop delivering to them.
376
- //
377
- // Removals are what makes this safe to do unconditionally: `dropFollower`
378
- // records every unfollow, ejection and account deletion, so a returning name
379
- // is either genuinely still a follower or genuinely a mistake we made.
380
- async reconcileFollowers(contacts) {
381
- let published = [];
382
- try {
383
- published = (await this.readPublishedFollowers()) || []; // walks pages
384
- } catch { return 0; } // no list to reconcile against
385
-
386
- const known = new Set(contacts.followers.map(f => f.actor));
387
- const removed = new Set((contacts.removedFollowers || []).map(r => r.actor));
388
- const missing = published.filter(a => typeof a === 'string' && !known.has(a) && !removed.has(a));
389
- if (!missing.length) return 0;
390
-
391
- // An inbox is what delivery needs, and only the actor document has it — so
392
- // recovering a follower costs one fetch each. Capped: a list this wrong is
393
- // a restore, and the rest catch up on the next publish.
394
- let recovered = 0;
395
- for (const actor of missing.slice(0, 200)) {
396
- try {
397
- const res = await this.deliverer.signedFetch(actor, { headers: { accept: ACCEPT_AP } });
398
- if (!res.ok) continue;
399
- const doc = await res.json();
400
- if (!doc?.inbox) continue;
401
- contacts.followers.push({
402
- actor, inbox: doc.inbox, sharedInbox: doc.endpoints?.sharedInbox || null, recovered: true,
403
- // Said explicitly, because onUndo reads it: the pod publishes WHO
404
- // follows, never the id of the Follow that did it, so a recovered
405
- // record has nothing an Undo can be matched against and must not be
406
- // evictable by one naming anything at all.
407
- followId: null,
408
- });
409
- recovered++;
410
- } catch { /* unreachable now; it will be there next time */ }
411
- }
412
- if (recovered) {
413
- this.store.setContacts(contacts);
414
- this.log(`reconciled ${recovered} follower(s) the pod knew about and this machine did not `
415
- + '— a restored or copied state was behind');
416
- }
417
- return recovered;
418
- }
419
-
420
- // The same argument as reconcileFollowers, for the other published list. It
421
- // matters more: the outbox is the INDEX a statuses rebuild reads, so a
422
- // republish from a restored-and-behind machine would destroy the record of
423
- // everything this actor ever posted — and destroy it before anyone noticed
424
- // there was anything to recover.
425
- //
426
- // Safe for the same reason: `unrecordOutbox` leaves a tombstone, so an entry
427
- // the pod still carries is one this machine has not heard of, never one it
428
- // deliberately took back.
429
- async reconcileOutbox(outbox) {
430
- let published = [];
431
- try {
432
- published = await this.readPublishedOutbox() || [];
433
- } catch { return 0; }
434
- if (!Array.isArray(published) || !published.length) return 0;
435
-
436
- const idOf = (i) => (typeof i === 'string' ? i : i?.id || null);
437
- const known = new Set(outbox.map(idOf).filter(Boolean));
438
- const removed = new Set((this.store.read('outbox-removed.json', [])).map(r => r.id));
439
- const missing = published.filter((i) => {
440
- const id = idOf(i);
441
- return id && !known.has(id) && !removed.has(id);
442
- });
443
- if (!missing.length) return 0;
444
- // Newest first, like recordOutbox leaves it; the pod's copy is already in
445
- // that order, so appending the tail is enough to keep both sorted.
446
- outbox.push(...missing);
447
- this.store.write('outbox.json', outbox);
448
- this.log(`reconciled ${missing.length} outbox entr(ies) the pod carried and this machine did not`);
449
- return missing.length;
450
- }
451
-
452
- // Recover this actor's own posts from the pod's public face. The private half
453
- // lives on this machine now, so a restored backup or a replaced machine loses
454
- // statuses.json while the pod still serves every note. Followers already come
455
- // back; this is the other half of the same gap.
456
- //
457
- // The INDEX is ap/outbox, not the ap/notes/ listing, and that difference is
458
- // the safety argument. Deleting a post rewrites the outbox in one PUT, so an
459
- // entry still there is a post that still stands. The note DOCUMENT can outlive
460
- // its own deletion — deleteNote's `remote.delete` is a request that can fail —
461
- // so walking the container can bring back something its author took down.
462
- // `fromNotes` is for when you would rather have that than lose the post; it is
463
- // not the default, and it says so where it is offered.
464
- //
465
- // MERGE ONLY. A status this machine already holds is left exactly as it is:
466
- // it carries local facts — favourited, reblogged, the activities an Undo has
467
- // to name — that the pod knows nothing about. That is also what makes the
468
- // failure modes harmless: a listing that fails returns nothing, and nothing
469
- // is what an empty listing recovers.
470
- async rebuildStatuses({ fromNotes = false } = {}) {
471
- const { urls } = this;
472
- const ids = new Set();
473
- const boosts = [];
474
- let indexed = false;
475
-
476
- const published = await this.readPublishedOutbox().catch(() => null);
477
- if (published) {
478
- indexed = true;
479
- for (const item of published) {
480
- if (typeof item === 'string') { if (item.startsWith(urls.notes)) ids.add(item); }
481
- else if (item?.type === 'Announce') boosts.push(item);
482
- }
483
- }
484
- if (fromNotes) {
485
- // Three documents are published per post — the note, `-create` and
486
- // `-replies` — plus a `.keep`. Which is which is settled by reading the
487
- // document below, not by its name: a slug is a date and eight hex
488
- // characters and says nothing about what it holds.
489
- for (const child of await podNotes.list(this.remote, urls).catch(() => [])) {
490
- ids.add(child.url);
491
- }
492
- indexed = true;
493
- }
494
- if (!indexed) return { indexed: 0, recovered: 0, reblogs: 0, landed: false, why: 'the pod would not answer for its outbox' };
495
-
496
- const statuses = this.store.getStatuses();
497
- const have = new Set(statuses.map(s => s.noteId));
498
- const removed = new Set(this.store.read('outbox-removed.json', []).map(r => r.id));
499
- const recovered = [];
500
- // Capped per run: a long-lived actor's rebuild is otherwise one pod request
501
- // per post it has ever made, in one burst. What is left is picked up by
502
- // running it again — the merge is idempotent, so that is safe to repeat.
503
- let budget = REBUILD_MAX_PER_RUN;
504
- for (const id of ids) {
505
- if (budget <= 0) { this.log(`rebuild: stopping at ${REBUILD_MAX_PER_RUN} this run — run it again for the rest`); break; }
506
- if (have.has(id) || removed.has(id)) continue;
507
- budget--;
508
- const note = await podNotes.read(this.remote, id).catch(() => null);
509
- if (note?.type !== 'Note' || note.id !== id || note.attributedTo !== urls.actor) continue;
510
- const attachments = wire.attachmentsOf(note);
511
- const mentions = (Array.isArray(note.tag) ? note.tag : [])
512
- .filter(t => t?.type === 'Mention' && t.href)
513
- .map(t => ({ href: t.href, name: t.name }));
514
- recovered.push({
515
- noteId: note.id, actor: urls.actor, content: note.content || '',
516
- published: note.published || null,
517
- ...(note.inReplyTo ? { inReplyTo: note.inReplyTo } : {}),
518
- kind: 'post', slug: note.id.slice(urls.notes.length),
519
- ...(attachments.length ? { attachments } : {}),
520
- ...(mentions.length ? { mentions } : {}),
521
- recovered: true,
522
- });
523
- }
524
-
525
- // A boost is only recoverable for a post we can name — the Announce carries
526
- // the activity a later Undo needs, but not the boosted post's text, which
527
- // belongs to whoever wrote it.
528
- const merged = [...statuses, ...recovered];
529
- let reblogs = 0;
530
- for (const act of boosts) {
531
- const object = typeof act.object === 'string' ? act.object : act.object?.id;
532
- const s = object && merged.find(x => x.noteId === object);
533
- if (!s || s.reblogged) continue;
534
- s.reblogged = true;
535
- s.announceActivity = act;
536
- reblogs++;
537
- }
538
- if (!recovered.length && !reblogs) return { indexed: ids.size, recovered: 0, reblogs: 0, landed: true };
539
-
540
- // Written whole and sorted: addStatus unshifts and fires the streaming
541
- // event, so a loop of it would arrive backwards and push every recovered
542
- // post at connected clients as new.
543
- merged.sort((a, b) => String(b.published || '').localeCompare(String(a.published || '')));
544
- const kept = merged.slice(0, 1000);
545
- this.store.write('statuses.json', kept);
546
- const landed = await this.store.commit();
547
-
548
- this.log(`rebuilt ${recovered.length} post(s) and ${reblogs} boost(s) from the pod`
549
- + `${landed ? '' : ' — THE STATE WRITE DID NOT LAND'}`);
550
- return {
551
- indexed: ids.size, recovered: recovered.length, reblogs, landed,
552
- dropped: Math.max(0, merged.length - kept.length),
553
- };
554
- }
555
-
556
- // Publish the collections a change actually TOUCHED.
557
- //
558
- // Publishing all three on every follower event cost nine pod requests where
559
- // two do: two reconcile reads, three collection PUTs, and three ACL PUTs
560
- // whose bodies are a pure function of the WebID and the target URL and so
561
- // are byte-identical to the ones written at setup. A new follower does not
562
- // change what this actor follows, and it does not change the outbox.
563
- //
564
- // `acls` is true only on the default path, which is publishProfile: that is
565
- // where the public surface is built, and where verifyPublicSurface already
566
- // checks the world can read it.
567
- //
568
- // Reconciliation stays welded to the collection it guards. It is what stops a
569
- // restored-and-behind machine publishing a short list over the pod's longer
570
- // one — erasing followers it would then stop delivering to, and erasing the
571
- // outbox that `rebuild` reads as its index — so a narrowed publish still runs
572
- // the one belonging to whatever it is about to overwrite.
573
- // Publish only the pages that actually changed.
574
- //
575
- // `known` is what we last wrote, so a post rewrites the newest page and the
576
- // head and nothing else — where the flat collection rewrote the actor's whole
577
- // history on every post. A page gets its ACL when it is first created; the
578
- // container above it is owner-only, so it cannot be inherited.
579
- // `force` is for the caller that publishes BECAUSE the pod does not have what
580
- // the digests say it has. Without it a repair republish rewrote the head and
581
- // skipped every page — the digests still matched the local record — so the
582
- // head advertised a `first:` that 404s, readPublishedOutbox came back empty,
583
- // rebuildStatuses recovered nothing, and the whole thing logged success. That
584
- // happens to an actor with one post as surely as one with five thousand.
585
- async publishOutbox(outbox, { acls = false, force = false } = {}) {
586
- const { urls } = this;
587
- const seen = this.store.read('published.json', {});
588
- const { pages, index } = wire.outboxPaging(outbox, seen.outboxIndex || []);
589
- const before = force ? {} : (seen.outboxPages || {});
590
- const after = {};
591
- let wrote = 0;
592
-
593
- for (let i = 0; i < pages.length; i++) {
594
- const n = i + 1; // 1 = oldest
595
- const doc = wire.outboxPage(urls.outbox, n, pages[i]);
596
- const digest = crypto.createHash('sha256').update(JSON.stringify(doc)).digest('hex').slice(0, 16);
597
- after[n] = digest;
598
- if (before[n] === digest) continue; // sealed and unchanged
599
- await podOutbox.writePage(this.remote, wire.outboxPageId(urls.outbox, n), doc,
600
- { publicRead: !before[n] || acls });
601
- wrote++;
602
- }
603
- // Pages above the new count are orphans: the outbox shrank past them, and
604
- // left where they were they keep serving activities that have been taken
605
- // back. The head no longer points at them, so nothing walks to them — but
606
- // the URL is guessable and public.
607
- const stale = Object.keys(seen.outboxPages || {}).map(Number)
608
- .filter(n => Number.isFinite(n) && n > pages.length);
609
- for (const n of stale) {
610
- await podOutbox.dropPage(this.remote, wire.outboxPageId(urls.outbox, n));
611
- }
612
- // The head carries totalItems, so it moves whenever the outbox does. Four
613
- // lines, and constant however much you have posted.
614
- await podOutbox.writeHead(this.remote, urls,
615
- wire.outboxHead(urls.outbox, outbox.length, pages.length), { publicRead: acls });
616
- this.store.write('published.json',
617
- { ...this.store.read('published.json', {}), outboxPages: after, outboxIndex: index });
618
- return wrote;
619
- }
620
-
621
- // Every activity in the published outbox, walking the pages. Also understands
622
- // the flat collection this used to write, so an actor published before paging
623
- // is still readable — which matters because rebuild reads this to recover
624
- // posts a lost machine no longer has.
625
- readPublishedOutbox() { return podOutbox.readPublished(this.remote, this.urls); }
626
-
627
- // The followers collection, paged like the outbox: a head that carries only
628
- // the count and the page bounds, and page documents holding the actor IRIs —
629
- // so a remote server reads a small head and walks pages instead of pulling one
630
- // document that grows without limit. Regenerated from the in-memory follow
631
- // graph: a follow extends the newest page and an unfollow leaves its page one
632
- // short (wire.pageItems), so only the pages that changed are rewritten.
633
- async publishFollowers(actors, { acls = false, force = false } = {}) {
634
- const { urls } = this;
635
- const seen = this.store.read('published.json', {});
636
- const { pages, index } = wire.followersPaging(actors, seen.followersIndex || []);
637
- const before = force ? {} : (seen.followersPages || {});
638
- const after = {};
639
-
640
- for (let i = 0; i < pages.length; i++) {
641
- const n = i + 1;
642
- const doc = wire.followersPage(urls.followers, n, pages[i], pages.length);
643
- const digest = crypto.createHash('sha256').update(JSON.stringify(doc)).digest('hex').slice(0, 16);
644
- after[n] = digest;
645
- if (before[n] === digest) continue; // unchanged page
646
- await podFollowers.writePage(this.remote, wire.followersPageId(urls.followers, n), doc,
647
- { publicRead: !before[n] || acls });
648
- }
649
- // Pages the collection shrank past would keep serving names that no longer
650
- // follow; the head stops pointing at them but the URL is guessable.
651
- const stale = Object.keys(seen.followersPages || {}).map(Number)
652
- .filter(n => Number.isFinite(n) && n > pages.length);
653
- for (const n of stale) {
654
- await podFollowers.dropPage(this.remote, wire.followersPageId(urls.followers, n));
655
- }
656
- await podFollowers.writeHead(this.remote, urls,
657
- wire.followersHead(urls.followers, actors.length, pages.length), { publicRead: acls });
658
- this.store.write('published.json',
659
- { ...this.store.read('published.json', {}), followersPages: after, followersIndex: index });
660
- }
661
-
662
- // Every actor in the published followers collection, walking pages. Also reads
663
- // the flat collection this used to write, so an actor published before paging
664
- // still reconciles.
665
- readPublishedFollowers() { return podFollowers.readPublished(this.remote, this.urls); }
666
-
667
- async publishCollections(which = ALL_COLLECTIONS) {
668
- const { urls } = this;
669
- const contacts = this.store.getContacts();
670
- if (which.followers) {
671
- // Reconcile walks the published pages, so — like the outbox — pay for it
672
- // only when the local record of what is up there is missing (a restore or
673
- // a copied machine), not on every ordinary save.
674
- const knownF = this.store.read('published.json', {}).followersIndex;
675
- if (which.force || !Array.isArray(knownF)) await this.reconcileFollowers(contacts);
676
- // Bluesky-only members are not AP actors; the published collection
677
- // lists only what a remote server could dereference.
678
- const actors = contacts.followers.filter(f => !f.bsky).map(f => f.actor);
679
- await this.publishFollowers(actors, { acls: which.acls, force: which.force });
680
- }
681
- if (which.following) {
682
- await podFollowing.write(this.remote, urls,
683
- wire.orderedCollection(urls.following, contacts.following.filter(f => f.accepted).map(f => f.actor)),
684
- { publicRead: which.acls });
685
- }
686
- if (which.pending) await this.publishPending();
687
- if (which.blocked) await this.publishBlocked();
688
- if (which.outbox) {
689
- const outbox = this.store.read('outbox.json', []);
690
- // Reconcile reads every published page, which is the expensive part of a
691
- // profile save. It is worth paying only when we are about to write pages
692
- // we did not write: on a repair, or on a machine whose state no longer
693
- // records what it put up there — which is exactly the restored backup the
694
- // reconcile exists for. With an intact record our copy IS what the pod
695
- // has, and re-reading it to confirm that is a page walk for nothing.
696
- const known = this.store.read('published.json', {}).outboxIndex;
697
- if (which.force || !Array.isArray(known)) await this.reconcileOutbox(outbox);
698
- await this.publishOutbox(outbox, { acls: which.acls, force: which.force });
699
- }
700
- }
701
-
702
- // FEP-4ccd: the follows in limbo, as owner-only collections of the Follow
703
- // activities themselves. Inside the private container so its ACL is
704
- // inherited — and published only where that ACL provably holds, the same
705
- // bar private posts clear. A Bluesky-only request has no Follow activity a
706
- // remote server could ever act on, so it is not listed.
707
- async publishPending() {
708
- if (await this.privateReady() !== true) return;
709
- const { urls } = this;
710
- const contacts = this.store.getContacts();
711
- await podPrivate.writePending(this.remote, urls, {
712
- followers: wire.orderedCollection(urls.pendingFollowers,
713
- this.store.getRequests().filter(r => r.activity && !r.bsky).map(r => r.activity)),
714
- following: wire.orderedCollection(urls.pendingFollowing,
715
- contacts.following.filter(f => !f.accepted && f.followActivity)
716
- .map(f => f.followActivity).reverse()),
717
- });
718
- }
719
-
720
- // The gateway policy: a small PUBLIC document a keyless inbox gateway reads
721
- // to decide, at the edge, what to forward and what to drop. It carries only
722
- // public facts — the actor/followers URLs, the REAL pod inbox to forward to,
723
- // the accepted-following list (already public) and a mirror of the blocklist.
724
- // Publishing the blocklist here makes it public; that is a deliberate part of
725
- // running a gateway, surfaced to the operator in the admin UI.
726
- async publishGatewayPolicy() {
727
- const { urls } = this;
728
- const contacts = this.store.getContacts();
729
- const bl = this.store.getBlocklist();
730
- await podPolicy.write(this.remote, urls, {
731
- v: 1,
732
- actorUrl: urls.actor,
733
- followersUrl: urls.followers,
734
- followingUrl: urls.following,
735
- inboxUrl: urls.inbox, // the pod inbox the gateway forwards to
736
- notesPrefix: urls.notes,
737
- kind: this.config.kind || 'person',
738
- following: contacts.following.filter(f => f.accepted && !f.bsky).map(f => f.actor),
739
- blocklist: { domains: bl.domains || [], actors: bl.actors || [] },
740
- });
741
- }
742
-
743
- // FEP-c648: the blocked actors, as an owner-only collection. Actors only —
744
- // domain blocks are ours, and the FEP does not carry them.
745
- async publishBlocked() {
746
- if (await this.privateReady() !== true) return;
747
- const { urls } = this;
748
- const actors = [...(this.store.getBlocklist().actors || [])].reverse();
749
- await podPrivate.writeBlocked(this.remote, urls, wire.orderedCollection(urls.blocked, actors));
750
- }
751
-
752
- // The outbox is the public record of everything this actor has said, boosts
753
- // included. A Create goes in as its note id, which dereferences; an Announce
754
- // has only a fragment id, so the activity itself goes in the collection —
755
- // legal AS2, and what Mastodon serves.
756
- async recordOutbox(item) {
757
- const outbox = this.store.read('outbox.json', []);
758
- outbox.unshift(item);
759
- this.store.write('outbox.json', outbox);
760
- await this.publishOutbox(outbox);
761
- }
762
-
763
- // Taking something out of the outbox is a DECISION — a post deleted, a boost
764
- // undone. It leaves a mark for the same reason dropFollower does: the pod's
765
- // copy is rewritten right after, but a rewrite that fails would otherwise let
766
- // the next reconcile put the entry back. Bounded, like the follower one.
767
- async unrecordOutbox(matches) {
768
- const before = this.store.read('outbox.json', []);
769
- const outbox = before.filter(i => !matches(i));
770
- const gone = before.filter(i => matches(i))
771
- .map(i => (typeof i === 'string' ? i : i?.id)).filter(Boolean);
772
- if (gone.length) {
773
- const marks = this.store.read('outbox-removed.json', []).filter(r => !gone.includes(r.id));
774
- const at = new Date().toISOString();
775
- this.store.write('outbox-removed.json',
776
- [...marks, ...gone.map(id => ({ id, at }))].slice(-500));
777
- }
778
- this.store.write('outbox.json', outbox);
779
- await this.publishOutbox(outbox);
780
- }
781
-
782
- // Media container on the remote pod — public-Read like notes, created lazily
783
- // at first upload (idempotent; the flag only saves round-trips).
784
- // Ask before writing. `_mediaReady` is per PROCESS, and a service worker is
785
- // restarted whenever the browser feels like it — so on the browser build this
786
- // re-created a container that has existed since sign-up, and rewrote its ACL,
787
- // on every restart. A HEAD is one request and usually the only one.
788
- async ensureMediaContainer() {
789
- if (this._mediaReady) return;
790
- if (await containers.exists(this.remote, this.urls.media)) { this._mediaReady = true; return; }
791
- await containers.provisionPublic(this.remote, this.urls.media);
792
- this._mediaReady = true;
793
- }
794
-
795
- // Whether the canary this class writes is already there. Only a definite
796
- // "yes" counts: anything else falls through to the write, which is
797
- // idempotent anyway, so a failed probe costs a request and never correctness.
798
- _containerExists(base) { return containers.exists(this.remote, base); }
799
-
800
- // The owner-only container that followers-only and direct posts live in.
801
- // Its ACL is set once; every note under it inherits.
802
- async ensurePrivateContainer() {
803
- if (this._privateContainer) return;
804
- // Same reasoning as ensureMediaContainer: probe before provisioning, so a
805
- // worker restart is not a re-provision. The ACL is NOT skipped on the
806
- // strength of the container existing alone — ensurePrivateAcls re-checks
807
- // that separately on every connect, which is the control that matters here.
808
- if (await containers.exists(this.remote, this.urls.privateNotes)) { this._privateContainer = true; return; }
809
- await containers.provisionOwnerOnly(this.remote, this.urls.privateNotes);
810
- this._privateContainer = true;
811
- }
812
-
813
- // Whether the pod actually enforces that ACL: a bare, unauthenticated read
814
- // of the private container's canary must be refused. Returns true, or the
815
- // reason private posts stay off. A definite answer is cached for the run; a
816
- // network failure is not, so a pod that was briefly unreachable is asked
817
- // again rather than refused forever.
818
- async privateReady() {
819
- if (this._privateVerdict !== undefined) return this._privateVerdict;
820
- try {
821
- await this.ensurePrivateContainer();
822
- // The probe is credential-free and bypasses RemotePod's url map, so a
823
- // fronted identity must map the advertised private container back to the
824
- // pod itself — the ACL we are testing is the pod's.
825
- const pn = this.urls.toPod ? this.urls.toPod(this.urls.privateNotes) : this.urls.privateNotes;
826
- const verdict = await containers.probePrivateEnforcement(this.probeFetch || fetch, pn + '.keep');
827
- this._privateVerdict = verdict === true ? true : `${verdict} — private posts stay off it`;
828
- return this._privateVerdict;
829
- } catch (e) {
830
- return `could not verify that the pod protects private posts (${e.message})`;
831
- }
832
- }
833
-
834
- // Compose → wire note on remote pod + RDF truth locally + deliver Create.
835
- // Who this text mentions, resolved. A mention nobody can resolve stays plain
836
- // text rather than failing the post. The text itself decides: trim a handle
837
- // out and that person is not notified, which is what every fediverse client
838
- // leads people to expect. A Group named in the parent is the one thing
839
- // carried forward regardless — drop it and the group stops carrying the
840
- // thread.
841
- async _mentionsFor(content, inReplyTo) {
842
- const inText = new Set(wire.mentionsIn(content));
843
- const carried = inReplyTo
844
- ? (this.store.getStatuses().find(s => s.noteId === inReplyTo)?.mentions || [])
845
- .map(m => String(m.name || '').replace(/^@/, '')).filter(Boolean)
846
- : [];
847
- const mentions = [];
848
- for (const handle of [...new Set([...inText, ...carried])]) {
849
- if (!this.resolveMention) break;
850
- const doc = await this.resolveMention(handle).catch(() => null);
851
- if (!doc?.id) { this.log(`mention @${handle} did not resolve — left as text`); continue; }
852
- if (!inText.has(handle) && doc.type !== 'Group') continue; // author trimmed them out
853
- mentions.push({ handle, actor: doc.id, page: doc.url || null, inbox: doc.endpoints?.sharedInbox || doc.inbox });
854
- }
855
- return mentions;
856
- }
857
-
858
- async publishNote(content, { inReplyTo, attachments, visibility = 'public', spoilerText = null } = {}) {
859
- const { urls } = this;
860
- const priv = visibility === 'private' || visibility === 'direct';
861
- if (priv) {
862
- const ready = await this.privateReady();
863
- if (ready !== true) throw new Error(ready);
864
- }
865
- const published = new Date().toISOString();
866
- const slug = published.slice(0, 10) + '-' + crypto.randomBytes(4).toString('hex');
867
- const mentions = await this._mentionsFor(content, inReplyTo);
868
- const note = wire.noteDoc({ urls, slug, content, published, inReplyTo, attachments, mentions,
869
- visibility, summary: spoilerText, container: priv ? urls.privateNotes : urls.notes });
870
-
871
- await podNotes.write(this.remote, note.id, note);
872
- // Empty, but present: a dangling `replies` that 404s is worse than none.
873
- await podNotes.writeEmptyReplies(this.remote, wire.repliesId(note.id),
874
- wire.collection(wire.repliesId(note.id), []));
875
- // The outbox is the PUBLIC index; a private or direct post is not in it.
876
- if (!priv) await this.recordOutbox(note.id);
877
-
878
- this.store.addStatus({
879
- noteId: note.id, actor: urls.actor, content: note.content, published, inReplyTo,
880
- kind: 'post', slug, text: content, visibility,
881
- ...(spoilerText ? { spoiler: spoilerText } : {}),
882
- ...(attachments?.length ? { attachments } : {}),
883
- ...(note.tag?.length ? { mentions: note.tag.map(t => ({ href: t.href, name: t.name })) } : {}),
884
- });
885
-
886
- const create = wire.createActivity(note, urls);
887
- // Published as its own document: a group that carries this post wraps the
888
- // whole activity, and the receiving server resolves it by fetching this id.
889
- // It inherits the notes container's public-Read acl:default.
890
- await podNotes.writeCreate(this.remote, create.id, create);
891
- const contacts = this.store.getContacts();
892
- // A direct post goes to the people it names and to nobody else.
893
- const inboxes = [...new Set([
894
- ...(visibility === 'direct' ? [] : contacts.followers.map(f => f.sharedInbox || f.inbox)),
895
- ...mentions.map(m => m.inbox),
896
- ].filter(Boolean))];
897
- await this.deliverer.deliverToAll(inboxes, create);
898
- this.log(`note published: ${note.id} → ${inboxes.length} inbox(es)`);
899
-
900
- // Bluesky mirror: PUBLIC posts only — unlisted, followers-only and direct
901
- // are never carried off the fediverse. A mirror failure never fails the
902
- // post; it is logged and recorded on the status for the admin page.
903
- if (visibility === 'public' && this.atproto?.connected()
904
- && this.store.getConfig()?.atproto?.crossPost) {
905
- try {
906
- const mirror = await this.atproto.crossPost(
907
- { text: content, published, attachments }, { noteUrl: note.id });
908
- this.store.updateStatus(note.id, { atproto: mirror });
909
- this.log(`cross-posted to bluesky: ${mirror.uri}${mirror.truncated ? ' (truncated, links back)' : ''}`);
910
- } catch (e) {
911
- this.store.updateStatus(note.id, { atproto: { error: e.message } });
912
- this.log(`bluesky cross-post failed: ${e.message}`);
913
- }
914
- }
915
- return note;
916
- }
917
-
918
- // --- polls ---------------------------------------------------------------
919
- //
920
- // A poll is published as a Question and answered by ordinary replies naming
921
- // an option, so the count is ours to keep and ours to republish. The roster
922
- // of who chose what lives in agent state (polls.VOTES_DOC); the tallies on
923
- // the status row and in the pod document are always derived from it.
924
-
925
- /**
926
- * Publish a poll. `options` is a list of choice titles, `multiple` lets a
927
- * voter pick more than one, and `expiresAt` is when voting stops.
928
- */
929
- async publishQuestion(content, { options = [], multiple = false, expiresAt = null,
930
- inReplyTo = undefined, visibility = 'public', spoilerText = null } = {}) {
931
- const { urls } = this;
932
- const priv = visibility === 'private' || visibility === 'direct';
933
- if (priv) {
934
- const ready = await this.privateReady();
935
- if (ready !== true) throw new Error(ready);
936
- }
937
- const titles = [].concat(options).map(o => String(o ?? '').trim()).filter(Boolean);
938
- if (titles.length < 2) throw new Error('a poll needs at least two options');
939
- // Options are matched BY NAME when a vote arrives — that is the whole of
940
- // the convention — so two options reading the same are one option that
941
- // cannot be told apart.
942
- if (new Set(titles).size !== titles.length) throw new Error('a poll’s options must differ from one another');
943
- // Bounded here as well as at the client API, because the outbox is a
944
- // second way in and an unbounded poll is a document a stranger sizes.
945
- if (titles.length > polls.MAX_OPTIONS) throw new Error(`a poll takes at most ${polls.MAX_OPTIONS} options`);
946
- if (titles.some(t => t.length > polls.MAX_OPTION_CHARS)) {
947
- throw new Error(`a poll option is at most ${polls.MAX_OPTION_CHARS} characters`);
948
- }
949
- if (expiresAt) {
950
- const ends = Date.parse(expiresAt);
951
- if (!Number.isFinite(ends)) throw new Error('the closing time is not a date');
952
- const seconds = (ends - Date.now()) / 1000;
953
- if (seconds < polls.MIN_SECONDS || seconds > polls.MAX_SECONDS) {
954
- throw new Error(`a poll runs between ${polls.MIN_SECONDS} and ${polls.MAX_SECONDS} seconds`);
955
- }
956
- }
957
-
958
- const published = new Date().toISOString();
959
- const slug = published.slice(0, 10) + '-' + crypto.randomBytes(4).toString('hex');
960
- const mentions = await this._mentionsFor(content, inReplyTo);
961
- const poll = {
962
- multiple: !!multiple,
963
- expiresAt: expiresAt || null,
964
- closed: null,
965
- options: titles.map(title => ({ title, votes: 0 })),
966
- votersCount: 0,
967
- // Resolved once, here: a tally rewrite must not cost a webfinger lookup
968
- // per vote for people the poll named.
969
- mentionInboxes: [...new Set(mentions.map(m => m.inbox).filter(Boolean))],
970
- };
971
- const question = wire.questionDoc({
972
- urls, slug, content, published, inReplyTo, attachments: [], mentions,
973
- visibility, summary: spoilerText,
974
- container: priv ? urls.privateNotes : urls.notes,
975
- options: poll.options, multiple: poll.multiple, endTime: poll.expiresAt, votersCount: 0,
976
- });
977
-
978
- await podNotes.write(this.remote, question.id, question);
979
- // Empty, but present: a dangling `replies` that 404s is worse than none.
980
- await podNotes.writeEmptyReplies(this.remote, wire.repliesId(question.id),
981
- wire.collection(wire.repliesId(question.id), []));
982
- if (!priv) await this.recordOutbox(question.id);
983
- this.store.addStatus({
984
- noteId: question.id, actor: urls.actor, content: question.content, published,
985
- kind: 'post', slug, text: content, visibility, poll, inReplyTo,
986
- ...(spoilerText ? { spoiler: spoilerText } : {}),
987
- ...(question.tag?.length ? { mentions: question.tag.map(t => ({ href: t.href, name: t.name })) } : {}),
988
- });
989
-
990
- const create = this._pollActivity('Create', question, wire.createActivityId(question.id));
991
- await podNotes.writeCreate(this.remote, create.id, create);
992
- const contacts = this.store.getContacts();
993
- const inboxes = [...new Set([
994
- ...(visibility === 'direct' ? [] : contacts.followers.map(f => f.sharedInbox || f.inbox)),
995
- ...poll.mentionInboxes,
996
- ].filter(Boolean))];
997
- await this.deliverer.deliverToAll(inboxes, create);
998
- this.log(`poll published: ${question.id} (${titles.length} options) → ${inboxes.length} inbox(es)`);
999
- return question;
1000
- }
1001
-
1002
- // The Create or Update carrying a Question. The context is hoisted onto the
1003
- // activity and the embedded object keeps none: a nested @context is legal
1004
- // JSON-LD but not every server reads one, and votersCount is declared there.
1005
- _pollActivity(type, question, id) {
1006
- const { '@context': ctx, ...object } = question;
1007
- return {
1008
- '@context': ctx, id, type,
1009
- actor: this.urls.actor,
1010
- published: question.published,
1011
- to: question.to, cc: question.cc,
1012
- object,
1013
- };
1014
- }
1015
-
1016
- /**
1017
- * One vote on one of OUR polls, named by the option's title. Returns true
1018
- * when it counted — a second answer to a single-choice poll, an option we do
1019
- * not offer, or a poll that has closed all count for nothing.
1020
- */
1021
- async recordVote(questionId, actor, optionName) {
1022
- const s = this.store.getStatuses().find(x => x.noteId === questionId);
1023
- if (!s?.poll || s.kind !== 'post' || s.actor !== this.urls.actor) return false;
1024
- if (polls.pollClosed(s.poll)) return false;
1025
- const index = polls.optionIndex(s.poll, optionName);
1026
- if (index < 0) return false;
1027
- const all = this.store.read(polls.VOTES_DOC, {});
1028
- const { roster, changed } = polls.addVote(all[questionId] || {}, polls.voterKey(actor), index,
1029
- { multiple: !!s.poll.multiple });
1030
- if (!changed) return false;
1031
- this.store.write(polls.VOTES_DOC, { ...all, [questionId]: roster });
1032
- this.store.updateStatus(questionId, { poll: polls.withTally(s.poll, roster) });
1033
- this._pollDirty(questionId);
1034
- return true;
1035
- }
1036
-
1037
- // Open the rewrite window for a poll whose count moved. Already open is
1038
- // already enough: the whole point is that a burst costs one rewrite.
1039
- _pollDirty(questionId) {
1040
- if (this.pollTimers.has(questionId)) return;
1041
- const t = setTimeout(() => {
1042
- this.pollTimers.delete(questionId);
1043
- this.republishPoll(questionId).catch(e => this.log(`poll rewrite: ${e.message}`));
1044
- }, POLL_REWRITE_MS);
1045
- t.unref?.();
1046
- this.pollTimers.set(questionId, t);
1047
- }
1048
-
1049
- /**
1050
- * Write the poll's current count back to the pod and tell everyone who has
1051
- * it. `closing` stamps it shut, which is a one-way door.
1052
- */
1053
- async republishPoll(questionId, { closing = null } = {}) {
1054
- const s = this.store.getStatuses().find(x => x.noteId === questionId);
1055
- if (!s?.poll) return null;
1056
- const { urls } = this;
1057
- const roster = this.store.read(polls.VOTES_DOC, {})[questionId] || {};
1058
- // A shut poll whose roster has been retired keeps the counts on its row:
1059
- // deriving them from an empty roster would publish a poll nobody voted in.
1060
- const shut = closing || s.poll.closed;
1061
- const counted = shut && !Object.keys(roster).length ? s.poll : polls.withTally(s.poll, roster);
1062
- const poll = { ...counted, ...(closing ? { closed: closing } : {}) };
1063
- const container = String(s.noteId).startsWith(urls.privateNotes) ? urls.privateNotes : urls.notes;
1064
- const slug = s.slug || String(s.noteId).slice(container.length);
1065
- const mentions = (s.mentions || []).map(m => ({
1066
- handle: String(m.name || '').replace(/^@/, ''), actor: m.href, page: null, inbox: null,
1067
- }));
1068
- const question = wire.questionDoc({
1069
- urls, slug, content: s.text ?? '', published: s.published, inReplyTo: s.inReplyTo,
1070
- attachments: [], mentions,
1071
- visibility: s.visibility || 'public', summary: s.spoiler || null, container,
1072
- options: poll.options, multiple: !!poll.multiple, endTime: poll.expiresAt,
1073
- closed: poll.closed, votersCount: poll.votersCount || 0,
1074
- });
1075
- await podNotes.write(this.remote, question.id, question);
1076
- // The Create is overwritten too, so a group's Announce resolves to the
1077
- // current count rather than to the one the poll opened with.
1078
- await podNotes.writeCreate(this.remote, wire.createActivityId(question.id),
1079
- this._pollActivity('Create', question, wire.createActivityId(question.id)));
1080
- this.store.updateStatus(questionId, { poll });
1081
-
1082
- // Not `updated`: a changed count is not an edit, and stamping one would
1083
- // have every client show the poll as edited each time somebody voted.
1084
- //
1085
- // The Update is named after the STATE it carries rather than the moment it
1086
- // was sent. A clock only tells two rewrites apart when they fall in
1087
- // different milliseconds, and a receiving server that has seen an activity
1088
- // id drops the next one wearing it — which would quietly freeze the count.
1089
- // Naming the state means an id changes exactly when there is something new
1090
- // to say, and two sends of the same numbers are the duplicate they look
1091
- // like.
1092
- const stamp = crypto.createHash('sha256').update(JSON.stringify([
1093
- poll.options.map(o => o.votes || 0), poll.votersCount || 0, poll.closed || '',
1094
- ])).digest('hex').slice(0, 12);
1095
- const update = this._pollActivity('Update', question, `${question.id}#poll-${stamp}`);
1096
- const contacts = this.store.getContacts();
1097
- const inboxes = [...new Set([
1098
- ...(s.visibility === 'direct' ? [] : contacts.followers.map(f => f.sharedInbox || f.inbox)),
1099
- ...(s.poll.mentionInboxes || []),
1100
- ].filter(Boolean))];
1101
- await this.deliverer.deliverToAll(inboxes, update);
1102
- this.log(`poll ${closing ? 'closed' : 'count published'}: ${question.id} → ${inboxes.length} inbox(es)`);
1103
- return poll;
1104
- }
1105
-
1106
- /**
1107
- * Shut any poll whose time is up. Called from the agent's sweep. Closing is
1108
- * recorded BEFORE the republish, so a failed republish cannot leave a poll
1109
- * open and collecting votes it has already refused.
1110
- */
1111
- async closeDuePolls(now = Date.now()) {
1112
- const due = this.store.getStatuses().filter(s => s.kind === 'post' && s.poll
1113
- && !s.poll.closed && s.poll.expiresAt && Date.parse(s.poll.expiresAt) <= now);
1114
- for (const s of due) {
1115
- const closed = new Date(Math.min(now, Date.parse(s.poll.expiresAt) || now)).toISOString();
1116
- clearTimeout(this.pollTimers.get(s.noteId));
1117
- this.pollTimers.delete(s.noteId);
1118
- this.store.updateStatus(s.noteId, { poll: { ...s.poll, closed } });
1119
- const done = await this.republishPoll(s.noteId, { closing: closed })
1120
- .catch(e => { this.log(`poll close ${s.noteId}: ${e.message}`); return null; });
1121
- // The roster only ever answered one question — has this person already
1122
- // voted — and a shut poll has stopped asking it. Dropping it keeps a
1123
- // document we serialize whole from carrying every poll's voters forever.
1124
- if (done) {
1125
- const all = this.store.read(polls.VOTES_DOC, {});
1126
- if (all[s.noteId]) {
1127
- delete all[s.noteId];
1128
- this.store.write(polls.VOTES_DOC, all);
1129
- }
1130
- }
1131
- }
1132
- return due.length;
1133
- }
1134
-
1135
- /** Stop the pending rewrite windows. Called at shutdown. */
1136
- stopPolls() {
1137
- for (const t of this.pollTimers.values()) clearTimeout(t);
1138
- this.pollTimers.clear();
1139
- }
1140
-
1141
- // The pinned posts, as the actor's featured collection — the one document a
1142
- // remote server reads when it shows this profile's pins.
1143
- async publishFeatured() {
1144
- const ids = this.store.getStatuses().filter(s => s.kind === 'post' && s.pinned).map(s => s.noteId);
1145
- await podFeatured.write(this.remote, this.urls, wire.orderedCollection(this.urls.featured, ids));
1146
- return ids.length;
1147
- }
1148
-
1149
- // An edit keeps the note's id, slug and published time; `updated` is the
1150
- // edit's own stamp. The pod documents are overwritten in place — the Create
1151
- // too, so a group's Announce resolves to the edited text — and an Update
1152
- // goes everywhere the Create went.
1153
- async updateNote(s, { content, spoilerText = null, attachments = null } = {}) {
1154
- const { urls } = this;
1155
- const updated = new Date().toISOString();
1156
- const inText = new Set(wire.mentionsIn(content));
1157
- const mentions = [];
1158
- for (const handle of inText) {
1159
- if (!this.resolveMention) break;
1160
- const doc = await this.resolveMention(handle).catch(() => null);
1161
- if (!doc?.id) { this.log(`mention @${handle} did not resolve — left as text`); continue; }
1162
- mentions.push({ handle, actor: doc.id, page: doc.url || null, inbox: doc.endpoints?.sharedInbox || doc.inbox });
1163
- }
1164
- const atts = attachments ?? s.attachments ?? [];
1165
- // The note stays in the container its visibility put it in; a recovered
1166
- // post may carry no slug, but the note id already contains it.
1167
- const container = String(s.noteId).startsWith(urls.privateNotes) ? urls.privateNotes : urls.notes;
1168
- const slug = s.slug || String(s.noteId).slice(container.length);
1169
- const note = wire.noteDoc({
1170
- urls, slug, content, published: s.published, inReplyTo: s.inReplyTo,
1171
- attachments: atts, mentions, visibility: s.visibility || 'public',
1172
- summary: spoilerText, updated, container,
1173
- });
1174
- await podNotes.write(this.remote, note.id, note);
1175
- await podNotes.writeCreate(this.remote, wire.createActivityId(note.id), wire.createActivity(note, urls));
1176
- const patched = this.store.updateStatus(s.noteId, {
1177
- content: note.content, text: content, editedAt: updated,
1178
- spoiler: spoilerText || undefined,
1179
- attachments: atts.length ? atts : undefined,
1180
- mentions: note.tag?.length ? note.tag.map(t => ({ href: t.href, name: t.name })) : undefined,
1181
- });
1182
- const update = wire.updateActivity(note, urls);
1183
- const contacts = this.store.getContacts();
1184
- const inboxes = [...new Set([
1185
- ...(s.visibility === 'direct' ? [] : contacts.followers.map(f => f.sharedInbox || f.inbox)),
1186
- ...mentions.map(m => m.inbox),
1187
- ].filter(Boolean))];
1188
- await this.deliverer.deliverToAll(inboxes, update);
1189
- this.log(`note edited: ${note.id} → ${inboxes.length} inbox(es)`);
1190
- return patched;
1191
- }
1192
- }