Refactor and code designing

This commit is contained in:
2026-03-02 03:30:22 +01:00
parent 6d7465ff18
commit e231f4ed05
80 changed files with 6690 additions and 4670 deletions

View File

@@ -321,13 +321,11 @@ function rowToBan(r) {
/* ------------------------------------------------------------------ */
function registerDatabaseIpc() {
// ── Lifecycle ──────────────────────────────────────────────────────
ipcMain.handle('db:initialize', async () => {
await initDatabase();
return true;
});
// ── Messages ───────────────────────────────────────────────────────
ipcMain.handle('db:saveMessage', (_e, message) => {
db.run(
`INSERT OR REPLACE INTO messages
@@ -390,7 +388,6 @@ function registerDatabaseIpc() {
persist();
});
// ── Reactions ──────────────────────────────────────────────────────
ipcMain.handle('db:saveReaction', (_e, reaction) => {
const check = db.exec(
'SELECT 1 FROM reactions WHERE messageId = ? AND userId = ? AND emoji = ?',
@@ -416,7 +413,6 @@ function registerDatabaseIpc() {
return rows.map(rowToReaction);
});
// ── Users ──────────────────────────────────────────────────────────
ipcMain.handle('db:saveUser', (_e, user) => {
db.run(
`INSERT OR REPLACE INTO users
@@ -486,7 +482,6 @@ function registerDatabaseIpc() {
persist();
});
// ── Rooms ──────────────────────────────────────────────────────────
ipcMain.handle('db:saveRoom', (_e, room) => {
db.run(
`INSERT OR REPLACE INTO rooms
@@ -542,7 +537,6 @@ function registerDatabaseIpc() {
persist();
});
// ── Bans ───────────────────────────────────────────────────────────
ipcMain.handle('db:saveBan', (_e, ban) => {
db.run(
`INSERT OR REPLACE INTO bans
@@ -579,7 +573,6 @@ function registerDatabaseIpc() {
return rows.some((r) => String(r.oderId) === userId);
});
// ── Attachments ─────────────────────────────────────────────────────
ipcMain.handle('db:saveAttachment', (_e, attachment) => {
db.run(
`INSERT OR REPLACE INTO attachments
@@ -610,7 +603,6 @@ function registerDatabaseIpc() {
persist();
});
// ── Utilities ──────────────────────────────────────────────────────
ipcMain.handle('db:clearAllData', () => {
db.run('DELETE FROM messages');
db.run('DELETE FROM users');

View File

@@ -1,4 +1,4 @@
const { app, BrowserWindow, ipcMain, desktopCapturer } = require('electron');
const { app, BrowserWindow, ipcMain, desktopCapturer, shell } = require('electron');
const fs = require('fs');
const fsp = fs.promises;
const path = require('path');
@@ -50,11 +50,36 @@ function createWindow() {
mainWindow.on('closed', () => {
mainWindow = null;
});
// Force all external links to open in the system default browser
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
shell.openExternal(url);
return { action: 'deny' };
});
mainWindow.webContents.on('will-navigate', (event, url) => {
// Allow navigation to the app itself (dev server or file://)
const currentUrl = mainWindow.webContents.getURL();
const isSameOrigin = new URL(url).origin === new URL(currentUrl).origin;
if (!isSameOrigin) {
event.preventDefault();
shell.openExternal(url);
}
});
}
// Register database IPC handlers before app is ready
registerDatabaseIpc();
// IPC handler for opening URLs in the system default browser
ipcMain.handle('open-external', async (_event, url) => {
if (typeof url === 'string' && (url.startsWith('http://') || url.startsWith('https://'))) {
await shell.openExternal(url);
return true;
}
return false;
});
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {

View File

@@ -6,6 +6,9 @@ contextBridge.exposeInMainWorld('electronAPI', {
maximizeWindow: () => ipcRenderer.send('window-maximize'),
closeWindow: () => ipcRenderer.send('window-close'),
// Open URL in system default browser
openExternal: (url) => ipcRenderer.invoke('open-external', url),
// Desktop capturer for screen sharing
getSources: () => ipcRenderer.invoke('get-sources'),
@@ -18,7 +21,6 @@ contextBridge.exposeInMainWorld('electronAPI', {
fileExists: (filePath) => ipcRenderer.invoke('file-exists', filePath),
ensureDir: (dirPath) => ipcRenderer.invoke('ensure-dir', dirPath),
// ── Database operations (all SQL lives in main process) ───────────
db: {
initialize: () => ipcRenderer.invoke('db:initialize'),