Notification summary

This commit is contained in:
2023-10-09 14:35:21 +01:00
parent 237ce498b7
commit b27bb47007
23 changed files with 710 additions and 60 deletions

View File

@ -1,42 +0,0 @@
import { DexieLike, DexieTableLike } from "@snort/shared";
import { MetadataCache, RelayMetrics, UsersRelays } from ".";
import { NostrEvent } from "../nostr";
const NAME = "snort-system";
const VERSION = 2;
const STORES = {
users: "++pubkey, name, display_name, picture, nip05, npub",
relayMetrics: "++addr",
userRelays: "++pubkey",
events: "++id, pubkey, created_at",
};
export class SnortSystemDb extends DexieLike {
ready = false;
users!: DexieTableLike<MetadataCache>;
relayMetrics!: DexieTableLike<RelayMetrics>;
userRelays!: DexieTableLike<UsersRelays>;
events!: DexieTableLike<NostrEvent>;
dms!: DexieTableLike<NostrEvent>;
constructor() {
super(NAME);
this.version(VERSION).stores(STORES);
}
isAvailable() {
if ("indexedDB" in window) {
return new Promise<boolean>(resolve => {
const req = window.indexedDB.open("dummy", 1);
req.onsuccess = () => {
resolve(true);
};
req.onerror = () => {
resolve(false);
};
});
}
return Promise.resolve(false);
}
}

View File

@ -1,10 +1,9 @@
import { NostrEvent } from "nostr";
import { db } from ".";
import { FeedCache } from "@snort/shared";
import { DexieTableLike, FeedCache } from "@snort/shared";
export class EventsCache extends FeedCache<NostrEvent> {
constructor() {
super("EventsCache", db.events);
constructor(table?: DexieTableLike<NostrEvent>) {
super("EventsCache", table);
}
key(of: NostrEvent): string {

View File

@ -1,8 +1,5 @@
import { FullRelaySettings, HexKey, NostrEvent, UserMetadata } from "..";
import { hexToBech32, unixNowMs } from "@snort/shared";
import { SnortSystemDb } from "./db";
export const db = new SnortSystemDb();
import { hexToBech32, unixNowMs, DexieTableLike } from "@snort/shared";
export interface MetadataCache extends UserMetadata {
/**
@ -71,3 +68,12 @@ export function mapEventToProfile(ev: NostrEvent) {
console.error("Failed to parse JSON", ev, e);
}
}
export interface SnortSystemDb {
users: DexieTableLike<MetadataCache>;
relayMetrics: DexieTableLike<RelayMetrics>;
userRelays: DexieTableLike<UsersRelays>;
events: DexieTableLike<NostrEvent>;
isAvailable(): Promise<boolean>;
}

View File

@ -1,9 +1,9 @@
import { db, RelayMetrics } from ".";
import { FeedCache } from "@snort/shared";
import { RelayMetrics } from ".";
import { DexieTableLike, FeedCache } from "@snort/shared";
export class RelayMetricCache extends FeedCache<RelayMetrics> {
constructor() {
super("RelayMetrics", db.relayMetrics);
constructor(table?: DexieTableLike<RelayMetrics>) {
super("RelayMetrics", table);
}
key(of: RelayMetrics): string {

View File

@ -1,12 +1,12 @@
import { db, MetadataCache } from ".";
import { fetchNip05Pubkey, FeedCache, LNURL } from "@snort/shared";
import { MetadataCache } from ".";
import { fetchNip05Pubkey, FeedCache, LNURL, DexieTableLike } from "@snort/shared";
export class UserProfileCache extends FeedCache<MetadataCache> {
#zapperQueue: Array<{ pubkey: string; lnurl: string }> = [];
#nip5Queue: Array<{ pubkey: string; nip05: string }> = [];
constructor() {
super("UserCache", db.users);
constructor(table?: DexieTableLike<MetadataCache>) {
super("UserCache", table);
this.#processZapperQueue();
this.#processNip5Queue();
}
@ -24,10 +24,10 @@ export class UserProfileCache extends FeedCache<MetadataCache> {
}
async search(q: string): Promise<Array<MetadataCache>> {
if (db.ready) {
if (this.table) {
// on-disk cache will always have more data
return (
await db.users
await this.table
.where("npub")
.startsWithIgnoreCase(q)
.or("name")

View File

@ -1,9 +1,9 @@
import { db, UsersRelays } from ".";
import { FeedCache } from "@snort/shared";
import { UsersRelays } from ".";
import { DexieTableLike, FeedCache } from "@snort/shared";
export class UserRelaysCache extends FeedCache<UsersRelays> {
constructor() {
super("UserRelays", db.userRelays);
constructor(table?: DexieTableLike<UsersRelays>) {
super("UserRelays", table);
}
key(of: UsersRelays): string {

View File

@ -16,8 +16,8 @@ import {
UserProfileCache,
UserRelaysCache,
RelayMetricCache,
db,
UsersRelays,
SnortSystemDb,
} from ".";
import { EventsCache } from "./cache/events";
import { RelayCache } from "./gossip-model";
@ -87,19 +87,21 @@ export class NostrSystem extends ExternalStore<SystemSnapshot> implements System
relayMetrics?: FeedCache<RelayMetrics>;
eventsCache?: FeedCache<NostrEvent>;
queryOptimizer?: QueryOptimizer;
db?: SnortSystemDb;
}) {
super();
this.#handleAuth = props.authHandler;
this.#relayCache = props.relayCache ?? new UserRelaysCache();
this.#profileCache = props.profileCache ?? new UserProfileCache();
this.#relayMetricsCache = props.relayMetrics ?? new RelayMetricCache();
this.#eventsCache = props.eventsCache ?? new EventsCache();
this.#relayCache = props.relayCache ?? new UserRelaysCache(props.db?.userRelays);
this.#profileCache = props.profileCache ?? new UserProfileCache(props.db?.users);
this.#relayMetricsCache = props.relayMetrics ?? new RelayMetricCache(props.db?.relayMetrics);
this.#eventsCache = props.eventsCache ?? new EventsCache(props.db?.events);
this.#queryOptimizer = props.queryOptimizer ?? DefaultQueryOptimizer;
this.#profileLoader = new ProfileLoaderService(this, this.#profileCache);
this.#relayMetrics = new RelayMetricHandler(this.#relayMetricsCache);
this.#cleanup();
}
HandleAuth?: AuthHandler | undefined;
get ProfileLoader() {
@ -122,7 +124,6 @@ export class NostrSystem extends ExternalStore<SystemSnapshot> implements System
* Setup caches
*/
async Init() {
db.ready = await db.isAvailable();
const t = [
this.#relayCache.preload(),
this.#profileCache.preload(),