wip: refactor

This commit is contained in:
Ren Amamiya
2023-08-15 21:13:58 +07:00
parent 6e28bcdb96
commit 2d53019c10
36 changed files with 603 additions and 552 deletions

View File

@@ -1,6 +1,5 @@
// inspire by: https://github.com/nostr-dev-kit/ndk-react/
import NDK from '@nostr-dev-kit/ndk';
import { fetch } from '@tauri-apps/plugin-http';
import { useEffect, useState } from 'react';
import TauriAdapter from '@libs/ndk/cache';
@@ -31,14 +30,21 @@ export const NDKInstance = () => {
}
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort('timeout'), 5000);
const res = await fetch(url, {
headers: { Accept: 'application/nostr+json' },
signal: controller.signal,
});
if (res.ok) {
const data = await res.json();
console.log('relay information: ', data);
verifiedRelays.push(relay);
clearTimeout(timeoutId);
} else {
console.log('relay not working: ', res);
}
} catch (e) {
console.log('fetch error', e);

View File

@@ -36,12 +36,6 @@ export async function getActiveAccount() {
'SELECT * FROM accounts WHERE is_active = 1;'
);
if (result.length > 0) {
result[0]['follows'] = result[0].follows
? JSON.parse(result[0].follows as unknown as string)
: null;
result[0]['network'] = result[0].network
? JSON.parse(result[0].network as unknown as string)
: null;
return result[0];
} else {
return null;

View File

@@ -7,10 +7,12 @@ import { Account, Relays, Widget } from '@utils/types';
export class LumeStorage {
public db: Database;
public secureDB: Stronghold;
public account: Account | null = null;
constructor(sqlite: Database, stronghold?: Stronghold) {
this.db = sqlite;
this.secureDB = stronghold ?? undefined;
this.account = null;
}
private async getSecureClient() {
@@ -45,19 +47,24 @@ export class LumeStorage {
}
public async getActiveAccount() {
const account: Account = await this.db.select(
'SELECT * FROM accounts WHERE is_active = 1;'
)?.[0];
if (account) {
const results: Array<Account> = await this.db.select(
'SELECT * FROM accounts WHERE is_active = "1" ORDER BY id DESC LIMIT 1;'
);
if (results.length > 0) {
const account = results[0];
if (typeof account.follows === 'string')
account.follows = JSON.parse(account.follows);
if (typeof account.network === 'string')
account.network = JSON.parse(account.network);
this.account = account;
return account;
} else {
throw new Error('Account not found');
console.log('no active account, please create new account');
return null;
}
}
@@ -75,27 +82,30 @@ export class LumeStorage {
}
public async updateAccount(column: string, value: string | string[]) {
const account = await this.getActiveAccount();
return await this.db.execute(`UPDATE accounts SET ${column} = $1 WHERE id = $2;`, [
value,
account.id,
]);
const insert = await this.db.execute(
`UPDATE accounts SET ${column} = $1 WHERE id = $2;`,
[value, this.account.id]
);
if (insert) {
const account = await this.getActiveAccount();
return account;
}
}
public async getWidgets() {
const account = await this.getActiveAccount();
const result: Array<Widget> = await this.db.select(
`SELECT * FROM widgets WHERE account_id = "${account.id}" ORDER BY created_at DESC;`
`SELECT * FROM widgets WHERE account_id = "${this.account.id}" ORDER BY created_at DESC;`
);
return result;
}
public async createWidget(kind: number, title: string, content: string | string[]) {
const account = await this.getActiveAccount();
const insert = await this.db.execute(
'INSERT OR IGNORE INTO widgets (account_id, kind, title, content) VALUES ($1, $2, $3, $4);',
[account.id, kind, title, content]
[this.account.id, kind, title, content]
);
if (insert) {
const widget: Widget = await this.db.select(
'SELECT * FROM widgets ORDER BY id DESC LIMIT 1;'
@@ -129,7 +139,7 @@ export class LumeStorage {
[cacheKey]
)?.[0];
if (!event) {
console.error('failed to get event by cache_key: ', cacheKey);
// console.error('failed to get event by cache_key: ', cacheKey);
return null;
}
return event;
@@ -141,16 +151,15 @@ export class LumeStorage {
[id]
)?.[0];
if (!event) {
console.error('failed to get event by id: ', id);
// console.error('failed to get event by id: ', id);
return null;
}
return event;
}
public async getExplicitRelayUrls() {
const account = await this.getActiveAccount();
const result: Relays[] = await this.db.select(
`SELECT * FROM relays WHERE account_id = "${account.id}";`
`SELECT * FROM relays WHERE account_id = "${this.account.id}";`
);
if (result.length > 0) return result.map((el) => el.relay);
@@ -158,10 +167,9 @@ export class LumeStorage {
}
public async createRelay(relay: string, purpose?: string) {
const account = await this.getActiveAccount();
return await this.db.execute(
'INSERT OR IGNORE INTO relays (account_id, relay, purpose) VALUES ($1, $2, $3);',
[account.id, relay, purpose || '']
[this.account.id, relay, purpose || '']
);
}

View File

@@ -17,6 +17,8 @@ const StorageProvider = ({ children }: PropsWithChildren<object>) => {
async function initLumeStorage() {
const sqlite = await Database.load('sqlite:lume.db');
const lumeStorage = new LumeStorage(sqlite);
if (!lumeStorage.account) await lumeStorage.getActiveAccount();
setDB(lumeStorage);
}