commit 2763a8c40f4b1528c76b8136f02cca83e2ffd276
parent c3110f7ab2801b363e74d47f01207eb7c48677d3
Author: triesap <137732411+triesap@users.noreply.github.com>
Date: Thu, 3 Oct 2024 07:23:32 +0000
Edit nostr relay document polling subscriber adding recursive fetch request, edit models nostr relay route, update IClient
Diffstat:
6 files changed, 162 insertions(+), 69 deletions(-)
diff --git a/src/lib/client.ts b/src/lib/client.ts
@@ -1,17 +1,22 @@
+import { PUBLIC_DATABASE_NAME } from "$env/static/public";
import { ClientCapacitor } from "@radroots/client";
import { location_gcs_table, nostr_profile_relay_table, nostr_profile_table, nostr_relay_table, trade_product_table } from "@radroots/models";
+
export const lc = new ClientCapacitor({
- sqlite_upgrade: [
- {
- toVersion: 1,
- statements: [
- `PRAGMA foreign_keys = ON;`,
- location_gcs_table,
- trade_product_table,
- nostr_profile_table,
- nostr_relay_table,
- nostr_profile_relay_table
- ]
- }
- ]
+ sqlite: {
+ database: PUBLIC_DATABASE_NAME,
+ upgrade: [
+ {
+ toVersion: 1,
+ statements: [
+ `PRAGMA foreign_keys = ON;`,
+ location_gcs_table,
+ trade_product_table,
+ nostr_profile_table,
+ nostr_relay_table,
+ nostr_profile_relay_table
+ ]
+ }
+ ]
+ }
});
diff --git a/src/lib/conf.ts b/src/lib/conf.ts
@@ -26,7 +26,8 @@ export const _conf = {
delay: {
load: 321,
notify: 123,
- mount_el: 500
+ mount_el: 500,
+ nostr_relay_poll_document: 3000
},
kv: {
nostr_key: (public_key: string) => `nostr:key:${public_key}`,
diff --git a/src/routes/(app)/+page.svelte b/src/routes/(app)/+page.svelte
@@ -68,6 +68,15 @@
label: `Relays`,
key: `network`,
weight: `fill`,
+ callback: async () => {
+ nav_prev.set([
+ ...$nav_prev,
+ {
+ route: `/`,
+ label: `Home`,
+ },
+ ]);
+ },
},
];
</script>
diff --git a/src/routes/(app)/models/nostr-relay/+page.svelte b/src/routes/(app)/models/nostr-relay/+page.svelte
@@ -3,10 +3,11 @@
import { app_nostr_key } from "$lib/stores";
import type { NostrRelay } from "@radroots/models";
import {
+ GlyphCircle,
LayoutTrellis,
LayoutView,
Nav,
- sleep,
+ nostr_relays_connected,
t,
Trellis,
type ITrellisKindTouch,
@@ -56,8 +57,8 @@
}
};
- let tr_list_1: ITrellisKindTouch[] = [];
- $: tr_list_1 = ld
+ let tr_list_relays: ITrellisKindTouch[] = [];
+ $: tr_list_relays = ld?.nostr_relays.length
? ld.nostr_relays.map((nostr_relay) => ({
layer: 1,
hide_active: show_edit,
@@ -77,11 +78,16 @@
offset: show_edit
? {
mod: {
- classes: `text-layer-2-glyph opacity-40 fade-in tap-scale`,
- key: `minus-circle`,
- weight: `fill`,
- dim: `sm`,
- loading: loading_edit_id === nostr_relay.id,
+ glyph_circle: {
+ classes_wrap: `bg-red-400/80`,
+ glyph: {
+ classes: `text-white fade-in tap-scale`,
+ key: `minus`,
+ weight: `bold`,
+ dim: `xs-`,
+ loading: loading_edit_id === nostr_relay.id,
+ },
+ },
},
callback: async () => {
await handle_disconnect_relay(nostr_relay);
@@ -89,10 +95,27 @@
}
: {
mod: {
- classes: `text-layer-2-glyph-hl group-active:opacity-60 fade-in`,
- key: `check-circle`,
- weight: `fill`,
- dim: `sm`,
+ glyph_circle: {
+ classes_wrap: $nostr_relays_connected.includes(
+ nostr_relay.id,
+ )
+ ? `bg-layer-1-glyph-hl/60 group-active:opacity-40`
+ : `bg-amber-700/80 group-active:opacity-40`,
+ glyph: {
+ classes: $nostr_relays_connected.includes(
+ nostr_relay.id,
+ )
+ ? `text-white group-active:opacity-60 fade-in`
+ : `text-amber-200 group-active:opacity-60 fade-in`,
+ key: $nostr_relays_connected.includes(
+ nostr_relay.id,
+ )
+ ? `check`
+ : `x`,
+ weight: `bold`,
+ dim: `xs-`,
+ },
+ },
},
callback: async (ev) => {
ev.stopPropagation();
@@ -111,9 +134,7 @@
);
if (confirm === false) return;
- console.log(`unset the relay`);
- console.log(`relay_id `, nostr_relay.id);
- await sleep(500);
+ alert(`@todo`);
} catch (e) {
console.log(`(error) handle_disconnect_relay `, e);
} finally {
@@ -125,7 +146,17 @@
<LayoutView>
<LayoutTrellis>
- {#if ld}
+ <GlyphCircle
+ basis={{
+ classes_wrap: `bg-red-400`,
+ glyph: {
+ classes: `text-white`,
+ key: `arrow-circle-up`,
+ dim: `md`,
+ },
+ }}
+ />
+ {#if tr_list_relays.length}
<Trellis
basis={{
args: {
@@ -133,7 +164,7 @@
title: {
value: `${$t(`icu.connected_*`, { value: `${$t(`common.relays`)}` })}`,
},
- list: [...tr_list_1],
+ list: tr_list_relays,
},
}}
/>
diff --git a/src/routes/(conf)/init/+page.svelte b/src/routes/(conf)/init/+page.svelte
@@ -131,8 +131,12 @@
return;
}
await lc.db.set_nostr_profile_relay({
- nostr_profile_id: nostr_profile_add.id,
- nostr_relay_id: nostr_relay_add.id,
+ nostr_profile: {
+ id: nostr_profile_add.id,
+ },
+ nostr_relay: {
+ id: nostr_relay_add.id,
+ },
});
}
diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte
@@ -1,6 +1,5 @@
<script lang="ts">
import { browser } from "$app/environment";
- import { PUBLIC_DATABASE_NAME } from "$env/static/public";
import { lc } from "$lib/client";
import { _conf } from "$lib/conf";
import {
@@ -11,8 +10,8 @@
app_thc,
} from "$lib/stores";
import {
- parse_nostr_relay_form_keys,
type NostrRelayFormFields,
+ parse_nostr_relay_form_keys,
} from "@radroots/models";
import {
app_config,
@@ -20,9 +19,12 @@
app_render,
AppConfig,
CssStatic,
+ CssStyles,
ndk,
ndk_init,
ndk_user,
+ nostr_relays_connected,
+ nostr_relays_poll_documents,
route,
sleep,
theme_set,
@@ -66,7 +68,7 @@
app_config.subscribe(async (app_config) => {
try {
if (!app_config) return;
- const db_connected = await lc.db.connect(PUBLIC_DATABASE_NAME);
+ const db_connected = await lc.db.connect();
if (!db_connected) {
// @todo
}
@@ -136,39 +138,7 @@
return;
}
- for (const { url } of nostr_relays) {
- $ndk.addExplicitRelay(url);
- const response = await lc.http.fetch({
- url: url.replace(`ws://`, `http://`),
- headers: {
- Accept: "application/nostr+json",
- },
- });
- if (typeof response === `string`) {
- console.log(`response `, response);
- return;
- }
-
- if (response.status === 200 && response.data) {
- const info_doc =
- parse_nostr_relay_information_document_fields(
- response.data,
- );
- if (!info_doc) return;
- const fields: Partial<NostrRelayFormFields> = {};
- for (const [k, v] of Object.entries(info_doc)) {
- const field_k = parse_nostr_relay_form_keys(k);
- if (field_k) fields[field_k] = v;
- }
- if (Object.keys(fields).length < 1) return;
- await lc.db.nostr_relay_update({
- on: {
- url,
- },
- fields,
- });
- }
- }
+ for (const { url } of nostr_relays) $ndk.addExplicitRelay(url);
await $ndk.connect().then(() => {
console.log(`(ndk) connected`);
@@ -186,6 +156,7 @@
$ndk_user = ndk_user;
$ndk_user.ndk = $ndk;
console.log(`(ndk) initialized`);
+ nostr_relays_poll_documents.set(true);
} catch (e) {
console.log(`(app_nostr_key) error `, e);
}
@@ -198,6 +169,77 @@
lc.dialog.alert(_app_notify);
app_notify.set(``);
});
+
+ nostr_relays_poll_documents.subscribe(
+ async (_nostr_relays_poll_documents) => {
+ try {
+ if (!_nostr_relays_poll_documents) return;
+ await fetch_relay_documents();
+ } catch (e) {
+ console.log(`(error) nostr_relays_poll_documents`, e);
+ }
+ },
+ );
+
+ const fetch_relay_documents = async (): Promise<void> => {
+ try {
+ const nostr_relays = await lc.db.nostr_relay_get({
+ list: ["on_key", { public_key: $app_nostr_key }],
+ });
+ if (typeof nostr_relays === `string`) throw new Error();
+
+ const unconnected_relays = nostr_relays.filter(
+ (i) => !$nostr_relays_connected.includes(i.id),
+ );
+ if (unconnected_relays.length === 0) {
+ nostr_relays_poll_documents.set(false);
+ return;
+ }
+
+ for (const nostr_relay of unconnected_relays) {
+ const res = await lc.http.fetch({
+ url: nostr_relay.url.replace(`ws://`, `http://`),
+ headers: {
+ Accept: "application/nostr+json",
+ },
+ });
+ if (typeof res === `string`) continue;
+ else if (res.status === 200 && res.data) {
+ const doc = parse_nostr_relay_information_document_fields(
+ res.data,
+ );
+ if (!doc) continue;
+ const fields: Partial<NostrRelayFormFields> = {};
+ for (const [k, v] of Object.entries(doc)) {
+ const field_k = parse_nostr_relay_form_keys(k);
+ if (field_k) fields[field_k] = v;
+ }
+ if (Object.keys(fields).length < 1) continue;
+ await lc.db.nostr_relay_update({
+ on: {
+ url: nostr_relay.url,
+ },
+ fields,
+ });
+ nostr_relays_connected.set(
+ Array.from(
+ new Set([
+ ...$nostr_relays_connected,
+ nostr_relay.id,
+ ]),
+ ),
+ );
+ }
+ }
+
+ setTimeout(
+ fetch_relay_documents,
+ _conf.delay.nostr_relay_poll_document,
+ );
+ } catch (e) {
+ console.log(`(error) fetch_relay_documents `, e);
+ }
+ };
</script>
<svelte:head>
@@ -210,6 +252,7 @@
{/if}
<AppConfig />
<CssStatic />
+<CssStyles />
<div
class="hidden h-nav_base pt-h_nav_base pb-h_nav_base h-nav_lg pt-h_nav_lg pb-h_nav_lg h-tabs_base pt-h_tabs_base pb-h_tabs_base h-tabs_lg pt-h_tabs_lg pb-h_tabs_lg top-dim_map_offset_top_base top-dim_map_offset_top_lg"
></div>