store events in indexeddb

This commit is contained in:
Martti Malmi 2023-02-23 23:34:51 +02:00
parent 183de8f84a
commit d035b3d061
3 changed files with 54 additions and 1 deletions

View File

@ -69,6 +69,7 @@
"bech32-buffer": "^0.2.1",
"browserify-cipher": ">=1",
"buffer": "^6.0.3",
"dexie": "^3.2.3",
"fuse.js": "^6.6.2",
"history": "5.3.0",
"htm": "^3.1.1",

View File

@ -14,11 +14,25 @@ import {
} from './lib/nostr-tools';
const bech32 = require('bech32-buffer'); /* eslint-disable-line @typescript-eslint/no-var-requires */
import { sha256 } from '@noble/hashes/sha256';
import Dexie, { Table } from 'dexie';
import localForage from 'localforage';
import { route } from 'preact-router';
import SortedLimitedEventSet from './SortedLimitedEventSet';
export class MyDexie extends Dexie {
events!: Table<Event>;
constructor() {
super('iris');
this.version(1).stores({
events: 'id, pubkey, kind', // Primary key and indexed props
});
}
}
const db = new MyDexie();
const startTime = Date.now() / 1000;
declare global {
@ -1140,7 +1154,7 @@ const Nostr = {
return false;
}
},
handleEvent(event: Event, force = false) {
handleEvent(event: Event, force = false, saveToIdb = true) {
if (!event) return;
if (this.eventsById.has(event.id) && !force) {
return;
@ -1233,6 +1247,11 @@ const Nostr = {
sub.callback && sub.callback(event);
}
}
if (saveToIdb) {
db.events.add(event).catch(() => {
// ignore
});
}
},
handleNextFutureEvent() {
if (this.futureEventIds.size === 0) {
@ -1330,6 +1349,33 @@ const Nostr = {
}
});
},
getPubKey() {
return iris.session.getKey()?.secp256k1?.rpub; // TODO use this everywhere :D
},
loadIDBEvents() {
let i = 0;
const myPub = this.getPubKey();
db.events.where({ pubkey: myPub }).each((event) => {
this.handleEvent(event, false, false);
console.log('loaded idb event', i++);
});
const follows: string[] = Array.from(this.followedByUser.get(myPub) || []);
db.events
.where('pubkey')
.anyOf(follows)
.each((event) => {
this.handleEvent(event, false, false);
console.log('loaded idb event', i++);
});
// all other events
db.events
.where('pubkey')
.noneOf([myPub, ...follows])
.each((event) => {
this.handleEvent(event, false, false);
console.log('loaded idb event', i++);
});
},
onLoggedIn() {
const key = iris.session.getKey();
const subscribe = (filters: Filter[], callback: (event: Event) => void): string => {
@ -1382,6 +1428,7 @@ const Nostr = {
this.knownUsers.add(myPub);
this.manageRelays();
this.loadLocalStorageEvents();
this.loadIDBEvents();
this.getProfile(key.secp256k1.rpub, undefined);
for (const suggestion of this.SUGGESTED_FOLLOWS) {
const hex = this.toNostrHexAddress(suggestion);

View File

@ -4664,6 +4664,11 @@ detect-node@^2.0.4:
resolved "https://registry.yarnpkg.com/detect-node/-/detect-node-2.1.0.tgz#c9c70775a49c3d03bc2c06d9a73be550f978f8b1"
integrity sha512-T0NIuQpnTvFDATNuHN5roPwSBG83rFsuO+MXXH9/3N1eFbn4wcPjttvjMLEPWJ0RGUYgQE7cGgS3tNxbqCGM7g==
dexie@^3.2.3:
version "3.2.3"
resolved "https://registry.yarnpkg.com/dexie/-/dexie-3.2.3.tgz#f35c91ca797599df8e771b998e9ae9669c877f8c"
integrity sha512-iHayBd4UYryDCVUNa3PMsJMEnd8yjyh5p7a+RFeC8i8n476BC9wMhVvqiImq5zJZJf5Tuer+s4SSj+AA3x+ZbQ==
diff-sequences@^29.3.1:
version "29.3.1"
resolved "https://registry.yarnpkg.com/diff-sequences/-/diff-sequences-29.3.1.tgz#104b5b95fe725932421a9c6e5b4bef84c3f2249e"