git ssb

5+

Matt McKegg / ferment



Tree: 7b78a80338b818ed7a5a94ed61ba8cbfe4c9fdd5

Files: 7b78a80338b818ed7a5a94ed61ba8cbfe4c9fdd5 / index.js

5420 bytesRaw
1process.on('uncaughtException', function (err) {
2 console.log(err)
3 process.exit()
4})
5
6var electron = require('electron')
7var setupIpc = require('./lib/background-ipc')
8var openWindow = require('./lib/window')
9var createSbot = require('./lib/ssb-server')
10var serveBlobs = require('./lib/serve-blobs')
11var makeSingleInstance = require('./lib/make-single-instance')
12var pull = require('pull-stream')
13var pullFile = require('pull-file')
14var Path = require('path')
15var fs = require('fs')
16var defaultMenu = require('electron-default-menu')
17var Menu = electron.Menu
18
19var windows = {
20 dialogs: new Set()
21}
22
23var context = null
24// TODO: rewrite this to just use ssbConfig
25if (process.argv.includes('--test-peer')) {
26 // helpful for testing peers on a single machine
27 context = setupContext('ferment-peer', {
28 port: 43762
29 })
30} else if (process.argv.includes('--create-invite')) {
31 context = setupContext('ferment', { allowPrivate: true })
32 context.sbot.invite.create(1, (err, code) => {
33 if (err) throw err
34 console.log(`invite code:\n\n${code}\n`)
35 })
36} else if (process.argv.includes('--use-global-ssb') || process.argv.includes('-g')) {
37 context = setupContext('ssb', {
38 port: 8008,
39 blobsPort: 7777,
40 server: false
41 })
42} else {
43 makeSingleInstance(windows, openMainWindow)
44 context = setupContext('ferment')
45}
46
47electron.ipcMain.on('add-blob', (ev, id, path, cb) => {
48 pull(
49 pullFile(path),
50 context.sbot.blobs.add((err, hash) => {
51 if (err) return ev.sender.send('response', id, err)
52 ev.sender.send('response', id, null, hash)
53 })
54 )
55})
56
57electron.app.on('ready', function () {
58 Menu.setApplicationMenu(Menu.buildFromTemplate(defaultMenu(electron.app, electron.shell)))
59 setupIpc(windows)
60 startBackgroundProcess()
61 openMainWindow()
62})
63
64electron.app.on('activate', function (e) {
65 openMainWindow()
66})
67
68electron.ipcMain.on('open-add-window', (ev, data) => openAddWindow(data))
69electron.ipcMain.on('open-edit-profile-window', (ev, data) => openEditProfileWindow(data))
70electron.ipcMain.on('open-join-pub-window', openJoinPubWindow)
71electron.ipcMain.on('open-background-devtools', openBackgroundDevTools)
72
73function openMainWindow () {
74 if (!windows.main) {
75 windows.main = openWindow(context, Path.join(__dirname, 'main-window.js'), {
76 minWidth: 800,
77 width: 1024,
78 height: 768,
79 titleBarStyle: 'hidden-inset',
80 title: 'Ferment',
81 show: true,
82 backgroundColor: '#444',
83 acceptFirstMouse: true,
84 webPreferences: {
85 experimentalFeatures: true
86 }
87 })
88 windows.main.setSheetOffset(40)
89 windows.main.on('closed', function () {
90 windows.main = null
91 })
92 }
93}
94
95function openAddWindow (opts) {
96 var window = openWindow(context, Path.join(__dirname, 'add-audio-window.js'), {
97 parent: windows.main,
98 show: true,
99 width: 850,
100 height: 350,
101 useContentSize: true,
102 maximizable: false,
103 fullscreenable: false,
104 skipTaskbar: true,
105 resizable: false,
106 title: opts && opts.id ? 'Edit Audio File' : 'Add Audio File',
107 backgroundColor: '#444',
108 acceptFirstMouse: true,
109 data: opts
110 })
111
112 windows.dialogs.add(window)
113
114 window.on('closed', function () {
115 windows.dialogs.delete(window)
116 })
117}
118
119function openEditProfileWindow (opts) {
120 var window = openWindow(context, Path.join(__dirname, 'edit-profile-window.js'), {
121 parent: windows.main,
122 modal: true,
123 show: true,
124 width: 800,
125 height: 300,
126 useContentSize: true,
127 maximizable: false,
128 fullscreenable: false,
129 skipTaskbar: true,
130 resizable: false,
131 title: 'Edit Profile',
132 backgroundColor: '#444',
133 acceptFirstMouse: true,
134 data: opts
135 })
136
137 windows.dialogs.add(window)
138
139 window.on('closed', function () {
140 windows.dialogs.delete(window)
141 })
142}
143
144function openJoinPubWindow () {
145 var window = openWindow(context, Path.join(__dirname, 'join-pub-window.js'), {
146 parent: windows.main,
147 modal: true,
148 show: true,
149 width: 650,
150 height: 280,
151 useContentSize: true,
152 maximizable: false,
153 fullscreenable: false,
154 skipTaskbar: true,
155 resizable: false,
156 title: 'Join Public Server',
157 backgroundColor: '#444',
158 acceptFirstMouse: true
159 })
160
161 windows.dialogs.add(window)
162
163 window.on('closed', function () {
164 windows.dialogs.delete(window)
165 })
166}
167
168function startBackgroundProcess () {
169 windows.background = openWindow(context, Path.join(__dirname, 'background-window.js'), {
170 center: true,
171 fullscreen: false,
172 fullscreenable: false,
173 height: 150,
174 maximizable: false,
175 minimizable: false,
176 resizable: false,
177 show: false,
178 skipTaskbar: true,
179 title: 'ferment-background-window',
180 useContentSize: true,
181 width: 150
182 })
183}
184
185function openBackgroundDevTools () {
186 if (windows.background) {
187 windows.background.webContents.openDevTools({detach: true})
188 }
189}
190
191function setupContext (appName, opts) {
192 var ssbConfig = require('./lib/ssb-config')(appName, opts)
193 if (opts && opts.server === false) {
194 return {
195 config: ssbConfig
196 }
197 } else {
198 var context = {
199 sbot: createSbot(ssbConfig),
200 config: ssbConfig
201 }
202 ssbConfig.manifest = context.sbot.getManifest()
203 serveBlobs(context)
204 fs.writeFileSync(Path.join(ssbConfig.path, 'manifest.json'), JSON.stringify(ssbConfig.manifest))
205 console.log(`Address: ${context.sbot.getAddress()}`)
206 return context
207 }
208}
209

Built with git-ssb-web