From 60e93965eab88cf5e7a5ef28312cad4e0fe92321 Mon Sep 17 00:00:00 2001 From: Ren Amamiya <123083837+reyamir@users.noreply.github.com> Date: Tue, 19 Sep 2023 08:01:57 +0700 Subject: [PATCH] respect user's relay list (kind 10002) --- .../20230918235335_add_uniq_to_relay.sql | 2 ++ src-tauri/src/main.rs | 6 ++++ src/app/auth/import/step-2.tsx | 2 +- src/app/auth/import/step-3.tsx | 4 +-- src/libs/ndk/instance.ts | 32 +++++++++---------- src/libs/storage/instance.ts | 7 ++-- src/utils/hooks/useNostr.ts | 3 +- 7 files changed, 30 insertions(+), 26 deletions(-) create mode 100644 src-tauri/migrations/20230918235335_add_uniq_to_relay.sql diff --git a/src-tauri/migrations/20230918235335_add_uniq_to_relay.sql b/src-tauri/migrations/20230918235335_add_uniq_to_relay.sql new file mode 100644 index 00000000..7bb950ba --- /dev/null +++ b/src-tauri/migrations/20230918235335_add_uniq_to_relay.sql @@ -0,0 +1,2 @@ +-- Add migration script here +CREATE UNIQUE INDEX unique_relay ON relays (relay); \ No newline at end of file diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index a8ca4f67..901ab4b7 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -211,6 +211,12 @@ fn main() { sql: include_str!("../migrations/20230817014932_add_last_login_time_to_account.sql"), kind: MigrationKind::Up, }, + Migration { + version: 20230918235335, + description: "add unique to relay", + sql: include_str!("../migrations/20230918235335_add_uniq_to_relay.sql"), + kind: MigrationKind::Up, + }, ], ) .build(), diff --git a/src/app/auth/import/step-2.tsx b/src/app/auth/import/step-2.tsx index 255e58b7..42da6e81 100644 --- a/src/app/auth/import/step-2.tsx +++ b/src/app/auth/import/step-2.tsx @@ -93,7 +93,7 @@ export function ImportStep2Screen() {

Password is not related to your Nostr account. It is only used to secure your keys stored on your local machine and to unlock the app (like unlocking your - phone with a passcode). When you move to other Nostr clients, you just need to + phone with a passcode). When you move to other Nostr clients, you only need to copy your private key.

diff --git a/src/app/auth/import/step-3.tsx b/src/app/auth/import/step-3.tsx index 96c9a26d..66f9f6dc 100644 --- a/src/app/auth/import/step-3.tsx +++ b/src/app/auth/import/step-3.tsx @@ -82,8 +82,8 @@ export function ImportStep3Screen() { )} - By clicking 'Continue', Lume will download your relay list and all - events from the last 24 hours. It may take a bit + By clicking 'Continue', Lume will download your old relay list and + all events from the last 24 hours. It may take a bit diff --git a/src/libs/ndk/instance.ts b/src/libs/ndk/instance.ts index 334b9bc9..57211b13 100644 --- a/src/libs/ndk/instance.ts +++ b/src/libs/ndk/instance.ts @@ -1,6 +1,7 @@ // inspire by: https://github.com/nostr-dev-kit/ndk-react/ import NDK from '@nostr-dev-kit/ndk'; import { message } from '@tauri-apps/api/dialog'; +import { fetch } from '@tauri-apps/api/http'; import { useEffect, useMemo, useState } from 'react'; import TauriAdapter from '@libs/ndk/cache'; @@ -16,39 +17,36 @@ export const NDKInstance = () => { // TODO: fully support NIP-11 async function getExplicitRelays() { try { - const controller = new AbortController(); - const timeoutId = setTimeout(() => controller.abort('timeout'), 10000); - // get relays const relays = await db.getExplicitRelayUrls(); - const requests = relays.map((relay) => { const url = new URL(relay); return fetch(`https://${url.hostname + url.pathname}`, { - headers: { Accept: 'application/nostr+json' }, - signal: controller.signal, + method: 'GET', + timeout: 10, + headers: { + Accept: 'application/nostr+json', + }, }); }); const responses = await Promise.all(requests); const successes = responses.filter((res) => res.ok); - const errors = responses.filter((res) => !res.ok); - - if (errors.length > 0) throw errors.map((response) => Error(response.statusText)); const verifiedRelays: string[] = successes.map((res) => { - const url = new URL(res.url); - if (url.protocol === 'http:') return `ws://${url.hostname + url.pathname}`; - if (url.protocol === 'https:') return `wss://${url.hostname + url.pathname}`; + // TODO: support payment + // @ts-expect-error, not have type yet + if (!res.data.limitation?.payment_required) { + const url = new URL(res.url); + if (url.protocol === 'http:') return `ws://${url.hostname + url.pathname}`; + if (url.protocol === 'https:') return `wss://${url.hostname + url.pathname}`; + } }); - // clear timeout - clearTimeout(timeoutId); - - // return all validate relays + // return all validated relays return verifiedRelays; } catch (e) { - await message(e, { title: 'Cannot connect to relays', type: 'error' }); + console.error(e); } } diff --git a/src/libs/storage/instance.ts b/src/libs/storage/instance.ts index 1114c796..6fa39fab 100644 --- a/src/libs/storage/instance.ts +++ b/src/libs/storage/instance.ts @@ -283,15 +283,14 @@ export class LumeStorage { } public async getExplicitRelayUrls() { - if (!this.account) return null; + if (!this.account) return FULL_RELAYS; const result: Relays[] = await this.db.select( `SELECT * FROM relays WHERE account_id = "${this.account.id}" ORDER BY id DESC LIMIT 50;` ); - if (result.length < 1) return FULL_RELAYS; - // return [...new Set(result.map((el) => el.relay))]; - return FULL_RELAYS; + if (!result || result.length < 1) return FULL_RELAYS; + return result.map((el) => el.relay); } public async createRelay(relay: string, purpose?: string) { diff --git a/src/utils/hooks/useNostr.ts b/src/utils/hooks/useNostr.ts index 7beffb7b..627da89f 100644 --- a/src/utils/hooks/useNostr.ts +++ b/src/utils/hooks/useNostr.ts @@ -57,7 +57,7 @@ export function useNostr() { const follows = new Set(preFollows || []); const lruNetwork = new LRUCache({ max: 300 }); - /* fetch user's relays + // fetch user's relays const relayEvents = await ndk.fetchEvents({ kinds: [NDKKind.RelayList], authors: [db.account.pubkey], @@ -74,7 +74,6 @@ export function useNostr() { } } } - */ // fetch user's follows if (!preFollows) {