web


git clone https://radroots.dev/git/web.git
Log | Files | Refs | Submodules | README | LICENSE

commit 985221965d4fd66fd8e6f96d98353cbd41f9736a
parent 57894938ca09df2820645f33b204de232309f6aa
Author: triesap <137732411+triesap@users.noreply.github.com>
Date:   Tue,  4 Mar 2025 08:15:52 +0000

Edit `app` removing key_nostr store and nostr sync utils.

Diffstat:
Dapp/src/lib/util/nostr/nostr-sync.ts | 131-------------------------------------------------------------------------------
Mapp/src/routes/(app)/+layout.svelte | 17++++++++---------
Mapp/src/routes/(app)/+layout.ts | 16++++++++++------
Mapp/src/routes/(cfg)/+layout.ts | 3+--
4 files changed, 19 insertions(+), 148 deletions(-)

diff --git a/app/src/lib/util/nostr/nostr-sync.ts b/app/src/lib/util/nostr/nostr-sync.ts @@ -1,131 +0,0 @@ -import { ls } from "$lib/locale/i18n"; -import { db, gui, nostre } from "$lib/util"; -import { cfg_nostr_client } from "$lib/util/conf"; -import { NDKKind } from "@nostr-dev-kit/ndk"; -import { get_store, handle_err, key_nostr, ndk, ndk_user, nostr_sync_prevent } from "@radroots/lib-app"; -import type { NostrRelay } from "@radroots/models"; -import { num_str, throw_err } from "@radroots/util"; -import { err } from "../err"; -import { ndk_event_metadata, ndk_event } from "@radroots/nostr-util"; - -export const nostr_sync_metadata = async (): Promise<void> => { - try { - const $ndk = get_store(ndk); - const $ndk_user = get_store(ndk_user); - const $key_nostr = get_store(key_nostr); - const tbl_nostr_profile = await db.nostr_profile_read({ - public_key: $key_nostr - }); - if (`err` in tbl_nostr_profile) return throw_err(tbl_nostr_profile); - const ev_metadata = await ndk_event_metadata({ - $ndk, - $ndk_user, - metadata: tbl_nostr_profile.result - }); - if (ev_metadata) await ev_metadata.publish(); - } catch (e) { - await handle_err(e, `nostr_sync_metadata`); - } -}; - -export const nostr_sync_classified = async (nostr_relays: NostrRelay[]): Promise<void> => { - try { - const $ndk = get_store(ndk); - const $ndk_user = get_store(ndk_user); - const tbl_trade_products = await db.trade_product_read_list(); - if (`err` in tbl_trade_products) return throw_err(tbl_trade_products); - for (const trade_product of tbl_trade_products.results) { - const tbl_location_gcss = await db.location_gcs_read_list({ - table: [`on_trade_product`, { id: trade_product.id }], - }); - if (`err` in tbl_location_gcss) return throw_err(tbl_location_gcss); - const trade_product_location = tbl_location_gcss.results[0]; - const tbl_media_uploads = await db.media_image_read_list({ - table: [`on_trade_product`, { id: trade_product.id }], - }); - if (`err` in tbl_media_uploads) return throw_err(tbl_media_uploads); - const ev = await ndk_event({ - $ndk, - $ndk_user, - basis: { - kind: NDKKind.Classified, - content: ``, - tags: nostre.fmt_tags_basis_nip99({ - d_tag: trade_product.id, - client: cfg_nostr_client, - listing: { - key: trade_product.key, - category: trade_product.category, - title: trade_product.title, - summary: trade_product.summary, - process: trade_product.process, - lot: trade_product.lot, - profile: trade_product.profile, - year: num_str(trade_product.year), - }, - quantity: { - amt: num_str(trade_product.qty_amt), - unit: trade_product.qty_unit, - label: trade_product.qty_label - }, - price: { - amt: num_str(trade_product.price_amt), - currency: trade_product.price_currency, - qty_amt: num_str(trade_product.price_qty_amt), - qty_unit: trade_product.price_qty_unit, - }, - location: { - city: trade_product_location.gc_name, - region: trade_product_location.gc_admin1_name, - region_code: trade_product_location.gc_admin1_id, - country: trade_product_location.gc_country_name, - country_code: trade_product_location.gc_country_id, - lat: trade_product_location.lat, - lng: trade_product_location.lng, - geohash: trade_product_location.geohash, - }, - images: tbl_media_uploads.results.length ? tbl_media_uploads.results.map(i => ({ url: `${i.res_base}/${i.res_path}.${i.mime_type}` })) : undefined - }), - }, - }); - if (ev) { - ev.content = `radroots:[nostr:${nostre.nevent_encode({ - id: ev.id, - author: ev.pubkey, - relays: nostr_relays.map(i => i.url), - kind: NDKKind.Classified, - })}]` - await ev.publish(); - } - } - } catch (e) { - await handle_err(e, `nostr_sync_classified`); - } -}; - -export const nostr_sync = async (): Promise<void> => { - try { - const $nostr_sync_prevent = get_store(nostr_sync_prevent); - const $lls = get_store(ls); - const $key_nostr = get_store(key_nostr); - if ($nostr_sync_prevent) { - const confirm = await gui.confirm({ - message: `${$lls(`error.client.nostr_sync_disabled`)}`, - }); - if (confirm) { - nostr_sync_prevent.set(false); - await nostr_sync(); - } - return; - } - const tbl_nostr_relays = await db.nostr_relay_read_list({ - table: [`on_profile`, { public_key: $key_nostr }], - }); - if (`err` in tbl_nostr_relays) return throw_err(tbl_nostr_relays); - if (!tbl_nostr_relays.results.length) return throw_err(err.nostr.no_relays); - await nostr_sync_metadata(); - await nostr_sync_classified(tbl_nostr_relays.results); - } catch (e) { - await handle_err(e, `nostr_sync`); - } -}; diff --git a/app/src/routes/(app)/+layout.svelte b/app/src/routes/(app)/+layout.svelte @@ -3,7 +3,6 @@ import { app_splash, handle_err, - key_nostr, kv_init, ndk, ndk_user, @@ -15,7 +14,7 @@ import { onMount } from "svelte"; import type { LayoutProps } from "./$types"; - let { children }: LayoutProps = $props(); + let { children, data }: LayoutProps = $props(); onMount(async () => { try { @@ -42,25 +41,25 @@ const nostr_init = async (): Promise<void> => { try { - if (!$key_nostr) return void throw_err(`*-key_nostr`); - const keys_nostr_read = await keys.nostr_read($key_nostr); + if (!data.public_key) return void throw_err(`*-key_nostr`); + const keys_nostr_read = await keys.nostr_read(data.public_key); if (`err` in keys_nostr_read) return void throw_err(keys_nostr_read.err); const tb_nostr_relays = await db.nostr_relay_read_list({ - table: [`on_profile`, { public_key: $key_nostr }], + table: [`on_profile`, { public_key: data.public_key }], }); if (`err` in tb_nostr_relays) return void throw_err(tb_nostr_relays.err); for (const { url } of tb_nostr_relays.results) $ndk.addExplicitRelay(url); await $ndk.connect(); - const _ndk_user = await ndk_init({ + const ndk_user_init = await ndk_init({ $ndk, secret_key: keys_nostr_read.secret_key, }); - nostr_ndk_configured.set(!!_ndk_user); - if (!_ndk_user) return; - $ndk_user = _ndk_user; + nostr_ndk_configured.set(!!ndk_user_init); + if (!ndk_user_init) return; //@todo + $ndk_user = ndk_user_init; $ndk_user.ndk = $ndk; } catch (e) { await handle_err(e, `nostr_init`); diff --git a/app/src/routes/(app)/+layout.ts b/app/src/routes/(app)/+layout.ts @@ -1,19 +1,23 @@ import { datastore, keys, route } from '$lib/util'; -import { handle_err, key_nostr } from '@radroots/lib-app'; +import { handle_err } from '@radroots/lib-app'; import type { LayoutLoad, LayoutLoadEvent } from '../$types'; -export const load: LayoutLoad = async (_: LayoutLoadEvent) => { +export type LayoutAppLoad = { + public_key: string; +} | undefined; + +export const load: LayoutLoad<LayoutAppLoad> = async (_: LayoutLoadEvent) => { try { await datastore.init(); const ks_keynostr = await datastore.get(`key_nostr`); if (`err` in ks_keynostr) return void await route(`/init`); const nostrkey = await keys.nostr_read(ks_keynostr.result); if (`err` in nostrkey) return void await route(`/init`); - key_nostr.set(ks_keynostr.result); + return { + public_key: ks_keynostr.result + }; } catch (e) { - await handle_err(e, `(app)load`) - } finally { - return {}; + await handle_err(e, `(app)load`); }; }; diff --git a/app/src/routes/(cfg)/+layout.ts b/app/src/routes/(cfg)/+layout.ts @@ -1,6 +1,6 @@ import { datastore, keys, route } from '$lib/util'; -import { handle_err, key_nostr } from '@radroots/lib-app'; +import { handle_err } from '@radroots/lib-app'; import type { LayoutLoad, LayoutLoadEvent } from './$types'; export const load: LayoutLoad = async (_: LayoutLoadEvent) => { @@ -12,7 +12,6 @@ export const load: LayoutLoad = async (_: LayoutLoadEvent) => { if (`result` in nostrkey) return void await route(`/`); await datastore.remove(`key_nostr`); } - key_nostr.set(``); } catch (e) { await handle_err(e, `(cfg)load`) } finally {