mirror of https://codeberg.org/pzp/zooboard.git
322 lines
9.4 KiB
JavaScript
322 lines
9.4 KiB
JavaScript
const { app, BrowserWindow, ipcMain, shell, clipboard } = require('electron')
|
|
const Path = require('node:path')
|
|
const URL = require('node:url')
|
|
const p = require('node:util').promisify
|
|
const Keypair = require('ppppp-keypair')
|
|
const awaitable = require('pull-awaitable')
|
|
|
|
// WARNING monkey patch! -------------------------------------------------------
|
|
const na = require('sodium-native')
|
|
na.sodium_malloc = function sodium_malloc_monkey_patched(n) {
|
|
return Buffer.alloc(n)
|
|
}
|
|
na.sodium_free = function sodium_free_monkey_patched() {}
|
|
// Electron > 20.3.8 breaks a napi method that `sodium_malloc` depends on to
|
|
// create external buffers. (see v8 memory cage)
|
|
//
|
|
// This crashes electron when called by various libraries, so we monkey-patch
|
|
// this particular function.
|
|
// -----------------------------------------------------------------------------
|
|
|
|
process.env.ZOOBOARD_DATA ??= Path.join(app.getPath('appData'), 'zooboard')
|
|
app.setPath('userData', process.env.ZOOBOARD_DATA)
|
|
const path = process.env.PZP_DIR ?? Path.resolve(app.getPath('userData'), 'ppppp')
|
|
|
|
const keypairPath = Path.join(path, 'keypair.json')
|
|
const keypair = Keypair.loadOrCreateSync(keypairPath)
|
|
let mainWindow
|
|
let globalAccountID = null
|
|
let globalAccountName = null
|
|
|
|
const peer = require('secret-stack/bare')()
|
|
.use(require('secret-stack/plugins/net'))
|
|
.use(require('secret-handshake-ext/secret-stack'))
|
|
.use(require('ppppp-net'))
|
|
.use(require('ppppp-db'))
|
|
.use(require('ppppp-set'))
|
|
.use(require('ppppp-dict'))
|
|
.use(require('ppppp-goals'))
|
|
.use(require('ppppp-sync'))
|
|
.use(require('ppppp-gc'))
|
|
.use(require('ppppp-conductor'))
|
|
.use(require('ppppp-hub-client'))
|
|
.use(require('ppppp-promise'))
|
|
.use(require('ppppp-invite'))
|
|
.call(null, {
|
|
shse: {
|
|
caps: require('ppppp-caps'),
|
|
},
|
|
global: {
|
|
keypair,
|
|
path,
|
|
timers: {
|
|
inactivity: 10 * 60e3,
|
|
},
|
|
connections: {
|
|
incoming: {
|
|
tunnel: [{ transform: 'shse', scope: 'public' }],
|
|
},
|
|
outgoing: {
|
|
net: [{ transform: 'shse' }],
|
|
tunnel: [{ transform: 'shse' }],
|
|
},
|
|
},
|
|
},
|
|
net: {
|
|
autostart: false,
|
|
},
|
|
})
|
|
|
|
function createWindow() {
|
|
mainWindow = new BrowserWindow({
|
|
width: 1200,
|
|
height: 1200,
|
|
title: 'Zooboard',
|
|
webPreferences: {
|
|
preload: Path.join(__dirname, 'preload.js'),
|
|
},
|
|
})
|
|
|
|
const startUrl =
|
|
process.env.ELECTRON_START_URL ??
|
|
URL.format({
|
|
pathname: Path.join(__dirname, '/../build/index.html'),
|
|
protocol: 'file:',
|
|
slashes: true,
|
|
})
|
|
mainWindow.loadURL(startUrl)
|
|
|
|
mainWindow.webContents.openDevTools({ mode: 'bottom', activate: true })
|
|
|
|
// open Web URLs in the default system browser
|
|
mainWindow.webContents.on('new-window', (ev, url) => {
|
|
ev.preventDefault()
|
|
shell.openExternal(url)
|
|
})
|
|
}
|
|
|
|
async function loadAccount() {
|
|
if (globalAccountID !== null) {
|
|
return { id: globalAccountID, name: globalAccountName }
|
|
}
|
|
|
|
// Kickstart
|
|
await peer.db.loaded()
|
|
const id = await p(peer.db.account.findOrCreate)({ subdomain: 'person' })
|
|
globalAccountID = id
|
|
await p(peer.set.load)(id)
|
|
await p(peer.dict.load)(id)
|
|
|
|
// Read profile
|
|
const profile = peer.dict.read(id, 'profile')
|
|
const name = profile?.name ?? ''
|
|
globalAccountName = name
|
|
|
|
peer.net.start()
|
|
|
|
return { id, name }
|
|
}
|
|
|
|
async function setProfileName(ev, name) {
|
|
await p(peer.dict.update)('profile', { name })
|
|
return name
|
|
}
|
|
|
|
async function writeElements(ev, elements) {
|
|
if (globalAccountID === null) throw new Error('account not loaded')
|
|
for (const element of elements) {
|
|
await p(peer.db.feed.publish)({
|
|
account: globalAccountID,
|
|
domain: 'zooboardElements',
|
|
data: element,
|
|
})
|
|
}
|
|
}
|
|
|
|
async function createInvite() {
|
|
if (globalAccountID === null) throw new Error('account not loaded')
|
|
let { url } = await p(peer.invite.createForFriend)({
|
|
hubs: 1,
|
|
id: globalAccountID,
|
|
})
|
|
// if the hub is on localhost, it's probably on the default port of 3000, so let's make things a bit easier for the user
|
|
if (url.indexOf('0.0.0.0') !== -1) url = url.replace("0.0.0.0", "0.0.0.0:3000")
|
|
return url
|
|
}
|
|
|
|
function copyToClipboard(ev, text) {
|
|
clipboard.writeText(text)
|
|
}
|
|
|
|
let hasSubscribedToReadElements = false
|
|
async function subscribeToReadElements() {
|
|
if (hasSubscribedToReadElements) return
|
|
hasSubscribedToReadElements = true
|
|
|
|
// Load initial elements and inform renderer
|
|
const elementsByID = new Map()
|
|
const msgIDToElemID = new Map()
|
|
for await (const { id: msgID, msg } of peer.db.records()) {
|
|
if (msg.data && msg.metadata.domain === 'zooboardElements') {
|
|
const { id: elemID, isDeleted } = msg.data
|
|
if (isDeleted) {
|
|
elementsByID.delete(elemID)
|
|
} else {
|
|
msgIDToElemID.set(msgID, elemID)
|
|
elementsByID.set(elemID, msg.data)
|
|
}
|
|
}
|
|
}
|
|
const initialElements = [...elementsByID.values()]
|
|
mainWindow.webContents.send('readElements', initialElements)
|
|
|
|
// Subscribe to new elements and inform renderer
|
|
peer.db.onRecordAdded(({ id: msgID, msg }) => {
|
|
if (msg.data && msg.metadata.domain === 'zooboardElements') {
|
|
const { id: elemID, isDeleted } = msg.data
|
|
if (isDeleted) {
|
|
elementsByID.delete(elemID)
|
|
} else {
|
|
msgIDToElemID.set(msgID, elemID)
|
|
elementsByID.set(elemID, msg.data)
|
|
}
|
|
mainWindow.webContents.send('readElements', [msg.data])
|
|
}
|
|
})
|
|
|
|
// Subscribe to deleted elements and inform renderer
|
|
peer.db.onRecordDeletedOrErased((msgID) => {
|
|
const elemID = msgIDToElemID.get(msgID)
|
|
if (!elemID) return
|
|
msgIDToElemID.delete(msgID)
|
|
// Is there some other msgID that supports this elemID? If so, bail out
|
|
for (const [, remainingElemID] of msgIDToElemID) {
|
|
if (remainingElemID === elemID) {
|
|
return
|
|
}
|
|
}
|
|
// If not, delete the element
|
|
elementsByID.delete(elemID)
|
|
mainWindow.webContents.send('readElements', [
|
|
{ id: elemID, isDeleted: true },
|
|
])
|
|
})
|
|
|
|
// Finally safe to kickstart replication and garbage collection
|
|
setTimeout(() => {
|
|
peer.conductor.start(
|
|
globalAccountID,
|
|
[
|
|
['profile@dict', 'zooboardElements@newest-100', 'hubs@set'],
|
|
['profile@dict', 'zooboardElements@newest-100'],
|
|
],
|
|
64_000
|
|
)
|
|
}, 32)
|
|
}
|
|
|
|
let hasSubscribedToConnections = false
|
|
async function subscribeToConnections() {
|
|
if (hasSubscribedToConnections) return
|
|
hasSubscribedToConnections = true
|
|
|
|
for await (const connections of awaitable(peer.net.peers())) {
|
|
mainWindow.webContents.send('connections', connections)
|
|
}
|
|
}
|
|
|
|
async function handlePPPPPUri(ev, uri) {
|
|
if (!globalAccountID) {
|
|
setTimeout(handlePPPPPUri, 100, null, uri)
|
|
return
|
|
}
|
|
if (uri.startsWith("http:") || uri.startsWith("https://")) {
|
|
uri = decodeURIComponent(uri.split('/invite#')[1])
|
|
}
|
|
if (!uri.startsWith('ppppp://')) return console.log('Not a ppppp invite URI', uri)
|
|
const commands = peer.invite.parse(uri)
|
|
for (const command of commands) {
|
|
console.log('Executing command', JSON.stringify(command))
|
|
switch (command.type) {
|
|
case 'join': {
|
|
try {
|
|
await p(peer.hubClient.addHub)(command.multiaddr)
|
|
} catch (err) {
|
|
console.error('Failed to properly join hub', err)
|
|
}
|
|
break
|
|
}
|
|
case 'follow': {
|
|
await p(peer.set.add)('follows', command.id)
|
|
break
|
|
}
|
|
case 'promise.follow': {
|
|
const [issuerType, issuerPubkey] = command.issuer
|
|
if (issuerType !== 'pubkey') {
|
|
throw new Error(`Dont know how to claim a ${issuerType} promise`)
|
|
}
|
|
// eslint-disable-next-line no-loop-func
|
|
peer.addListener('rpc:connect', function onConnect(rpc) {
|
|
if (rpc.shse.pubkey === issuerPubkey) {
|
|
peer.removeListener('rpc:connect', onConnect)
|
|
rpc.promise.follow(command.token, globalAccountID, (err) => {
|
|
if (err) return console.error('Failed to use follow promise', err)
|
|
})
|
|
}
|
|
})
|
|
break
|
|
}
|
|
default:
|
|
console.log('Unknown command type', command.type)
|
|
}
|
|
}
|
|
}
|
|
|
|
if (process.defaultApp) {
|
|
if (process.argv.length >= 2) {
|
|
app.setAsDefaultProtocolClient('ppppp', process.execPath, [
|
|
Path.resolve(process.argv[1]),
|
|
])
|
|
}
|
|
} else {
|
|
app.setAsDefaultProtocolClient('ppppp')
|
|
}
|
|
|
|
//const hasLock = app.requestSingleInstanceLock()
|
|
|
|
//if (!hasLock) {
|
|
// app.quit()
|
|
//} else {
|
|
// app.on('second-instance', (ev, argv, cwd, extraData) => {
|
|
// if (mainWindow) {
|
|
// if (mainWindow.isMinimized()) mainWindow.restore()
|
|
// mainWindow.focus()
|
|
// if (argv.length > 1) {
|
|
// handlePPPPPUri(null, argv[argv.length - 1])
|
|
// }
|
|
// }
|
|
// })
|
|
|
|
app.whenReady().then(() => {
|
|
ipcMain.handle('loadAccount', loadAccount)
|
|
ipcMain.handle('setProfileName', setProfileName)
|
|
ipcMain.handle('createInvite', createInvite)
|
|
ipcMain.handle('copyToClipboard', copyToClipboard)
|
|
ipcMain.handle('writeElements', writeElements)
|
|
ipcMain.handle('consumeInvite', handlePPPPPUri)
|
|
ipcMain.handle('subscribeToReadElements', subscribeToReadElements)
|
|
ipcMain.handle('subscribeToConnections', subscribeToConnections)
|
|
createWindow()
|
|
if (process.argv.length > 1) {
|
|
handlePPPPPUri(null, process.argv[process.argv.length - 1])
|
|
}
|
|
|
|
app.on('activate', function () {
|
|
if (BrowserWindow.getAllWindows().length === 0) createWindow()
|
|
})
|
|
app.on('window-all-closed', function () {
|
|
if (process.platform !== 'darwin') app.quit()
|
|
})
|
|
})
|
|
//}
|