wip: use new storage layer

This commit is contained in:
Ren Amamiya
2023-08-15 08:29:04 +07:00
parent adca37223c
commit 6e28bcdb96
19 changed files with 182 additions and 104 deletions

View File

@@ -1,13 +1,14 @@
import { NDKCacheAdapter } from '@nostr-dev-kit/ndk';
import { NDKEvent, NDKSubscription } from '@nostr-dev-kit/ndk';
import { Store } from '@tauri-apps/plugin-store';
import { LumeStorage } from '@libs/storage/instance';
export default class TauriAdapter implements NDKCacheAdapter {
public store: Store;
public store: LumeStorage;
readonly locking: boolean;
constructor() {
this.store = new Store('.ndkcache.dat');
constructor(db: LumeStorage) {
this.store = db;
this.locking = true;
}
@@ -20,7 +21,7 @@ export default class TauriAdapter implements NDKCacheAdapter {
for (const author of filter.authors) {
for (const kind of filter.kinds) {
const key = `${author}:${kind}`;
promises.push(this.store.get(key));
promises.push(this.store.getEventByKey(key));
}
}
@@ -28,25 +29,26 @@ export default class TauriAdapter implements NDKCacheAdapter {
for (const result of results) {
if (result) {
const event = await this.store.get(result as string);
if (event) {
console.log('cache hit: ', result);
const ndkEvent = new NDKEvent(subscription.ndk, JSON.parse(event as string));
subscription.eventReceived(ndkEvent, undefined, true);
}
console.log('cache hit: ', result);
const ndkEvent = new NDKEvent(
subscription.ndk,
JSON.parse(result.event as string)
);
subscription.eventReceived(ndkEvent, undefined, true);
}
}
}
if (filter.ids) {
for (const id of filter.ids) {
const key = id;
const event = await this.store.get(key);
const cacheEvent = await this.store.getEventByID(id);
if (event) {
if (cacheEvent) {
console.log('cache hit: ', id);
const ndkEvent = new NDKEvent(subscription.ndk, JSON.parse(event as string));
const ndkEvent = new NDKEvent(
subscription.ndk,
JSON.parse(cacheEvent.event as string)
);
subscription.eventReceived(ndkEvent, undefined, true);
}
}
@@ -59,13 +61,8 @@ export default class TauriAdapter implements NDKCacheAdapter {
return new Promise((resolve) => {
Promise.all([
this.store.set(event.id, JSON.stringify(nostrEvent)),
this.store.set(key, event.id),
this.store.createEvent(key, event.id, event.kind, JSON.stringify(nostrEvent)),
]).then(() => resolve());
});
}
public save() {
return this.store.save();
}
}

View File

@@ -1,19 +1,20 @@
// inspire by: https://github.com/nostr-dev-kit/ndk-react/
import NDK from '@nostr-dev-kit/ndk';
import { fetch } from '@tauri-apps/plugin-http';
import { useEffect, useMemo, useState } from 'react';
import { useEffect, useState } from 'react';
import TauriAdapter from '@libs/ndk/cache';
import { getExplicitRelayUrls } from '@libs/storage';
import { useStorage } from '@libs/storage/provider';
import { FULL_RELAYS } from '@stores/constants';
export const NDKInstance = () => {
const { db } = useStorage();
const [ndk, setNDK] = useState<NDK | undefined>(undefined);
const [relayUrls, setRelayUrls] = useState<string[]>([]);
const cacheAdapter = useMemo(() => new TauriAdapter(), []);
// TODO: fully support NIP-11
async function verifyRelays(relays: string[]) {
const verifiedRelays: string[] = [];
@@ -57,6 +58,8 @@ export const NDKInstance = () => {
explicitRelayUrls = await verifyRelays(FULL_RELAYS);
}
const cacheAdapter = new TauriAdapter(db);
console.log('ndk cache adapter: ', cacheAdapter);
const instance = new NDK({ explicitRelayUrls, cacheAdapter });
try {
@@ -71,10 +74,6 @@ export const NDKInstance = () => {
useEffect(() => {
if (!ndk) initNDK();
return () => {
cacheAdapter.save();
};
}, []);
return {

View File

@@ -1,7 +1,8 @@
import { BaseDirectory, removeFile } from '@tauri-apps/plugin-fs';
import Database from '@tauri-apps/plugin-sql';
import { Stronghold } from '@tauri-apps/plugin-stronghold';
import { Account, Widget } from '@utils/types';
import { Account, Relays, Widget } from '@utils/types';
export class LumeStorage {
public db: Database;
@@ -39,6 +40,10 @@ export class LumeStorage {
return decoded;
}
public async secureReset() {
return await removeFile('lume.stronghold', { dir: BaseDirectory.AppConfig });
}
public async getActiveAccount() {
const account: Account = await this.db.select(
'SELECT * FROM accounts WHERE is_active = 1;'
@@ -123,7 +128,10 @@ export class LumeStorage {
'SELECT * FROM events WHERE cache_key = $1 ORDER BY id DESC LIMIT 1;',
[cacheKey]
)?.[0];
if (!event) console.error('failed to get event by cache_key: ', cacheKey);
if (!event) {
console.error('failed to get event by cache_key: ', cacheKey);
return null;
}
return event;
}
@@ -132,10 +140,42 @@ export class LumeStorage {
'SELECT * FROM events WHERE event_id = $1 ORDER BY id DESC LIMIT 1;',
[id]
)?.[0];
if (!event) console.error('failed to get event by id: ', id);
if (!event) {
console.error('failed to get event by id: ', id);
return null;
}
return event;
}
public async getExplicitRelayUrls() {
const account = await this.getActiveAccount();
const result: Relays[] = await this.db.select(
`SELECT * FROM relays WHERE account_id = "${account.id}";`
);
if (result.length > 0) return result.map((el) => el.relay);
return null;
}
public async createRelay(relay: string, purpose?: string) {
const account = await this.getActiveAccount();
return await this.db.execute(
'INSERT OR IGNORE INTO relays (account_id, relay, purpose) VALUES ($1, $2, $3);',
[account.id, relay, purpose || '']
);
}
public async removeRelay(relay: string) {
return await this.db.execute(`DELETE FROM relays WHERE relay = "${relay}";`);
}
public async updateLastLogin(time: number) {
return await this.db.execute(
'UPDATE settings SET value = $1 WHERE key = "last_login";',
[time]
);
}
public async close() {
return this.db.close();
}

View File

@@ -28,15 +28,9 @@ const StorageProvider = ({ children }: PropsWithChildren<object>) => {
};
}, []);
return (
<StorageContext.Provider
value={{
db,
}}
>
{children}
</StorageContext.Provider>
);
if (db) {
return <StorageContext.Provider value={{ db }}>{children}</StorageContext.Provider>;
}
};
const useStorage = () => {