@fedify/relay 2.5.0-dev.2687 → 2.5.0-dev.2726
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.
- package/package.json +20 -7
- package/dist/factory.test.d.ts +0 -2
- package/dist/factory.test.js +0 -21
- package/dist/litepub.test.d.ts +0 -2
- package/dist/litepub.test.js +0 -891
- package/dist/mastodon.test.d.ts +0 -2
- package/dist/mastodon.test.js +0 -710
- package/dist/rolldown-runtime-BoYiifaO.js +0 -29
- package/dist/types-Bg90GFuF.js +0 -27762
- package/dist/undici-7BbNHW-y.js +0 -22899
package/dist/litepub.test.js
DELETED
|
@@ -1,891 +0,0 @@
|
|
|
1
|
-
import "temporal-polyfill";
|
|
2
|
-
import "urlpattern-polyfill";
|
|
3
|
-
globalThis.addEventListener = () => {};
|
|
4
|
-
import { n as exportSpki, r as getDocumentLoader, t as isRelayFollowerData } from "./types-Bg90GFuF.js";
|
|
5
|
-
import { MemoryKvStore, signRequest } from "@fedify/fedify";
|
|
6
|
-
import { createRelay } from "@fedify/relay";
|
|
7
|
-
import { deepStrictEqual, ok, strictEqual } from "node:assert";
|
|
8
|
-
import test, { describe } from "node:test";
|
|
9
|
-
import { Accept, Announce, Create, Delete, Follow, Move, Note, Person, Undo, Update } from "@fedify/vocab";
|
|
10
|
-
//#region src/litepub.test.ts
|
|
11
|
-
const mockDocumentLoader = async (url) => {
|
|
12
|
-
if (url === "https://remote.example.com/users/alice" || url === "https://remote.example.com/users/alice#main-key") return {
|
|
13
|
-
contextUrl: null,
|
|
14
|
-
documentUrl: url.replace(/#main-key$/, ""),
|
|
15
|
-
document: {
|
|
16
|
-
"@context": ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/v1"],
|
|
17
|
-
id: url,
|
|
18
|
-
type: "Person",
|
|
19
|
-
preferredUsername: "alice",
|
|
20
|
-
inbox: aliceInbox,
|
|
21
|
-
publicKey: {
|
|
22
|
-
id: "https://remote.example.com/users/alice#main-key",
|
|
23
|
-
owner: url.replace(/#main-key$/, ""),
|
|
24
|
-
publicKeyPem: await exportSpki(rsaKeyPair.publicKey)
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
else if (url === "https://remote.example.com/users/dave" || url === "https://remote.example.com/users/dave#main-key") return {
|
|
29
|
-
contextUrl: null,
|
|
30
|
-
documentUrl: url.replace(/#main-key$/, ""),
|
|
31
|
-
document: {
|
|
32
|
-
"@context": ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/v1"],
|
|
33
|
-
id: url,
|
|
34
|
-
type: "Person",
|
|
35
|
-
preferredUsername: "dave",
|
|
36
|
-
inbox: daveInbox,
|
|
37
|
-
publicKey: {
|
|
38
|
-
id: "https://remote.example.com/users/dave#main-key",
|
|
39
|
-
owner: url.replace(/#main-key$/, ""),
|
|
40
|
-
publicKeyPem: await exportSpki(rsaKeyPair.publicKey)
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
else if (url === "https://remote.example.com/notes/1") return {
|
|
45
|
-
contextUrl: null,
|
|
46
|
-
documentUrl: url,
|
|
47
|
-
document: {
|
|
48
|
-
"@context": "https://www.w3.org/ns/activitystreams",
|
|
49
|
-
id: url,
|
|
50
|
-
type: "Note",
|
|
51
|
-
content: "Hello world"
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
else if (url.startsWith("https://remote.example.com/")) throw new Error(`Document not found: ${url}`);
|
|
55
|
-
return await getDocumentLoader()(url);
|
|
56
|
-
};
|
|
57
|
-
const rsaKeyPair = await crypto.subtle.generateKey({
|
|
58
|
-
name: "RSASSA-PKCS1-v1_5",
|
|
59
|
-
modulusLength: 2048,
|
|
60
|
-
publicExponent: new Uint8Array([
|
|
61
|
-
1,
|
|
62
|
-
0,
|
|
63
|
-
1
|
|
64
|
-
]),
|
|
65
|
-
hash: "SHA-256"
|
|
66
|
-
}, true, ["sign", "verify"]);
|
|
67
|
-
const rsaPublicKey = {
|
|
68
|
-
id: new URL("https://remote.example.com/users/alice#main-key"),
|
|
69
|
-
...rsaKeyPair.publicKey
|
|
70
|
-
};
|
|
71
|
-
const davePublicKey = {
|
|
72
|
-
id: new URL("https://remote.example.com/users/dave#main-key"),
|
|
73
|
-
...rsaKeyPair.publicKey
|
|
74
|
-
};
|
|
75
|
-
const INBOX_PREFIX = "https://[2001:db8:1::";
|
|
76
|
-
const aliceInbox = `${INBOX_PREFIX}4]/users/alice/inbox`;
|
|
77
|
-
const daveInbox = `${INBOX_PREFIX}1]/users/dave/inbox`;
|
|
78
|
-
const pendingInbox = `${INBOX_PREFIX}2]/users/bob/inbox`;
|
|
79
|
-
const acceptedInbox = `${INBOX_PREFIX}3]/users/carol/inbox`;
|
|
80
|
-
const recorders = /* @__PURE__ */ new Map();
|
|
81
|
-
function recordInbox(inbox) {
|
|
82
|
-
const recorded = [];
|
|
83
|
-
recorders.set(inbox, recorded);
|
|
84
|
-
return recorded;
|
|
85
|
-
}
|
|
86
|
-
const aliceDeliveries = recordInbox(aliceInbox);
|
|
87
|
-
function assertAcceptDelivered(follow, deliveries = aliceDeliveries) {
|
|
88
|
-
const accepts = deliveries.filter(({ body }) => body.type === "Accept" && body.object?.id === follow.id?.href);
|
|
89
|
-
strictEqual(accepts.length, 1, "Expected exactly one Accept for this Follow");
|
|
90
|
-
strictEqual(accepts[0].method, "POST");
|
|
91
|
-
strictEqual(accepts[0].body.actor, "https://relay.example.com/users/relay");
|
|
92
|
-
strictEqual(accepts[0].body.object.type, "Follow");
|
|
93
|
-
strictEqual(accepts[0].body.object.actor.id, follow.actorId?.href);
|
|
94
|
-
}
|
|
95
|
-
const nextFetch = globalThis.fetch;
|
|
96
|
-
globalThis.fetch = (async (input, init) => {
|
|
97
|
-
const request = input instanceof Request ? input : new Request(input, init);
|
|
98
|
-
if (!request.url.startsWith(INBOX_PREFIX)) return await nextFetch(input, init);
|
|
99
|
-
const recorded = recorders.get(request.url);
|
|
100
|
-
if (recorded != null) recorded.push({
|
|
101
|
-
method: request.method,
|
|
102
|
-
body: await request.json()
|
|
103
|
-
});
|
|
104
|
-
return new Response(null, { status: 202 });
|
|
105
|
-
});
|
|
106
|
-
describe("LitePubRelay", () => {
|
|
107
|
-
test("constructor with required options", () => {
|
|
108
|
-
ok(createRelay("litepub", {
|
|
109
|
-
kv: new MemoryKvStore(),
|
|
110
|
-
origin: "https://relay.example.com",
|
|
111
|
-
documentLoaderFactory: () => mockDocumentLoader,
|
|
112
|
-
subscriptionHandler: async (_ctx, _actor) => {
|
|
113
|
-
return await Promise.resolve(true);
|
|
114
|
-
}
|
|
115
|
-
}));
|
|
116
|
-
});
|
|
117
|
-
test("fetch method returns Response", async () => {
|
|
118
|
-
const relay = createRelay("litepub", {
|
|
119
|
-
kv: new MemoryKvStore(),
|
|
120
|
-
origin: "https://relay.example.com",
|
|
121
|
-
documentLoaderFactory: () => mockDocumentLoader,
|
|
122
|
-
subscriptionHandler: () => Promise.resolve(true)
|
|
123
|
-
});
|
|
124
|
-
const request = new Request("https://relay.example.com/users/relay", { headers: { "Accept": "application/activity+json" } });
|
|
125
|
-
ok(await relay.fetch(request) instanceof Response);
|
|
126
|
-
});
|
|
127
|
-
test("fetching relay actor returns Application", async () => {
|
|
128
|
-
const relay = createRelay("litepub", {
|
|
129
|
-
kv: new MemoryKvStore(),
|
|
130
|
-
origin: "https://relay.example.com",
|
|
131
|
-
documentLoaderFactory: () => mockDocumentLoader,
|
|
132
|
-
subscriptionHandler: () => Promise.resolve(true)
|
|
133
|
-
});
|
|
134
|
-
const request = new Request("https://relay.example.com/users/relay", { headers: { "Accept": "application/activity+json" } });
|
|
135
|
-
const response = await relay.fetch(request);
|
|
136
|
-
strictEqual(response.status, 200);
|
|
137
|
-
const json = await response.json();
|
|
138
|
-
strictEqual(json.type, "Application");
|
|
139
|
-
strictEqual(json.preferredUsername, "relay");
|
|
140
|
-
strictEqual(json.name, "ActivityPub Relay");
|
|
141
|
-
});
|
|
142
|
-
test("fetching non-relay actor returns 404", async () => {
|
|
143
|
-
const relay = createRelay("litepub", {
|
|
144
|
-
kv: new MemoryKvStore(),
|
|
145
|
-
origin: "https://relay.example.com",
|
|
146
|
-
documentLoaderFactory: () => mockDocumentLoader,
|
|
147
|
-
subscriptionHandler: () => Promise.resolve(true)
|
|
148
|
-
});
|
|
149
|
-
const request = new Request("https://relay.example.com/users/non-existent", { headers: { "Accept": "application/activity+json" } });
|
|
150
|
-
strictEqual((await relay.fetch(request)).status, 404);
|
|
151
|
-
});
|
|
152
|
-
test("followers collection returns empty list initially", async () => {
|
|
153
|
-
const relay = createRelay("litepub", {
|
|
154
|
-
kv: new MemoryKvStore(),
|
|
155
|
-
origin: "https://relay.example.com",
|
|
156
|
-
documentLoaderFactory: () => mockDocumentLoader,
|
|
157
|
-
subscriptionHandler: () => Promise.resolve(true)
|
|
158
|
-
});
|
|
159
|
-
const request = new Request("https://relay.example.com/users/relay/followers", { headers: { "Accept": "application/activity+json" } });
|
|
160
|
-
const response = await relay.fetch(request);
|
|
161
|
-
strictEqual(response.status, 200);
|
|
162
|
-
const json = await response.json();
|
|
163
|
-
ok(json);
|
|
164
|
-
ok(json.type === "Collection" || json.type === "OrderedCollection");
|
|
165
|
-
});
|
|
166
|
-
test("followers collection returns populated list", async () => {
|
|
167
|
-
const kv = new MemoryKvStore();
|
|
168
|
-
const follower1 = new Person({
|
|
169
|
-
id: new URL("https://remote1.example.com/users/alice"),
|
|
170
|
-
preferredUsername: "alice",
|
|
171
|
-
inbox: new URL("https://remote1.example.com/users/alice/inbox")
|
|
172
|
-
});
|
|
173
|
-
const follower2 = new Person({
|
|
174
|
-
id: new URL("https://remote2.example.com/users/bob"),
|
|
175
|
-
preferredUsername: "bob",
|
|
176
|
-
inbox: new URL("https://remote2.example.com/users/bob/inbox")
|
|
177
|
-
});
|
|
178
|
-
const follower1Id = "https://remote1.example.com/users/alice";
|
|
179
|
-
const follower2Id = "https://remote2.example.com/users/bob";
|
|
180
|
-
await kv.set(["follower", follower1Id], {
|
|
181
|
-
actor: await follower1.toJsonLd(),
|
|
182
|
-
state: "accepted"
|
|
183
|
-
});
|
|
184
|
-
await kv.set(["follower", follower2Id], {
|
|
185
|
-
actor: await follower2.toJsonLd(),
|
|
186
|
-
state: "accepted"
|
|
187
|
-
});
|
|
188
|
-
const relay = createRelay("litepub", {
|
|
189
|
-
kv,
|
|
190
|
-
origin: "https://relay.example.com",
|
|
191
|
-
documentLoaderFactory: () => mockDocumentLoader,
|
|
192
|
-
subscriptionHandler: () => Promise.resolve(true)
|
|
193
|
-
});
|
|
194
|
-
const request = new Request("https://relay.example.com/users/relay/followers", { headers: { "Accept": "application/activity+json" } });
|
|
195
|
-
const response = await relay.fetch(request);
|
|
196
|
-
strictEqual(response.status, 200);
|
|
197
|
-
const json = await response.json();
|
|
198
|
-
ok(json);
|
|
199
|
-
ok(json.type === "Collection" || json.type === "OrderedCollection");
|
|
200
|
-
if (json.totalItems !== void 0) strictEqual(json.totalItems, 2);
|
|
201
|
-
});
|
|
202
|
-
test("relay actor has correct properties", async () => {
|
|
203
|
-
const relay = createRelay("litepub", {
|
|
204
|
-
kv: new MemoryKvStore(),
|
|
205
|
-
origin: "https://relay.example.com",
|
|
206
|
-
documentLoaderFactory: () => mockDocumentLoader,
|
|
207
|
-
subscriptionHandler: () => Promise.resolve(true)
|
|
208
|
-
});
|
|
209
|
-
const request = new Request("https://relay.example.com/users/relay", { headers: { "Accept": "application/activity+json" } });
|
|
210
|
-
const response = await relay.fetch(request);
|
|
211
|
-
strictEqual(response.status, 200);
|
|
212
|
-
const json = await response.json();
|
|
213
|
-
strictEqual(json.type, "Application");
|
|
214
|
-
strictEqual(json.preferredUsername, "relay");
|
|
215
|
-
strictEqual(json.name, "ActivityPub Relay");
|
|
216
|
-
strictEqual(json.id, "https://relay.example.com/users/relay");
|
|
217
|
-
strictEqual(json.inbox, "https://relay.example.com/inbox");
|
|
218
|
-
strictEqual(json.followers, "https://relay.example.com/users/relay/followers");
|
|
219
|
-
strictEqual(json.following, "https://relay.example.com/users/relay/following");
|
|
220
|
-
});
|
|
221
|
-
test("handles Follow activity with subscription approval", async () => {
|
|
222
|
-
const kv = new MemoryKvStore();
|
|
223
|
-
let handlerCalled = false;
|
|
224
|
-
let handlerActor = null;
|
|
225
|
-
const relay = createRelay("litepub", {
|
|
226
|
-
kv,
|
|
227
|
-
origin: "https://relay.example.com",
|
|
228
|
-
documentLoaderFactory: () => mockDocumentLoader,
|
|
229
|
-
authenticatedDocumentLoaderFactory: () => mockDocumentLoader,
|
|
230
|
-
subscriptionHandler: async (_ctx, actor) => {
|
|
231
|
-
handlerCalled = true;
|
|
232
|
-
handlerActor = actor;
|
|
233
|
-
return await Promise.resolve(true);
|
|
234
|
-
}
|
|
235
|
-
});
|
|
236
|
-
const follower = new Person({
|
|
237
|
-
id: new URL("https://remote.example.com/users/dave"),
|
|
238
|
-
preferredUsername: "dave",
|
|
239
|
-
inbox: new URL(daveInbox)
|
|
240
|
-
});
|
|
241
|
-
const followActivity = new Follow({
|
|
242
|
-
id: new URL(`https://remote.example.com/activities/follow/${crypto.randomUUID()}`),
|
|
243
|
-
actor: follower.id,
|
|
244
|
-
object: new URL("https://relay.example.com/users/relay")
|
|
245
|
-
});
|
|
246
|
-
let request = new Request("https://relay.example.com/inbox", {
|
|
247
|
-
method: "POST",
|
|
248
|
-
headers: { "Content-Type": "application/activity+json" },
|
|
249
|
-
body: JSON.stringify(await followActivity.toJsonLd({ contextLoader: mockDocumentLoader }))
|
|
250
|
-
});
|
|
251
|
-
request = await signRequest(request, rsaKeyPair.privateKey, davePublicKey.id);
|
|
252
|
-
const deliveredActivities = recordInbox(daveInbox);
|
|
253
|
-
strictEqual((await relay.fetch(request)).status, 202);
|
|
254
|
-
assertAcceptDelivered(followActivity, deliveredActivities);
|
|
255
|
-
strictEqual(handlerCalled, true);
|
|
256
|
-
ok(handlerActor);
|
|
257
|
-
const followerData = await kv.get(["follower", "https://remote.example.com/users/dave"]);
|
|
258
|
-
ok(isRelayFollowerData(followerData));
|
|
259
|
-
strictEqual(followerData.state, "pending");
|
|
260
|
-
const reciprocalFollow = deliveredActivities.map(({ body }) => body).find((activity) => activity.type === "Follow");
|
|
261
|
-
ok(reciprocalFollow, "Expected a reciprocal Follow activity");
|
|
262
|
-
strictEqual(reciprocalFollow.actor, "https://relay.example.com/users/relay");
|
|
263
|
-
strictEqual(reciprocalFollow.object, "https://remote.example.com/users/dave");
|
|
264
|
-
strictEqual(reciprocalFollow.to, "https://remote.example.com/users/dave");
|
|
265
|
-
});
|
|
266
|
-
test("handles Follow activity with subscription rejection", async () => {
|
|
267
|
-
const kv = new MemoryKvStore();
|
|
268
|
-
const relay = createRelay("litepub", {
|
|
269
|
-
kv,
|
|
270
|
-
origin: "https://relay.example.com",
|
|
271
|
-
documentLoaderFactory: () => mockDocumentLoader,
|
|
272
|
-
authenticatedDocumentLoaderFactory: () => mockDocumentLoader,
|
|
273
|
-
subscriptionHandler: async (_ctx, _actor) => {
|
|
274
|
-
return await Promise.resolve(false);
|
|
275
|
-
}
|
|
276
|
-
});
|
|
277
|
-
const follower = new Person({
|
|
278
|
-
id: new URL("https://remote.example.com/users/alice"),
|
|
279
|
-
preferredUsername: "alice",
|
|
280
|
-
inbox: new URL(aliceInbox)
|
|
281
|
-
});
|
|
282
|
-
const followActivity = new Follow({
|
|
283
|
-
id: new URL("https://remote.example.com/activities/follow/1"),
|
|
284
|
-
actor: follower.id,
|
|
285
|
-
object: new URL("https://relay.example.com/users/relay")
|
|
286
|
-
});
|
|
287
|
-
let request = new Request("https://relay.example.com/inbox", {
|
|
288
|
-
method: "POST",
|
|
289
|
-
headers: { "Content-Type": "application/activity+json" },
|
|
290
|
-
body: JSON.stringify(await followActivity.toJsonLd({ contextLoader: mockDocumentLoader }))
|
|
291
|
-
});
|
|
292
|
-
request = await signRequest(request, rsaKeyPair.privateKey, rsaPublicKey.id);
|
|
293
|
-
strictEqual((await relay.fetch(request)).status, 202);
|
|
294
|
-
strictEqual(await kv.get(["follower", "https://remote.example.com/users/alice"]), void 0);
|
|
295
|
-
});
|
|
296
|
-
test("handles public Follow activity", async () => {
|
|
297
|
-
const kv = new MemoryKvStore();
|
|
298
|
-
const relay = createRelay("litepub", {
|
|
299
|
-
kv,
|
|
300
|
-
origin: "https://relay.example.com",
|
|
301
|
-
documentLoaderFactory: () => mockDocumentLoader,
|
|
302
|
-
authenticatedDocumentLoaderFactory: () => mockDocumentLoader,
|
|
303
|
-
subscriptionHandler: async (_ctx, _actor) => await Promise.resolve(true)
|
|
304
|
-
});
|
|
305
|
-
const follower = new Person({
|
|
306
|
-
id: new URL("https://remote.example.com/users/alice"),
|
|
307
|
-
preferredUsername: "alice",
|
|
308
|
-
inbox: new URL(aliceInbox)
|
|
309
|
-
});
|
|
310
|
-
const followActivity = new Follow({
|
|
311
|
-
id: new URL(`https://remote.example.com/activities/follow/${crypto.randomUUID()}`),
|
|
312
|
-
actor: follower.id,
|
|
313
|
-
object: new URL("https://www.w3.org/ns/activitystreams#Public")
|
|
314
|
-
});
|
|
315
|
-
let request = new Request("https://relay.example.com/inbox", {
|
|
316
|
-
method: "POST",
|
|
317
|
-
headers: { "Content-Type": "application/activity+json" },
|
|
318
|
-
body: JSON.stringify(await followActivity.toJsonLd({ contextLoader: mockDocumentLoader }))
|
|
319
|
-
});
|
|
320
|
-
request = await signRequest(request, rsaKeyPair.privateKey, rsaPublicKey.id);
|
|
321
|
-
strictEqual((await relay.fetch(request)).status, 202);
|
|
322
|
-
assertAcceptDelivered(followActivity);
|
|
323
|
-
const followerData = await kv.get(["follower", "https://remote.example.com/users/alice"]);
|
|
324
|
-
ok(isRelayFollowerData(followerData));
|
|
325
|
-
strictEqual(followerData.state, "pending");
|
|
326
|
-
});
|
|
327
|
-
test("ignores Follow activity without required fields", async () => {
|
|
328
|
-
const kv = new MemoryKvStore();
|
|
329
|
-
const relay = createRelay("litepub", {
|
|
330
|
-
kv,
|
|
331
|
-
origin: "https://relay.example.com",
|
|
332
|
-
documentLoaderFactory: () => mockDocumentLoader,
|
|
333
|
-
authenticatedDocumentLoaderFactory: () => mockDocumentLoader,
|
|
334
|
-
subscriptionHandler: async (_ctx, _actor) => await Promise.resolve(true)
|
|
335
|
-
});
|
|
336
|
-
const followActivity = new Follow({
|
|
337
|
-
actor: new URL("https://remote.example.com/users/alice"),
|
|
338
|
-
object: new URL("https://relay.example.com/users/relay")
|
|
339
|
-
});
|
|
340
|
-
let request = new Request("https://relay.example.com/inbox", {
|
|
341
|
-
method: "POST",
|
|
342
|
-
headers: { "Content-Type": "application/activity+json" },
|
|
343
|
-
body: JSON.stringify(await followActivity.toJsonLd({ contextLoader: mockDocumentLoader }))
|
|
344
|
-
});
|
|
345
|
-
request = await signRequest(request, rsaKeyPair.privateKey, rsaPublicKey.id);
|
|
346
|
-
strictEqual((await relay.fetch(request)).status, 202);
|
|
347
|
-
strictEqual(await kv.get(["follower", "https://remote.example.com/users/alice"]), void 0);
|
|
348
|
-
});
|
|
349
|
-
test("replaces malformed follower data on Follow", async () => {
|
|
350
|
-
const followerId = "https://remote.example.com/users/alice";
|
|
351
|
-
const malformedRows = [
|
|
352
|
-
{ state: "pending" },
|
|
353
|
-
{
|
|
354
|
-
actor: null,
|
|
355
|
-
state: "pending"
|
|
356
|
-
},
|
|
357
|
-
{
|
|
358
|
-
actor: {},
|
|
359
|
-
state: "pending"
|
|
360
|
-
},
|
|
361
|
-
{
|
|
362
|
-
actor: await new Person({ id: new URL("https://remote.example.com/users/bob") }).toJsonLd(),
|
|
363
|
-
state: "pending"
|
|
364
|
-
}
|
|
365
|
-
];
|
|
366
|
-
for (const malformedRow of malformedRows) {
|
|
367
|
-
const kv = new MemoryKvStore();
|
|
368
|
-
await kv.set(["follower", followerId], malformedRow);
|
|
369
|
-
let handlerCallCount = 0;
|
|
370
|
-
const relay = createRelay("litepub", {
|
|
371
|
-
kv,
|
|
372
|
-
origin: "https://relay.example.com",
|
|
373
|
-
documentLoaderFactory: () => mockDocumentLoader,
|
|
374
|
-
authenticatedDocumentLoaderFactory: () => mockDocumentLoader,
|
|
375
|
-
subscriptionHandler: () => {
|
|
376
|
-
handlerCallCount++;
|
|
377
|
-
return Promise.resolve(true);
|
|
378
|
-
}
|
|
379
|
-
});
|
|
380
|
-
strictEqual(await relay.getFollower(followerId), null);
|
|
381
|
-
const followActivity = new Follow({
|
|
382
|
-
id: new URL(`https://remote.example.com/activities/follow/${crypto.randomUUID()}`),
|
|
383
|
-
actor: new URL(followerId),
|
|
384
|
-
object: new URL("https://relay.example.com/users/relay")
|
|
385
|
-
});
|
|
386
|
-
let request = new Request("https://relay.example.com/inbox", {
|
|
387
|
-
method: "POST",
|
|
388
|
-
headers: { "Content-Type": "application/activity+json" },
|
|
389
|
-
body: JSON.stringify(await followActivity.toJsonLd({ contextLoader: mockDocumentLoader }))
|
|
390
|
-
});
|
|
391
|
-
request = await signRequest(request, rsaKeyPair.privateKey, rsaPublicKey.id);
|
|
392
|
-
strictEqual((await relay.fetch(request)).status, 202);
|
|
393
|
-
assertAcceptDelivered(followActivity);
|
|
394
|
-
strictEqual(handlerCallCount, 1);
|
|
395
|
-
const follower = await relay.getFollower(followerId);
|
|
396
|
-
ok(follower);
|
|
397
|
-
strictEqual(follower.state, "pending");
|
|
398
|
-
strictEqual(follower.actor.id?.href, followerId);
|
|
399
|
-
}
|
|
400
|
-
});
|
|
401
|
-
for (const state of ["pending", "accepted"]) test(`ignores duplicate Follow activity from ${state} follower`, async () => {
|
|
402
|
-
const kv = new MemoryKvStore();
|
|
403
|
-
let handlerCallCount = 0;
|
|
404
|
-
const relay = createRelay("litepub", {
|
|
405
|
-
kv,
|
|
406
|
-
origin: "https://relay.example.com",
|
|
407
|
-
documentLoaderFactory: () => mockDocumentLoader,
|
|
408
|
-
authenticatedDocumentLoaderFactory: () => mockDocumentLoader,
|
|
409
|
-
subscriptionHandler: async (_ctx, _actor) => {
|
|
410
|
-
handlerCallCount++;
|
|
411
|
-
return await Promise.resolve(true);
|
|
412
|
-
}
|
|
413
|
-
});
|
|
414
|
-
const follower = new Person({
|
|
415
|
-
id: new URL("https://remote.example.com/users/alice"),
|
|
416
|
-
preferredUsername: "alice",
|
|
417
|
-
inbox: new URL(aliceInbox)
|
|
418
|
-
});
|
|
419
|
-
await kv.set(["follower", "https://remote.example.com/users/alice"], {
|
|
420
|
-
actor: await follower.toJsonLd(),
|
|
421
|
-
state
|
|
422
|
-
});
|
|
423
|
-
const followActivity = new Follow({
|
|
424
|
-
id: new URL("https://remote.example.com/activities/follow/1"),
|
|
425
|
-
actor: follower.id,
|
|
426
|
-
object: new URL("https://relay.example.com/users/relay")
|
|
427
|
-
});
|
|
428
|
-
let request = new Request("https://relay.example.com/inbox", {
|
|
429
|
-
method: "POST",
|
|
430
|
-
headers: { "Content-Type": "application/activity+json" },
|
|
431
|
-
body: JSON.stringify(await followActivity.toJsonLd({ contextLoader: mockDocumentLoader }))
|
|
432
|
-
});
|
|
433
|
-
request = await signRequest(request, rsaKeyPair.privateKey, rsaPublicKey.id);
|
|
434
|
-
strictEqual((await relay.fetch(request)).status, 202);
|
|
435
|
-
strictEqual(handlerCallCount, 0);
|
|
436
|
-
const followerData = await kv.get(["follower", "https://remote.example.com/users/alice"]);
|
|
437
|
-
ok(isRelayFollowerData(followerData));
|
|
438
|
-
strictEqual(followerData.state, state);
|
|
439
|
-
});
|
|
440
|
-
test("handles Accept activity completing reciprocal follow", async () => {
|
|
441
|
-
const kv = new MemoryKvStore();
|
|
442
|
-
const follower = new Person({
|
|
443
|
-
id: new URL("https://remote.example.com/users/alice"),
|
|
444
|
-
preferredUsername: "alice",
|
|
445
|
-
inbox: new URL(aliceInbox)
|
|
446
|
-
});
|
|
447
|
-
await kv.set(["follower", "https://remote.example.com/users/alice"], {
|
|
448
|
-
actor: await follower.toJsonLd(),
|
|
449
|
-
state: "pending"
|
|
450
|
-
});
|
|
451
|
-
const relay = createRelay("litepub", {
|
|
452
|
-
kv,
|
|
453
|
-
origin: "https://relay.example.com",
|
|
454
|
-
documentLoaderFactory: () => mockDocumentLoader,
|
|
455
|
-
authenticatedDocumentLoaderFactory: () => mockDocumentLoader,
|
|
456
|
-
subscriptionHandler: () => Promise.resolve(true)
|
|
457
|
-
});
|
|
458
|
-
const relayFollow = new Follow({
|
|
459
|
-
id: new URL("https://relay.example.com/activities/follow/1"),
|
|
460
|
-
actor: new URL("https://relay.example.com/users/relay"),
|
|
461
|
-
object: new URL("https://remote.example.com/users/alice")
|
|
462
|
-
});
|
|
463
|
-
const acceptActivity = new Accept({
|
|
464
|
-
id: new URL("https://remote.example.com/activities/accept/1"),
|
|
465
|
-
actor: new URL("https://remote.example.com/users/alice"),
|
|
466
|
-
object: relayFollow
|
|
467
|
-
});
|
|
468
|
-
let request = new Request("https://relay.example.com/inbox", {
|
|
469
|
-
method: "POST",
|
|
470
|
-
headers: { "Content-Type": "application/activity+json" },
|
|
471
|
-
body: JSON.stringify(await acceptActivity.toJsonLd({ contextLoader: mockDocumentLoader }))
|
|
472
|
-
});
|
|
473
|
-
request = await signRequest(request, rsaKeyPair.privateKey, rsaPublicKey.id);
|
|
474
|
-
strictEqual((await relay.fetch(request)).status, 202);
|
|
475
|
-
const followerData = await kv.get(["follower", "https://remote.example.com/users/alice"]);
|
|
476
|
-
ok(isRelayFollowerData(followerData));
|
|
477
|
-
strictEqual(followerData.state, "accepted");
|
|
478
|
-
});
|
|
479
|
-
test("ignores Accept activity for invalid follower data", async () => {
|
|
480
|
-
const followerId = "https://remote.example.com/users/alice";
|
|
481
|
-
const invalidRows = [
|
|
482
|
-
{ state: "pending" },
|
|
483
|
-
{
|
|
484
|
-
actor: null,
|
|
485
|
-
state: "pending"
|
|
486
|
-
},
|
|
487
|
-
{
|
|
488
|
-
actor: {},
|
|
489
|
-
state: "pending"
|
|
490
|
-
},
|
|
491
|
-
{
|
|
492
|
-
actor: await new Person({ id: new URL("https://remote.example.com/users/bob") }).toJsonLd(),
|
|
493
|
-
state: "pending"
|
|
494
|
-
}
|
|
495
|
-
];
|
|
496
|
-
for (const invalidRow of invalidRows) {
|
|
497
|
-
const kv = new MemoryKvStore();
|
|
498
|
-
await kv.set(["follower", followerId], invalidRow);
|
|
499
|
-
const relay = createRelay("litepub", {
|
|
500
|
-
kv,
|
|
501
|
-
origin: "https://relay.example.com",
|
|
502
|
-
documentLoaderFactory: () => mockDocumentLoader,
|
|
503
|
-
authenticatedDocumentLoaderFactory: () => mockDocumentLoader,
|
|
504
|
-
subscriptionHandler: () => Promise.resolve(true)
|
|
505
|
-
});
|
|
506
|
-
const relayFollow = new Follow({
|
|
507
|
-
id: new URL("https://relay.example.com/activities/follow/1"),
|
|
508
|
-
actor: new URL("https://relay.example.com/users/relay"),
|
|
509
|
-
object: new URL(followerId)
|
|
510
|
-
});
|
|
511
|
-
const acceptActivity = new Accept({
|
|
512
|
-
id: new URL("https://remote.example.com/activities/accept/1"),
|
|
513
|
-
actor: new URL(followerId),
|
|
514
|
-
object: relayFollow
|
|
515
|
-
});
|
|
516
|
-
let request = new Request("https://relay.example.com/inbox", {
|
|
517
|
-
method: "POST",
|
|
518
|
-
headers: { "Content-Type": "application/activity+json" },
|
|
519
|
-
body: JSON.stringify(await acceptActivity.toJsonLd({ contextLoader: mockDocumentLoader }))
|
|
520
|
-
});
|
|
521
|
-
request = await signRequest(request, rsaKeyPair.privateKey, rsaPublicKey.id);
|
|
522
|
-
strictEqual((await relay.fetch(request)).status, 202);
|
|
523
|
-
deepStrictEqual(await kv.get(["follower", followerId]), invalidRow);
|
|
524
|
-
}
|
|
525
|
-
});
|
|
526
|
-
for (const state of ["pending", "accepted"]) test(`handles Undo Follow activity for ${state} follower`, async () => {
|
|
527
|
-
const kv = new MemoryKvStore();
|
|
528
|
-
const followerId = "https://remote.example.com/users/alice";
|
|
529
|
-
const follower = new Person({
|
|
530
|
-
id: new URL(followerId),
|
|
531
|
-
preferredUsername: "alice",
|
|
532
|
-
inbox: new URL(aliceInbox)
|
|
533
|
-
});
|
|
534
|
-
await kv.set(["follower", followerId], {
|
|
535
|
-
actor: await follower.toJsonLd(),
|
|
536
|
-
state
|
|
537
|
-
});
|
|
538
|
-
const relay = createRelay("litepub", {
|
|
539
|
-
kv,
|
|
540
|
-
origin: "https://relay.example.com",
|
|
541
|
-
documentLoaderFactory: () => mockDocumentLoader,
|
|
542
|
-
authenticatedDocumentLoaderFactory: () => mockDocumentLoader,
|
|
543
|
-
subscriptionHandler: () => Promise.resolve(true)
|
|
544
|
-
});
|
|
545
|
-
const originalFollow = new Follow({
|
|
546
|
-
id: new URL("https://remote.example.com/activities/follow/1"),
|
|
547
|
-
actor: new URL(followerId),
|
|
548
|
-
object: new URL("https://relay.example.com/users/relay")
|
|
549
|
-
});
|
|
550
|
-
const undoActivity = new Undo({
|
|
551
|
-
id: new URL("https://remote.example.com/activities/undo/1"),
|
|
552
|
-
actor: new URL(followerId),
|
|
553
|
-
object: originalFollow
|
|
554
|
-
});
|
|
555
|
-
let request = new Request("https://relay.example.com/inbox", {
|
|
556
|
-
method: "POST",
|
|
557
|
-
headers: { "Content-Type": "application/activity+json" },
|
|
558
|
-
body: JSON.stringify(await undoActivity.toJsonLd({ contextLoader: mockDocumentLoader }))
|
|
559
|
-
});
|
|
560
|
-
request = await signRequest(request, rsaKeyPair.privateKey, rsaPublicKey.id);
|
|
561
|
-
strictEqual((await relay.fetch(request)).status, 202);
|
|
562
|
-
strictEqual(await kv.get(["follower", followerId]), void 0);
|
|
563
|
-
});
|
|
564
|
-
test("handles Create activity with Announce forwarding", async () => {
|
|
565
|
-
const kv = new MemoryKvStore();
|
|
566
|
-
const followerId = "https://remote.example.com/users/alice";
|
|
567
|
-
const follower = new Person({
|
|
568
|
-
id: new URL(followerId),
|
|
569
|
-
preferredUsername: "alice",
|
|
570
|
-
inbox: new URL(aliceInbox)
|
|
571
|
-
});
|
|
572
|
-
await kv.set(["follower", followerId], {
|
|
573
|
-
actor: await follower.toJsonLd(),
|
|
574
|
-
state: "accepted"
|
|
575
|
-
});
|
|
576
|
-
const relay = createRelay("litepub", {
|
|
577
|
-
kv,
|
|
578
|
-
origin: "https://relay.example.com",
|
|
579
|
-
documentLoaderFactory: () => mockDocumentLoader,
|
|
580
|
-
authenticatedDocumentLoaderFactory: () => mockDocumentLoader,
|
|
581
|
-
subscriptionHandler: () => Promise.resolve(true)
|
|
582
|
-
});
|
|
583
|
-
const note = new Note({
|
|
584
|
-
id: new URL("https://remote.example.com/notes/1"),
|
|
585
|
-
content: "Hello world"
|
|
586
|
-
});
|
|
587
|
-
const createActivity = new Create({
|
|
588
|
-
id: new URL("https://remote.example.com/activities/create/1"),
|
|
589
|
-
actor: new URL(followerId),
|
|
590
|
-
object: note
|
|
591
|
-
});
|
|
592
|
-
let request = new Request("https://relay.example.com/inbox", {
|
|
593
|
-
method: "POST",
|
|
594
|
-
headers: { "Content-Type": "application/activity+json" },
|
|
595
|
-
body: JSON.stringify(await createActivity.toJsonLd({ contextLoader: mockDocumentLoader }))
|
|
596
|
-
});
|
|
597
|
-
request = await signRequest(request, rsaKeyPair.privateKey, rsaPublicKey.id);
|
|
598
|
-
const response = await relay.fetch(request);
|
|
599
|
-
ok(response.status === 200 || response.status === 202);
|
|
600
|
-
});
|
|
601
|
-
test("forwards activities only to accepted followers", async () => {
|
|
602
|
-
const kv = new MemoryKvStore();
|
|
603
|
-
const pendingFollower = new Person({
|
|
604
|
-
id: new URL("https://pending.example.com/users/bob"),
|
|
605
|
-
preferredUsername: "bob",
|
|
606
|
-
inbox: new URL(pendingInbox)
|
|
607
|
-
});
|
|
608
|
-
const acceptedFollower = new Person({
|
|
609
|
-
id: new URL("https://accepted.example.com/users/carol"),
|
|
610
|
-
preferredUsername: "carol",
|
|
611
|
-
inbox: new URL(acceptedInbox)
|
|
612
|
-
});
|
|
613
|
-
await kv.set(["follower", pendingFollower.id.href], {
|
|
614
|
-
actor: await pendingFollower.toJsonLd(),
|
|
615
|
-
state: "pending"
|
|
616
|
-
});
|
|
617
|
-
await kv.set(["follower", acceptedFollower.id.href], {
|
|
618
|
-
actor: await acceptedFollower.toJsonLd(),
|
|
619
|
-
state: "accepted"
|
|
620
|
-
});
|
|
621
|
-
const relay = createRelay("litepub", {
|
|
622
|
-
kv,
|
|
623
|
-
origin: "https://relay.example.com",
|
|
624
|
-
documentLoaderFactory: () => mockDocumentLoader,
|
|
625
|
-
authenticatedDocumentLoaderFactory: () => mockDocumentLoader,
|
|
626
|
-
subscriptionHandler: () => Promise.resolve(true)
|
|
627
|
-
});
|
|
628
|
-
const createActivity = new Create({
|
|
629
|
-
id: new URL("https://remote.example.com/activities/create/1"),
|
|
630
|
-
actor: new URL("https://remote.example.com/users/alice"),
|
|
631
|
-
object: new Note({
|
|
632
|
-
id: new URL("https://remote.example.com/notes/1"),
|
|
633
|
-
content: "Hello world"
|
|
634
|
-
})
|
|
635
|
-
});
|
|
636
|
-
let request = new Request("https://relay.example.com/inbox", {
|
|
637
|
-
method: "POST",
|
|
638
|
-
headers: { "Content-Type": "application/activity+json" },
|
|
639
|
-
body: JSON.stringify(await createActivity.toJsonLd({ contextLoader: mockDocumentLoader }))
|
|
640
|
-
});
|
|
641
|
-
request = await signRequest(request, rsaKeyPair.privateKey, rsaPublicKey.id);
|
|
642
|
-
const deliveredToPending = recordInbox(pendingInbox);
|
|
643
|
-
const deliveredToAccepted = recordInbox(acceptedInbox);
|
|
644
|
-
const response = await relay.fetch(request);
|
|
645
|
-
ok(response.status === 200 || response.status === 202, `Unexpected inbox response status: ${response.status}`);
|
|
646
|
-
strictEqual(deliveredToAccepted.length, 1);
|
|
647
|
-
deepStrictEqual(deliveredToPending, []);
|
|
648
|
-
});
|
|
649
|
-
test("handles Update activity with Announce forwarding", async () => {
|
|
650
|
-
const relay = createRelay("litepub", {
|
|
651
|
-
kv: new MemoryKvStore(),
|
|
652
|
-
origin: "https://relay.example.com",
|
|
653
|
-
documentLoaderFactory: () => mockDocumentLoader,
|
|
654
|
-
authenticatedDocumentLoaderFactory: () => mockDocumentLoader,
|
|
655
|
-
subscriptionHandler: () => Promise.resolve(true)
|
|
656
|
-
});
|
|
657
|
-
const note = new Note({
|
|
658
|
-
id: new URL("https://remote.example.com/notes/1"),
|
|
659
|
-
content: "Updated content"
|
|
660
|
-
});
|
|
661
|
-
const updateActivity = new Update({
|
|
662
|
-
id: new URL("https://remote.example.com/activities/update/1"),
|
|
663
|
-
actor: new URL("https://remote.example.com/users/alice"),
|
|
664
|
-
object: note
|
|
665
|
-
});
|
|
666
|
-
let request = new Request("https://relay.example.com/inbox", {
|
|
667
|
-
method: "POST",
|
|
668
|
-
headers: { "Content-Type": "application/activity+json" },
|
|
669
|
-
body: JSON.stringify(await updateActivity.toJsonLd({ contextLoader: mockDocumentLoader }))
|
|
670
|
-
});
|
|
671
|
-
request = await signRequest(request, rsaKeyPair.privateKey, rsaPublicKey.id);
|
|
672
|
-
const response = await relay.fetch(request);
|
|
673
|
-
ok(response.status === 200 || response.status === 202);
|
|
674
|
-
});
|
|
675
|
-
test("handles Move activity with Announce forwarding", async () => {
|
|
676
|
-
const relay = createRelay("litepub", {
|
|
677
|
-
kv: new MemoryKvStore(),
|
|
678
|
-
origin: "https://relay.example.com",
|
|
679
|
-
documentLoaderFactory: () => mockDocumentLoader,
|
|
680
|
-
authenticatedDocumentLoaderFactory: () => mockDocumentLoader,
|
|
681
|
-
subscriptionHandler: () => Promise.resolve(true)
|
|
682
|
-
});
|
|
683
|
-
const moveActivity = new Move({
|
|
684
|
-
id: new URL("https://remote.example.com/activities/move/1"),
|
|
685
|
-
actor: new URL("https://remote.example.com/users/alice"),
|
|
686
|
-
object: new URL("https://remote.example.com/users/alice"),
|
|
687
|
-
target: new URL("https://other.example.com/users/alice")
|
|
688
|
-
});
|
|
689
|
-
let request = new Request("https://relay.example.com/inbox", {
|
|
690
|
-
method: "POST",
|
|
691
|
-
headers: { "Content-Type": "application/activity+json" },
|
|
692
|
-
body: JSON.stringify(await moveActivity.toJsonLd({ contextLoader: mockDocumentLoader }))
|
|
693
|
-
});
|
|
694
|
-
request = await signRequest(request, rsaKeyPair.privateKey, rsaPublicKey.id);
|
|
695
|
-
const response = await relay.fetch(request);
|
|
696
|
-
ok(response.status === 200 || response.status === 202);
|
|
697
|
-
});
|
|
698
|
-
test("handles Delete activity with Announce forwarding", async () => {
|
|
699
|
-
const relay = createRelay("litepub", {
|
|
700
|
-
kv: new MemoryKvStore(),
|
|
701
|
-
origin: "https://relay.example.com",
|
|
702
|
-
documentLoaderFactory: () => mockDocumentLoader,
|
|
703
|
-
authenticatedDocumentLoaderFactory: () => mockDocumentLoader,
|
|
704
|
-
subscriptionHandler: () => Promise.resolve(true)
|
|
705
|
-
});
|
|
706
|
-
const deleteActivity = new Delete({
|
|
707
|
-
id: new URL("https://remote.example.com/activities/delete/1"),
|
|
708
|
-
actor: new URL("https://remote.example.com/users/alice"),
|
|
709
|
-
object: new URL("https://remote.example.com/notes/1")
|
|
710
|
-
});
|
|
711
|
-
let request = new Request("https://relay.example.com/inbox", {
|
|
712
|
-
method: "POST",
|
|
713
|
-
headers: { "Content-Type": "application/activity+json" },
|
|
714
|
-
body: JSON.stringify(await deleteActivity.toJsonLd({ contextLoader: mockDocumentLoader }))
|
|
715
|
-
});
|
|
716
|
-
request = await signRequest(request, rsaKeyPair.privateKey, rsaPublicKey.id);
|
|
717
|
-
const response = await relay.fetch(request);
|
|
718
|
-
ok(response.status === 200 || response.status === 202);
|
|
719
|
-
});
|
|
720
|
-
test("handles Announce activity forwarding", async () => {
|
|
721
|
-
const relay = createRelay("litepub", {
|
|
722
|
-
kv: new MemoryKvStore(),
|
|
723
|
-
origin: "https://relay.example.com",
|
|
724
|
-
documentLoaderFactory: () => mockDocumentLoader,
|
|
725
|
-
authenticatedDocumentLoaderFactory: () => mockDocumentLoader,
|
|
726
|
-
subscriptionHandler: () => Promise.resolve(true)
|
|
727
|
-
});
|
|
728
|
-
const announceActivity = new Announce({
|
|
729
|
-
id: new URL("https://remote.example.com/activities/announce/1"),
|
|
730
|
-
actor: new URL("https://remote.example.com/users/alice"),
|
|
731
|
-
object: new URL("https://remote.example.com/notes/1")
|
|
732
|
-
});
|
|
733
|
-
let request = new Request("https://relay.example.com/inbox", {
|
|
734
|
-
method: "POST",
|
|
735
|
-
headers: { "Content-Type": "application/activity+json" },
|
|
736
|
-
body: JSON.stringify(await announceActivity.toJsonLd({ contextLoader: mockDocumentLoader }))
|
|
737
|
-
});
|
|
738
|
-
request = await signRequest(request, rsaKeyPair.privateKey, rsaPublicKey.id);
|
|
739
|
-
const response = await relay.fetch(request);
|
|
740
|
-
ok(response.status === 200 || response.status === 202);
|
|
741
|
-
});
|
|
742
|
-
test("multiple followers can be stored", async () => {
|
|
743
|
-
const kv = new MemoryKvStore();
|
|
744
|
-
const followerIds = [
|
|
745
|
-
"https://remote1.example.com/users/user1",
|
|
746
|
-
"https://remote2.example.com/users/user2",
|
|
747
|
-
"https://remote3.example.com/users/user3"
|
|
748
|
-
];
|
|
749
|
-
for (let i = 0; i < followerIds.length; i++) {
|
|
750
|
-
const followerId = followerIds[i];
|
|
751
|
-
const actor = new Person({
|
|
752
|
-
id: new URL(followerId),
|
|
753
|
-
preferredUsername: `user${i + 1}`,
|
|
754
|
-
inbox: new URL(`${followerId}/inbox`)
|
|
755
|
-
});
|
|
756
|
-
await kv.set(["follower", followerId], {
|
|
757
|
-
actor: await actor.toJsonLd(),
|
|
758
|
-
state: "accepted"
|
|
759
|
-
});
|
|
760
|
-
}
|
|
761
|
-
let count = 0;
|
|
762
|
-
for await (const _ of kv.list(["follower"])) count++;
|
|
763
|
-
strictEqual(count, 3);
|
|
764
|
-
});
|
|
765
|
-
test("list() returns empty when no followers exist", async () => {
|
|
766
|
-
const kv = new MemoryKvStore();
|
|
767
|
-
let count = 0;
|
|
768
|
-
for await (const _ of kv.list(["follower"])) count++;
|
|
769
|
-
strictEqual(count, 0);
|
|
770
|
-
});
|
|
771
|
-
test("list() returns all followers after additions", async () => {
|
|
772
|
-
const kv = new MemoryKvStore();
|
|
773
|
-
const followerIds = [
|
|
774
|
-
"https://server1.example.com/users/alice",
|
|
775
|
-
"https://server2.example.com/users/bob",
|
|
776
|
-
"https://server3.example.com/users/carol"
|
|
777
|
-
];
|
|
778
|
-
for (const followerId of followerIds) {
|
|
779
|
-
const actor = new Person({
|
|
780
|
-
id: new URL(followerId),
|
|
781
|
-
preferredUsername: followerId.split("/").pop(),
|
|
782
|
-
inbox: new URL(`${followerId}/inbox`)
|
|
783
|
-
});
|
|
784
|
-
await kv.set(["follower", followerId], {
|
|
785
|
-
actor: await actor.toJsonLd(),
|
|
786
|
-
state: "accepted"
|
|
787
|
-
});
|
|
788
|
-
}
|
|
789
|
-
const retrievedIds = [];
|
|
790
|
-
for await (const { key, value } of kv.list(["follower"])) {
|
|
791
|
-
strictEqual(key.length, 2);
|
|
792
|
-
strictEqual(key[0], "follower");
|
|
793
|
-
retrievedIds.push(key[1]);
|
|
794
|
-
ok(isRelayFollowerData(value));
|
|
795
|
-
strictEqual(value.state, "accepted");
|
|
796
|
-
}
|
|
797
|
-
strictEqual(retrievedIds.length, 3);
|
|
798
|
-
for (const id of followerIds) ok(retrievedIds.includes(id));
|
|
799
|
-
});
|
|
800
|
-
test("list() excludes followers after deletion", async () => {
|
|
801
|
-
const kv = new MemoryKvStore();
|
|
802
|
-
const follower1Id = "https://server1.example.com/users/alice";
|
|
803
|
-
const follower2Id = "https://server2.example.com/users/bob";
|
|
804
|
-
const follower3Id = "https://server3.example.com/users/carol";
|
|
805
|
-
for (const followerId of [
|
|
806
|
-
follower1Id,
|
|
807
|
-
follower2Id,
|
|
808
|
-
follower3Id
|
|
809
|
-
]) {
|
|
810
|
-
const actor = new Person({
|
|
811
|
-
id: new URL(followerId),
|
|
812
|
-
preferredUsername: followerId.split("/").pop(),
|
|
813
|
-
inbox: new URL(`${followerId}/inbox`)
|
|
814
|
-
});
|
|
815
|
-
await kv.set(["follower", followerId], {
|
|
816
|
-
actor: await actor.toJsonLd(),
|
|
817
|
-
state: "accepted"
|
|
818
|
-
});
|
|
819
|
-
}
|
|
820
|
-
await kv.delete(["follower", follower2Id]);
|
|
821
|
-
const retrievedIds = [];
|
|
822
|
-
for await (const { key } of kv.list(["follower"])) retrievedIds.push(key[1]);
|
|
823
|
-
strictEqual(retrievedIds.length, 2);
|
|
824
|
-
ok(retrievedIds.includes(follower1Id));
|
|
825
|
-
ok(!retrievedIds.includes(follower2Id));
|
|
826
|
-
ok(retrievedIds.includes(follower3Id));
|
|
827
|
-
});
|
|
828
|
-
test("list() distinguishes between pending and accepted followers", async () => {
|
|
829
|
-
const kv = new MemoryKvStore();
|
|
830
|
-
const pendingFollowerId = "https://server1.example.com/users/alice";
|
|
831
|
-
const acceptedFollowerId = "https://server2.example.com/users/bob";
|
|
832
|
-
const pendingFollower = new Person({
|
|
833
|
-
id: new URL(pendingFollowerId),
|
|
834
|
-
preferredUsername: "alice",
|
|
835
|
-
inbox: new URL("https://server1.example.com/users/alice/inbox")
|
|
836
|
-
});
|
|
837
|
-
const acceptedFollower = new Person({
|
|
838
|
-
id: new URL(acceptedFollowerId),
|
|
839
|
-
preferredUsername: "bob",
|
|
840
|
-
inbox: new URL("https://server2.example.com/users/bob/inbox")
|
|
841
|
-
});
|
|
842
|
-
await kv.set(["follower", pendingFollowerId], {
|
|
843
|
-
actor: await pendingFollower.toJsonLd(),
|
|
844
|
-
state: "pending"
|
|
845
|
-
});
|
|
846
|
-
await kv.set(["follower", acceptedFollowerId], {
|
|
847
|
-
actor: await acceptedFollower.toJsonLd(),
|
|
848
|
-
state: "accepted"
|
|
849
|
-
});
|
|
850
|
-
const followers = [];
|
|
851
|
-
for await (const { key, value } of kv.list(["follower"])) {
|
|
852
|
-
if (!isRelayFollowerData(value)) continue;
|
|
853
|
-
followers.push({
|
|
854
|
-
id: key[1],
|
|
855
|
-
state: value.state
|
|
856
|
-
});
|
|
857
|
-
}
|
|
858
|
-
strictEqual(followers.length, 2);
|
|
859
|
-
const pendingEntry = followers.find((f) => f.id === pendingFollowerId);
|
|
860
|
-
ok(pendingEntry);
|
|
861
|
-
strictEqual(pendingEntry.state, "pending");
|
|
862
|
-
const acceptedEntry = followers.find((f) => f.id === acceptedFollowerId);
|
|
863
|
-
ok(acceptedEntry);
|
|
864
|
-
strictEqual(acceptedEntry.state, "accepted");
|
|
865
|
-
});
|
|
866
|
-
test("list() returns correct actor data", async () => {
|
|
867
|
-
const kv = new MemoryKvStore();
|
|
868
|
-
const followerId = "https://remote.example.com/users/alice";
|
|
869
|
-
const follower = new Person({
|
|
870
|
-
id: new URL(followerId),
|
|
871
|
-
preferredUsername: "alice",
|
|
872
|
-
name: "Alice Wonderland",
|
|
873
|
-
inbox: new URL(aliceInbox)
|
|
874
|
-
});
|
|
875
|
-
await kv.set(["follower", followerId], {
|
|
876
|
-
actor: await follower.toJsonLd(),
|
|
877
|
-
state: "accepted"
|
|
878
|
-
});
|
|
879
|
-
for await (const { key, value } of kv.list(["follower"])) {
|
|
880
|
-
strictEqual(key[1], followerId);
|
|
881
|
-
ok(isRelayFollowerData(value));
|
|
882
|
-
strictEqual(value.state, "accepted");
|
|
883
|
-
ok(value.actor && typeof value.actor === "object");
|
|
884
|
-
const actor = value.actor;
|
|
885
|
-
strictEqual(actor.preferredUsername, "alice");
|
|
886
|
-
strictEqual(actor.name, "Alice Wonderland");
|
|
887
|
-
}
|
|
888
|
-
});
|
|
889
|
-
});
|
|
890
|
-
//#endregion
|
|
891
|
-
export {};
|