git ssb

5+

Matt McKegg / ferment



Tree: 13868e1aa6a7204af20406c760dac884fd24fdf1

Files: 13868e1aa6a7204af20406c760dac884fd24fdf1 / index.js

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

Built with git-ssb-web