Load profiles on profile page and cache circles

This commit is contained in:
SondreB 2023-01-08 20:14:07 +01:00
parent 0d3a94cdcc
commit 2d7e39d354
No known key found for this signature in database
GPG Key ID: D6CC44C75005FDBF
2 changed files with 38 additions and 23 deletions

View File

@ -14,6 +14,8 @@ export class CircleService {
private table;
circles: Circle[] = [];
cache = new CacheService();
items$ = from(liveQuery(() => this.items()));
@ -44,6 +46,11 @@ export class CircleService {
if (!defaultCircle) {
await this.putCircle(CircleService.DEFAULT);
}
// Cache the circle so we can lookup quickly.
this.items$.subscribe((circles) => {
this.circles = circles;
});
}
// async #filter(predicate: (value: Circle, key: string) => boolean): Promise<Circle[]> {
@ -81,11 +88,16 @@ export class CircleService {
// }
async getCircle(id?: number) {
if (!id) {
if (id == null) {
return undefined;
}
return this.table.get(id);
// Use the cache if loaded already.
if (this.circles.length > 0) {
return this.circles.find((c) => c.id == id);
} else {
return this.table.get(id);
}
}
async putCircle(document: Circle | any) {

View File

@ -92,32 +92,35 @@ export class UserComponent {
}
this.pubkey = pubkey;
// this.profile = await this.profiles.getProfile(pubkey);
if (!this.profile) {
this.profile = this.profiles.emptyProfile(pubkey);
this.circle = undefined;
}
this.profiles.getProfile(pubkey).subscribe(async (profile) => {
this.profile = profile;
this.npub = this.utilities.getNostrIdentifier(pubkey);
if (!this.profile.name) {
this.profile.name = this.npub;
}
this.profileName = this.profile.name;
if (this.profileName)
if (!this.profile.display_name) {
this.profile.display_name = this.profileName;
if (!this.profile) {
this.profile = this.profiles.emptyProfile(pubkey);
this.circle = undefined;
}
this.imagePath = this.profile.picture || '/assets/profile.png';
this.npub = this.utilities.getNostrIdentifier(pubkey);
this.circle = await this.circleService.getCircle(this.profile.circle);
// If the user has name in their profile, show that and not pubkey.
this.appState.title = `@${this.profile.name}`;
if (!this.profile.name) {
this.profile.name = this.npub;
}
this.profileName = this.profile.name;
if (this.profileName)
if (!this.profile.display_name) {
this.profile.display_name = this.profileName;
}
this.imagePath = this.profile.picture || '/assets/profile.png';
this.circle = await this.circleService.getCircle(this.profile.circle);
// If the user has name in their profile, show that and not pubkey.
this.appState.title = `@${this.profile.name}`;
});
})
);
}