diff --git a/package.json b/package.json index 04aac901..a908982e 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "@tauri-apps/plugin-os": "github:tauri-apps/tauri-plugin-os#v2", "@tauri-apps/plugin-process": "github:tauri-apps/tauri-plugin-process#v2", "@tauri-apps/plugin-sql": "github:tauri-apps/tauri-plugin-sql#v2", + "@tauri-apps/plugin-store": "github:tauri-apps/tauri-plugin-store#v2", "@tauri-apps/plugin-stronghold": "github:tauri-apps/tauri-plugin-stronghold#v2", "@tauri-apps/plugin-upload": "github:tauri-apps/tauri-plugin-upload#v2", "@tauri-apps/plugin-window": "2.0.0-alpha.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 49907bb2..5809b971 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -61,6 +61,9 @@ dependencies: '@tauri-apps/plugin-sql': specifier: github:tauri-apps/tauri-plugin-sql#v2 version: github.com/tauri-apps/tauri-plugin-sql/0591e9f63e0f86be79c209f4bcb564ccce4bd05c + '@tauri-apps/plugin-store': + specifier: github:tauri-apps/tauri-plugin-store#v2 + version: github.com/tauri-apps/tauri-plugin-store/2f5e470ba6d746b54da46fcb3eb10325fa224351 '@tauri-apps/plugin-stronghold': specifier: github:tauri-apps/tauri-plugin-stronghold#v2 version: github.com/tauri-apps/tauri-plugin-stronghold/a87861766f3520b5fc1ea1f34665d814bbee9b06 @@ -7755,6 +7758,14 @@ packages: '@tauri-apps/api': 2.0.0-alpha.5 dev: false + github.com/tauri-apps/tauri-plugin-store/2f5e470ba6d746b54da46fcb3eb10325fa224351: + resolution: {tarball: https://codeload.github.com/tauri-apps/tauri-plugin-store/tar.gz/2f5e470ba6d746b54da46fcb3eb10325fa224351} + name: '@tauri-apps/plugin-store' + version: 2.0.0-alpha.0 + dependencies: + '@tauri-apps/api': 2.0.0-alpha.5 + dev: false + github.com/tauri-apps/tauri-plugin-stronghold/a87861766f3520b5fc1ea1f34665d814bbee9b06: resolution: {tarball: https://codeload.github.com/tauri-apps/tauri-plugin-stronghold/tar.gz/a87861766f3520b5fc1ea1f34665d814bbee9b06} name: '@tauri-apps/plugin-stronghold' diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 21afde2e..6af57852 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -2582,6 +2582,7 @@ dependencies = [ "tauri-plugin-process", "tauri-plugin-single-instance", "tauri-plugin-sql", + "tauri-plugin-store", "tauri-plugin-stronghold", "tauri-plugin-updater", "tauri-plugin-upload", @@ -4981,6 +4982,18 @@ dependencies = [ "tokio", ] +[[package]] +name = "tauri-plugin-store" +version = "2.0.0-alpha.0" +source = "git+https://github.com/tauri-apps/plugins-workspace?branch=v2#6f01bc11ab5be762d6cbe0ca924f15cdde47ce0d" +dependencies = [ + "log", + "serde", + "serde_json", + "tauri", + "thiserror", +] + [[package]] name = "tauri-plugin-stronghold" version = "2.0.0-alpha.0" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index e395769c..cbbd13aa 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -31,6 +31,7 @@ tauri-plugin-app = { git = "https://github.com/tauri-apps/plugins-workspace", br tauri-plugin-process = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" } tauri-plugin-os = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" } tauri-plugin-window = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" } +tauri-plugin-store = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" } sqlx-cli = { version = "0.7.0", default-features = false, features = [ "sqlite", ] } diff --git a/src/libs/ndk/cache.tsx b/src/libs/ndk/cache.tsx new file mode 100644 index 00000000..273fa72b --- /dev/null +++ b/src/libs/ndk/cache.tsx @@ -0,0 +1,56 @@ +import { NDKCacheAdapter } from '@nostr-dev-kit/ndk'; +import { NDKEvent, NDKSubscription } from '@nostr-dev-kit/ndk'; +import { Store } from '@tauri-apps/plugin-store'; + +export default class TauriAdapter implements NDKCacheAdapter { + public store: Store; + readonly locking: boolean; + + constructor() { + this.store = new Store('.ndkcache.dat'); + this.locking = true; + } + + public async query(subscription: NDKSubscription): Promise { + const { filter } = subscription; + + // if this filter uses both authors and kinds, then we need to query for each combination of author and kind + // and then combine the results + if (filter.authors && filter.kinds) { + const promises = []; + + for (const author of filter.authors) { + for (const kind of filter.kinds) { + const key = `${author}:${kind}`; + promises.push(this.store.get(key)); + } + } + + const results = await Promise.all(promises); + + for (const result of results) { + if (result) { + const event = await this.store.get(result as string); + + if (event) { + const ndkEvent = new NDKEvent(subscription.ndk, JSON.parse(event as string)); + subscription.eventReceived(ndkEvent, undefined, true); + } + } + } + } + } + + public async setEvent(event: NDKEvent): Promise { + const nostrEvent = await event.toNostrEvent(); + const key = `${nostrEvent.pubkey}:${nostrEvent.kind}`; + + return new Promise((resolve) => { + Promise.all([ + this.store.set(event.id, JSON.stringify(nostrEvent)), + this.store.set(key, event.id), + this.store.save(), + ]).then(() => resolve()); + }); + } +} diff --git a/src/libs/ndk/instance.ts b/src/libs/ndk/instance.ts index d73572ad..76d00567 100644 --- a/src/libs/ndk/instance.ts +++ b/src/libs/ndk/instance.ts @@ -4,6 +4,7 @@ import { ndkAdapter } from '@nostr-fetch/adapter-ndk'; import { NostrFetcher, normalizeRelayUrlSet } from 'nostr-fetch'; import { useEffect, useState } from 'react'; +import TauriAdapter from '@libs/ndk/cache'; import { getSetting } from '@libs/storage'; const setting = await getSetting('relays'); @@ -19,7 +20,8 @@ export const NDKInstance = () => { }, []); async function loadNdk(explicitRelayUrls: string[]) { - const ndkInstance = new NDK({ explicitRelayUrls }); + const cacheAdapter = new TauriAdapter(); + const ndkInstance = new NDK({ explicitRelayUrls, cacheAdapter }); try { await ndkInstance.connect();