From 7fa1e89dc8a4c1d9b7e3a752943ad20f250d6bdf Mon Sep 17 00:00:00 2001 From: reya Date: Tue, 17 Oct 2023 16:33:41 +0700 Subject: [PATCH] wip: complete new onboarding --- .../20230418013219_initial_data.sql | 62 +++--- .../20230418080146_create_chats.sql | 12 -- ...20040005_insert_last_login_to_settings.sql | 14 -- .../20230425023912_add_pubkey_to_channel.sql | 3 - .../20230425024708_add_default_channels.sql | 38 ---- .../20230425050745_add_blacklist_model.sql | 11 - .../20230521092300_add_block_model.sql | 11 - .../20230617003135_add_channel_messages.sql | 15 -- .../migrations/20230619082415_add_replies.sql | 13 -- .../20230718072634_clean_up_old_tables.sql | 6 - .../20230725010250_update_default_relays.sql | 6 - .../20230804083544_add_network_to_account.sql | 2 - .../20230808085847_add_relays_table.sql | 10 - ...0230811074423_rename_blocks_to_widgets.sql | 3 - .../20230814083543_add_events_table.sql | 13 -- .../20230816090508_clean_up_tables.sql | 8 - ...7014932_add_last_login_time_to_account.sql | 3 - .../20230918235335_add_uniq_to_relay.sql | 2 - src-tauri/src/main.rs | 116 +--------- src/app.tsx | 23 +- src/app/auth/complete.tsx | 42 ---- .../components/features/allowNotification.tsx | 50 +++++ .../auth/components/features/customRelay.tsx | 21 -- .../auth/components/features/enableCircle.tsx | 99 +++++++++ .../auth/components/features/enableOutbox.tsx | 47 +++++ .../components/features/favoriteHashtag.tsx | 26 ++- .../auth/components/features/followList.tsx | 57 ++++- src/app/auth/components/features/linkList.tsx | 21 -- src/app/auth/components/features/nip04.tsx | 21 -- .../components/features/suggestFollow.tsx | 24 ++- src/app/auth/import.tsx | 20 +- src/app/auth/onboarding/enrich.tsx | 25 ++- src/app/auth/onboarding/hashtag.tsx | 116 +++++----- src/app/auth/onboarding/list.tsx | 12 +- src/app/auth/onboarding/relays.tsx | 198 ++++++++---------- src/app/communities/index.tsx | 7 - src/libs/ndk/instance.ts | 3 + src/libs/storage/instance.ts | 25 ++- src/main.jsx | 2 +- src/shared/user.tsx | 2 +- src/shared/widgets/eventLoader.tsx | 5 +- src/stores/onboarding.ts | 40 ++++ src/utils/hooks/useNostr.ts | 76 +------ src/utils/types.d.ts | 2 +- 44 files changed, 580 insertions(+), 732 deletions(-) delete mode 100644 src-tauri/migrations/20230418080146_create_chats.sql delete mode 100644 src-tauri/migrations/20230420040005_insert_last_login_to_settings.sql delete mode 100644 src-tauri/migrations/20230425023912_add_pubkey_to_channel.sql delete mode 100644 src-tauri/migrations/20230425024708_add_default_channels.sql delete mode 100644 src-tauri/migrations/20230425050745_add_blacklist_model.sql delete mode 100644 src-tauri/migrations/20230521092300_add_block_model.sql delete mode 100644 src-tauri/migrations/20230617003135_add_channel_messages.sql delete mode 100644 src-tauri/migrations/20230619082415_add_replies.sql delete mode 100644 src-tauri/migrations/20230718072634_clean_up_old_tables.sql delete mode 100644 src-tauri/migrations/20230725010250_update_default_relays.sql delete mode 100644 src-tauri/migrations/20230804083544_add_network_to_account.sql delete mode 100644 src-tauri/migrations/20230808085847_add_relays_table.sql delete mode 100644 src-tauri/migrations/20230811074423_rename_blocks_to_widgets.sql delete mode 100644 src-tauri/migrations/20230814083543_add_events_table.sql delete mode 100644 src-tauri/migrations/20230816090508_clean_up_tables.sql delete mode 100644 src-tauri/migrations/20230817014932_add_last_login_time_to_account.sql delete mode 100644 src-tauri/migrations/20230918235335_add_uniq_to_relay.sql delete mode 100644 src/app/auth/complete.tsx create mode 100644 src/app/auth/components/features/allowNotification.tsx delete mode 100644 src/app/auth/components/features/customRelay.tsx create mode 100644 src/app/auth/components/features/enableCircle.tsx create mode 100644 src/app/auth/components/features/enableOutbox.tsx delete mode 100644 src/app/auth/components/features/linkList.tsx delete mode 100644 src/app/auth/components/features/nip04.tsx delete mode 100644 src/app/communities/index.tsx create mode 100644 src/stores/onboarding.ts diff --git a/src-tauri/migrations/20230418013219_initial_data.sql b/src-tauri/migrations/20230418013219_initial_data.sql index 9f24dcd7..74b5f95d 100644 --- a/src-tauri/migrations/20230418013219_initial_data.sql +++ b/src-tauri/migrations/20230418013219_initial_data.sql @@ -1,45 +1,29 @@ --- Add migration script here -- create accounts table --- is_active (multi-account feature), value: --- 0: false --- 1: true CREATE TABLE accounts ( - id INTEGER NOT NULL PRIMARY KEY, - npub TEXT NOT NULL UNIQUE, + id TEXT NOT NULL PRIMARY KEY, pubkey TEXT NOT NULL UNIQUE, - privkey TEXT NOT NULL, - follows JSON, + follows TEXT, + circles TEXT, is_active INTEGER NOT NULL DEFAULT 0, + last_login_at NUMBER NOT NULL DEFAULT 0, created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ); -- create notes table CREATE TABLE - notes ( - id INTEGER NOT NULL PRIMARY KEY, - event_id TEXT NOT NULL UNIQUE, + events ( + id TEXT NOT NULL PRIMARY KEY, account_id INTEGER NOT NULL, - pubkey TEXT NOT NULL, - kind INTEGER NOT NULL DEFAULT 1, - tags JSON, - content TEXT NOT NULL, + event TEXT NOT NULL, + author TEXT NOT NULL, + kind NUMBER NOT NULL DEFAULt 1, + root_id TEXT, + reply_id TEXT, created_at INTEGER NOT NULL, - parent_id TEXT, FOREIGN KEY (account_id) REFERENCES accounts (id) ); --- create channels table -CREATE TABLE - channels ( - id INTEGER NOT NULL PRIMARY KEY, - event_id TEXT NOT NULL UNIQUE, - name TEXT, - about TEXT, - picture TEXT, - created_at INTEGER NOT NULL - ); - -- create settings table CREATE TABLE settings ( @@ -49,11 +33,23 @@ CREATE TABLE created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ); --- create metadata table CREATE TABLE - metadata ( - id TEXT NOT NULL PRIMARY KEY, - pubkey TEXT NOT NULL, + widgets ( + id INTEGER NOT NULL PRIMARY KEY, + account_id INTEGER NOT NULL, + kind INTEGER NOT NULL, + title TEXT NOT NULL, content TEXT NOT NULL, - created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP - ); \ No newline at end of file + created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (account_id) REFERENCES accounts (id) + ); + +CREATE TABLE + relays ( + id INTEGER NOT NULL PRIMARY KEY, + account_id INTEGER NOT NULL, + relay TEXT NOT NULL UNIQUE, + purpose TEXT NOT NULL DEFAULT '', + created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (account_id) REFERENCES accounts (id) + ); diff --git a/src-tauri/migrations/20230418080146_create_chats.sql b/src-tauri/migrations/20230418080146_create_chats.sql deleted file mode 100644 index 1dcd8cd0..00000000 --- a/src-tauri/migrations/20230418080146_create_chats.sql +++ /dev/null @@ -1,12 +0,0 @@ --- Add migration script here --- create chats table -CREATE TABLE - chats ( - id INTEGER NOT NULL PRIMARY KEY, - event_id TEXT NOT NULL UNIQUE, - receiver_pubkey INTEGER NOT NULL, - sender_pubkey TEXT NOT NULL, - content TEXT NOT NULL, - tags JSON, - created_at INTEGER NOT NULL - ); \ No newline at end of file diff --git a/src-tauri/migrations/20230420040005_insert_last_login_to_settings.sql b/src-tauri/migrations/20230420040005_insert_last_login_to_settings.sql deleted file mode 100644 index 322110a0..00000000 --- a/src-tauri/migrations/20230420040005_insert_last_login_to_settings.sql +++ /dev/null @@ -1,14 +0,0 @@ --- Add migration script here -INSERT INTO - settings (key, value) -VALUES - ("last_login", "0"), - ( - "relays", - '["wss://relayable.org","wss://relay.damus.io","wss://relay.nostr.band/all","wss://relay.nostrgraph.net","wss://nostr.mutinywallet.com"]' - ), - ("auto_start", "0"), - ("cache_time", "86400000"), - ("compose_shortcut", "meta+n"), - ("add_imageblock_shortcut", "meta+i"), - ("add_feedblock_shortcut", "meta+f") \ No newline at end of file diff --git a/src-tauri/migrations/20230425023912_add_pubkey_to_channel.sql b/src-tauri/migrations/20230425023912_add_pubkey_to_channel.sql deleted file mode 100644 index 61d01078..00000000 --- a/src-tauri/migrations/20230425023912_add_pubkey_to_channel.sql +++ /dev/null @@ -1,3 +0,0 @@ --- Add migration script here --- add pubkey to channel -ALTER TABLE channels ADD pubkey TEXT NOT NULL DEFAULT ''; \ No newline at end of file diff --git a/src-tauri/migrations/20230425024708_add_default_channels.sql b/src-tauri/migrations/20230425024708_add_default_channels.sql deleted file mode 100644 index 3f7f42d9..00000000 --- a/src-tauri/migrations/20230425024708_add_default_channels.sql +++ /dev/null @@ -1,38 +0,0 @@ --- Add migration script here -INSERT -OR IGNORE INTO channels ( - event_id, - pubkey, - name, - about, - picture, - created_at -) -VALUES - ( - "e3cadf5beca1b2af1cddaa41a633679bedf263e3de1eb229c6686c50d85df753", - "126103bfddc8df256b6e0abfd7f3797c80dcc4ea88f7c2f87dd4104220b4d65f", - "lume-general", - "General channel for Lume", - "https://void.cat/d/UNyxBmAh1MUx5gQTX95jyf.webp", - 1681898574 - ); - -INSERT -OR IGNORE INTO channels ( - event_id, - pubkey, - name, - about, - picture, - created_at -) -VALUES - ( - "25e5c82273a271cb1a840d0060391a0bf4965cafeb029d5ab55350b418953fbb", - "ed1d0e1f743a7d19aa2dfb0162df73bacdbc699f67cc55bb91a98c35f7deac69", - "Nostr", - "", - "https://cloudflare-ipfs.com/ipfs/QmTN4Eas9atUULVbEAbUU8cowhtvK7g3t7jfKztY7wc8eP?.png", - 1661333723 - ); \ No newline at end of file diff --git a/src-tauri/migrations/20230425050745_add_blacklist_model.sql b/src-tauri/migrations/20230425050745_add_blacklist_model.sql deleted file mode 100644 index 67b25c7a..00000000 --- a/src-tauri/migrations/20230425050745_add_blacklist_model.sql +++ /dev/null @@ -1,11 +0,0 @@ --- Add migration script here --- create blacklist table -CREATE TABLE - blacklist ( - id INTEGER NOT NULL PRIMARY KEY, - account_id INTEGER NOT NULL, - content TEXT NOT NULL UNIQUE, - status INTEGER NOT NULL DEFAULT 0, - kind INTEGER NOT NULL, - FOREIGN KEY (account_id) REFERENCES accounts (id) - ); \ No newline at end of file diff --git a/src-tauri/migrations/20230521092300_add_block_model.sql b/src-tauri/migrations/20230521092300_add_block_model.sql deleted file mode 100644 index eb4616d6..00000000 --- a/src-tauri/migrations/20230521092300_add_block_model.sql +++ /dev/null @@ -1,11 +0,0 @@ --- Add migration script here -CREATE TABLE - blocks ( - id INTEGER NOT NULL PRIMARY KEY, - account_id INTEGER NOT NULL, - kind INTEGER NOT NULL, - title TEXT NOT NULL, - content TEXT NOT NULL, - created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (account_id) REFERENCES accounts (id) - ); \ No newline at end of file diff --git a/src-tauri/migrations/20230617003135_add_channel_messages.sql b/src-tauri/migrations/20230617003135_add_channel_messages.sql deleted file mode 100644 index a9486778..00000000 --- a/src-tauri/migrations/20230617003135_add_channel_messages.sql +++ /dev/null @@ -1,15 +0,0 @@ --- Add migration script here -CREATE TABLE - channel_messages ( - id INTEGER NOT NULL PRIMARY KEY, - channel_id TEXT NOT NULL, - event_id TEXT NOT NULL UNIQUE, - pubkey TEXT NOT NULL, - kind INTEGER NOT NULL, - content TEXT NOT NULL, - tags JSON, - mute BOOLEAN DEFAULT 0, - hide BOOLEAN DEFAULT 0, - created_at INTEGER NOT NULL, - FOREIGN KEY (channel_id) REFERENCES channels (event_id) - ); \ No newline at end of file diff --git a/src-tauri/migrations/20230619082415_add_replies.sql b/src-tauri/migrations/20230619082415_add_replies.sql deleted file mode 100644 index 9dc877ca..00000000 --- a/src-tauri/migrations/20230619082415_add_replies.sql +++ /dev/null @@ -1,13 +0,0 @@ --- Add migration script here -CREATE TABLE - replies ( - id INTEGER NOT NULL PRIMARY KEY, - parent_id TEXT NOT NULL, - event_id TEXT NOT NULL UNIQUE, - pubkey TEXT NOT NULL, - kind INTEGER NOT NULL DEFAULT 1, - tags JSON, - content TEXT NOT NULL, - created_at INTEGER NOT NULL, - FOREIGN KEY (parent_id) REFERENCES notes (event_id) - ); \ No newline at end of file diff --git a/src-tauri/migrations/20230718072634_clean_up_old_tables.sql b/src-tauri/migrations/20230718072634_clean_up_old_tables.sql deleted file mode 100644 index 707d2f9c..00000000 --- a/src-tauri/migrations/20230718072634_clean_up_old_tables.sql +++ /dev/null @@ -1,6 +0,0 @@ --- Add migration script here -DROP TABLE IF EXISTS blacklist; - -DROP TABLE IF EXISTS channel_messages; - -DROP TABLE IF EXISTS channels; \ No newline at end of file diff --git a/src-tauri/migrations/20230725010250_update_default_relays.sql b/src-tauri/migrations/20230725010250_update_default_relays.sql deleted file mode 100644 index 5c427e5d..00000000 --- a/src-tauri/migrations/20230725010250_update_default_relays.sql +++ /dev/null @@ -1,6 +0,0 @@ --- Add migration script here -UPDATE settings -SET - value = '["wss://relayable.org","wss://relay.damus.io","wss://relay.nostr.band/all","wss://nostr.mutinywallet.com"]' -WHERE - key = 'relays'; \ No newline at end of file diff --git a/src-tauri/migrations/20230804083544_add_network_to_account.sql b/src-tauri/migrations/20230804083544_add_network_to_account.sql deleted file mode 100644 index 3fac9cfd..00000000 --- a/src-tauri/migrations/20230804083544_add_network_to_account.sql +++ /dev/null @@ -1,2 +0,0 @@ --- Add migration script here -ALTER TABLE accounts ADD network JSON; \ No newline at end of file diff --git a/src-tauri/migrations/20230808085847_add_relays_table.sql b/src-tauri/migrations/20230808085847_add_relays_table.sql deleted file mode 100644 index 17953d97..00000000 --- a/src-tauri/migrations/20230808085847_add_relays_table.sql +++ /dev/null @@ -1,10 +0,0 @@ --- Add migration script here -CREATE TABLE - relays ( - id INTEGER NOT NULL PRIMARY KEY, - account_id INTEGER NOT NULL, - relay TEXT NOT NULL, - purpose TEXT NOT NULL DEFAULT '', - created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (account_id) REFERENCES accounts (id) - ); \ No newline at end of file diff --git a/src-tauri/migrations/20230811074423_rename_blocks_to_widgets.sql b/src-tauri/migrations/20230811074423_rename_blocks_to_widgets.sql deleted file mode 100644 index b267ba0f..00000000 --- a/src-tauri/migrations/20230811074423_rename_blocks_to_widgets.sql +++ /dev/null @@ -1,3 +0,0 @@ --- Add migration script here -ALTER TABLE blocks -RENAME TO widgets; \ No newline at end of file diff --git a/src-tauri/migrations/20230814083543_add_events_table.sql b/src-tauri/migrations/20230814083543_add_events_table.sql deleted file mode 100644 index 6db58368..00000000 --- a/src-tauri/migrations/20230814083543_add_events_table.sql +++ /dev/null @@ -1,13 +0,0 @@ --- Add migration script here -CREATE TABLE - events ( - id TEXT NOT NULL PRIMARY KEY, - account_id INTEGER NOT NULL, - event TEXT NOT NULL, - author TEXT NOT NULL, - kind NUMBER NOT NULL DEFAULt 1, - root_id TEXT, - reply_id TEXT, - created_at INTEGER NOT NULL, - FOREIGN KEY (account_id) REFERENCES accounts (id) - ); \ No newline at end of file diff --git a/src-tauri/migrations/20230816090508_clean_up_tables.sql b/src-tauri/migrations/20230816090508_clean_up_tables.sql deleted file mode 100644 index 0814840b..00000000 --- a/src-tauri/migrations/20230816090508_clean_up_tables.sql +++ /dev/null @@ -1,8 +0,0 @@ --- Add migration script here -DROP TABLE IF EXISTS notes; - -DROP TABLE IF EXISTS chats; - -DROP TABLE IF EXISTS metadata; - -DROP TABLE IF EXISTS replies; \ No newline at end of file diff --git a/src-tauri/migrations/20230817014932_add_last_login_time_to_account.sql b/src-tauri/migrations/20230817014932_add_last_login_time_to_account.sql deleted file mode 100644 index a1b25281..00000000 --- a/src-tauri/migrations/20230817014932_add_last_login_time_to_account.sql +++ /dev/null @@ -1,3 +0,0 @@ --- Add migration script here -ALTER TABLE accounts -ADD COLUMN last_login_at NUMBER NOT NULL DEFAULT 0; \ No newline at end of file diff --git a/src-tauri/migrations/20230918235335_add_uniq_to_relay.sql b/src-tauri/migrations/20230918235335_add_uniq_to_relay.sql deleted file mode 100644 index 7bb950ba..00000000 --- a/src-tauri/migrations/20230918235335_add_uniq_to_relay.sql +++ /dev/null @@ -1,2 +0,0 @@ --- 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 3ad278f5..512e74ef 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -116,116 +116,12 @@ fn main() { tauri_plugin_sql::Builder::default() .add_migrations( "sqlite:lume_v2.db", - vec![ - Migration { - version: 20230418013219, - description: "initial data", - sql: include_str!("../migrations/20230418013219_initial_data.sql"), - kind: MigrationKind::Up, - }, - Migration { - version: 20230418080146, - description: "create chats", - sql: include_str!("../migrations/20230418080146_create_chats.sql"), - kind: MigrationKind::Up, - }, - Migration { - version: 20230420040005, - description: "insert last login to settings", - sql: include_str!("../migrations/20230420040005_insert_last_login_to_settings.sql"), - kind: MigrationKind::Up, - }, - Migration { - version: 20230425023912, - description: "add pubkey to channel", - sql: include_str!("../migrations/20230425023912_add_pubkey_to_channel.sql"), - kind: MigrationKind::Up, - }, - Migration { - version: 20230425024708, - description: "add default channels", - sql: include_str!("../migrations/20230425024708_add_default_channels.sql"), - kind: MigrationKind::Up, - }, - Migration { - version: 20230425050745, - description: "create blacklist", - sql: include_str!("../migrations/20230425050745_add_blacklist_model.sql"), - kind: MigrationKind::Up, - }, - Migration { - version: 20230521092300, - description: "create block", - sql: include_str!("../migrations/20230521092300_add_block_model.sql"), - kind: MigrationKind::Up, - }, - Migration { - version: 20230617003135, - description: "add channel messages", - sql: include_str!("../migrations/20230617003135_add_channel_messages.sql"), - kind: MigrationKind::Up, - }, - Migration { - version: 20230619082415, - description: "add replies", - sql: include_str!("../migrations/20230619082415_add_replies.sql"), - kind: MigrationKind::Up, - }, - Migration { - version: 20230718072634, - description: "clean up", - sql: include_str!("../migrations/20230718072634_clean_up_old_tables.sql"), - kind: MigrationKind::Up, - }, - Migration { - version: 20230725010250, - description: "update default relays", - sql: include_str!("../migrations/20230725010250_update_default_relays.sql"), - kind: MigrationKind::Up, - }, - Migration { - version: 20230804083544, - description: "add network to accounts", - sql: include_str!("../migrations/20230804083544_add_network_to_account.sql"), - kind: MigrationKind::Up, - }, - Migration { - version: 20230808085847, - description: "add relays", - sql: include_str!("../migrations/20230808085847_add_relays_table.sql"), - kind: MigrationKind::Up, - }, - Migration { - version: 20230811074423, - description: "rename blocks to widgets", - sql: include_str!("../migrations/20230811074423_rename_blocks_to_widgets.sql"), - kind: MigrationKind::Up, - }, - Migration { - version: 20230814083543, - description: "add events", - sql: include_str!("../migrations/20230814083543_add_events_table.sql"), - kind: MigrationKind::Up, - }, - Migration { - version: 20230816090508, - description: "clean up tables", - sql: include_str!("../migrations/20230816090508_clean_up_tables.sql"), - kind: MigrationKind::Up, - }, - Migration { - version: 20230817014932, - description: "add last login to account", - 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, - }, - ], + vec![Migration { + version: 20230418013219, + description: "initial data", + sql: include_str!("../migrations/20230418013219_initial_data.sql"), + kind: MigrationKind::Up, + }], ) .build(), ) diff --git a/src/app.tsx b/src/app.tsx index e9ed5db0..2b132f4f 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -94,13 +94,6 @@ export default function App() { return { Component: RelayScreen }; }, }, - { - path: 'communities', - async lazy() { - const { CommunitiesScreen } = await import('@app/communities'); - return { Component: CommunitiesScreen }; - }, - }, { path: 'explore', element: ( @@ -173,13 +166,6 @@ export default function App() { return { Component: ImportAccountScreen }; }, }, - { - path: 'complete', - async lazy() { - const { CompleteScreen } = await import('@app/auth/complete'); - return { Component: CompleteScreen }; - }, - }, { path: 'onboarding', element: , @@ -203,6 +189,15 @@ export default function App() { return { Component: OnboardEnrichScreen }; }, }, + { + path: 'hashtag', + async lazy() { + const { OnboardHashtagScreen } = await import( + '@app/auth/onboarding/hashtag' + ); + return { Component: OnboardHashtagScreen }; + }, + }, ], }, ], diff --git a/src/app/auth/complete.tsx b/src/app/auth/complete.tsx deleted file mode 100644 index 740697df..00000000 --- a/src/app/auth/complete.tsx +++ /dev/null @@ -1,42 +0,0 @@ -import { useEffect, useState } from 'react'; -import { useNavigate } from 'react-router-dom'; - -export function CompleteScreen() { - const navigate = useNavigate(); - const [count, setCount] = useState(5); - - useEffect(() => { - let counter: NodeJS.Timeout; - - if (count > 0) { - counter = setTimeout(() => setCount(count - 1), 1000); - } - - if (count === 0) { - navigate('/', { replace: true }); - } - - return () => { - clearTimeout(counter); - }; - }, [count]); - - return ( -
-
-

- You're ready, redirecting in {count} -

-

- Thank you for using Lume. Lume doesn't use telemetry. If you encounter any - problems, please submit a report via the "Report Issue" button. -
- You can find it while using the application. -

-
-
- lume -
-
- ); -} diff --git a/src/app/auth/components/features/allowNotification.tsx b/src/app/auth/components/features/allowNotification.tsx new file mode 100644 index 00000000..4e23bc1b --- /dev/null +++ b/src/app/auth/components/features/allowNotification.tsx @@ -0,0 +1,50 @@ +import { isPermissionGranted, requestPermission } from '@tauri-apps/plugin-notification'; + +import { CheckCircleIcon } from '@shared/icons'; + +import { useOnboarding } from '@stores/onboarding'; + +export function AllowNotification() { + const [notification, setNotification] = useOnboarding((state) => [ + state.notification, + state.toggleNotification, + ]); + + const allow = async () => { + let permissionGranted = await isPermissionGranted(); + if (!permissionGranted) { + const permission = await requestPermission(); + permissionGranted = permission === 'granted'; + } + if (permissionGranted) { + setNotification(); + } + }; + + return ( +
+
+
+
Allow notification
+

+ By allowing Lume to send notifications in your OS settings, you will receive + notification messages when someone interacts with you or your content. +

+
+ {notification ? ( +
+ +
+ ) : ( + + )} +
+
+ ); +} diff --git a/src/app/auth/components/features/customRelay.tsx b/src/app/auth/components/features/customRelay.tsx deleted file mode 100644 index 9e9660bb..00000000 --- a/src/app/auth/components/features/customRelay.tsx +++ /dev/null @@ -1,21 +0,0 @@ -export function CustomRelay() { - return ( -
-
-
-
Personalize relay list
-

- Lume offers some default relays for users who are not familiar with Nostr, but - you can consider adding more relays to discover more content. -

-
- -
-
- ); -} diff --git a/src/app/auth/components/features/enableCircle.tsx b/src/app/auth/components/features/enableCircle.tsx new file mode 100644 index 00000000..0776a0c2 --- /dev/null +++ b/src/app/auth/components/features/enableCircle.tsx @@ -0,0 +1,99 @@ +import { NDKEvent, NDKKind } from '@nostr-dev-kit/ndk'; +import { LRUCache } from 'lru-cache'; +import { useState } from 'react'; +import { toast } from 'sonner'; + +import { useNDK } from '@libs/ndk/provider'; +import { useStorage } from '@libs/storage/provider'; + +import { CheckCircleIcon, LoaderIcon } from '@shared/icons'; + +import { useOnboarding } from '@stores/onboarding'; + +export function Circle() { + const { db } = useStorage(); + const { ndk } = useNDK(); + + const [circle, setCircle] = useOnboarding((state) => [ + state.circle, + state.toggleCircle, + ]); + const [loading, setLoading] = useState(false); + + const enableLinks = async () => { + setLoading(true); + + const users = ndk.getUser({ hexpubkey: db.account.pubkey }); + const follows = await users.follows(); + + if (follows.size === 0) { + setLoading(false); + return toast('You need to follow at least 1 account'); + } + + const lru = new LRUCache({ max: 300 }); + const followsAsArr = []; + + // add user's follows to lru + follows.forEach((user) => { + lru.set(user.pubkey, user.pubkey); + followsAsArr.push(user.pubkey); + }); + + // get follows from follows + const events = await ndk.fetchEvents({ + kinds: [NDKKind.Contacts], + authors: followsAsArr, + limit: 300, + }); + + events.forEach((event: NDKEvent) => { + event.tags.forEach((tag) => { + if (tag[0] === 'p') lru.set(tag[1], tag[1]); + }); + }); + + // get lru values + const circleList = [...lru.values()] as string[]; + + // update db + await db.updateAccount('follows', JSON.stringify(followsAsArr)); + await db.updateAccount('circles', JSON.stringify(circleList)); + + db.account.follows = followsAsArr; + db.account.circles = circleList; + + // clear lru + lru.clear(); + + // done + setCircle(); + }; + + return ( +
+
+
+
Enable Circle
+

+ Beside newsfeed from your follows, you will see more content from all people + that followed by your follows. +

+
+ {circle ? ( +
+ +
+ ) : ( + + )} +
+
+ ); +} diff --git a/src/app/auth/components/features/enableOutbox.tsx b/src/app/auth/components/features/enableOutbox.tsx new file mode 100644 index 00000000..18b46765 --- /dev/null +++ b/src/app/auth/components/features/enableOutbox.tsx @@ -0,0 +1,47 @@ +import { useStorage } from '@libs/storage/provider'; + +import { CheckCircleIcon } from '@shared/icons'; + +import { useOnboarding } from '@stores/onboarding'; + +export function OutboxModel() { + const { db } = useStorage(); + + const [outbox, setOutbox] = useOnboarding((state) => [ + state.outbox, + state.toggleOutbox, + ]); + + const enableOutbox = async () => { + await db.createSetting('outbox', '1'); + setOutbox(); + }; + + return ( +
+
+
+
Enable Outbox (experiment)
+

+ When you request information about a user, Lume will automatically query the + user's outbox relays and subsequent queries will favour using those + relays for queries with that user's pubkey. +

+
+ {outbox ? ( +
+ +
+ ) : ( + + )} +
+
+ ); +} diff --git a/src/app/auth/components/features/favoriteHashtag.tsx b/src/app/auth/components/features/favoriteHashtag.tsx index 6756b933..92dabd67 100644 --- a/src/app/auth/components/features/favoriteHashtag.tsx +++ b/src/app/auth/components/features/favoriteHashtag.tsx @@ -1,4 +1,12 @@ +import { Link } from 'react-router-dom'; + +import { CheckCircleIcon } from '@shared/icons'; + +import { useOnboarding } from '@stores/onboarding'; + export function FavoriteHashtag() { + const hashtag = useOnboarding((state) => state.hashtag); + return (
@@ -9,12 +17,18 @@ export function FavoriteHashtag() { hashtag as a column

- + {hashtag ? ( +
+ +
+ ) : ( + + Add + + )}
); diff --git a/src/app/auth/components/features/followList.tsx b/src/app/auth/components/features/followList.tsx index 0b52715c..77fafabd 100644 --- a/src/app/auth/components/features/followList.tsx +++ b/src/app/auth/components/features/followList.tsx @@ -1,3 +1,58 @@ +import { useQuery } from '@tanstack/react-query'; + +import { useNDK } from '@libs/ndk/provider'; +import { useStorage } from '@libs/storage/provider'; + +import { LoaderIcon } from '@shared/icons'; +import { User } from '@shared/user'; + export function FollowList() { - return
; + const { db } = useStorage(); + const { ndk } = useNDK(); + const { status, data } = useQuery( + ['follows'], + async () => { + const user = ndk.getUser({ hexpubkey: db.account.pubkey }); + const follows = await user.follows(); + const followsAsArr = []; + + follows.forEach((user) => { + followsAsArr.push(user.pubkey); + }); + + // update db + await db.updateAccount('follows', JSON.stringify(followsAsArr)); + await db.updateAccount('circles', JSON.stringify(followsAsArr)); + + db.account.follows = followsAsArr; + db.account.circles = followsAsArr; + + return followsAsArr; + }, + { + refetchOnWindowFocus: false, + } + ); + + return ( +
+
Your follows
+
+ {status === 'loading' ? ( + + ) : ( +
+ {data.slice(0, 16).map((item) => ( + + ))} + {data.length > 16 ? ( +
+ +{data.length} +
+ ) : null} +
+ )} +
+
+ ); } diff --git a/src/app/auth/components/features/linkList.tsx b/src/app/auth/components/features/linkList.tsx deleted file mode 100644 index 87429b6e..00000000 --- a/src/app/auth/components/features/linkList.tsx +++ /dev/null @@ -1,21 +0,0 @@ -export function LinkList() { - return ( -
-
-
-
Enable Links
-

- Beside newsfeed from your follows, you will see more content from all people - that followed by your follows. -

-
- -
-
- ); -} diff --git a/src/app/auth/components/features/nip04.tsx b/src/app/auth/components/features/nip04.tsx deleted file mode 100644 index 79b4a108..00000000 --- a/src/app/auth/components/features/nip04.tsx +++ /dev/null @@ -1,21 +0,0 @@ -export function NIP04() { - return ( -
-
-
-
Enable direct message (Deprecated)
-

- Send direct message to other user (NIP-04), all messages will be encrypted, - but your metadata will be leaked. -

-
- -
-
- ); -} diff --git a/src/app/auth/components/features/suggestFollow.tsx b/src/app/auth/components/features/suggestFollow.tsx index b731038a..8884baa2 100644 --- a/src/app/auth/components/features/suggestFollow.tsx +++ b/src/app/auth/components/features/suggestFollow.tsx @@ -1,6 +1,12 @@ import { Link } from 'react-router-dom'; +import { CheckCircleIcon } from '@shared/icons'; + +import { useOnboarding } from '@stores/onboarding'; + export function SuggestFollow() { + const enrich = useOnboarding((state) => state.enrich); + return (
@@ -11,12 +17,18 @@ export function SuggestFollow() { world.

- - Check - + {enrich ? ( +
+ +
+ ) : ( + + Check + + )}
); diff --git a/src/app/auth/import.tsx b/src/app/auth/import.tsx index ccbb2efa..5a20cba9 100644 --- a/src/app/auth/import.tsx +++ b/src/app/auth/import.tsx @@ -82,7 +82,7 @@ export function ImportAccountScreen() {

- nsec is used to sign your event. For example, if you want to - make a new post or send a message to your contact, you need to use - nsec to sign this event. + Private Key is used to sign your event. For example, if you + want to make a new post or send a message to your contact, you need to + use your private key to sign this event.

- 1. In case you store nsec in Lume + 1. In case you store private key in Lume

Lume will put your nsec to{' '} @@ -204,12 +204,12 @@ export function ImportAccountScreen() { , it will be secured by your OS

- 2. In case you do not store nsec in Lume + 2. In case you do not store private key in Lume

- When you make an event that requires a sign by your nsec, Lume will - show a prompt popup for you to enter nsec. It will be cleared after - signing and not stored anywhere. + When you make an event that requires a sign by your private key, Lume + will show a prompt for you to enter private key. It will be cleared + after signing and not stored anywhere.

diff --git a/src/app/auth/onboarding/enrich.tsx b/src/app/auth/onboarding/enrich.tsx index 9b4d0f9b..ec69c273 100644 --- a/src/app/auth/onboarding/enrich.tsx +++ b/src/app/auth/onboarding/enrich.tsx @@ -1,17 +1,19 @@ import { useQuery } from '@tanstack/react-query'; import { useState } from 'react'; import { useNavigate } from 'react-router-dom'; +import { toast } from 'sonner'; import { useStorage } from '@libs/storage/provider'; import { ArrowLeftIcon, CheckCircleIcon, LoaderIcon } from '@shared/icons'; import { User } from '@shared/user'; +import { useOnboarding } from '@stores/onboarding'; + import { useNostr } from '@utils/hooks/useNostr'; import { arrayToNIP02 } from '@utils/transform'; export function OnboardEnrichScreen() { - const { publish, fetchUserData } = useNostr(); const { db } = useStorage(); const { status, data } = useQuery(['trending-profiles-widget'], async () => { const res = await fetch('https://api.nostr.band/v0/trending/profiles'); @@ -20,11 +22,13 @@ export function OnboardEnrichScreen() { } return res.json(); }); + const { publish } = useNostr(); const [loading, setLoading] = useState(false); const [follows, setFollows] = useState([]); const navigate = useNavigate(); + const setEnrich = useOnboarding((state) => state.toggleEnrich); // toggle follow state const toggleFollow = (pubkey: string) => { @@ -38,21 +42,24 @@ export function OnboardEnrichScreen() { try { setLoading(true); - const tags = arrayToNIP02([...follows, db.account.pubkey]); + const tags = arrayToNIP02(follows); const event = await publish({ content: '', kind: 3, tags: tags }); - // prefetch data - const user = await fetchUserData(follows); - // redirect to next step - if (event && user.status === 'ok') { - navigate('/auth/onboarding/step-2', { replace: true }); + if (event) { + db.account.follows = follows; + + await db.updateAccount('follows', JSON.stringify(follows)); + await db.updateAccount('circles', JSON.stringify(follows)); + + setEnrich(); + navigate(-1); } else { setLoading(false); } } catch (e) { setLoading(false); - console.log('error: ', e); + toast(e); } }; @@ -71,7 +78,7 @@ export function OnboardEnrichScreen() {

- {loading ? 'Loading...' : 'Enrich your network'} + Enrich your network

diff --git a/src/app/auth/onboarding/hashtag.tsx b/src/app/auth/onboarding/hashtag.tsx index de592306..1175ecf4 100644 --- a/src/app/auth/onboarding/hashtag.tsx +++ b/src/app/auth/onboarding/hashtag.tsx @@ -4,17 +4,20 @@ import { useNavigate } from 'react-router-dom'; import { useStorage } from '@libs/storage/provider'; -import { ArrowRightCircleIcon, CheckCircleIcon, LoaderIcon } from '@shared/icons'; +import { ArrowLeftIcon, CheckCircleIcon, LoaderIcon } from '@shared/icons'; +import { useOnboarding } from '@stores/onboarding'; import { WidgetKinds } from '@stores/widgets'; const data = [ { hashtag: '#bitcoin' }, { hashtag: '#nostr' }, { hashtag: '#nostrdesign' }, + { hashtag: '#security' }, { hashtag: '#zap' }, { hashtag: '#LFG' }, { hashtag: '#zapchain' }, + { hashtag: '#shitcoin' }, { hashtag: '#plebchain' }, { hashtag: '#nodes' }, { hashtag: '#hodl' }, @@ -23,21 +26,26 @@ const data = [ { hashtag: '#meme' }, { hashtag: '#memes' }, { hashtag: '#memestr' }, - { hashtag: '#penisbutter' }, + { hashtag: '#nostriches' }, + { hashtag: '#dev' }, { hashtag: '#anime' }, { hashtag: '#waifu' }, { hashtag: '#manga' }, - { hashtag: '#nostriches' }, - { hashtag: '#dev' }, + { hashtag: '#lume' }, + { hashtag: '#snort' }, + { hashtag: '#damus' }, + { hashtag: '#primal' }, ]; export function OnboardHashtagScreen() { const { db } = useStorage(); - const navigate = useNavigate(); const [loading, setLoading] = useState(false); const [tags, setTags] = useState(new Set()); + const navigate = useNavigate(); + const setHashtag = useOnboarding((state) => state.toggleHashtag); + const toggleTag = (tag: string) => { if (tags.has(tag)) { setTags((prev) => { @@ -50,13 +58,6 @@ export function OnboardHashtagScreen() { } }; - const skip = async () => { - // update last login - await db.updateLastLogin(); - - navigate('/auth/complete', { replace: true }); - }; - const submit = async () => { try { setLoading(true); @@ -65,10 +66,8 @@ export function OnboardHashtagScreen() { await db.createWidget(WidgetKinds.global.hashtag, tag, tag.replace('#', '')); } - // update last login - await db.updateLastLogin(); - - navigate('/auth/complete', { replace: true }); + setHashtag(); + navigate(-1); } catch (e) { setLoading(false); await message(e, { title: 'Lume', type: 'error' }); @@ -76,64 +75,55 @@ export function OnboardHashtagScreen() { }; return ( -
-
-

- Choose {tags.size}/3 your favorite hashtags -

-

- Hashtags are an easy way to discover more content. By adding a hashtag, Lume - will show all related posts. You can always add more later. -

+
+
+
-
-
- {data.map((item: { hashtag: string }) => ( - - ))} -
-
+
+

+ Choose {tags.size}/3 your favorite hashtag +

+
+
+ {data.map((item: { hashtag: string }) => ( + + ))} +
- {!loading ? ( - - ) : null}
diff --git a/src/app/auth/onboarding/list.tsx b/src/app/auth/onboarding/list.tsx index e7cc6d88..b8d644cc 100644 --- a/src/app/auth/onboarding/list.tsx +++ b/src/app/auth/onboarding/list.tsx @@ -1,10 +1,10 @@ import { Link, useLocation } from 'react-router-dom'; -import { CustomRelay } from '@app/auth/components/features/customRelay'; +import { AllowNotification } from '@app/auth/components/features/allowNotification'; +import { Circle } from '@app/auth/components/features/enableCircle'; +import { OutboxModel } from '@app/auth/components/features/enableOutbox'; import { FavoriteHashtag } from '@app/auth/components/features/favoriteHashtag'; import { FollowList } from '@app/auth/components/features/followList'; -import { LinkList } from '@app/auth/components/features/linkList'; -import { NIP04 } from '@app/auth/components/features/nip04'; import { SuggestFollow } from '@app/auth/components/features/suggestFollow'; export function OnboardingListScreen() { @@ -25,9 +25,9 @@ export function OnboardingListScreen() {
{newuser ? : } - - - + + + state.toggleRelays); const [loading, setLoading] = useState(false); const [relays, setRelays] = useState(new Set()); const { publish } = useNostr(); const { db } = useStorage(); - const { ndk } = useNDK(); + const { getAllRelaysByUsers } = useNostr(); const { status, data } = useQuery( ['relays'], async () => { - const tmp = new Map(); - const events = await ndk.fetchEvents({ - kinds: [10002], - authors: db.account.follows, - }); - - if (events) { - events.forEach((event) => { - event.tags.forEach((tag) => { - tmp.set(tag[1], event.pubkey); - }); - }); - } - - return tmp; + return await getAllRelaysByUsers(); }, { - enabled: db.account ? true : false, refetchOnWindowFocus: false, } ); - const relaysAsArray = Array.from(data?.keys() || []); - const toggleRelay = (relay: string) => { if (relays.has(relay)) { setRelays((prev) => { @@ -59,120 +43,110 @@ export function OnboardRelaysScreen() { } }; - const submit = async (skip?: boolean) => { + const submit = async () => { try { setLoading(true); - if (!skip) { - for (const relay of relays) { - await db.createRelay(relay); - } - - const tags = Array.from(relays).map((relay) => ['r', relay.replace(/\/+$/, '')]); - await publish({ content: '', kind: 10002, tags: tags }); - } else { - for (const relay of FULL_RELAYS) { - await db.createRelay(relay); - } + for (const relay of relays) { + await db.createRelay(relay); } - // update last login - await db.updateLastLogin(); + const tags = Array.from(relays).map((relay) => ['r', relay.replace(/\/+$/, '')]); + await publish({ content: '', kind: 10002, tags: tags }); - navigate('/', { replace: true }); + toggleRelays(); + navigate(-1); } catch (e) { setLoading(false); - console.log('error: ', e); + toast.error(e); } }; return ( -
-
-

Relay discovery

-

- You can add relay which is using by who you're following to easier reach - their content. Learn more about relay{' '} - - here (nostr.com) - -

+
+
+
-
-
- {status === 'loading' ? ( -
- -
- ) : relaysAsArray.length === 0 ? ( -
-

- Lume couldn't find any relays from your follows. -
- You can skip this step and use default relays instead. -

-
- ) : ( - relaysAsArray.map((item, index) => ( - - )) - )} - {relays.size > 5 && ( -
-

- Using too much relay can cause high resource usage -

-
- )} -
-
+ + )) + )} +
-
diff --git a/src/app/communities/index.tsx b/src/app/communities/index.tsx deleted file mode 100644 index 420e920b..00000000 --- a/src/app/communities/index.tsx +++ /dev/null @@ -1,7 +0,0 @@ -export function CommunitiesScreen() { - return ( -
-

TODO

-
- ); -} diff --git a/src/libs/ndk/instance.ts b/src/libs/ndk/instance.ts index 0a07e0a6..1d1f7f58 100644 --- a/src/libs/ndk/instance.ts +++ b/src/libs/ndk/instance.ts @@ -54,10 +54,13 @@ export const NDKInstance = () => { async function initNDK() { const explicitRelayUrls = await getExplicitRelays(); + const outboxSetting = await db.getSettingValue('outbox'); const dexieAdapter = new NDKCacheAdapterDexie({ dbName: 'lume_ndkcache' }); const instance = new NDK({ explicitRelayUrls, cacheAdapter: dexieAdapter, + outboxRelayUrls: ['wss://purplepag.es'], + enableOutboxModel: outboxSetting === '1', }); try { diff --git a/src/libs/storage/instance.ts b/src/libs/storage/instance.ts index eded08dd..f68f9eea 100644 --- a/src/libs/storage/instance.ts +++ b/src/libs/storage/instance.ts @@ -24,6 +24,7 @@ export class LumeStorage { public async secureLoad(key?: string) { const value: string = await invoke('secure_load', { key }); + if (!value) return null; return value; } @@ -45,8 +46,8 @@ export class LumeStorage { if (typeof account.follows === 'string') account.follows = JSON.parse(account.follows); - if (typeof account.network === 'string') - account.network = JSON.parse(account.network); + if (typeof account.circles === 'string') + account.circles = JSON.parse(account.circles); if (typeof account.last_login_at === 'string') account.last_login_at = parseInt(account.last_login_at); @@ -71,8 +72,8 @@ export class LumeStorage { ]); } else { await this.db.execute( - 'INSERT OR IGNORE INTO accounts (npub, pubkey, privkey, is_active) VALUES ($1, $2, $3, $4);', - [npub, pubkey, 'privkey is stored in secure storage', 1] + 'INSERT OR IGNORE INTO accounts (id, pubkey, is_active) VALUES ($1, $2, $3);', + [npub, pubkey, 1] ); } @@ -80,7 +81,7 @@ export class LumeStorage { return account; } - public async updateAccount(column: string, value: string | string[]) { + public async updateAccount(column: string, value: string) { const insert = await this.db.execute( `UPDATE accounts SET ${column} = $1 WHERE id = $2;`, [value, this.account.id] @@ -298,12 +299,22 @@ export class LumeStorage { return await this.db.execute(`DELETE FROM relays WHERE relay = "${relay}";`); } - public async removePrivkey() { + public async createSetting(key: string, value: string) { return await this.db.execute( - `UPDATE accounts SET privkey = "privkey is stored in secure storage" WHERE id = "${this.account.id}";` + 'INSERT OR IGNORE INTO settings (key, value) VALUES ($1, $2);', + [key, value] ); } + public async getSettingValue(key: string) { + const results: { key: string; value: string }[] = await this.db.select( + 'SELECT * FROM settings WHERE key = $1 ORDER BY id DESC LIMIT 1;', + [key] + ); + if (results.length < 1) return null; + return results[0].value; + } + public async accountLogout() { // update current account status await this.db.execute("UPDATE accounts SET is_active = '0' WHERE id = $1;", [ diff --git a/src/main.jsx b/src/main.jsx index e5eb2f24..6048b434 100644 --- a/src/main.jsx +++ b/src/main.jsx @@ -16,7 +16,7 @@ root.render( - + diff --git a/src/shared/user.tsx b/src/shared/user.tsx index 17c32f63..faa8df68 100644 --- a/src/shared/user.tsx +++ b/src/shared/user.tsx @@ -127,7 +127,7 @@ export const User = memo(function User({

👋 -

+

Hello, this is the first time you're using Lume

-

+

Lume is downloading all events since the last 24 hours. It will auto refresh when it done, please be patient

diff --git a/src/stores/onboarding.ts b/src/stores/onboarding.ts new file mode 100644 index 00000000..97863439 --- /dev/null +++ b/src/stores/onboarding.ts @@ -0,0 +1,40 @@ +import { create } from 'zustand'; +import { createJSONStorage, persist } from 'zustand/middleware'; + +interface OnboardingState { + enrich: boolean; + hashtag: boolean; + circle: boolean; + relays: boolean; + outbox: boolean; + notification: boolean; + toggleEnrich: () => void; + toggleHashtag: () => void; + toggleCircle: () => void; + toggleRelays: () => void; + toggleOutbox: () => void; + toggleNotification: () => void; +} + +export const useOnboarding = create()( + persist( + (set) => ({ + enrich: false, + hashtag: false, + circle: false, + relays: false, + outbox: false, + notification: false, + toggleEnrich: () => set((state) => ({ enrich: !state.enrich })), + toggleHashtag: () => set((state) => ({ hashtag: !state.hashtag })), + toggleCircle: () => set((state) => ({ circle: !state.circle })), + toggleRelays: () => set((state) => ({ relays: !state.relays })), + toggleOutbox: () => set((state) => ({ outbox: !state.outbox })), + toggleNotification: () => set((state) => ({ notification: !state.notification })), + }), + { + name: 'onboarding', + storage: createJSONStorage(() => sessionStorage), + } + ) +); diff --git a/src/utils/hooks/useNostr.ts b/src/utils/hooks/useNostr.ts index c9fb1ff7..72b1da9d 100644 --- a/src/utils/hooks/useNostr.ts +++ b/src/utils/hooks/useNostr.ts @@ -4,13 +4,11 @@ import { NDKKind, NDKPrivateKeySigner, NDKSubscription, - NDKUser, } from '@nostr-dev-kit/ndk'; import { message, open } from '@tauri-apps/plugin-dialog'; import { fetch } from '@tauri-apps/plugin-http'; import { LRUCache } from 'lru-cache'; import { NostrEventExt } from 'nostr-fetch'; -import { nip19 } from 'nostr-tools'; import { useMemo } from 'react'; import { useNDK } from '@libs/ndk/provider'; @@ -53,67 +51,6 @@ export function useNostr() { console.log('current active sub: ', subManager.size); }; - const fetchUserData = async (preFollows?: string[]) => { - try { - const follows = new Set(preFollows || []); - const lruNetwork = new LRUCache({ max: 300 }); - - // fetch user's relays - const relayEvents = await ndk.fetchEvents({ - kinds: [NDKKind.RelayList], - authors: [db.account.pubkey], - }); - - if (relayEvents) { - const latestRelayEvent = [...relayEvents].sort( - (a, b) => b.created_at - a.created_at - )[0]; - - if (latestRelayEvent) { - for (const item of latestRelayEvent.tags) { - await db.createRelay(item[1], item[2]); - } - } - } - - // fetch user's follows - if (!preFollows) { - const user = ndk.getUser({ hexpubkey: db.account.pubkey }); - const list = await user.follows(); - list.forEach((item: NDKUser) => { - follows.add(nip19.decode(item.npub).data as string); - }); - } - - // build user's network - const followEvents = await ndk.fetchEvents({ - kinds: [NDKKind.Contacts], - authors: [...follows], - limit: 300, - }); - - followEvents.forEach((event: NDKEvent) => { - event.tags.forEach((tag) => { - if (tag[0] === 'p') lruNetwork.set(tag[1], tag[1]); - }); - }); - - // get lru values - const network = [...lruNetwork.values()] as string[]; - - // update db - await db.updateAccount('follows', [...follows]); - await db.updateAccount('network', [...new Set([...follows, ...network])]); - - // clear lru caches - lruNetwork.clear(); - - return { status: 'ok', message: 'User data fetched' }; - } catch (e) { - return { status: 'failed', message: e }; - } - }; - const addContact = async (pubkey: string) => { const list = new Set(db.account.follows); list.add(pubkey); @@ -270,7 +207,7 @@ export function useNostr() { if (!customSince) { if (dbEventsEmpty || db.account.last_login_at === 0) { - since = db.account.network.length > 500 ? nHoursAgo(12) : nHoursAgo(24); + since = db.account.circles.length > 500 ? nHoursAgo(12) : nHoursAgo(24); } else { since = db.account.last_login_at; } @@ -282,7 +219,7 @@ export function useNostr() { relayUrls, { kinds: [NDKKind.Text, NDKKind.Repost, 1063, NDKKind.Article], - authors: db.account.network, + authors: db.account.circles, }, { since: since } )) as unknown as NDKEvent[]; @@ -344,7 +281,9 @@ export function useNostr() { kind: NDKKind | number; tags: string[][]; }): Promise => { - const privkey: string = await db.secureLoad(); + const privkey: string = await db.secureLoad(db.account.pubkey); + // #TODO: show prompt + if (!privkey) return; const event = new NDKEvent(ndk); const signer = new NDKPrivateKeySigner(privkey); @@ -362,7 +301,9 @@ export function useNostr() { }; const createZap = async (event: NDKEvent, amount: number, message?: string) => { - const privkey: string = await db.secureLoad(); + const privkey: string = await db.secureLoad(db.account.pubkey); + // #TODO: show prompt + if (!privkey) return; if (!ndk.signer) { const signer = new NDKPrivateKeySigner(privkey); @@ -459,7 +400,6 @@ export function useNostr() { return { sub, - fetchUserData, addContact, removeContact, getAllNIP04Chats, diff --git a/src/utils/types.d.ts b/src/utils/types.d.ts index 9d0005a8..06d1617b 100644 --- a/src/utils/types.d.ts +++ b/src/utils/types.d.ts @@ -26,7 +26,7 @@ export interface Account extends NDKUserProfile { npub: string; pubkey: string; follows: null | string[]; - network: null | string[]; + circles: null | string[]; is_active: number; last_login_at: number; }