git ssb

1+

Daan Patchwork / patchwork



Tree: a40d71e4d68bf04bb03d8040fbf7614880514dc8

Files: a40d71e4d68bf04bb03d8040fbf7614880514dc8 / index.js

7858 bytesRaw
1process.on('uncaughtException', function (err) {
2 console.log(err)
3 process.exit()
4})
5
6const electron = require('electron')
7const openWindow = require('./lib/window')
8
9const Path = require('path')
10const defaultMenu = require('electron-default-menu')
11const WindowState = require('electron-window-state')
12const Menu = electron.Menu
13const extend = require('xtend')
14const ssbKeys = require('ssb-keys')
15
16const windows = {
17 dialogs: new Set()
18}
19let ssbConfig = null
20let quitting = false
21
22/**
23 * It's not possible to run two instances of patchwork as it would create two
24 * ssb-server instances that conflict on the same port. Before opening patchwork,
25 * we check if it's already running and if it is we focus the existing window
26 * rather than opening a new instance.
27 */
28function quitIfAlreadyRunning () {
29 if (!electron.app.requestSingleInstanceLock()) {
30 console.log('Patchwork is already running!')
31 console.log('Please close the existing instance before starting a new one.')
32 return electron.app.quit()
33 }
34 electron.app.on('second-instance', () => {
35 // Someone tried to run a second instance, we should focus our window.
36 if (windows.main) {
37 if (windows.main.isMinimized()) windows.main.restore()
38 windows.main.focus()
39 }
40 })
41}
42
43const config = {
44 server: !(process.argv.includes('-g') || process.argv.includes('--use-global-ssb'))
45}
46// a flag so we don't start git-ssb-web if a custom path is passed in
47if (process.argv.includes('--path')) {
48 config.customPath = true
49}
50
51quitIfAlreadyRunning()
52
53electron.app.on('ready', () => {
54 setupContext(process.env.ssb_appname || 'ssb', {
55 server: !(process.argv.includes('-g') || process.argv.includes('--use-global-ssb'))
56 }, () => {
57 const browserWindow = openMainWindow()
58
59 browserWindow.on('app-command', (e, cmd) => {
60 switch (cmd) {
61 case 'browser-backward': {
62 browserWindow.webContents.send('goBack')
63 break
64 }
65 case 'browser-forward': {
66 browserWindow.webContents.send('goForward')
67 break
68 }
69 }
70 })
71
72 const menu = defaultMenu(electron.app, electron.shell)
73
74 menu.splice(4, 0, {
75 label: 'Navigation',
76 submenu: [
77 {
78 label: 'Activate Search Field',
79 accelerator: 'CmdOrCtrl+L',
80 click: () => {
81 browserWindow.webContents.send('activateSearch')
82 }
83 },
84 {
85 label: 'Back',
86 accelerator: 'CmdOrCtrl+[',
87 click: () => {
88 browserWindow.webContents.send('goBack')
89 }
90 },
91 {
92 label: 'Forward',
93 accelerator: 'CmdOrCtrl+]',
94 click: () => {
95 browserWindow.webContents.send('goForward')
96 }
97 },
98 {
99 type: 'separator'
100 },
101 {
102 label: 'Settings',
103 accelerator: 'CmdOrCtrl+,',
104 click: () => {
105 browserWindow.webContents.send('goToSettings')
106 }
107 }
108 ]
109 })
110
111 const view = menu.find(x => x.label === 'View')
112 view.submenu = [
113 { role: 'reload' },
114 { role: 'toggledevtools' },
115 { type: 'separator' },
116 { role: 'resetzoom' },
117 { role: 'zoomin', accelerator: 'CmdOrCtrl+=' },
118 { role: 'zoomout', accelerator: 'CmdOrCtrl+-' },
119 { type: 'separator' },
120 { role: 'togglefullscreen' }
121 ]
122 const help = menu.find(x => x.label === 'Help')
123 help.submenu = [
124 {
125 label: 'Learn More',
126 click () { require('electron').shell.openExternal('https://scuttlebutt.nz') }
127 }
128 ]
129 if (process.platform === 'darwin') {
130 const win = menu.find(x => x.label === 'Window')
131 win.submenu = [
132 { role: 'minimize' },
133 { role: 'zoom' },
134 { role: 'close', label: 'Close' },
135 { type: 'separator' },
136 { role: 'front' }
137 ]
138 }
139
140 Menu.setApplicationMenu(Menu.buildFromTemplate(menu))
141 })
142
143 electron.app.on('activate', function () {
144 if (windows.main) {
145 windows.main.show()
146 }
147 })
148
149 electron.app.on('before-quit', function () {
150 quitting = true
151 })
152
153 electron.ipcMain.handle('consoleLog', (ev, o) => console.log(o))
154 electron.ipcMain.handle('consoleError', (ev, o) => console.error(o))
155 electron.ipcMain.handle('badgeCount', (ev, count) => {
156 electron.app.badgeCount = count;
157 });
158 electron.ipcMain.on('exit', (ev, code) => process.exit(code))
159
160 electron.ipcMain.on('open-background-devtools', function () {
161 if (windows.background) {
162 windows.background.webContents.openDevTools({ mode: 'detach' })
163 }
164 })
165})
166
167function openMainWindow () {
168 if (!windows.main) {
169 const windowState = WindowState({
170 defaultWidth: 1024,
171 defaultHeight: 768
172 })
173 windows.main = openWindow(ssbConfig, Path.join(__dirname, 'lib', 'main-window.js'), {
174 minWidth: 800,
175 x: windowState.x,
176 y: windowState.y,
177 width: windowState.width,
178 height: windowState.height,
179 titleBarStyle: 'hiddenInset',
180 autoHideMenuBar: true,
181 title: 'Patchwork',
182 show: true,
183 backgroundColor: '#EEE',
184 icon: Path.join(__dirname, 'assets/icon.png')
185 })
186
187 windowState.manage(windows.main)
188 windows.main.setSheetOffset(40)
189 windows.main.on('close', function (e) {
190 if (!quitting && process.platform === 'darwin') {
191 e.preventDefault()
192 windows.main.hide()
193 }
194 })
195 windows.main.on('closed', function () {
196 windows.main = null
197 if (process.platform !== 'darwin') electron.app.quit()
198 })
199 }
200 return windows.main
201}
202
203function setupContext (appName, opts, cb) {
204 ssbConfig = require('ssb-config/inject')(appName, extend({
205 port: 8008,
206 blobsPort: 8989, // matches ssb-ws
207 friends: { // not using ssb-friends (sbot/contacts fixes hops at 2, so this setting won't do anything)
208 dunbar: 150,
209 hops: 2 // down from 3
210 }
211 }, opts))
212
213 // disable gossip auto-population from {type: 'pub'} messages as we handle this manually in sbot/index.js
214 if (!ssbConfig.gossip) ssbConfig.gossip = {}
215 ssbConfig.gossip.autoPopulate = false
216
217 ssbConfig.keys = ssbKeys.loadOrCreateSync(Path.join(ssbConfig.path, 'secret'))
218
219 const keys = ssbConfig.keys
220 const pubkey = keys.id.slice(1).replace(`.${keys.curve}`, '')
221
222 if (process.platform === 'win32') {
223 // fix offline on windows by specifying 127.0.0.1 instead of localhost (default)
224 ssbConfig.remote = `net:127.0.0.1:${ssbConfig.port}~shs:${pubkey}`
225 } else {
226 const socketPath = Path.join(ssbConfig.path, 'socket')
227 ssbConfig.connections.incoming.unix = [{ scope: 'device', transform: 'noauth' }]
228 ssbConfig.remote = `unix:${socketPath}:~noauth:${pubkey}`
229 }
230
231 // Support rooms
232 ssbConfig.connections.incoming.tunnel = [{ scope: 'public', transform: 'shs' }]
233 ssbConfig.connections.outgoing.tunnel = [{ transform: 'shs' }]
234
235 // Support DHT invites (only as a client, for now)
236 ssbConfig.connections.outgoing.dht = [{ transform: 'shs' }]
237
238 const redactedConfig = JSON.parse(JSON.stringify(ssbConfig))
239 redactedConfig.keys.private = null
240 console.dir(redactedConfig, { depth: null })
241
242 if (opts.server === false) {
243 cb && cb()
244 } else {
245 electron.ipcMain.once('server-started', function (ev, config) {
246 ssbConfig = config
247 cb && cb()
248 })
249 windows.background = openWindow(ssbConfig, Path.join(__dirname, 'lib', 'server-process.js'), {
250 connect: false,
251 center: true,
252 fullscreen: false,
253 fullscreenable: false,
254 height: 150,
255 maximizable: false,
256 minimizable: false,
257 resizable: false,
258 show: false,
259 skipTaskbar: true,
260 title: 'patchwork-server',
261 useContentSize: true,
262 width: 150
263 })
264 // windows.background.on('close', (ev) => {
265 // ev.preventDefault()
266 // windows.background.hide()
267 // })
268 }
269}
270

Built with git-ssb-web