completed migrate to tauri-sql

This commit is contained in:
Ren Amamiya
2023-04-18 19:15:04 +07:00
parent fc1101f97b
commit c72798507e
40 changed files with 321 additions and 476 deletions

View File

@@ -1,110 +0,0 @@
// This file was generated by [tauri-specta](https://github.com/oscartbeaumont/tauri-specta). Do not edit this file manually.
declare global {
interface Window {
__TAURI_INVOKE__<T>(cmd: string, args?: Record<string, unknown>): Promise<T>;
}
}
const invoke = window.__TAURI_INVOKE__;
export function getAccounts() {
return invoke<Account[]>('get_accounts');
}
export function createAccount(data: CreateAccountData) {
return invoke<Account>('create_account', { data });
}
export function getPlebs(data: GetPlebData) {
return invoke<Pleb[]>('get_plebs', { data });
}
export function getPlebByPubkey(data: GetPlebPubkeyData) {
return invoke<Pleb | null>('get_pleb_by_pubkey', { data });
}
export function createPleb(data: CreatePlebData) {
return invoke<Pleb>('create_pleb', { data });
}
export function createNote(data: CreateNoteData) {
return invoke<Note>('create_note', { data });
}
export function getNotes(data: GetNoteData) {
return invoke<Note[]>('get_notes', { data });
}
export function getLatestNotes(data: GetLatestNoteData) {
return invoke<Note[]>('get_latest_notes', { data });
}
export function getNoteById(data: GetNoteByIdData) {
return invoke<Note | null>('get_note_by_id', { data });
}
export function createChannel(data: CreateChannelData) {
return invoke<Channel>('create_channel', { data });
}
export function updateChannel(data: UpdateChannelData) {
return invoke<Channel>('update_channel', { data });
}
export function getChannels(data: GetChannelData) {
return invoke<Channel[]>('get_channels', { data });
}
export function getActiveChannels(data: GetActiveChannelData) {
return invoke<Channel[]>('get_active_channels', { data });
}
export function createChat(data: CreateChatData) {
return invoke<Chat>('create_chat', { data });
}
export function getChats(data: GetChatData) {
return invoke<Chat[]>('get_chats', { data });
}
export type GetLatestNoteData = { date: number };
export type Note = {
id: number;
eventId: string;
pubkey: string;
kind: number;
tags: string;
content: string;
parent_id: string;
parent_comment_id: string;
createdAt: number;
accountId: number;
};
export type GetActiveChannelData = { active: boolean };
export type CreateChannelData = { event_id: string; content: string; account_id: number };
export type CreateNoteData = {
event_id: string;
pubkey: string;
kind: number;
tags: string;
content: string;
parent_id: string;
parent_comment_id: string;
created_at: number;
account_id: number;
};
export type GetPlebData = { account_id: number; kind: number };
export type CreatePlebData = { pleb_id: string; pubkey: string; kind: number; metadata: string; account_id: number };
export type Chat = { id: number; pubkey: string; createdAt: number; accountId: number };
export type Account = { id: number; pubkey: string; privkey: string; active: boolean; metadata: string };
export type GetNoteByIdData = { event_id: string };
export type GetPlebPubkeyData = { pubkey: string };
export type GetChatData = { account_id: number };
export type GetNoteData = { date: number; limit: number; offset: number };
export type Channel = { id: number; eventId: string; content: string; active: boolean; accountId: number };
export type UpdateChannelData = { event_id: string; active: boolean };
export type CreateChatData = { pubkey: string; created_at: number; account_id: number };
export type CreateAccountData = { pubkey: string; privkey: string; metadata: string };
export type GetChannelData = { limit: number; offset: number };
export type Pleb = { id: number; plebId: string; pubkey: string; kind: number; metadata: string; accountId: number };

View File

@@ -1,13 +0,0 @@
import { invoke } from '@tauri-apps/api';
export function countTotalNotes() {
return invoke('count_total_notes');
}
export function countTotalChannels() {
return invoke('count_total_channels');
}
export function countTotalChats() {
return invoke('count_total_chats');
}

View File

@@ -1,3 +1,5 @@
import { createPleb } from '@utils/storage';
import useLocalStorage from '@rehooks/local-storage';
import { fetch } from '@tauri-apps/api/http';
import { useCallback, useEffect, useMemo, useState } from 'react';
@@ -11,8 +13,8 @@ export const fetchProfileMetadata = async (pubkey: string) => {
};
export const useProfileMetadata = (pubkey) => {
const [activeAccount]: any = useLocalStorage('activeAccount', {});
const [plebs] = useLocalStorage('activeAccountFollows', []);
const [activeAccount]: any = useLocalStorage('account', {});
const [plebs] = useLocalStorage('plebs', []);
const [profile, setProfile] = useState(null);
const cacheProfile = useMemo(() => {
@@ -31,19 +33,9 @@ export const useProfileMetadata = (pubkey) => {
return metadata;
}, [plebs, pubkey, activeAccount.pubkey, activeAccount.metadata]);
const insertPlebToDB = useCallback(
async (pubkey: string, metadata: string) => {
const { createPleb } = await import('@utils/bindings');
return await createPleb({
pleb_id: pubkey + '-lume' + activeAccount.id,
pubkey: pubkey,
kind: 1,
metadata: metadata,
account_id: activeAccount.id,
}).catch(console.error);
},
[activeAccount.id]
);
const insertPlebToDB = useCallback(async (pubkey: string, metadata: string) => {
return createPleb(pubkey, metadata);
}, []);
useEffect(() => {
if (!cacheProfile) {

View File

@@ -20,6 +20,12 @@ export async function getActiveAccount() {
return result[0];
}
// get all accounts
export async function getAccounts() {
const db = await connect();
return await db.select(`SELECT * FROM accounts ORDER BY created_at DESC;`);
}
// create account
export async function createAccount(pubkey: string, privkey: string, metadata: string) {
const db = await connect();
@@ -36,12 +42,25 @@ export async function updateAccount(column: string, value: string | string[], pu
return await db.execute(`UPDATE accounts SET ${column} = '${JSON.stringify(value)}' WHERE pubkey = "${pubkey}";`);
}
// get all plebs
export async function getPlebs() {
const db = await connect();
return await db.select(`SELECT * FROM plebs ORDER BY created_at DESC;`);
}
// create pleb
export async function createPleb(pubkey: string, metadata: string) {
const db = await connect();
return await db.execute('INSERT OR IGNORE INTO plebs (pubkey, metadata) VALUES (?, ?);', [pubkey, metadata]);
}
// count total notes
export async function countTotalChannels() {
const db = await connect();
const result = await db.select('SELECT COUNT(*) AS "total" FROM channels;');
return result[0];
}
// count total notes
export async function countTotalNotes() {
const db = await connect();
@@ -50,15 +69,72 @@ export async function countTotalNotes() {
}
// get all notes
export async function getNotes(time: string, limit: number, offset: number) {
export async function getNotes(time: number, limit: number, offset: number) {
const db = await connect();
return await db.select(
`SELECT * FROM notes WHERE created_at <= "${time}" ORDER BY created_at DESC LIMIT "${limit}" OFFSET "${offset}";`
);
}
// get note by id
export async function getNoteByID(event_id) {
const db = await connect();
const result = await db.select(`SELECT * FROM notes WHERE event_id = "${event_id}";`);
return result[0];
}
// get all latest notes
export async function getLatestNotes(time) {
export async function getLatestNotes(time: number) {
const db = await connect();
return await db.select(`SELECT * FROM cache_notes WHERE created_at > "${time}" ORDER BY created_at DESC;`);
}
// create note
export async function createNote(
event_id: string,
account_id: number,
pubkey: string,
kind: number,
tags: string[],
content: string,
created_at: number,
parent_id: string
) {
const db = await connect();
return await db.execute(
'INSERT OR IGNORE INTO notes (event_id, account_id, pubkey, kind, tags, content, created_at, parent_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?);',
[event_id, account_id, pubkey, kind, tags, content, created_at, parent_id]
);
}
// get all channels
export async function getChannels(limit: number, offset: number) {
const db = await connect();
return await db.select(`SELECT * FROM channels ORDER BY created_at DESC LIMIT "${limit}" OFFSET "${offset}";`);
}
// create channel
export async function createChannel(event_id: string, metadata: string, created_at: number) {
const db = await connect();
return await db.execute('INSERT OR IGNORE INTO channels (event_id, metadata, created_at) VALUES (?, ?, ?);', [
event_id,
metadata,
created_at,
]);
}
// get all chats
export async function getChats(account_id: number) {
const db = await connect();
return await db.select(`SELECT * FROM chats WHERE account_id <= "${account_id}" ORDER BY created_at DESC;`);
}
// create chat
export async function createChat(account_id: number, pubkey: string, created_at: number) {
const db = await connect();
return await db.execute('INSERT OR IGNORE INTO chats (account_id, pubkey, created_at) VALUES (?, ?, ?);', [
account_id,
pubkey,
created_at,
]);
}

View File

@@ -1,33 +1,26 @@
import destr from 'destr';
export const tagsToArray = (arr) => {
const newarr = [];
// push item to newarr
arr.forEach((item) => {
newarr.push(item[1]);
// convert NIP-02 to array of pubkey
export const nip02ToArray = (tags: string[]) => {
const arr = [];
tags.forEach((item) => {
arr.push(item[1]);
});
return newarr;
return arr;
};
// convert array to NIP-02 tag list
export const arrayToNIP02 = (arr: string[]) => {
const nip03_array = [];
const nip02_arr = [];
arr.forEach((item) => {
nip03_array.push(['p', item]);
nip02_arr.push(['p', item]);
});
return nip03_array;
return nip02_arr;
};
export const pubkeyArray = (arr) => {
const newarr = [];
// push item to newarr
arr.forEach((item) => {
newarr.push(item.pubkey);
});
return newarr;
};
export const getParentID = (arr, fallback) => {
// get parent id from event tags
export const getParentID = (arr: string[], fallback: string) => {
const tags = destr(arr);
let parentID = fallback;