2023-01-18 21:58:49 +00:00
|
|
|
import Dexie, { Table } from "dexie";
|
2023-03-29 12:10:22 +00:00
|
|
|
import { FullRelaySettings, HexKey, RawEvent, u256 } from "@snort/nostr";
|
|
|
|
import { MetadataCache } from "Cache";
|
2023-01-16 13:17:29 +00:00
|
|
|
|
2023-02-07 20:04:50 +00:00
|
|
|
export const NAME = "snortDB";
|
2023-04-25 11:57:09 +00:00
|
|
|
export const VERSION = 8;
|
2023-02-01 13:22:11 +00:00
|
|
|
|
|
|
|
export interface SubCache {
|
2023-02-07 20:04:50 +00:00
|
|
|
id: string;
|
|
|
|
ids: u256[];
|
|
|
|
until?: number;
|
|
|
|
since?: number;
|
2023-02-01 13:22:11 +00:00
|
|
|
}
|
2023-01-27 21:38:41 +00:00
|
|
|
|
2023-03-28 14:34:01 +00:00
|
|
|
export interface RelayMetrics {
|
|
|
|
addr: string;
|
|
|
|
events: number;
|
|
|
|
disconnects: number;
|
|
|
|
latency: number[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface UsersRelays {
|
|
|
|
pubkey: HexKey;
|
|
|
|
relays: FullRelaySettings[];
|
|
|
|
}
|
|
|
|
|
2023-04-25 11:57:09 +00:00
|
|
|
export interface EventInteraction {
|
|
|
|
id: u256;
|
|
|
|
event: u256;
|
|
|
|
by: HexKey;
|
|
|
|
reacted: boolean;
|
|
|
|
zapped: boolean;
|
|
|
|
reposted: boolean;
|
|
|
|
}
|
|
|
|
|
2023-01-27 21:38:41 +00:00
|
|
|
const STORES = {
|
2023-02-07 20:04:50 +00:00
|
|
|
users: "++pubkey, name, display_name, picture, nip05, npub",
|
2023-03-28 14:34:01 +00:00
|
|
|
relays: "++addr",
|
|
|
|
userRelays: "++pubkey",
|
2023-03-29 12:10:22 +00:00
|
|
|
events: "++id, pubkey, created_at",
|
|
|
|
dms: "++id, pubkey",
|
2023-04-25 11:57:09 +00:00
|
|
|
eventInteraction: "++id",
|
2023-02-07 20:04:50 +00:00
|
|
|
};
|
2023-01-15 01:46:13 +00:00
|
|
|
|
2023-01-15 11:06:44 +00:00
|
|
|
export class SnortDB extends Dexie {
|
2023-03-03 14:30:31 +00:00
|
|
|
ready = false;
|
2023-01-16 00:07:27 +00:00
|
|
|
users!: Table<MetadataCache>;
|
2023-03-28 14:34:01 +00:00
|
|
|
relayMetrics!: Table<RelayMetrics>;
|
|
|
|
userRelays!: Table<UsersRelays>;
|
2023-03-29 12:10:22 +00:00
|
|
|
events!: Table<RawEvent>;
|
|
|
|
dms!: Table<RawEvent>;
|
2023-04-25 11:57:09 +00:00
|
|
|
eventInteraction!: Table<EventInteraction>;
|
2023-01-15 01:46:13 +00:00
|
|
|
|
|
|
|
constructor() {
|
2023-01-27 21:38:41 +00:00
|
|
|
super(NAME);
|
2023-02-21 12:03:14 +00:00
|
|
|
this.version(VERSION).stores(STORES);
|
2023-01-15 01:46:13 +00:00
|
|
|
}
|
2023-03-03 14:30:31 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2023-01-15 01:46:13 +00:00
|
|
|
}
|
|
|
|
|
2023-01-15 11:06:44 +00:00
|
|
|
export const db = new SnortDB();
|