Persist events from following

This commit is contained in:
SondreB 2023-01-16 13:41:31 +01:00
parent db88cbece4
commit 2802281742
No known key found for this signature in database
GPG Key ID: D6CC44C75005FDBF
6 changed files with 40 additions and 33 deletions

View File

@ -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);

View File

@ -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<NostrRelayDocument, string>;
events!: Table<EventItem, string>;
events!: Table<NostrEventDocument, string>;
notes!: Table<NostrNoteDocument, string>;
profiles!: Table<NostrProfileDocument, string>;
circles!: Table<Circle, number>;

View File

@ -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<NostrProfileDocument[]> = new BehaviorSubject<NostrProfileDocument[]>(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;

View File

@ -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<EventItem, string> {
get events(): Table<NostrEventDocument, string> {
return this.db.events;
}

View File

@ -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) => {

View File

@ -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)];