-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathdata.ts
More file actions
858 lines (706 loc) · 30.4 KB
/
data.ts
File metadata and controls
858 lines (706 loc) · 30.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
import { Injectable } from '@angular/core';
import { NostrEvent, NostrEventDocument, NostrProfileDocument, NostrRelay, NostrSubscription, QueryJob } from './interfaces';
import { ProfileService } from './profile';
import { EventService } from './event';
import { RelayService } from './relay';
import { Filter, Relay, Event, getEventHash, validateEvent, verifySignature, Kind, UnsignedEvent } from 'nostr-tools';
import { DataValidation } from './data-validation';
import { ApplicationState } from './applicationstate';
import { timeout, map, merge, Observable, delay, Observer, race, take, switchMap, mergeMap, tap, finalize, concatMap, mergeAll, exhaustMap, catchError, of, combineAll, combineLatestAll, filter, from } from 'rxjs';
import { Utilities } from './utilities';
import { StorageService } from './storage';
import { QueueService } from './queue.service';
import { UIService } from './ui';
import { CircleService } from './circle';
import { NostrService } from './nostr';
@Injectable({
providedIn: 'root',
})
export class DataService {
daysToKeepProfiles = 14;
cleanProfileInterval = 1000 * 60 * 60; // Every hour
profileBatchSize = 20;
refreshUserProfile = 1000 * 60 * 60 * 2; // Every second hour
connected = false;
// Observable that can be merged with to avoid performing calls unless we have connected to relays.
connected$ = this.appState.connected$.pipe(map((status) => status === true));
constructor(
private nostr: NostrService,
private ui: UIService,
private circleService: CircleService,
private storage: StorageService,
private queueService: QueueService,
private profileService: ProfileService,
private appState: ApplicationState,
private utilities: Utilities,
private validator: DataValidation,
private eventService: EventService,
private relayService: RelayService
) {
// We use a global observable for the connected state to avoid having many subscriptions and we'll skip processing until this is true.
this.appState.connected$.subscribe((connected) => {
console.log('Connection state changed: ', connected);
this.connected = connected;
if (this.connected) {
this.initialDataLoad();
// console.log('Connection established, start processing queues.');
// this.processQueues();
}
});
// Schedule a timeout whenever the queues has been triggered, to wait for additional items that might arrive in a loop.
// this.queueService.queues$.subscribe(() => {
// setTimeout(() => {
// this.processQueues();
// }, 250);
// });
}
async publishRelays() {
const mappedRelays = this.getArrayFomattedRelayList();
let originalEvent: UnsignedEvent = {
kind: 10002, // NIP-65: https://github.com/nostr-protocol/nips/blob/master/65.md
created_at: Math.floor(Date.now() / 1000),
content: '',
pubkey: this.appState.getPublicKey(),
tags: mappedRelays,
};
const event = await this.createAndSignEvent(originalEvent);
if (!event) {
return;
}
console.log('PUBLISH EVENT:', event);
this.relayService.publish(event);
}
private getArrayFomattedRelayList() {
return this.relayService.items
.filter((r) => r.public === true)
.map((r) => {
let relayEntry = ['r', r.url];
if (r.type == 2) {
relayEntry.push('read');
} else if (r.type == 3) {
relayEntry.push('write');
}
return relayEntry;
});
}
private getJsonFormattedRelayList() {
let mappedRelays: any = {};
this.relayService.items
.filter((r) => r.public)
.map((r) => {
mappedRelays[r.url] = {
read: r.type === 1 || r.type === 2,
write: r.type === 1 || r.type === 3,
};
});
return mappedRelays;
}
private async createAndSignEvent(originalEvent: UnsignedEvent) {
let signedEvent = originalEvent as Event;
signedEvent.id = getEventHash(originalEvent);
// Use nostr directly on global, similar to how most Nostr app will interact with the provider.
signedEvent = await this.nostr.sign(originalEvent);
// We force validation upon user so we make sure they don't create content that we won't be able to parse back later.
// We must do this before we run nostr-tools validate and signature validation.
const event = this.eventService.processEvent(signedEvent as NostrEventDocument);
if (!event) {
throw new Error('The event is not valid. Cannot publish.');
}
let ok = validateEvent(signedEvent);
if (!ok) {
throw new Error('The event is not valid. Cannot publish.');
}
let veryOk = await verifySignature(event as any); // Required .id and .sig, which we know has been added at this stage.
if (!veryOk) {
throw new Error('The event signature not valid. Maybe you choose a different account than the one specified?');
}
return event;
}
/** Get's all following and all public relays. */
async getContactsAndRelays() {
const contacts = await this.storage.storage.getContacts(this.appState.getPublicKey());
return contacts;
// const nonPublicCircles = this.circleService.circles.filter((c) => !c.public).map((c) => c.id);
// const publicFollowing = this.profileService.following.filter((p) => nonPublicCircles.indexOf(p.circle) == -1).map((p) => p.pubkey);
// const mappedContacts = publicFollowing.map((c) => {
// return ['p', c];
// });
// const mappedRelays = this.getJsonFormattedRelayList();
// let originalEvent: Event = {
// kind: Kind.Contacts,
// created_at: Math.floor(Date.now() / 1000),
// content: JSON.stringify(mappedRelays),
// pubkey: this.appState.getPublicKey(),
// tags: mappedContacts,
// };
// const event = await this.createAndSignEvent(originalEvent);
// if (!event) {
// return;
// }
// console.log('PUBLISH EVENT:', event);
// this.relayService.publish(event);
}
/** Get's all following and all public relays. */
async publishContactsAndRelays() {
const nonPublicCircles = this.circleService.circles.filter((c) => !c.public).map((c) => c.id);
const publicFollowing = this.profileService.following.filter((p) => nonPublicCircles.indexOf(p.circle) == -1).map((p) => p.pubkey);
const mappedContacts = publicFollowing.map((c) => {
return ['p', c];
});
const mappedRelays = this.getJsonFormattedRelayList();
let originalEvent: UnsignedEvent = {
kind: Kind.Contacts,
created_at: Math.floor(Date.now() / 1000),
content: JSON.stringify(mappedRelays),
pubkey: this.appState.getPublicKey(),
tags: mappedContacts,
};
const event = await this.createAndSignEvent(originalEvent);
if (!event) {
return;
}
console.log('PUBLISH EVENT:', event);
this.relayService.publish(event);
}
async initialDataLoad() {
// Listen to profile and contacts of the logged on user.
this.relayService.subscribe([{ authors: [this.appState.getPublicKey()], kinds: [Kind.Metadata, Kind.Contacts] }], 'self');
// Download the profile of the user.
// this.enque({
// identifier: this.appState.getPublicKey(),
// type: 'Profile',
// });
// // Download the following of the user.
// this.enque({
// identifier: this.appState.getPublicKey(),
// type: 'Contacts',
// });
// Create the listeners (filters) for relays:
// TODO: There is limit on maximum following, we need a strategy to handle that.
// potentially subscribing and unsubscribing on intervals with a .since field between each interval.
const pubKeys = this.profileService.following.map((p) => p.pubkey);
// Add self to the top of listening list:
pubKeys.unshift(this.appState.getPublicKey());
// console.log('PUB KEYS:', pubKeys);
// const filter = [{ authors: pubKeys, since: this.storage.state.since, kinds: [Kind.Text, Kind.Reaction, 6] }];
// Subscribe to new events but don't get any history (limit: 0).
// console.log('queueSubscription:', filter);
// this.relayService.queueSubscription([{ authors: pubKeys, since: this.storage.state.since }]);
// this.relayService.subscribe(filter);
// Notifications is a hard-coded subscription identifier.
// Previously there was no filter on kind, then "started following you" events was shown due to kind 3, but downloading kind 3 for everyone is
// fairly heavy operation so disabled for now.
this.relayService.subscribe([{ ['#p']: [this.appState.getPublicKey()], limit: 100, kinds: [Kind.Text, Kind.Reaction, 6] }], 'notifications');
// Load the 10 latest notifications to be displayed on home page.
const notifications = await this.storage.storage.getNotifications(10);
this.ui.putNotifications(notifications);
}
async initialize() {
setTimeout(async () => {
await this.cleanProfiles();
}, this.cleanProfileInterval);
}
isFetching = false;
profileQueue: string[] = [];
queue: QueryJob[] = [];
/** Enques a job to be processed against connected relays. */
enque(job: QueryJob) {
// It is way more optimal to just delegate jobs into separate queues when enquing than querying later.
// if (job.type == 'Profile') {
// this.queueService.queues.profile.jobs.push(job);
// } else if (job.type == 'Contacts') {
// this.queueService.queues.contacts.jobs.push(job);
// } else if (job.type == 'Event') {
// this.queueService.queues.event.jobs.push(job);
// } else {
// throw Error(`This type of job (${job.type}) is currently not supported.`);
// }
// Enque the job on all web workers.
this.relayService.action('enque', job);
// We always delay the processing in case we receive
// setTimeout(() => {
// this.processQueues();
// }, 100);
}
/**
* Since most relays limits at 10 we must ensure we don't go above that.
* 1 reserved for profile retrieval and we queue up maximum 50 on each in batches.
* 1 reserved for live public feed.
* 1 reserved for current profile feed.
* 1 reserved for logged on user feed.
* ...
*/
processQueues() {
// if (!this.connected) {
// console.warn('Cannot process queues, no connection to relays.');
// return;
// }
// // Processing queues basically just copies the jobs from data service to the individual web workers.
// if (this.queueService.queues.profile.jobs.length > 0) {
// this.processProfileQueue();
// }
// if (this.queueService.queues.contacts.jobs.length > 0) {
// this.processContactsQueue();
// }
// if (this.queueService.queues.event.jobs.length > 0) {
// this.processEventQueue();
// }
}
processEventQueue() {
// if (this.queueService.queues.event.active) {
// console.log('Events are already active... Skipping.');
// return;
// }
// const jobs = this.queueService.queues.event.jobs.splice(0, 10);
// const filters = jobs.map((j) => {
// return {
// kinds: [1],
// authors: [j.identifier],
// // limit: j.limit,
// } as Filter;
// });
// return this.downloadNewestEventsByQuery(filters).subscribe(async (event) => {
// if (!event) {
// return;
// }
// // If we are following this user, we'll persist this event.
// const following = this.profileService.isFollowing(event.pubkey);
// if (following) {
// await this.storage.storage.putEvents(event);
// }
// // for (let i = 0; i < jobs.length; i++) {
// // if (jobs[i].callback) {
// // jobs[i].callback(event);
// // }
// // }
// });
}
processProfileQueue() {
// console.log('processProfileQueue');
// // If already active, just skip processing for now.
// if (this.queueService.queues.profile.active) {
// console.log('processProfileQueue: skip');
// return;
// }
// // Grab a batch of jobs.
// const jobs = this.queueService.queues.profile.jobs.splice(0, 50);
// const pubkeys = jobs.map((j) => j.identifier);
// console.log('processProfileQueue: pubkeys', pubkeys);
// // Download the profiles that was queued up.
// this.downloadNewestProfiles(pubkeys, 10000, pubkeys.length).subscribe(async (event) => {
// // const e = await event;
// console.log('processProfileQueue: event', event);
// if (!event) {
// return;
// }
// // Make sure we run update and not put whenever we download the latest profile.
// await this.profileService.updateProfile(event.pubkey, event);
// // for (let i = 0; i < jobs.length; i++) {
// // if (jobs[i].callback) {
// // jobs[i].callback(event);
// // }
// // }
// });
}
processContactsQueue() {
// console.log('processContactsQueue');
// // If already active, just skip processing for now.
// if (this.queueService.queues.contacts.active) {
// console.log('processContactsQueue: skip');
// return;
// }
// this.queueService.queues.contacts.active = true;
// // Grab a batch of jobs.
// const jobs = this.queueService.queues.contacts.jobs.splice(0, 50);
// const pubkeys = jobs.map((j) => j.identifier);
// // Use a dynamic timeout depending on the number of pubkeys requested.
// // const timeout = pubkeys.length * 1000;
// const timeout = pubkeys.length < 10 ? 10000 : 20000;
// // Download the profiles that was queued up.
// this.downloadNewestContactsEvents(pubkeys, timeout)
// .pipe(
// finalize(() => {
// this.queueService.queues.contacts.active = false;
// })
// )
// .subscribe(async (event) => {
// if (!event) {
// return;
// }
// // Whenever we download the contacts document, we'll refresh the RELAYS and FOLLOWING
// // on the profile in question.
// const following = event.tags.map((t) => t[1]);
// // Make sure we run update and not put whenever we download the latest profile.
// this.profileService.followingAndRelays(event.pubkey, following, event.content);
// // for (let i = 0; i < jobs.length; i++) {
// // if (jobs[i].callback) {
// // jobs[i].callback(event);
// // }
// // }
// });
}
/** Creates an observable that will attempt to get newest profile entry across all relays and perform multiple callbacks if newer is found. */
downloadNewestProfiles(pubkeys: string[], requestTimeout = 10000, expectedCount = -1) {
return this.downloadNewestProfileEvents(pubkeys, requestTimeout, expectedCount).pipe(
map((event: any) => {
if (!event) {
return;
}
const profile = this.utilities.mapProfileEvent(event);
return profile;
})
);
}
/** Creates an observable that will attempt to get newest profile events across all relays and perform multiple callbacks if newer is found. */
downloadNewestProfileEvents(pubkeys: string[], requestTimeout = 10000, expectedCount = -1) {
return this.downloadNewestProfileEventByQuery([{ kinds: [0], authors: pubkeys }], requestTimeout, expectedCount);
}
// downloadNewestContactsEvents(pubkeys: string[], requestTimeout = 10000, expectedEventCount = -1) {
// return this.downloadNewestEvents(pubkeys, [3], requestTimeout, expectedEventCount);
// }
downloadNewestEvents(pubkeys: string[], kinds: number[], requestTimeout = 10000, expectedEventCount = -1) {
return this.downloadNewestEventsByQuery([{ kinds: kinds, authors: pubkeys }], requestTimeout, expectedEventCount);
}
downloadEventsByTags(query: any[], requestTimeout = 10000) {
return this.downloadEventsByQuery(query);
}
/** Download a single event from all relays. */
downloadEvent(id: string, requestTimeout = 5000) {
return this.downloadEventByQuery([{ ids: [id] }], requestTimeout);
}
downloadEventByQuery(query: any, requestTimeout = 10000) {
let event: any;
return this.connected$
.pipe(mergeMap(() => this.relayService.connectedRelays())) // TODO: Time this, it appears to take a lot of time??
.pipe(mergeMap((relay) => this.downloadFromRelay(query, relay, requestTimeout)))
.pipe(
filter((data) => {
// Only trigger the reply once.
if (!event) {
event = data;
return true;
} else {
return false;
}
})
)
.pipe(
timeout(requestTimeout),
catchError((error) => {
console.warn('The observable was timed out.');
return of(undefined);
}) // Simply return undefined when the timeout is reached.
);
}
/** Creates an observable that will attempt to get newest events across all relays and perform multiple callbacks if newer is found. */
downloadNewestEventsByQuery(query: any, requestTimeout = 10000, expectedEventCount = -1) {
// TODO: Tune the timeout. There is no point waiting for too long if the relay is overwhelmed with requests as we will simply build up massive backpressure in the client.
const totalEvents: NostrEventDocument[] = [];
// TODO: Figure out if we end up having memory leak with this totalEvents array.
const observables = this.relayService.connectedRelays().map((relay) => this.downloadFromRelay(query, relay));
return merge(...observables)
.pipe(
filter((data, index) => {
let result = false;
// This logic is to ensure we don't care about receiving the same data more than once.
const existingEventIndex = totalEvents.findIndex((e) => e.id === data.id);
if (existingEventIndex > -1) {
result = false;
} else {
totalEvents.push(data);
result = true;
}
if (expectedEventCount > -1 && expectedEventCount != 0) {
expectedEventCount--;
}
return result;
})
)
.pipe(
timeout(requestTimeout),
catchError((error) => {
console.warn('The observable was timed out.');
return of(undefined);
}) // Simply return undefined when the timeout is reached.
);
}
/** Creates an observable that will attempt to get newest events across all relays and perform multiple callbacks if newer is found. */
downloadNewestProfileEventByQuery(query: any, requestTimeout = 10000, expectedEventCount = -1) {
// TODO: Tune the timeout. There is no point waiting for too long if the relay is overwhelmed with requests as we will simply build up massive backpressure in the client.
// const totalEvents: NostrEventDocument[] = [];
// TODO: Figure out if we end up having memory leak with this totalEvents array.
const observables = this.relayService.connectedRelays().map((relay) => this.downloadFromRelay(query, relay));
return merge(...observables).pipe(
timeout(requestTimeout),
catchError((error) => {
console.warn('The observable was timed out.');
return of(undefined);
}) // Simply return undefined when the timeout is reached.
);
}
downloadEventsByQuery(query: any[], requestTimeout = 10000) {
return this.connected$
.pipe(mergeMap(() => this.relayService.connectedRelays())) // TODO: Time this, it appears to take a lot of time??
.pipe(mergeMap((relay) => this.downloadFromRelay(query, relay)));
}
// subscribeLatestEvents(kinds: number[], pubkeys: string[], limit: number) {
// // Make individual filters on the subscription so we will get limit for each individual pubkey.
// let filters: Filter[] = pubkeys.map((a) => {
// return { kinds: kinds, limit: limit, authors: [a] };
// });
// if (filters.length === 0) {
// filters = [{ kinds: kinds, limit: limit }];
// }
// return this.connected$.pipe(mergeMap(() => this.relayService.connectedRelays())).pipe(mergeMap((relay) => this.subscribeToRelay(filters, relay)));
// }
// subscribeToRelay(filters: Filter[], relay: NostrRelay): Observable<NostrEventDocument> {
// return new Observable<NostrEventDocument>((observer: Observer<NostrEventDocument>) => {
// const sub = relay.sub(filters, {}) as NostrSubscription;
// // relay.subscriptions.push(sub);
// sub.on('event', (originalEvent: any) => {
// const event = this.eventService.processEvent(originalEvent);
// if (!event) {
// return;
// }
// observer.next(event);
// });
// sub.on('eose', () => {});
// return () => {
// console.log('subscribeToRelay:finished:unsub');
// // When the observable is finished, this return function is called.
// sub.unsub();
// // const subIndex = relay.subscriptions.findIndex((s) => s == sub);
// // if (subIndex > -1) {
// // relay.subscriptions.splice(subIndex, 1);
// // }
// };
// });
// }
async updateMetadata(profile: NostrProfileDocument) {
const profileContent = this.utilities.reduceProfile(profile!);
let event = this.createEvent(Kind.Metadata, JSON.stringify(profileContent));
const signedEvent = await this.signEvent(event);
// await this.feedService.publish(event, false); // Don't persist this locally.
profile!.created_at = event.created_at;
// Use the whole document for this update as we don't want to loose additional metadata we have, such
// as follow (on self).
await this.profileService.updateProfile(profile!.pubkey, profile!);
await this.publishEvent(signedEvent);
}
downloadFromRelay(filters: Filter[], relay: NostrRelay, requestTimeout = 10000): Observable<NostrEventDocument> {
return new Observable<NostrEventDocument>((observer: Observer<NostrEventDocument>) => {
const sub = relay.sub([...filters], {}) as NostrSubscription;
sub.on('event', (originalEvent: any) => {
const event = this.eventService.processEvent(originalEvent);
if (!event) {
return;
}
observer.next(event);
});
sub.on('eose', () => {
observer.complete();
});
return () => {
sub.unsub();
};
}).pipe(
timeout(requestTimeout),
catchError((error) => {
console.warn('The observable was timed out.');
return of();
})
);
}
fetchProfiles(relay: Relay, authors: string[]) {
if (!authors || authors.length === 0) {
return;
}
// Add a protection timeout if we never receive the profiles. After 30 seconds, cancel and allow query to continue.
setTimeout(() => {
this.isFetching = false;
try {
profileSub.unsub();
} catch (err) {
console.warn('Error during automatic failover for profile fetch.', err);
}
}, 30000);
this.isFetching = true;
let profileSub = relay.sub([{ kinds: [0], authors: authors }], {});
profileSub.on('event', async (event: Event) => {
const originalEvent = event as NostrEvent;
const prossedEvent = this.eventService.processEvent(originalEvent);
if (!prossedEvent) {
return;
}
try {
const jsonParsed = JSON.parse(prossedEvent.content) as NostrProfileDocument;
const profile = this.validator.sanitizeProfile(jsonParsed) as NostrProfileDocument;
// Keep a copy of the created_at value.
profile.created_at = prossedEvent.created_at;
// Persist the profile.
// await this.profileService.updateProfile(prossedEvent.pubkey, profile);
// TODO: Add NIP-05 and nostr.directory verification.
// const displayName = encodeURIComponent(profile.name);
// const url = `https://www.nostr.directory/.well-known/nostr.json?name=${displayName}`;
// const rawResponse = await fetch(url, {
// method: 'GET',
// mode: 'cors',
// });
// if (rawResponse.status === 200) {
// const content = await rawResponse.json();
// const directoryPublicKey = content.names[displayName];
// if (event.pubkey === directoryPublicKey) {
// if (!profile.verifications) {
// profile.verifications = [];
// }
// profile.verifications.push('@nostr.directory');
// // Update the profile with verification data.
// await this.profile.putProfile(event.pubkey, profile);
// } else {
// // profile.verified = false;
// console.warn('Nickname reuse:', url);
// }
// } else {
// // profile.verified = false;
// }
} catch (err) {
console.warn('This profile event was not parsed due to errors:', prossedEvent);
}
});
profileSub.on('eose', () => {
profileSub.unsub();
this.isFetching = false;
});
}
async cleanProfiles() {
// const profileTable = this.storage.table<NostrProfileDocument>('profile');
// const iterator = profileTable.iterator<string, NostrProfileDocument>({ keyEncoding: 'utf8', valueEncoding: 'json' });
// const now = moment();
// for await (const [key, value] of iterator) {
// // Skip all profiles that the user is following, blocked or muted.
// if (value.follow || value.block || value.mute) {
// continue;
// }
// const lastChanged = value.modified || value.created;
// const date = moment.unix(lastChanged).add(-2, 'days');
// var days = now.diff(date, 'days');
// if (days > this.daysToKeepProfiles) {
// console.log('Profile removed from cache: ', value);
// await profileTable.del(key);
// }
// }
// setTimeout(async () => {
// await this.cleanProfiles();
// }, this.cleanProfileInterval);
}
/** Creates an event ready for modification, signing and publish. */
createEvent(kind: Kind | number, content: any): UnsignedEvent {
return this.createEventWithPubkey(kind, content, this.appState.getPublicKey());
}
createEventWithPubkey(kind: Kind | number, content: any, pubkey: string): UnsignedEvent {
let event: UnsignedEvent = {
kind: kind,
created_at: Math.floor(Date.now() / 1000),
content: content,
pubkey: pubkey,
tags: [],
};
return event;
}
/** Request an article to be signed. This method does not add id. */
async signArticle(event: UnsignedEvent) {
let signedEvent = event as Event;
// Use nostr directly on global, similar to how most Nostr app will interact with the provider.
signedEvent = await this.nostr.sign(event);
// We force validation upon user so we make sure they don't create content that we won't be able to parse back later.
// We must do this before we run nostr-tools validate and signature validation.
const verifiedEvent = this.eventService.processEvent(signedEvent as NostrEventDocument);
if (!verifiedEvent) {
throw new Error('The event is not valid. Cannot publish.');
}
let ok = validateEvent(signedEvent);
if (!ok) {
throw new Error('The event is not valid. Cannot publish.');
}
let veryOk = await verifySignature(signedEvent as any); // Required .id and .sig, which we know has been added at this stage.
if (!veryOk) {
throw new Error('The event signature not valid. Maybe you choose a different account than the one specified?');
}
return signedEvent;
}
/** Request an badge definition to be signed. This method does not add id. */
async signBadgeDefinition(event: UnsignedEvent) {
return this.signArticle(event);
}
/** Request an event to be signed. This method will calculate the content id automatically. */
async signEvent(event: UnsignedEvent) {
let signedEvent = event as Event;
if (!signedEvent.id) {
signedEvent.id = getEventHash(event);
}
return this.signArticle(signedEvent);
// // Use nostr directly on global, similar to how most Nostr app will interact with the provider.
// signedEvent = await this.nostr.sign(event);
// // We force validation upon user so we make sure they don't create content that we won't be able to parse back later.
// // We must do this before we run nostr-tools validate and signature validation.
// const verifiedEvent = this.eventService.processEvent(signedEvent as NostrEventDocument);
// let ok = validateEvent(signedEvent);
// if (!ok) {
// throw new Error('The event is not valid. Cannot publish.');
// }
// let veryOk = await verifySignature(signedEvent as any); // Required .id and .sig, which we know has been added at this stage.
// if (!veryOk) {
// throw new Error('The event signature not valid. Maybe you choose a different account than the one specified?');
// }
// return signedEvent;
}
async publishEvent(event: Event) {
this.relayService.publish(event);
}
async publishContacts(pubkeys: string[]) {
const mappedContacts = pubkeys.map((c) => {
return ['p', c];
});
let originalEvent: UnsignedEvent = {
kind: 3,
created_at: Math.floor(Date.now() / 1000),
content: '',
pubkey: this.appState.getPublicKey(),
tags: mappedContacts,
};
let signedEvent = originalEvent as Event;
signedEvent.id = getEventHash(originalEvent);
// Use nostr directly on global, similar to how most Nostr app will interact with the provider.
signedEvent = await this.nostr.sign(originalEvent);
// We force validation upon user so we make sure they don't create content that we won't be able to parse back later.
// We must do this before we run nostr-tools validate and signature validation.
const event = this.eventService.processEvent(signedEvent as NostrEventDocument);
let ok = validateEvent(signedEvent);
if (!ok) {
throw new Error('The event is not valid. Cannot publish.');
}
let veryOk = await verifySignature(signedEvent as any); // Required .id and .sig, which we know has been added at this stage.
if (!veryOk) {
throw new Error('The event signature not valid. Maybe you choose a different account than the one specified?');
}
if (!event) {
return;
}
console.log('PUBLISH EVENT:', signedEvent);
// First we persist our own event like would normally happen if we receive this event.
// await this.#persist(event);
for (let i = 0; i < this.relayService.relays.length; i++) {
const relay = this.relayService.relays[i];
let pub = relay.publish(event);
pub.on('ok', () => {
console.log(`${relay.url} has accepted our event`);
});
// pub.on('seen', () => {
// console.log(`we saw the event on ${relay.url}`);
// });
pub.on('failed', (reason: any) => {
console.log(`failed to publish to ${relay.url}: ${reason}`);
});
}
}
}