This commit is contained in:
rosetta
2026-01-30 05:01:05 +02:00
commit 83f38dc63f
327 changed files with 18725 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
import { useDatabase } from "@/app/providers/DatabaseProvider/useDatabase";
import { useEffect, useState } from "react";
import { createContext } from "react";
import { TABLES } from "./tables";
export const DatabaseContext = createContext<any>({});
interface DatabaseProviderProps {
children: React.ReactNode;
}
export function DatabaseProvider(props: DatabaseProviderProps) {
const [initialized, setInitialized] = useState(false);
const {runQuery} = useDatabase();
useEffect(() => {
(async () => {
await createAllTables();
setInitialized(true);
})();
}, []);
const createAllTables = async () => {
for(let i = 0; i < TABLES.length; i++){
await runQuery(TABLES[i]);
}
}
return (
<DatabaseContext.Provider value={{}}>
{initialized && props.children}
</DatabaseContext.Provider>
);
}

View File

@@ -0,0 +1,77 @@
export const TABLES = [
`CREATE TABLE IF NOT EXISTS accounts (
public_key TEXT PRIMARY KEY,
private_key TEXT NOT NULL,
sfen TEXT NOT NULL,
UNIQUE (public_key)
)`,
`CREATE TABLE IF NOT EXISTS blacklist (
id INTEGER PRIMARY KEY,
public_key TEXT NOT NULL,
account TEXT NOT NULL
)`,
`CREATE TABLE IF NOT EXISTS avatar_delivery (
id INTEGER PRIMARY KEY,
public_key TEXT NOT NULL,
account TEXT NOT NULL
)`,
`CREATE TABLE IF NOT EXISTS avatar_cache (
id INTEGER PRIMARY KEY,
public_key TEXT,
avatar TEXT NOT NULL,
timestamp INTEGER NOT NULL,
UNIQUE (id)
)`,
`CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY,
account TEXT NOT NULL,
from_public_key TEXT NOT NULL,
to_public_key BLOB NOT NULL,
content BLOB NOT NULL,
timestamp INTEGER NOT NULL,
chacha_key BLOB NOT NULL,
read INTEGER NOT NULL DEFAULT 0,
from_me INTEGER NOT NULL DEFAULT 0,
delivered INTEGER NOT NULL DEFAULT 0,
message_id TEXT NOT NULL DEFAULT '',
plain_message BLOB NOT NULL,
attachments TEXT NOT NULL DEFAULT '[]',
UNIQUE (id)
)`,
`CREATE TABLE IF NOT EXISTS cached_users (
public_key TEXT PRIMARY KEY,
title TEXT NOT NULL,
username TEXT NOT NULL,
verified INTEGER NOT NULL DEFAULT 0,
UNIQUE (public_key)
)`,
`CREATE TABLE IF NOT EXISTS groups (
id INTEGER PRIMARY KEY,
account TEXT NOT NULL,
group_id TEXT NOT NULL,
title TEXT NOT NULL,
description TEXT NOT NULL,
key TEXT NOT NULL,
UNIQUE (id)
)`,
/**
* dialog_id can be a public key for individual chats or a group ID for group chats
* last_message encoded with private key of the account
* last_message_from is the public key of the sender
*/
`CREATE TABLE IF NOT EXISTS dialogs (
id INTEGER PRIMARY KEY,
account TEXT NOT NULL,
dialog_id TEXT NOT NULL,
last_message_id TEXT NOT NULL,
last_timestamp INTEGER NOT NULL,
is_request INTEGER NOT NULL DEFAULT 0,
UNIQUE (id)
)`
]

View File

@@ -0,0 +1,38 @@
export function useDatabase() {
const buildDebug = (query: string, params: any[]) => {
console.info("-----------------");
//build final query
let finalQuery = query;
params.forEach((param) => {
let value = param;
if(typeof param === 'string'){
value = `'${param}'`;
}
finalQuery = finalQuery.replace('?', value);
});
console.info("Final Query: ", finalQuery);
console.info("-----------------");
}
const runQuery = async (query: string, params: any[] = []) => {
return await window.electron.ipcRenderer.invoke('db:run', query, params);
};
const getQuery = async (query: string, params: any[] = []) => {
return await window.electron.ipcRenderer.invoke('db:get', query, params);
};
const allQuery = async (query: string, params: any[] = [], debug: boolean = false) => {
if(debug){
buildDebug(query, params);
}
return await window.electron.ipcRenderer.invoke('db:all', query, params);
};
return {
runQuery,
getQuery,
allQuery,
};
}