From beb622f05e8ea841e806112101600abe34da4038 Mon Sep 17 00:00:00 2001 From: Ren Amamiya <123083837+reyamir@users.noreply.github.com> Date: Wed, 5 Apr 2023 09:48:57 +0700 Subject: [PATCH] clean up --- package.json | 2 - pnpm-lock.yaml | 24 --- src/components/form/base.tsx | 13 +- src/components/form/comment.tsx | 4 +- .../multiAccounts/activeAccount.tsx | 8 +- src/components/multiAccounts/index.tsx | 8 +- src/components/note/meta/comment.tsx | 6 +- src/components/note/meta/reaction.tsx | 2 +- src/components/note/metadata.tsx | 4 +- src/components/note/parent.tsx | 2 +- src/components/note/repost.tsx | 2 +- src/pages/newsfeed/[id].tsx | 6 +- src/utils/storage.tsx | 175 ------------------ 13 files changed, 23 insertions(+), 233 deletions(-) delete mode 100644 src/utils/storage.tsx diff --git a/package.json b/package.json index 6edca038..1c97c254 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,6 @@ "emoji-mart": "^5.5.2", "framer-motion": "^9.1.7", "jotai": "^2.0.3", - "jotai-cache": "^0.3.0", "next": "^13.2.4", "nostr-relaypool": "^0.5.18", "nostr-tools": "^1.8.1", @@ -37,7 +36,6 @@ "react-player": "^2.12.0", "react-string-replace": "^1.1.0", "react-virtuoso": "^4.1.1", - "tauri-plugin-sql-api": "github:tauri-apps/tauri-plugin-sql", "unique-names-generator": "^4.7.1", "ws": "^8.13.0" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b2b8a461..b262bab6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -32,7 +32,6 @@ specifiers: framer-motion: ^9.1.7 husky: ^8.0.3 jotai: ^2.0.3 - jotai-cache: ^0.3.0 lint-staged: ^13.2.0 next: ^13.2.4 nostr-relaypool: ^0.5.18 @@ -48,7 +47,6 @@ specifiers: react-string-replace: ^1.1.0 react-virtuoso: ^4.1.1 tailwindcss: ^3.3.1 - tauri-plugin-sql-api: github:tauri-apps/tauri-plugin-sql typescript: ^4.9.5 unique-names-generator: ^4.7.1 ws: ^8.13.0 @@ -69,7 +67,6 @@ dependencies: emoji-mart: 5.5.2 framer-motion: 9.1.7_biqbaboplfbrettd7655fr4n2y jotai: 2.0.3_react@18.2.0 - jotai-cache: 0.3.0_jotai@2.0.3 next: 13.2.4_biqbaboplfbrettd7655fr4n2y nostr-relaypool: 0.5.18_ws@8.13.0 nostr-tools: 1.8.1 @@ -79,7 +76,6 @@ dependencies: react-player: 2.12.0_react@18.2.0 react-string-replace: 1.1.0 react-virtuoso: 4.1.1_biqbaboplfbrettd7655fr4n2y - tauri-plugin-sql-api: github.com/tauri-apps/tauri-plugin-sql/3a8b9a6b244df7512bc5ef8692cebdedbab3ccce unique-names-generator: 4.7.1 ws: 8.13.0 @@ -3231,15 +3227,6 @@ packages: hasBin: true dev: true - /jotai-cache/0.3.0_jotai@2.0.3: - resolution: - { integrity: sha512-hV6DUD1frRpW0EN8Ss7n4KNaMZRBokQw6KPT3seA4P35QRXctMNUn4pHRlg3hzO+KBPpEZw+fyKlWAqagmuIwQ== } - peerDependencies: - jotai: '>=1.11.0' - dependencies: - jotai: 2.0.3_react@18.2.0 - dev: false - /jotai/2.0.3_react@18.2.0: resolution: { integrity: sha512-MMjhSPAL3RoeZD9WbObufRT2quThEAEknHHridf2ma8Ml7ZVQmUiHk0ssdbR3F0h3kcwhYqSGJ59OjhPge7RRg== } @@ -4995,14 +4982,3 @@ packages: { integrity: sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q== } engines: { node: '>=10' } dev: true - - github.com/tauri-apps/tauri-plugin-sql/3a8b9a6b244df7512bc5ef8692cebdedbab3ccce: - resolution: - { - tarball: https://codeload.github.com/tauri-apps/tauri-plugin-sql/tar.gz/3a8b9a6b244df7512bc5ef8692cebdedbab3ccce, - } - name: tauri-plugin-sql-api - version: 0.0.0 - dependencies: - '@tauri-apps/api': 1.2.0 - dev: false diff --git a/src/components/form/base.tsx b/src/components/form/base.tsx index 1a940502..c34e270e 100644 --- a/src/components/form/base.tsx +++ b/src/components/form/base.tsx @@ -2,12 +2,11 @@ import EmojiPicker from '@components/form/emojiPicker'; import ImagePicker from '@components/form/imagePicker'; import { RelayContext } from '@components/relaysProvider'; -import { activeAccountAtom } from '@stores/account'; import { noteContentAtom } from '@stores/note'; import { dateToUnix } from '@utils/getDate'; -import { useAtom, useAtomValue } from 'jotai'; +import { useAtom } from 'jotai'; import { useResetAtom } from 'jotai/utils'; import { getEventHash, signEvent } from 'nostr-tools'; import { useContext } from 'react'; @@ -15,23 +14,21 @@ import { useContext } from 'react'; export default function FormBase() { const [pool, relays]: any = useContext(RelayContext); - const activeAccount: any = useAtomValue(activeAccountAtom); const [value, setValue] = useAtom(noteContentAtom); const resetValue = useResetAtom(noteContentAtom); - const pubkey = activeAccount.id; - const privkey = activeAccount.privkey; - const submitEvent = () => { + const activeAccount = JSON.parse(localStorage.getItem('activeAccount')); + const event: any = { content: value, created_at: dateToUnix(), kind: 1, - pubkey: pubkey, + pubkey: activeAccount.pubkey, tags: [], }; event.id = getEventHash(event); - event.sig = signEvent(event, privkey); + event.sig = signEvent(event, activeAccount.privkey); // publish note pool.publish(event, relays); diff --git a/src/components/form/comment.tsx b/src/components/form/comment.tsx index f8212896..08f13212 100644 --- a/src/components/form/comment.tsx +++ b/src/components/form/comment.tsx @@ -23,7 +23,7 @@ export default function FormComment({ eventID }: { eventID: any }) { content: value, created_at: dateToUnix(), kind: 1, - pubkey: activeAccount.id, + pubkey: activeAccount.pubkey, tags: [['e', eventID]], }; event.id = getEventHash(event); @@ -42,7 +42,7 @@ export default function FormComment({ eventID }: { eventID: any }) {
diff --git a/src/components/multiAccounts/activeAccount.tsx b/src/components/multiAccounts/activeAccount.tsx index 9aa088df..effd9c2b 100644 --- a/src/components/multiAccounts/activeAccount.tsx +++ b/src/components/multiAccounts/activeAccount.tsx @@ -19,11 +19,11 @@ export const ActiveAccount = memo(function ActiveAccount({ user }: { user: any } const userData = JSON.parse(user.metadata); const openProfilePage = () => { - router.push(`/users/${user.id}`); + router.push(`/users/${user.pubkey}`); }; const copyPublicKey = async () => { - await writeText(nip19.npubEncode(user.id)); + await writeText(nip19.npubEncode(user.pubkey)); }; const insertFollowsToStorage = useCallback( @@ -46,7 +46,7 @@ export const ActiveAccount = memo(function ActiveAccount({ user }: { user: any } [ { kinds: [3], - authors: [user.id], + authors: [user.pubkey], }, ], relays, @@ -65,7 +65,7 @@ export const ActiveAccount = memo(function ActiveAccount({ user }: { user: any } return () => { unsubscribe; }; - }, [insertFollowsToStorage, pool, relays, user.id]); + }, [insertFollowsToStorage, pool, relays, user.pubkey]); return ( diff --git a/src/components/multiAccounts/index.tsx b/src/components/multiAccounts/index.tsx index d23161ab..931127c5 100644 --- a/src/components/multiAccounts/index.tsx +++ b/src/components/multiAccounts/index.tsx @@ -12,13 +12,13 @@ import { useCallback, useEffect, useState } from 'react'; export default function MultiAccounts() { const [users, setUsers] = useState([]); - const renderAccount = useCallback((user: { id: string }) => { + const renderAccount = useCallback((user: { pubkey: string }) => { const activeAccount = JSON.parse(localStorage.getItem('activeAccount')); - if (user.id === activeAccount.id) { - return ; + if (user.pubkey === activeAccount.pubkey) { + return ; } else { - return ; + return ; } }, []); diff --git a/src/components/note/meta/comment.tsx b/src/components/note/meta/comment.tsx index 7ea74e15..9c9a6d60 100644 --- a/src/components/note/meta/comment.tsx +++ b/src/components/note/meta/comment.tsx @@ -3,7 +3,6 @@ import { RelayContext } from '@components/relaysProvider'; import { UserExtend } from '@components/user/extend'; import { activeAccountAtom } from '@stores/account'; -import { relaysAtom } from '@stores/relays'; import { dateToUnix } from '@utils/getDate'; @@ -33,11 +32,10 @@ export const NoteComment = memo(function NoteComment({ const router = useRouter(); const [pool, relays]: any = useContext(RelayContext); - const activeAccount: any = useAtomValue(activeAccountAtom); - const [open, setOpen] = useState(false); const [value, setValue] = useState(''); + const activeAccount: any = useAtomValue(activeAccountAtom); const profile = destr(activeAccount.metadata); const openThread = () => { @@ -49,7 +47,7 @@ export const NoteComment = memo(function NoteComment({ content: value, created_at: dateToUnix(), kind: 1, - pubkey: activeAccount.id, + pubkey: activeAccount.pubkey, tags: [['e', eventID]], }; event.id = getEventHash(event); diff --git a/src/components/note/meta/reaction.tsx b/src/components/note/meta/reaction.tsx index 018d196b..00db6551 100644 --- a/src/components/note/meta/reaction.tsx +++ b/src/components/note/meta/reaction.tsx @@ -38,7 +38,7 @@ export const NoteReaction = memo(function NoteReaction({ ['p', eventPubkey], ], created_at: dateToUnix(), - pubkey: activeAccount.id, + pubkey: activeAccount.pubkey, }; event.id = getEventHash(event); event.sig = signEvent(event, activeAccount.privkey); diff --git a/src/components/note/metadata.tsx b/src/components/note/metadata.tsx index 0f7c0be4..15cbc5a5 100644 --- a/src/components/note/metadata.tsx +++ b/src/components/note/metadata.tsx @@ -2,8 +2,6 @@ import { NoteComment } from '@components/note/meta/comment'; import { NoteReaction } from '@components/note/meta/reaction'; import { RelayContext } from '@components/relaysProvider'; -import { createCacheCommentNote } from '@utils/storage'; - import { useContext, useEffect, useState } from 'react'; export default function NoteMetadata({ @@ -39,7 +37,7 @@ export default function NoteMetadata({ // update state setComments((comments) => (comments += 1)); // save comment to database - createCacheCommentNote(event, eventID); + // createCacheCommentNote(event, eventID); break; case 7: if (event.content === '🤙' || event.content === '+') { diff --git a/src/components/note/parent.tsx b/src/components/note/parent.tsx index 8614e3f9..07399915 100644 --- a/src/components/note/parent.tsx +++ b/src/components/note/parent.tsx @@ -45,7 +45,7 @@ export const NoteParent = memo(function NoteParent({ id }: { id: string }) { parent_id: parentID, parent_comment_id: '', created_at: event.created_at, - account_id: activeAccount.id, + account_id: activeAccount.pubkey, }).catch(console.error); }, undefined, diff --git a/src/components/note/repost.tsx b/src/components/note/repost.tsx index 1a8590ce..80394934 100644 --- a/src/components/note/repost.tsx +++ b/src/components/note/repost.tsx @@ -41,7 +41,7 @@ export const NoteRepost = memo(function NoteRepost({ id }: { id: string }) { parent_id: parentID, parent_comment_id: '', created_at: event.created_at, - account_id: activeAccount.id, + account_id: activeAccount.pubkey, }).catch(console.error); }, undefined, diff --git a/src/pages/newsfeed/[id].tsx b/src/pages/newsfeed/[id].tsx index 8fb1509d..33104674 100644 --- a/src/pages/newsfeed/[id].tsx +++ b/src/pages/newsfeed/[id].tsx @@ -6,8 +6,6 @@ import { NoteComment } from '@components/note/comment'; import { NoteExtend } from '@components/note/extend'; import { RelayContext } from '@components/relaysProvider'; -import { getAllCommentNotes, getNoteByID } from '@utils/storage'; - import { useRouter } from 'next/router'; import { JSXElementConstructor, @@ -29,12 +27,12 @@ export default function Page() { const [comments, setComments] = useState([]); useEffect(() => { - getNoteByID(id) + /*getNoteByID(id) .then((res) => { setRootEvent(res); getAllCommentNotes(id).then((res: any) => setComments(res)); }) - .catch(console.error); + .catch(console.error);*/ }, [id, pool, relays]); return ( diff --git a/src/utils/storage.tsx b/src/utils/storage.tsx deleted file mode 100644 index e9a5a812..00000000 --- a/src/utils/storage.tsx +++ /dev/null @@ -1,175 +0,0 @@ -import { getParentID } from '@utils/transform'; - -import Database from 'tauri-plugin-sql-api'; - -let db: null | Database = null; - -// connect database (sqlite) -// path: tauri::api::path::BaseDirectory::App -export async function connect(): Promise { - if (db) { - return db; - } - db = await Database.load('sqlite:lume.db'); - return db; -} - -// get all relays -export async function getAllRelays() { - const db = await connect(); - const result: any = await db.select('SELECT relay_url FROM relays WHERE relay_status = "1";'); - return result.reduce((relays, { relay_url }) => { - relays.push(relay_url); - return relays; - }, []); -} - -// get active account -export async function getActiveAccount() { - const db = await connect(); - const result = await db.select(`SELECT * FROM accounts LIMIT 1;`); - return result[0]; -} - -// get all accounts -export async function getAccounts() { - const db = await connect(); - return await db.select(`SELECT * FROM accounts`); -} - -// get all follows by account id -export async function getAllFollowsByID(id) { - const db = await connect(); - return await db.select(`SELECT pubkey FROM follows WHERE account = "${id}";`); -} - -// create account -export async function createAccount(data) { - const db = await connect(); - return await db.execute( - 'INSERT OR IGNORE INTO accounts (id, privkey, npub, nsec, metadata) VALUES (?, ?, ?, ?, ?);', - [data.pubkey, data.privkey, data.npub, data.nsec, data.metadata] - ); -} - -// create follow -export async function createFollow(pubkey, account, kind) { - const db = await connect(); - return await db.execute('INSERT OR IGNORE INTO follows (pubkey, account, kind) VALUES (?, ?, ?);', [ - pubkey, - account, - kind || 0, - ]); -} - -// create follow -export async function createFollows(data, account, kind) { - const db = await connect(); - data.forEach(async (item) => { - await db.execute('INSERT OR IGNORE INTO follows (pubkey, account, kind) VALUES (?, ?, ?);', [ - item, - account, - kind || 0, - ]); - }); - return 'ok'; -} - -// create cache profile -export async function createCacheProfile(id, metadata) { - const db = await connect(); - return await db.execute('INSERT OR IGNORE INTO cache_profiles (id, metadata) VALUES (?, ?);', [id, metadata]); -} - -// get cache profile -export async function getCacheProfile(id) { - const db = await connect(); - const result = await db.select(`SELECT metadata FROM cache_profiles WHERE id = "${id}"`); - return result[0]; -} - -// get all notes -export async function getNotes(time, limit, offset) { - const db = await connect(); - return await db.select( - `SELECT * FROM cache_notes WHERE created_at <= "${time}" GROUP BY parent_id ORDER BY created_at DESC LIMIT "${limit}" OFFSET "${offset}"` - ); -} - -// get all latest notes -export async function getLatestNotes(time) { - const db = await connect(); - return await db.select( - `SELECT * FROM cache_notes WHERE created_at > "${time}" GROUP BY parent_id ORDER BY created_at DESC` - ); -} - -// get note by id -export async function getNoteByID(id) { - const db = await connect(); - const result = await db.select(`SELECT * FROM cache_notes WHERE id = "${id}"`); - return result[0]; -} - -// create cache note -export async function createCacheNote(data) { - const db = await connect(); - return await db.execute( - 'INSERT OR IGNORE INTO cache_notes (id, pubkey, created_at, kind, content, tags, parent_id) VALUES (?, ?, ?, ?, ?, ?, ?);', - [ - data.id, - data.pubkey, - data.created_at, - data.kind, - data.content, - JSON.stringify(data.tags), - getParentID(data.tags, data.id), - ] - ); -} - -// get all comment notes -export async function getAllCommentNotes(eid) { - const db = await connect(); - return await db.select( - `SELECT * FROM cache_notes WHERE parent_comment_id = "${eid}" ORDER BY created_at DESC LIMIT 500` - ); -} - -// create cache comment note -export async function createCacheCommentNote(data, eid) { - const db = await connect(); - return await db.execute( - 'INSERT OR IGNORE INTO cache_notes (id, pubkey, created_at, kind, content, tags, parent_id, parent_comment_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?);', - [ - data.id, - data.pubkey, - data.created_at, - data.kind, - data.content, - JSON.stringify(data.tags), - getParentID(data.tags, data.id), - eid, - ] - ); -} - -// create cache comment note -export async function countTotalNotes() { - const db = await connect(); - const result = await db.select('SELECT COUNT(*) AS "total" FROM cache_notes;'); - return result[0]; -} - -// get last login time -export async function getLastLoginTime() { - const db = await connect(); - const result = await db.select('SELECT setting_value FROM settings WHERE setting_key = "last_login"'); - return result[0]; -} - -// update last login time -export async function updateLastLoginTime(time) { - const db = await connect(); - return await db.execute(`UPDATE settings SET setting_value = "${time}" WHERE setting_key = "last_login"`); -}