Load events on user profiles

This commit is contained in:
SondreB 2023-02-07 13:46:57 +01:00
parent 6a61b93135
commit 930ce14779
No known key found for this signature in database
GPG Key ID: D6CC44C75005FDBF
5 changed files with 74 additions and 11 deletions

View File

@ -173,5 +173,7 @@ export class NoteComponent {
if (this.sub) {
this.sub.unsub();
}
this.ui.clear();
}
}

View File

@ -41,6 +41,8 @@ export class RelayService {
threadSubscription?: string;
profileEventSubscription?: string;
#eventsChanged: BehaviorSubject<NostrEventDocument[]> = new BehaviorSubject<NostrEventDocument[]>(this.events);
#filteredEventsChanged: BehaviorSubject<NostrEventDocument[]> = new BehaviorSubject<NostrEventDocument[]>([]);
@ -90,6 +92,30 @@ export class RelayService {
}
});
// Whenever the pubkey changes, we'll load the profile and start loading the user's events.
// If the ID is reset, we'll also unsubscribe the subscription.
this.ui.pubkey$.subscribe(async (id) => {
if (!id) {
if (this.profileEventSubscription) {
this.unsubscribe(this.profileEventSubscription);
this.profileEventSubscription = undefined;
}
return;
}
const profile = await this.db.storage.getProfile(id);
if (profile) {
this.ui.setProfile(profile);
} else {
this.enque({ type: 'Profile', identifier: id });
}
// Subscribe to events for the current user profile.
this.profileEventSubscription = this.subscribe([{ authors: [id], kinds: [Kind.Text, Kind.Reaction, 6] }]);
});
// Whenever the event ID changes, we'll attempt to load the event.
this.ui.eventId$.subscribe(async (id) => {
if (!id) {
@ -315,6 +341,10 @@ export class RelayService {
await this.profileService.updateProfile(nostrProfileDocument.pubkey, nostrProfileDocument);
console.log('END UPDATE PROFILE');
}
if (this.ui.pubkey == event.pubkey) {
this.ui.setProfile(nostrProfileDocument);
}
} else if (event.kind == Kind.Contacts) {
const pubkey = this.appState.getPublicKey();
@ -401,6 +431,9 @@ export class RelayService {
this.ui.setEvent(event);
} else if (this.ui.parentEventId == event.id) {
this.ui.setParentEvent(event);
} else if (this.ui.pubkey == event.pubkey) {
// If the event belongs to current visible profile.
this.ui.putEvent(event);
} else {
// If we receive event on the thread subscription, and only then, update the events array.
if (response.subscription == this.threadSubscription) {

View File

@ -214,7 +214,11 @@ export class ThreadService {
}
buildTree(events: NostrEventDocument[]) {
console.log('Build tree from:', events);
if (events.length > 1000) {
console.log('There are more than 1000 events... likely spam!');
} else {
console.log('Build tree from:', events);
}
// console.log('THREAD EVENT:', event);
// if (!event || !event.id) {

View File

@ -159,7 +159,10 @@ export class UIService {
}
putEvents(events: NostrEventDocument[]) {
this.events = events;
// For now, filter out only text.
this.events = events.filter((e) => e.kind == Kind.Text);
this.events = this.events.map((e) => this.calculateFields(e));
this.events = this.events.sort((a, b) => {
return a.created_at < b.created_at ? -1 : 1;
@ -168,6 +171,23 @@ export class UIService {
this.#eventsChanged.next(this.events);
}
clear() {
this.#eventId = undefined;
this.#event = undefined;
this.parentEventId = undefined;
this.#parentEvent = undefined;
this.events = [];
this.#pubkey = undefined;
this.#profile = undefined;
this.#eventChanged.next(this.#event);
this.#eventsChanged.next(this.events);
this.#profileChanged.next(this.#profile);
}
clearEvents() {
this.events = [];
this.#eventsChanged.next(this.events);

View File

@ -292,15 +292,16 @@ export class UserComponent {
this.ui.putEvents(events);
// Then query for the latest events.
this.queueService.enqueEvent(
pubkey
// (data: NostrEventDocument) => {
// this.ui.putEvent(data);
// // this.notesService.currentViewNotes.sort((a, b) => (a.created_at < b.created_at ? 1 : -1));
// // this.#changed();
// },
// 200
);
// ?? This is not going to work, this function operates on IDs, not on pubkeys.
// this.queueService.enqueEvent(
// pubkey
// // (data: NostrEventDocument) => {
// // this.ui.putEvent(data);
// // // this.notesService.currentViewNotes.sort((a, b) => (a.created_at < b.created_at ? 1 : -1));
// // // this.#changed();
// // },
// // 200
// );
// setTimeout(async () => {
// const events = await this.storage.events.where('pubkey').equals(pubkey).toArray();
@ -405,6 +406,9 @@ export class UserComponent {
}
ngOnDestroy() {
// Reset the profile selection.
this.ui.setPubKey(undefined);
this.utilities.unsubscribe(this.subscriptions);
if (this.profileSubscription) {