From 2802281742b2f16f4b1b37337136684adc2b98ff Mon Sep 17 00:00:00 2001 From: SondreB Date: Mon, 16 Jan 2023 13:41:31 +0100 Subject: [PATCH] Persist events from following --- src/app/services/data.ts | 7 ++++ src/app/services/database.ts | 15 ++------- src/app/services/profile.ts | 32 ++++++++++++++++--- src/app/services/storage.ts | 15 ++------- .../shared/content-photos/content-photos.ts | 2 +- src/app/shared/content/content.ts | 2 +- 6 files changed, 40 insertions(+), 33 deletions(-) diff --git a/src/app/services/data.ts b/src/app/services/data.ts index f19618b..5ccec75 100644 --- a/src/app/services/data.ts +++ b/src/app/services/data.ts @@ -30,6 +30,7 @@ export class DataService { constructor( private ui: UIService, + private storage: StorageService, private queueService: QueueService, private profileService: ProfileService, private appState: ApplicationState, @@ -168,6 +169,12 @@ export class DataService { return; } + // If we are following this user, we'll persist this event. + const following = this.profileService.isFollowing(event.pubkey); + if (following) { + await this.storage.events.put(event, event.id); + } + for (let i = 0; i < jobs.length; i++) { if (jobs[i].callback) { jobs[i].callback(event); diff --git a/src/app/services/database.ts b/src/app/services/database.ts index 26d33de..e0ffe10 100644 --- a/src/app/services/database.ts +++ b/src/app/services/database.ts @@ -1,20 +1,9 @@ import Dexie, { Table } from 'dexie'; -import { Circle, NostrNoteDocument, NostrProfileDocument, NostrRelayDocument } from './interfaces'; - -export interface EventItem { - id?: number; - pubkey: number; - content: string; -} - -export interface ProfileItem { - pubkey: string; - content: string; -} +import { Circle, NostrEventDocument, NostrNoteDocument, NostrProfileDocument, NostrRelayDocument } from './interfaces'; export class DatabaseService extends Dexie { relays!: Table; - events!: Table; + events!: Table; notes!: Table; profiles!: Table; circles!: Table; diff --git a/src/app/services/profile.ts b/src/app/services/profile.ts index ca76164..3b776e0 100644 --- a/src/app/services/profile.ts +++ b/src/app/services/profile.ts @@ -55,9 +55,7 @@ export class ProfileService { return dexieToRx(liveQuery(() => this.list(ProfileStatus.Mute))); } - // #profile: NostrProfileDocument; - - /** TODO: Destroy this array when there are zero subscribers left. */ + /** The profiles the user is following. */ profiles: NostrProfileDocument[] = []; #followingChanged: BehaviorSubject = new BehaviorSubject(this.profiles); @@ -313,6 +311,7 @@ export class ProfileService { this.cache.set(profile.pubkey, profile); await this.table.put(profile); + this.#putFollowingProfile(profile); this.updateItemIfSelected(profile); } @@ -361,6 +360,11 @@ export class ProfileService { // Load the logged on user profile and have it immediately available. const profile = await this.getLocalProfile(pubkey); this.userProfileUpdated(profile); + + // Perform initial population of .profiles, which contains profile the user is following. + const items = await this.table.where('status').equals(1).toArray(); + this.profiles = items; + this.#profilesChanged.next(this.profiles); } /** Populate the observable with profiles which we are following. */ @@ -467,6 +471,16 @@ export class ProfileService { // } } + #putFollowingProfile(profile: NostrProfileDocument) { + const existingIndex = this.profiles.findIndex((p) => p.pubkey == profile.pubkey); + + if (existingIndex === -1) { + this.profiles.push(profile); + } else { + this.profiles[existingIndex] = profile; + } + } + /** Follow can be called without having an existing profile persisted. */ async follow(pubkey?: string, circle: number = 0, existingProfile?: NostrProfileDocument) { if (!pubkey) { @@ -489,6 +503,7 @@ export class ProfileService { // Save directly, don't put in cache. await this.table.put(existingProfile); + this.#putFollowingProfile(existingProfile); // Queue up to get this profile. this.queueService.enqueProfile(existingProfile.pubkey); @@ -609,8 +624,15 @@ export class ProfileService { // this.profiles.splice(index, 1); // } - async isFollowing(pubkey: string) { - const profile = await this.table.get(pubkey); + isFollowing(pubkey: string) { + const existingIndex = this.profiles.findIndex((p) => p.pubkey == pubkey); + + if (existingIndex === -1) { + return false; + } + + const profile = this.profiles[existingIndex]; + // const profile = await this.table.get(pubkey); if (!profile) { return false; diff --git a/src/app/services/storage.ts b/src/app/services/storage.ts index 646121d..fb88cde 100644 --- a/src/app/services/storage.ts +++ b/src/app/services/storage.ts @@ -1,24 +1,13 @@ import { Injectable } from '@angular/core'; import Dexie, { Table } from 'dexie'; import { DatabaseService } from './database'; -import { Circle, NostrNoteDocument, NostrProfileDocument, NostrRelayDocument } from './interfaces'; - -export interface EventItem { - id?: number; - pubkey: number; - content: string; -} - -export interface ProfileItem { - pubkey: string; - content: string; -} +import { Circle, NostrEventDocument, NostrNoteDocument, NostrProfileDocument, NostrRelayDocument } from './interfaces'; @Injectable({ providedIn: 'root', }) export class StorageService { - get events(): Table { + get events(): Table { return this.db.events; } diff --git a/src/app/shared/content-photos/content-photos.ts b/src/app/shared/content-photos/content-photos.ts index b703727..12c0e12 100644 --- a/src/app/shared/content-photos/content-photos.ts +++ b/src/app/shared/content-photos/content-photos.ts @@ -194,7 +194,7 @@ export class ContentPhotosComponent { return; } - const isFollowing = await this.profileService.isFollowing(this.events[0].pubkey); + const isFollowing = this.profileService.isFollowing(this.events[0].pubkey); if (isFollowing) { const images = this.events.flatMap((e) => { diff --git a/src/app/shared/content/content.ts b/src/app/shared/content/content.ts index 09c5242..b9cc005 100644 --- a/src/app/shared/content/content.ts +++ b/src/app/shared/content/content.ts @@ -68,7 +68,7 @@ export class ContentComponent { content = content.substring(0, content.indexOf('#[')) + '@' + profile?.name + content.substring(endIndex + 1); } - const isFollowing = await this.profileService.isFollowing(this.event.pubkey); + const isFollowing = this.profileService.isFollowing(this.event.pubkey); if (isFollowing) { const images = [...content.matchAll(ContentComponent.regexpImage)];