commit eb0d458e595b4653a5cda5c65b373161157c8bd0
parent f1abc36a0527af45952524b5a20702f41c4a195f
Author: triesap <137732411+triesap@users.noreply.github.com>
Date: Sun, 27 Apr 2025 03:35:15 +0000
apps-lib: edit nostr utils, services
Diffstat:
3 files changed, 43 insertions(+), 4 deletions(-)
diff --git a/apps-lib/src/lib/index.ts b/apps-lib/src/lib/index.ts
@@ -118,4 +118,3 @@ export { default as Notifications } from "./view/notifications.svelte"
export { default as ProfileEdit } from "./view/profile-edit.svelte"
export { default as Profile } from "./view/profile.svelte"
export { default as Settings } from "./view/settings.svelte"
-
diff --git a/apps-lib/src/lib/util/nostr/nostr-sync.ts b/apps-lib/src/lib/util/nostr/nostr-sync.ts
@@ -1,4 +1,6 @@
-import { get_store, nostr_sync_attempts, nostr_sync_attempts_max, nostr_sync_stop } from "$root";
+import { get_store, handle_err, ndk_user, nostr_sync, nostr_sync_attempts, nostr_sync_attempts_max, nostr_sync_prevent, nostr_sync_stop } from "$root";
+import type { INostrMetadata } from "@radroots/nostr-util";
+import { throw_err, type CallbackPromiseFull, type CallbackPromiseGeneric, type I18nTranslateFunction } from "@radroots/util";
export const nostr_sync_retry_handler = async (callback: () => Promise<any>) => {
let current_count = 0;
@@ -24,3 +26,38 @@ export const nostr_sync_retry_handler = async (callback: () => Promise<any>) =>
await exe();
};
+
+export const nostr_sync_handler = async (opts: {
+ ls: I18nTranslateFunction;
+ callback_alert: CallbackPromiseGeneric<string>;
+ callback_confirm: CallbackPromiseFull<string, boolean>;
+ callback_metadata: CallbackPromiseFull<string, INostrMetadata>;
+ callback_relay_urls: CallbackPromiseFull<string, { id: string; url: string; }[]>;
+}): Promise<void> => {
+ try {
+ const { ls, callback_alert, callback_confirm, callback_metadata, callback_relay_urls } = opts;
+ const $ls = get_store(ls);
+ const $ndk_user = get_store(ndk_user);
+ const public_key = $ndk_user.pubkey;
+ if (!public_key) return void await callback_alert(`${$ls(`error.client.nostr_sync_failure`)}`);
+
+ const $nostr_sync_prevent = get_store(nostr_sync_prevent);
+ if ($nostr_sync_prevent) {
+ const confirm = await callback_confirm(`${$ls(`error.client.nostr_sync_disabled`)}`);
+ if (confirm) nostr_sync_prevent.set(false);
+ else return;
+ }
+
+ const metadata = await callback_metadata(public_key);
+ const ev = await nostr_sync.metadata({ metadata });
+ if (`err` in ev) throw_err(ev);
+ await ev.publish();
+
+ const relays = await callback_relay_urls(public_key);
+ for (const relay of relays) {
+ //@todo
+ }
+ } catch (e) {
+ await handle_err(e, `nostr_sync_handler`);
+ }
+};
+\ No newline at end of file
diff --git a/apps-lib/src/lib/util/service/nostr-sync.ts b/apps-lib/src/lib/util/service/nostr-sync.ts
@@ -37,4 +37,6 @@ export class NostrSyncService implements INostrSyncService {
return err_msg(`error.nostr.sync.failure`); //@todo
}
}
-}
-\ No newline at end of file
+}
+
+export const nostr_sync = new NostrSyncService();
+\ No newline at end of file