holosphere 2.0.0-alpha21 → 2.0.0-alpha22

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 (69) hide show
  1. package/README.md +1 -2
  2. package/dist/cjs/holosphere.cjs +1 -1
  3. package/dist/esm/holosphere.js +43 -41
  4. package/dist/{index-COpLk9gL.cjs → index-B4xe-N5-.cjs} +2 -2
  5. package/dist/{index-COpLk9gL.cjs.map → index-B4xe-N5-.cjs.map} +1 -1
  6. package/dist/{index-D2WstuZJ.js → index-Bug_CGNq.js} +2 -2
  7. package/dist/{index-D2WstuZJ.js.map → index-Bug_CGNq.js.map} +1 -1
  8. package/dist/index-CaCPzdlv.cjs +29 -0
  9. package/dist/index-CaCPzdlv.cjs.map +1 -0
  10. package/dist/{index-B6-8KAQm.js → index-D76zMgwU.js} +2 -2
  11. package/dist/{index-B6-8KAQm.js.map → index-D76zMgwU.js.map} +1 -1
  12. package/dist/{index--QsHG_gD.cjs → index-DuOuk96g.cjs} +2 -2
  13. package/dist/{index--QsHG_gD.cjs.map → index-DuOuk96g.cjs.map} +1 -1
  14. package/dist/{index-BHptWysv.js → index-bYHRpACA.js} +2951 -7736
  15. package/dist/index-bYHRpACA.js.map +1 -0
  16. package/dist/{indexeddb-storage-kQ53UHEE.js → indexeddb-storage-BrIwr42m.js} +2 -2
  17. package/dist/{indexeddb-storage-kQ53UHEE.js.map → indexeddb-storage-BrIwr42m.js.map} +1 -1
  18. package/dist/{indexeddb-storage-wKG4mICM.cjs → indexeddb-storage-CFWfkdX9.cjs} +2 -2
  19. package/dist/{indexeddb-storage-wKG4mICM.cjs.map → indexeddb-storage-CFWfkdX9.cjs.map} +1 -1
  20. package/dist/{memory-storage-DnXCSbBl.js → memory-storage-BDQRj-2j.js} +2 -2
  21. package/dist/{memory-storage-DnXCSbBl.js.map → memory-storage-BDQRj-2j.js.map} +1 -1
  22. package/dist/{memory-storage-CGC8xM2G.cjs → memory-storage-bkatDnuR.cjs} +2 -2
  23. package/dist/{memory-storage-CGC8xM2G.cjs.map → memory-storage-bkatDnuR.cjs.map} +1 -1
  24. package/examples/demo.html +2 -29
  25. package/package.json +3 -8
  26. package/src/content/social-protocols.js +3 -59
  27. package/src/core/holosphere.js +16 -554
  28. package/src/crypto/nostr-utils.js +98 -1
  29. package/src/crypto/secp256k1.js +4 -393
  30. package/src/federation/discovery.js +7 -75
  31. package/src/federation/handshake.js +69 -202
  32. package/src/federation/hologram.js +222 -298
  33. package/src/federation/index.js +2 -9
  34. package/src/federation/registry.js +67 -1257
  35. package/src/federation/request-card.js +21 -35
  36. package/src/hierarchical/upcast.js +4 -9
  37. package/src/index.js +142 -296
  38. package/src/lib/federation-methods.js +370 -909
  39. package/src/storage/global-tables.js +1 -1
  40. package/src/storage/nostr-wrapper.js +9 -5
  41. package/src/subscriptions/manager.js +1 -1
  42. package/types/index.d.ts +145 -37
  43. package/bin/holosphere-activitypub.js +0 -158
  44. package/dist/2019-BzVkRcax.js +0 -6680
  45. package/dist/2019-BzVkRcax.js.map +0 -1
  46. package/dist/2019-C1hPR_Os.cjs +0 -8
  47. package/dist/2019-C1hPR_Os.cjs.map +0 -1
  48. package/dist/browser-BcmACE3G.js +0 -3058
  49. package/dist/browser-BcmACE3G.js.map +0 -1
  50. package/dist/browser-DaqYUTcG.cjs +0 -2
  51. package/dist/browser-DaqYUTcG.cjs.map +0 -1
  52. package/dist/index-BHptWysv.js.map +0 -1
  53. package/dist/index-CDlhzxT2.cjs +0 -29
  54. package/dist/index-CDlhzxT2.cjs.map +0 -1
  55. package/src/federation/capabilities.js +0 -46
  56. package/src/storage/backend-factory.js +0 -130
  57. package/src/storage/backend-interface.js +0 -161
  58. package/src/storage/backends/activitypub/server.js +0 -675
  59. package/src/storage/backends/activitypub-backend.js +0 -295
  60. package/src/storage/backends/gundb-backend.js +0 -875
  61. package/src/storage/backends/nostr-backend.js +0 -251
  62. package/src/storage/gun-async.js +0 -341
  63. package/src/storage/gun-auth.js +0 -373
  64. package/src/storage/gun-federation.js +0 -785
  65. package/src/storage/gun-references.js +0 -209
  66. package/src/storage/gun-schema.js +0 -306
  67. package/src/storage/gun-wrapper.js +0 -642
  68. package/src/storage/migration.js +0 -351
  69. package/src/storage/unified-storage.js +0 -161
@@ -1,675 +0,0 @@
1
- /**
2
- * @fileoverview Self-hosted ActivityPub server for HoloSphere federation.
3
- * Provides complete ActivityPub protocol support for federation with Mastodon, Pleroma,
4
- * and other fediverse servers. Includes WebFinger, NodeInfo, actor endpoints, and SSE subscriptions.
5
- * @module storage/backends/activitypub/server
6
- */
7
-
8
- import express from 'express';
9
- import crypto from 'crypto';
10
- import { createPersistentStorage } from '../../persistent-storage.js';
11
-
12
- /** @constant {string} ActivityStreams JSON-LD context */
13
- const ACTIVITY_STREAMS_CONTEXT = 'https://www.w3.org/ns/activitystreams';
14
- /** @constant {string} Security vocabulary JSON-LD context */
15
- const SECURITY_CONTEXT = 'https://w3id.org/security/v1';
16
-
17
- /**
18
- * Self-hosted ActivityPub server for HoloSphere data federation.
19
- * Implements WebFinger discovery, actor endpoints, inbox/outbox, and HoloSphere data API.
20
- *
21
- * @class ActivityPubServer
22
- * @example
23
- * const server = new ActivityPubServer({
24
- * port: 3000,
25
- * domain: 'holon.example.com',
26
- * dataDir: './data'
27
- * });
28
- * await server.start();
29
- */
30
- export class ActivityPubServer {
31
- /**
32
- * Create a new ActivityPub server instance.
33
- * @param {Object} [config={}] - Server configuration
34
- * @param {number} [config.port=3000] - HTTP port to listen on
35
- * @param {string} [config.domain='localhost'] - Server domain for URLs
36
- * @param {string} [config.protocol='http'] - Protocol (http or https)
37
- * @param {string} [config.dataDir] - Data directory for persistent storage
38
- */
39
- constructor(config = {}) {
40
- this.config = {
41
- port: config.port || 3000,
42
- domain: config.domain || 'localhost',
43
- dataDir: config.dataDir,
44
- ...config,
45
- };
46
-
47
- this.app = express();
48
- this.storage = null;
49
- this.actors = new Map();
50
- this.subscriptions = new Map();
51
- this.server = null;
52
-
53
- // Generate server key pair for signing
54
- this.serverKeyPair = null;
55
- }
56
-
57
- async start() {
58
- // Initialize storage
59
- this.storage = await createPersistentStorage('activitypub', {
60
- dataDir: this.config.dataDir,
61
- });
62
-
63
- // Generate or load server keys
64
- await this._initializeKeys();
65
-
66
- // Middleware
67
- this.app.use(express.json({
68
- type: ['application/json', 'application/activity+json', 'application/ld+json'],
69
- }));
70
-
71
- // CORS for federation
72
- this.app.use((req, res, next) => {
73
- res.header('Access-Control-Allow-Origin', '*');
74
- res.header('Access-Control-Allow-Headers', 'Content-Type, Accept, Signature, Date, Digest');
75
- res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
76
- if (req.method === 'OPTIONS') {
77
- return res.sendStatus(200);
78
- }
79
- next();
80
- });
81
-
82
- // WebFinger endpoint (required for federation discovery)
83
- this.app.get('/.well-known/webfinger', this._handleWebFinger.bind(this));
84
-
85
- // NodeInfo endpoints (optional but recommended)
86
- this.app.get('/.well-known/nodeinfo', this._handleNodeInfoWellKnown.bind(this));
87
- this.app.get('/nodeinfo/2.0', this._handleNodeInfo.bind(this));
88
-
89
- // Actor endpoints
90
- this.app.get('/actor/:name', this._handleGetActor.bind(this));
91
- this.app.post('/actor/:name/inbox', this._handlePostInbox.bind(this));
92
- this.app.get('/actor/:name/outbox', this._handleGetOutbox.bind(this));
93
- this.app.post('/actor/:name/outbox', this._handlePostOutbox.bind(this));
94
- this.app.get('/actor/:name/followers', this._handleGetFollowers.bind(this));
95
- this.app.get('/actor/:name/following', this._handleGetFollowing.bind(this));
96
-
97
- // Object endpoints (for HoloSphere data)
98
- this.app.get('/objects/:appName/:holonId/:lensName', this._handleGetCollection.bind(this));
99
- this.app.get('/objects/:appName/:holonId/:lensName/:key', this._handleGetObject.bind(this));
100
-
101
- // API endpoints (for HoloSphere client)
102
- this.app.post('/api/actors', this._handleCreateActor.bind(this));
103
- this.app.post('/api/data', this._handleWriteData.bind(this));
104
- this.app.get('/api/data/*', this._handleReadData.bind(this));
105
- this.app.delete('/api/data/*', this._handleDeleteData.bind(this));
106
-
107
- // SSE for real-time subscriptions
108
- this.app.get('/subscribe', this._handleSubscribe.bind(this));
109
-
110
- // Health check
111
- this.app.get('/health', (req, res) => {
112
- res.json({ status: 'ok', timestamp: Date.now() });
113
- });
114
-
115
- // Start server
116
- return new Promise((resolve) => {
117
- this.server = this.app.listen(this.config.port, () => {
118
- console.log(`ActivityPub server listening on port ${this.config.port}`);
119
- console.log(`Domain: ${this.config.domain}`);
120
- resolve();
121
- });
122
- });
123
- }
124
-
125
- async _initializeKeys() {
126
- // Try to load existing keys
127
- const keysKey = '_server_keys';
128
- const existingKeys = await this.storage.get(keysKey);
129
-
130
- if (existingKeys) {
131
- this.serverKeyPair = existingKeys;
132
- } else {
133
- // Generate new RSA key pair
134
- const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', {
135
- modulusLength: 2048,
136
- publicKeyEncoding: { type: 'spki', format: 'pem' },
137
- privateKeyEncoding: { type: 'pkcs8', format: 'pem' },
138
- });
139
-
140
- this.serverKeyPair = { publicKey, privateKey };
141
- await this.storage.put(keysKey, this.serverKeyPair);
142
- }
143
- }
144
-
145
- // ============ WebFinger ============
146
-
147
- _handleWebFinger(req, res) {
148
- const resource = req.query.resource;
149
- if (!resource) {
150
- return res.status(400).json({ error: 'resource parameter required' });
151
- }
152
-
153
- // Parse acct:user@domain
154
- const match = resource.match(/^acct:(.+)@(.+)$/);
155
- if (!match) {
156
- return res.status(400).json({ error: 'invalid resource format' });
157
- }
158
-
159
- const username = match[1];
160
- const domain = match[2];
161
-
162
- // Check if domain matches (allow localhost variants)
163
- const validDomains = [this.config.domain, 'localhost', `localhost:${this.config.port}`];
164
- if (!validDomains.includes(domain)) {
165
- return res.status(404).json({ error: 'user not found' });
166
- }
167
-
168
- const baseUrl = this._getBaseUrl();
169
-
170
- res.set('Content-Type', 'application/jrd+json');
171
- res.json({
172
- subject: resource,
173
- aliases: [`${baseUrl}/actor/${username}`],
174
- links: [
175
- {
176
- rel: 'self',
177
- type: 'application/activity+json',
178
- href: `${baseUrl}/actor/${username}`,
179
- },
180
- ],
181
- });
182
- }
183
-
184
- // ============ NodeInfo ============
185
-
186
- _handleNodeInfoWellKnown(req, res) {
187
- const baseUrl = this._getBaseUrl();
188
- res.json({
189
- links: [
190
- {
191
- rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0',
192
- href: `${baseUrl}/nodeinfo/2.0`,
193
- },
194
- ],
195
- });
196
- }
197
-
198
- _handleNodeInfo(req, res) {
199
- res.json({
200
- version: '2.0',
201
- software: {
202
- name: 'holosphere-activitypub',
203
- version: '1.0.0',
204
- },
205
- protocols: ['activitypub'],
206
- usage: {
207
- users: { total: this.actors.size },
208
- localPosts: 0,
209
- },
210
- openRegistrations: true,
211
- });
212
- }
213
-
214
- // ============ Actor Endpoints ============
215
-
216
- async _handleGetActor(req, res) {
217
- const { name } = req.params;
218
- const baseUrl = this._getBaseUrl();
219
-
220
- // Load or create actor
221
- let actor = await this._getOrCreateActor(name);
222
-
223
- const actorObject = {
224
- '@context': [ACTIVITY_STREAMS_CONTEXT, SECURITY_CONTEXT],
225
- id: `${baseUrl}/actor/${name}`,
226
- type: 'Service',
227
- preferredUsername: name,
228
- name: `HoloSphere: ${name}`,
229
- summary: `HoloSphere application: ${name}`,
230
- inbox: `${baseUrl}/actor/${name}/inbox`,
231
- outbox: `${baseUrl}/actor/${name}/outbox`,
232
- followers: `${baseUrl}/actor/${name}/followers`,
233
- following: `${baseUrl}/actor/${name}/following`,
234
- publicKey: {
235
- id: `${baseUrl}/actor/${name}#main-key`,
236
- owner: `${baseUrl}/actor/${name}`,
237
- publicKeyPem: actor.publicKey || this.serverKeyPair.publicKey,
238
- },
239
- };
240
-
241
- res.set('Content-Type', 'application/activity+json');
242
- res.json(actorObject);
243
- }
244
-
245
- async _handlePostInbox(req, res) {
246
- const { name } = req.params;
247
- const activity = req.body;
248
-
249
- // TODO: Verify HTTP Signature for federation security
250
- // For now, accept all activities
251
-
252
- try {
253
- await this._processIncomingActivity(name, activity);
254
- res.status(202).send();
255
- } catch (error) {
256
- console.error('Inbox error:', error);
257
- res.status(500).json({ error: error.message });
258
- }
259
- }
260
-
261
- async _handleGetOutbox(req, res) {
262
- const { name } = req.params;
263
- const baseUrl = this._getBaseUrl();
264
-
265
- // Get activities from storage
266
- const activities = await this._getActorActivities(name);
267
-
268
- const collection = {
269
- '@context': ACTIVITY_STREAMS_CONTEXT,
270
- id: `${baseUrl}/actor/${name}/outbox`,
271
- type: 'OrderedCollection',
272
- totalItems: activities.length,
273
- orderedItems: activities,
274
- };
275
-
276
- res.set('Content-Type', 'application/activity+json');
277
- res.json(collection);
278
- }
279
-
280
- async _handlePostOutbox(req, res) {
281
- const { name } = req.params;
282
- const activity = req.body;
283
-
284
- // Verify authorization (API key or signature)
285
- const apiKey = req.headers.authorization?.replace('Bearer ', '');
286
- const actor = await this._getOrCreateActor(name);
287
-
288
- if (actor.apiKey && actor.apiKey !== apiKey) {
289
- return res.status(401).json({ error: 'unauthorized' });
290
- }
291
-
292
- try {
293
- const result = await this._processOutgoingActivity(name, activity);
294
- res.status(201).json(result);
295
- } catch (error) {
296
- console.error('Outbox error:', error);
297
- res.status(500).json({ error: error.message });
298
- }
299
- }
300
-
301
- async _handleGetFollowers(req, res) {
302
- const { name } = req.params;
303
- const baseUrl = this._getBaseUrl();
304
-
305
- const actor = await this._getOrCreateActor(name);
306
- const followers = actor.followers || [];
307
-
308
- res.set('Content-Type', 'application/activity+json');
309
- res.json({
310
- '@context': ACTIVITY_STREAMS_CONTEXT,
311
- id: `${baseUrl}/actor/${name}/followers`,
312
- type: 'OrderedCollection',
313
- totalItems: followers.length,
314
- orderedItems: followers,
315
- });
316
- }
317
-
318
- async _handleGetFollowing(req, res) {
319
- const { name } = req.params;
320
- const baseUrl = this._getBaseUrl();
321
-
322
- const actor = await this._getOrCreateActor(name);
323
- const following = actor.following || [];
324
-
325
- res.set('Content-Type', 'application/activity+json');
326
- res.json({
327
- '@context': ACTIVITY_STREAMS_CONTEXT,
328
- id: `${baseUrl}/actor/${name}/following`,
329
- type: 'OrderedCollection',
330
- totalItems: following.length,
331
- orderedItems: following,
332
- });
333
- }
334
-
335
- // ============ Object Endpoints ============
336
-
337
- async _handleGetCollection(req, res) {
338
- const { appName, holonId, lensName } = req.params;
339
- const path = `${appName}/${holonId}/${lensName}`;
340
- const baseUrl = this._getBaseUrl();
341
-
342
- const items = await this._getDataByPrefix(path);
343
-
344
- const collection = {
345
- '@context': ACTIVITY_STREAMS_CONTEXT,
346
- id: `${baseUrl}/objects/${path}`,
347
- type: 'OrderedCollection',
348
- totalItems: items.length,
349
- orderedItems: items.map(item => this._dataToObject(item, path)),
350
- };
351
-
352
- res.set('Content-Type', 'application/activity+json');
353
- res.json(collection);
354
- }
355
-
356
- async _handleGetObject(req, res) {
357
- const { appName, holonId, lensName, key } = req.params;
358
- const path = `${appName}/${holonId}/${lensName}/${key}`;
359
-
360
- const data = await this.storage.get(`data:${path}`);
361
- if (!data || data._deleted) {
362
- return res.status(404).json({ error: 'not found' });
363
- }
364
-
365
- res.set('Content-Type', 'application/activity+json');
366
- res.json(this._dataToObject(data, path));
367
- }
368
-
369
- // ============ API Endpoints ============
370
-
371
- async _handleCreateActor(req, res) {
372
- const { name, apiKey } = req.body;
373
-
374
- if (!name) {
375
- return res.status(400).json({ error: 'name required' });
376
- }
377
-
378
- const actor = await this._getOrCreateActor(name);
379
- if (apiKey) {
380
- actor.apiKey = apiKey;
381
- await this.storage.put(`actor:${name}`, actor);
382
- }
383
-
384
- res.json({
385
- name,
386
- id: `${this._getBaseUrl()}/actor/${name}`,
387
- publicKey: actor.publicKey,
388
- });
389
- }
390
-
391
- async _handleWriteData(req, res) {
392
- const { path, data, actorName } = req.body;
393
-
394
- if (!path || !data) {
395
- return res.status(400).json({ error: 'path and data required' });
396
- }
397
-
398
- // Store data
399
- const record = {
400
- ...data,
401
- _meta: {
402
- ...(data._meta || {}),
403
- timestamp: Date.now(),
404
- path,
405
- },
406
- };
407
-
408
- await this.storage.put(`data:${path}`, record);
409
-
410
- // Create activity
411
- if (actorName) {
412
- await this._createActivity(actorName, 'Create', record, path);
413
- }
414
-
415
- // Notify subscribers
416
- this._notifySubscribers(path, record);
417
-
418
- res.json({ success: true, path });
419
- }
420
-
421
- async _handleReadData(req, res) {
422
- const path = req.params[0];
423
-
424
- // Check if this is a prefix query
425
- const isPrefix = path.split('/').length <= 3;
426
-
427
- if (isPrefix) {
428
- const items = await this._getDataByPrefix(path);
429
- res.json(items);
430
- } else {
431
- const data = await this.storage.get(`data:${path}`);
432
- if (!data || data._deleted) {
433
- return res.status(404).json({ error: 'not found' });
434
- }
435
- res.json(data);
436
- }
437
- }
438
-
439
- async _handleDeleteData(req, res) {
440
- const path = req.params[0];
441
- const actorName = req.query.actor;
442
-
443
- // Tombstone delete
444
- const existing = await this.storage.get(`data:${path}`);
445
- if (existing) {
446
- const tombstone = {
447
- id: existing.id,
448
- _deleted: true,
449
- _deletedAt: Date.now(),
450
- };
451
- await this.storage.put(`data:${path}`, tombstone);
452
-
453
- // Create delete activity
454
- if (actorName) {
455
- await this._createActivity(actorName, 'Delete', { id: path }, path);
456
- }
457
-
458
- // Notify subscribers
459
- this._notifySubscribers(path, tombstone);
460
- }
461
-
462
- res.json({ success: true });
463
- }
464
-
465
- // ============ Subscriptions ============
466
-
467
- _handleSubscribe(req, res) {
468
- const path = req.query.path || '';
469
-
470
- // Set up SSE
471
- res.writeHead(200, {
472
- 'Content-Type': 'text/event-stream',
473
- 'Cache-Control': 'no-cache',
474
- 'Connection': 'keep-alive',
475
- });
476
-
477
- // Send initial ping
478
- res.write('event: ping\ndata: connected\n\n');
479
-
480
- // Register subscription
481
- const subId = crypto.randomUUID();
482
- this.subscriptions.set(subId, { path, res });
483
-
484
- // Clean up on close
485
- req.on('close', () => {
486
- this.subscriptions.delete(subId);
487
- });
488
- }
489
-
490
- _notifySubscribers(path, data) {
491
- for (const [id, sub] of this.subscriptions) {
492
- if (path.startsWith(sub.path)) {
493
- try {
494
- sub.res.write(`event: update\ndata: ${JSON.stringify({ path, data })}\n\n`);
495
- } catch (e) {
496
- this.subscriptions.delete(id);
497
- }
498
- }
499
- }
500
- }
501
-
502
- // ============ Helper Methods ============
503
-
504
- _getBaseUrl() {
505
- const protocol = this.config.protocol || 'http';
506
- const port = this.config.port;
507
- const domain = this.config.domain;
508
-
509
- if (domain === 'localhost' || domain.includes(':')) {
510
- return `${protocol}://${domain}`;
511
- }
512
- if (port === 80 || port === 443) {
513
- return `${protocol}://${domain}`;
514
- }
515
- return `${protocol}://${domain}:${port}`;
516
- }
517
-
518
- async _getOrCreateActor(name) {
519
- // Check cache
520
- if (this.actors.has(name)) {
521
- return this.actors.get(name);
522
- }
523
-
524
- // Check storage
525
- let actor = await this.storage.get(`actor:${name}`);
526
- if (!actor) {
527
- // Create new actor
528
- const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', {
529
- modulusLength: 2048,
530
- publicKeyEncoding: { type: 'spki', format: 'pem' },
531
- privateKeyEncoding: { type: 'pkcs8', format: 'pem' },
532
- });
533
-
534
- actor = {
535
- name,
536
- publicKey,
537
- privateKey,
538
- followers: [],
539
- following: [],
540
- createdAt: Date.now(),
541
- };
542
-
543
- await this.storage.put(`actor:${name}`, actor);
544
- }
545
-
546
- this.actors.set(name, actor);
547
- return actor;
548
- }
549
-
550
- async _processIncomingActivity(actorName, activity) {
551
- const type = activity.type;
552
-
553
- switch (type) {
554
- case 'Follow':
555
- await this._handleFollow(actorName, activity);
556
- break;
557
- case 'Undo':
558
- if (activity.object?.type === 'Follow') {
559
- await this._handleUnfollow(actorName, activity);
560
- }
561
- break;
562
- case 'Create':
563
- case 'Update':
564
- case 'Delete':
565
- // Store federated content
566
- await this._storeFederatedActivity(actorName, activity);
567
- break;
568
- }
569
-
570
- // Store activity
571
- await this.storage.put(`inbox:${actorName}:${Date.now()}`, activity);
572
- }
573
-
574
- async _processOutgoingActivity(actorName, activity) {
575
- const baseUrl = this._getBaseUrl();
576
-
577
- // Assign ID if not present
578
- if (!activity.id) {
579
- activity.id = `${baseUrl}/activities/${crypto.randomUUID()}`;
580
- }
581
-
582
- // Set actor if not present
583
- if (!activity.actor) {
584
- activity.actor = `${baseUrl}/actor/${actorName}`;
585
- }
586
-
587
- // Store activity
588
- await this.storage.put(`outbox:${actorName}:${Date.now()}`, activity);
589
-
590
- // Handle based on type
591
- if (activity.type === 'Create' && activity.object?.['holosphere:data']) {
592
- const data = activity.object['holosphere:data'];
593
- const path = activity.object['holosphere:path'];
594
- if (path) {
595
- await this.storage.put(`data:${path}`, data);
596
- this._notifySubscribers(path, data);
597
- }
598
- }
599
-
600
- return activity;
601
- }
602
-
603
- async _handleFollow(actorName, activity) {
604
- const actor = await this._getOrCreateActor(actorName);
605
- const follower = activity.actor;
606
-
607
- if (!actor.followers.includes(follower)) {
608
- actor.followers.push(follower);
609
- await this.storage.put(`actor:${actorName}`, actor);
610
- }
611
-
612
- // Send Accept response
613
- // TODO: Implement federation delivery
614
- }
615
-
616
- async _handleUnfollow(actorName, activity) {
617
- const actor = await this._getOrCreateActor(actorName);
618
- const follower = activity.actor;
619
-
620
- actor.followers = actor.followers.filter(f => f !== follower);
621
- await this.storage.put(`actor:${actorName}`, actor);
622
- }
623
-
624
- async _storeFederatedActivity(actorName, activity) {
625
- // Store federated content for potential use
626
- const key = `federated:${actorName}:${Date.now()}`;
627
- await this.storage.put(key, activity);
628
- }
629
-
630
- async _getActorActivities(actorName) {
631
- const activities = await this.storage.getAll(`outbox:${actorName}:`);
632
- return activities.sort((a, b) => (b.published || 0) - (a.published || 0));
633
- }
634
-
635
- async _getDataByPrefix(prefix) {
636
- const all = await this.storage.getAll(`data:${prefix}`);
637
- return all.filter(item => item && !item._deleted);
638
- }
639
-
640
- async _createActivity(actorName, type, data, path) {
641
- const baseUrl = this._getBaseUrl();
642
- const activity = {
643
- '@context': ACTIVITY_STREAMS_CONTEXT,
644
- id: `${baseUrl}/activities/${crypto.randomUUID()}`,
645
- type,
646
- actor: `${baseUrl}/actor/${actorName}`,
647
- published: new Date().toISOString(),
648
- object: this._dataToObject(data, path),
649
- };
650
-
651
- await this.storage.put(`outbox:${actorName}:${Date.now()}`, activity);
652
- return activity;
653
- }
654
-
655
- _dataToObject(data, path) {
656
- const baseUrl = this._getBaseUrl();
657
- return {
658
- '@context': ACTIVITY_STREAMS_CONTEXT,
659
- id: `${baseUrl}/objects/${path}`,
660
- type: 'Note',
661
- content: JSON.stringify(data),
662
- 'holosphere:data': data,
663
- 'holosphere:path': path,
664
- published: data._meta?.timestamp ? new Date(data._meta.timestamp).toISOString() : new Date().toISOString(),
665
- };
666
- }
667
-
668
- stop() {
669
- if (this.server) {
670
- this.server.close();
671
- }
672
- }
673
- }
674
-
675
- export default ActivityPubServer;