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