2026-06-30 17:49:41 +05:30
|
|
|
// Biz Connect — technician desktop client (Electron main process).
|
|
|
|
|
//
|
|
|
|
|
// This is a thin shell: it loads the live Connect web UI from the server origin, so every
|
|
|
|
|
// relative /api and /ws URL in the web app keeps working unchanged. What it adds over a
|
|
|
|
|
// browser tab:
|
|
|
|
|
// - native full-screen capture for "Share Screen" (setDisplayMediaRequestHandler)
|
|
|
|
|
// - a real desktop window (no browser chrome), persisted login session
|
|
|
|
|
// - external links open in the user's browser, not inside the app
|
|
|
|
|
//
|
|
|
|
|
// Server origin is configurable so the same build works against prod or a dev server.
|
2026-07-01 16:28:15 +05:30
|
|
|
const { app, BrowserWindow, session, desktopCapturer, shell, Menu, ipcMain, nativeImage } = require('electron');
|
2026-06-30 17:49:41 +05:30
|
|
|
const path = require('path');
|
2026-07-01 21:28:53 +05:30
|
|
|
const fs = require('fs');
|
|
|
|
|
const os = require('os');
|
|
|
|
|
const crypto = require('crypto');
|
|
|
|
|
|
|
|
|
|
// A stable per-install id (persisted in userData), so the server can count installs and
|
|
|
|
|
// associate them with the user who signs in. Created once, then reused across launches.
|
|
|
|
|
function getInstallId() {
|
|
|
|
|
try {
|
|
|
|
|
const p = path.join(app.getPath('userData'), 'install-id');
|
|
|
|
|
if (fs.existsSync(p)) { const v = fs.readFileSync(p, 'utf8').trim(); if (v) return v; }
|
|
|
|
|
const id = crypto.randomUUID();
|
|
|
|
|
fs.writeFileSync(p, id);
|
|
|
|
|
return id;
|
|
|
|
|
} catch (_) { return 'unknown'; }
|
|
|
|
|
}
|
|
|
|
|
// The renderer (web app) reads this synchronously to report telemetry after login.
|
|
|
|
|
ipcMain.on('get-install-info', (e) => {
|
|
|
|
|
e.returnValue = { installId: getInstallId(), appVersion: app.getVersion(), os: process.platform + ' ' + os.release() };
|
|
|
|
|
});
|
2026-07-01 17:03:18 +05:30
|
|
|
// Auto-update: only NATIVE shell changes (this .exe) need this — all web/UI changes arrive
|
|
|
|
|
// live from the server. Checks the self-hosted feed (publish config in package.json →
|
|
|
|
|
// https://remote.bizgaze.com/downloads/latest.yml), downloads in the background, and installs
|
|
|
|
|
// on the next restart. No-op in dev (unpackaged).
|
|
|
|
|
let autoUpdater = null;
|
|
|
|
|
try { ({ autoUpdater } = require('electron-updater')); } catch (_) { /* not installed in dev */ }
|
2026-07-02 00:06:01 +05:30
|
|
|
// Native Windows toast WITH a reply box. node-notifier's WindowsToaster reliably SHOWS the
|
|
|
|
|
// toast (avatar + reply box) but its option whitelist has no `-w`, so SnoreToast never waits
|
|
|
|
|
// and the typed reply is lost. So we drive the bundled SnoreToast binary DIRECTLY with `-w` +
|
|
|
|
|
// our own named pipe to capture the reply — and fall back to WindowsToaster (display only) if
|
|
|
|
|
// the binary can't be found/spawned, so a toast always appears. Installed-app only (AppUserModelID).
|
2026-07-01 23:44:08 +05:30
|
|
|
const { spawn } = require('child_process');
|
|
|
|
|
const net = require('net');
|
2026-07-02 00:06:01 +05:30
|
|
|
let WindowsToaster = null;
|
|
|
|
|
try { WindowsToaster = require('node-notifier').WindowsToaster; } catch (_) { /* optional */ }
|
|
|
|
|
const APP_ID = 'com.bizgaze.connect.desktop';
|
|
|
|
|
const dbg = (o) => { try { fs.appendFileSync(path.join(app.getPath('userData'), 'toast-debug.log'), JSON.stringify(Object.assign({ t: Date.now() }, o)) + '\n'); } catch (_) {} };
|
2026-07-01 22:36:02 +05:30
|
|
|
|
2026-07-01 23:26:24 +05:30
|
|
|
// Write the avatar the renderer drew (a data: URL) to a temp PNG for SnoreToast's -p image.
|
|
|
|
|
function writeTempPng(dataUrl) {
|
|
|
|
|
try {
|
|
|
|
|
if (!dataUrl || !/^data:image\/png;base64,/.test(dataUrl)) return null;
|
|
|
|
|
const p = path.join(app.getPath('temp'), 'bizc-toast-' + crypto.randomBytes(4).toString('hex') + '.png');
|
|
|
|
|
fs.writeFileSync(p, Buffer.from(dataUrl.split(',')[1], 'base64'));
|
|
|
|
|
return p;
|
|
|
|
|
} catch (_) { return null; }
|
|
|
|
|
}
|
2026-07-02 00:06:01 +05:30
|
|
|
// Locate the SnoreToast binary node-notifier bundles (handles asar.unpacked in a packaged app).
|
|
|
|
|
function snoreExe() {
|
2026-07-01 22:36:02 +05:30
|
|
|
try {
|
2026-07-02 00:06:01 +05:30
|
|
|
const dir = path.dirname(require.resolve('node-notifier'));
|
|
|
|
|
const exe = os.arch() === 'ia32' ? 'snoretoast-x86.exe' : 'snoretoast-x64.exe';
|
|
|
|
|
const base = path.join(dir, 'vendor', 'snoreToast', exe);
|
|
|
|
|
for (const c of [base, base.replace('app.asar' + path.sep, 'app.asar.unpacked' + path.sep)]) {
|
|
|
|
|
if (fs.existsSync(c)) return c;
|
|
|
|
|
}
|
|
|
|
|
} catch (_) {}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
// Direct SnoreToast with -w so it waits and writes the reply to our pipe. Rejects if the binary
|
|
|
|
|
// is missing or fails to show a toast (so the caller can fall back). Args match node-notifier's
|
|
|
|
|
// known-good set + -w (no -application, which broke the toast).
|
|
|
|
|
function directToast(payload, img) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
const exe = snoreExe();
|
|
|
|
|
if (!exe) return reject(new Error('snoretoast not found'));
|
|
|
|
|
const pipe = '\\\\.\\pipe\\bizc-' + crypto.randomBytes(6).toString('hex');
|
|
|
|
|
let raw = Buffer.alloc(0), settled = false, server = null;
|
|
|
|
|
const settle = (fn, v) => { if (!settled) { settled = true; try { server && server.close(); } catch (_) {} fn(v); } };
|
2026-07-01 23:44:08 +05:30
|
|
|
server = net.createServer((sock) => { sock.on('data', (d) => { raw = Buffer.concat([raw, d]); }); });
|
2026-07-02 00:06:01 +05:30
|
|
|
server.on('error', (e) => settle(reject, e));
|
|
|
|
|
server.listen(pipe, () => {
|
|
|
|
|
const args = ['-appID', APP_ID, '-tb', '-w', '-pipeName', pipe];
|
2026-07-01 23:44:08 +05:30
|
|
|
if (img) args.push('-p', img);
|
2026-07-02 00:06:01 +05:30
|
|
|
args.push('-m', payload.body || ' ', '-t', payload.title || 'Biz Connect');
|
2026-07-01 23:44:08 +05:30
|
|
|
const child = spawn(exe, args, { windowsHide: true });
|
2026-07-02 00:06:01 +05:30
|
|
|
child.on('error', (e) => settle(reject, e));
|
|
|
|
|
child.on('exit', (code) => {
|
|
|
|
|
const s = raw.toString('utf16le').replace(/[\u0000\r\n]+/g, '').trim();
|
|
|
|
|
dbg({ code, raw: s });
|
|
|
|
|
const r = {};
|
|
|
|
|
s.split(';').forEach((kv) => { const i = kv.indexOf('='); if (i > 0) r[kv.slice(0, i).trim().toLowerCase()] = kv.slice(i + 1); });
|
|
|
|
|
let text = String(r.text || r.value || r.userinput || r.textbox || r.reply || '').trim();
|
|
|
|
|
if (!text && code === 5 && s) { const parts = s.split('='); text = (parts.length > 1 ? parts.slice(1).join('=') : s).trim(); }
|
|
|
|
|
if (text) return settle(resolve, { kind: payload.kind, id: payload.id, text });
|
|
|
|
|
// 0=activated, 1=hidden, 2=dismissed, 3=timedout, 4=button, 5=text. Anything else = failed → fall back.
|
|
|
|
|
if (![0, 1, 2, 3, 4, 5].includes(code)) return settle(reject, new Error('snoretoast exit ' + code));
|
|
|
|
|
const act = String(r.action || '').toLowerCase();
|
|
|
|
|
if (code === 0 || act.includes('activat') || act.includes('click')) return settle(resolve, { kind: payload.kind, id: payload.id, open: true });
|
|
|
|
|
settle(resolve, null);
|
|
|
|
|
});
|
2026-07-01 22:36:02 +05:30
|
|
|
});
|
2026-07-02 00:06:01 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// Fallback: node-notifier shows the toast (avatar + reply box) but can't capture the reply.
|
|
|
|
|
function fallbackToast(payload, img) {
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
if (!WindowsToaster) return resolve(null);
|
|
|
|
|
try {
|
|
|
|
|
new WindowsToaster({ withFallback: false }).notify(
|
|
|
|
|
Object.assign({ title: payload.title || 'Biz Connect', message: payload.body || ' ', appID: APP_ID, tb: true }, img ? { icon: img } : {}),
|
|
|
|
|
(err, response, metadata) => {
|
|
|
|
|
const act = String(response || (metadata && metadata.action) || '').toLowerCase();
|
|
|
|
|
resolve(act.includes('activat') || act === 'click' ? { kind: payload.kind, id: payload.id, open: true } : null);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
} catch (_) { resolve(null); }
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// Resolves with {text} (replied), {open} (toast clicked), or null (dismissed/timeout).
|
|
|
|
|
ipcMain.handle('reply-notification', async (_e, payload = {}) => {
|
|
|
|
|
const img = writeTempPng(payload.avatar);
|
|
|
|
|
const cleanup = () => { if (img) { try { fs.unlinkSync(img); } catch (_) {} } };
|
|
|
|
|
try {
|
|
|
|
|
const r = await directToast(payload, img);
|
|
|
|
|
cleanup();
|
|
|
|
|
return r;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
dbg({ fallback: String(e && e.message) });
|
|
|
|
|
const r = await fallbackToast(payload, img);
|
|
|
|
|
cleanup();
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
});
|
2026-06-30 17:49:41 +05:30
|
|
|
|
2026-07-01 18:19:01 +05:30
|
|
|
// Windows attributes notifications to the AppUserModelID. Without setting it, toasts read
|
|
|
|
|
// "electron.app.<name>"; setting it to the installer's appId makes Windows resolve the
|
|
|
|
|
// installed "Biz Connect" shortcut, so notifications show "Biz Connect".
|
|
|
|
|
app.setAppUserModelId('com.bizgaze.connect.desktop');
|
|
|
|
|
|
2026-07-01 16:16:40 +05:30
|
|
|
// Renderer asks (via preload) to raise the window — e.g. when an OS notification is clicked.
|
|
|
|
|
ipcMain.on('focus-window', () => {
|
|
|
|
|
if (!win) return;
|
|
|
|
|
if (win.isMinimized()) win.restore();
|
|
|
|
|
win.show();
|
|
|
|
|
win.focus();
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-01 16:28:15 +05:30
|
|
|
// Unread badge on the taskbar icon. The renderer computes the count (chats with unread) and
|
|
|
|
|
// draws the badge image (it has a canvas); Windows shows it via an overlay icon, macOS/Linux
|
|
|
|
|
// via the dock badge count.
|
|
|
|
|
ipcMain.on('set-unread', (_e, { count, dataUrl } = {}) => {
|
|
|
|
|
try {
|
|
|
|
|
if (typeof app.setBadgeCount === 'function') app.setBadgeCount(count || 0); // macOS/Linux dock
|
|
|
|
|
if (!win) return;
|
|
|
|
|
const overlay = (count > 0 && dataUrl) ? nativeImage.createFromDataURL(dataUrl) : null;
|
|
|
|
|
win.setOverlayIcon(overlay, count > 0 ? (count + ' unread chats') : ''); // Windows taskbar
|
|
|
|
|
} catch (_) {}
|
|
|
|
|
});
|
|
|
|
|
|
2026-06-30 17:49:41 +05:30
|
|
|
const SERVER_URL = (process.env.SERVER_URL || 'https://remote.bizgaze.com').replace(/\/+$/, '');
|
|
|
|
|
|
|
|
|
|
let win;
|
|
|
|
|
|
|
|
|
|
function createWindow() {
|
|
|
|
|
win = new BrowserWindow({
|
|
|
|
|
width: 1200,
|
|
|
|
|
height: 800,
|
|
|
|
|
minWidth: 880,
|
|
|
|
|
minHeight: 600,
|
|
|
|
|
title: 'Biz Connect',
|
|
|
|
|
backgroundColor: '#0f1830',
|
|
|
|
|
webPreferences: {
|
|
|
|
|
preload: path.join(__dirname, 'preload.js'),
|
|
|
|
|
contextIsolation: true,
|
|
|
|
|
nodeIntegration: false,
|
|
|
|
|
// Persist cookies/localStorage so the technician stays logged in between launches.
|
|
|
|
|
partition: 'persist:bizconnect',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-01 17:55:49 +05:30
|
|
|
// Open the landing page (same entry as the website): the "before login" screen with the
|
|
|
|
|
// no-login "Share my screen" option + sign-in. It redirects logged-in users straight to /home.
|
|
|
|
|
win.loadURL(SERVER_URL + '/');
|
2026-06-30 17:49:41 +05:30
|
|
|
|
|
|
|
|
// Open target=_blank / external links in the system browser instead of a new Electron window.
|
|
|
|
|
win.webContents.setWindowOpenHandler(({ url }) => {
|
|
|
|
|
if (!url.startsWith(SERVER_URL)) { shell.openExternal(url); return { action: 'deny' }; }
|
|
|
|
|
return { action: 'allow' };
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-01 16:16:40 +05:30
|
|
|
// The full Connect experience needs several web capabilities that Electron denies by
|
|
|
|
|
// default. We grant them for our own trusted origin:
|
|
|
|
|
// - media → camera + mic for meetings/calls (getUserMedia)
|
|
|
|
|
// - display-capture → "Share my screen" (getDisplayMedia)
|
|
|
|
|
// - notifications → in-app alerts
|
|
|
|
|
// - clipboard, fullscreen, pointerLock → chat paste + meeting UX
|
|
|
|
|
// Without this, meetings silently have no camera/mic and notifications never fire.
|
|
|
|
|
const GRANTED = new Set([
|
|
|
|
|
'media', 'display-capture', 'notifications',
|
|
|
|
|
'clipboard-read', 'clipboard-sanitized-write', 'fullscreen', 'pointerLock',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
function configureSession() {
|
|
|
|
|
const ses = session.fromPartition('persist:bizconnect');
|
|
|
|
|
// getDisplayMedia needs an explicit source. Default to the primary display + loopback audio.
|
|
|
|
|
// A production build can swap this for a source-picker window.
|
|
|
|
|
ses.setDisplayMediaRequestHandler((request, callback) => {
|
2026-06-30 17:49:41 +05:30
|
|
|
desktopCapturer.getSources({ types: ['screen', 'window'] }).then((sources) => {
|
|
|
|
|
callback(sources.length ? { video: sources[0], audio: 'loopback' } : {});
|
|
|
|
|
}).catch(() => callback({}));
|
2026-07-01 16:16:40 +05:30
|
|
|
}, { useSystemPicker: false });
|
|
|
|
|
// Async grant (getUserMedia, notifications, …)
|
|
|
|
|
ses.setPermissionRequestHandler((_wc, permission, callback) => callback(GRANTED.has(permission)));
|
|
|
|
|
// Sync check (some getUserMedia paths query this before requesting)
|
|
|
|
|
ses.setPermissionCheckHandler((_wc, permission) => GRANTED.has(permission));
|
2026-06-30 17:49:41 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.whenReady().then(() => {
|
2026-07-01 16:16:40 +05:30
|
|
|
configureSession();
|
2026-06-30 17:49:41 +05:30
|
|
|
createWindow();
|
|
|
|
|
Menu.setApplicationMenu(null); // hide the default menu bar; the web UI is the chrome
|
|
|
|
|
app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) createWindow(); });
|
2026-07-01 17:03:18 +05:30
|
|
|
// Check for shell updates on launch, then every 6 hours. Only in packaged builds.
|
|
|
|
|
if (app.isPackaged && autoUpdater) {
|
|
|
|
|
const check = () => autoUpdater.checkForUpdatesAndNotify().catch(() => {});
|
|
|
|
|
check();
|
|
|
|
|
setInterval(check, 6 * 60 * 60 * 1000);
|
|
|
|
|
}
|
2026-06-30 17:49:41 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.on('window-all-closed', () => { if (process.platform !== 'darwin') app.quit(); });
|