-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
684 lines (586 loc) · 16.9 KB
/
app.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
'use strict'
const EE = require('events')
const path = require('path')
const inherits = require('util').inherits
const delegate = require('delegate-dom')
const h = require('virtual-dom/h')
const diff = require('virtual-dom/diff')
const patch = require('virtual-dom/patch')
const createElement = require('virtual-dom/create-element')
const debug = require('debug')('eyearesee:app')
const CommandManager = require('./lib/commands')
const mapUtil = require('map-util')
const Tooltip = require('./lib/tooltip')
const Styles = require('./lib/styles/manager')
const Themes = require('./lib/themes')
const Router = require('./lib/router')
const Panels = require('./lib/panels')
const utils = require('./lib/utils')
const About = require('./lib/about')
const Logger = require('daily-file-writer')
const Notifications = require('./lib/notifications')
module.exports = window.App = App
const IRC = require('eyearesee-client')
const Connection = IRC.Connection
const Settings = IRC.Settings
const auth = IRC.auth
const defaultSettings = new Map([
['theme.active', 'dusk.css']
, ['invites.accept.auto', false]
, ['sounds.enabled', true]
, ['userbar.hidden', false]
, ['user.color', '#fb73fa']
, ['inline.images', false]
])
function App(el, currentWindow) {
if (!(this instanceof App))
return new App(el, currentWindow)
EE.call(this)
this._notifications = 0
global.app = this
this.el = el
this.window = currentWindow
this._isLoading = true
this.db = require('./lib/db')
this.settings = new Settings(defaultSettings)
this.inputHandler = require('./lib/handle-input')(this)
this.commandManager = new CommandManager()
this.styles = new Styles()
this.themes = new Themes(this)
this.panels = new Panels(this)
this.about = new About()
this.notifications = new Notifications(this)
this.loggers = new Map()
this.activeModel = null
this._addCommands()
this.views = require('./lib/views')(this)
this._views = new WeakMap()
this.connections = new Map()
this.tooltips = new Map()
this.router = new Router(this)
this.url = '/'
this._shouldAutoScroll = true
this._addRoutes()
this._addStyles()
this._addHandlers()
this._addWindowHandlers()
this.once('loaded', () => {
const v = document.querySelector('irc-loading-view')
if (v) {
document.body.removeChild(v)
}
var tree = this._renderInside(this.views.login.render(), 2)
var rootNode = createElement(tree)
el.appendChild(rootNode)
this.on('render', (view) => {
var newTree = view
if (!newTree) return
const patches = diff(tree, newTree)
rootNode = patch(rootNode, patches)
tree = newTree
if (this.activeModel && this._shouldAutoScroll) {
if (this.activeModel.ele) {
const ele = document.querySelector(this.activeModel.ele)
if (ele) {
// TODO(evanlucas) Fix this so it auto scrolls only
// if we are not already scrolling
//
// if (ele.scrollHeight - ele.clientHeight <= ele.scrollTop + 1) {
// ele.scrollTop = ele.scrollHeight
// }
ele.scrollTop = ele.scrollHeight
}
} else if (this.activeModel instanceof Settings) {
const ele = document.querySelector('.settings-container')
if (ele) {
ele.scrollTop = 0
}
}
}
if (this.views.input.commandBar.isShowing()) {
const item = document.querySelector('.command.active')
if (item) {
item.scrollIntoViewIfNeeded(false)
}
}
})
})
this.load()
}
inherits(App, EE)
App.prototype._addWindowHandlers = function _addWindowHandlers() {
this.window.on('focus', () => {
if (this.activeModel && this.activeModel.unread) {
// clear the sidebar badges for this channel
const before = this.activeModel.unread
this.activeModel.unread = 0
// decrement the badge count by _before_
this.decrementBadgeCount(before)
this.needsLayout()
}
})
}
App.prototype._addCommands = function _addCommands() {
this.commandManager.addDefaults()
}
App.prototype._addStyles = function _addStyles() {
const ele = this.styles.buildElement()
document.head.appendChild(ele)
}
App.prototype.isActive = function isActive(item) {
return this.url === item.url
}
App.prototype.decrementBadgeCount = function decrementBadgeCount(n) {
const diff = this._notifications - n
if (diff > 0) {
this.setBadge(diff)
} else if (diff === 0) {
this.setBadge(false)
}
}
App.prototype._clearViewBadge = function _clearViewBadge(model) {
if (!model) return
const unread = model.unread
if (unread) {
model.unread = 0
this.decrementBadgeCount(unread)
}
}
App.prototype._addRoutes = function _addRoutes() {
const views = this.views
this.router.add('/login', () => {
this.activeModel = null
this.renderInside(views.login.render(), 2)
})
this.router.add('/about', () => {
this.activeModel = this.about
this.renderOutside(views.about.render())
})
this.router.add('/settings', () => {
this.activeModel = this.settings
this.renderInside(views.settings.render(), 2)
})
this.router.add('/connection', () => {
const conn = mapUtil.firstVal(this.connections)
if (conn) {
this.router.goto(conn.url)
}
})
this.router.add('/connections/:name', (params) => {
const conn = this.connections.get(params.name.toLowerCase())
if (!conn) {
debug('404...cannot find connection %s', params.name)
return
}
this.activeModel = conn
this.renderInside([
views.connection.render(conn)
, views.input.render()
], 2)
})
this.router.add('/connections/:name/settings', (params) => {
const conn = this.connections.get(params.name.toLowerCase())
if (!conn) {
debug('404...cannot find connection %s', params.name)
return
}
this.activeModel = conn.settings
this.renderInside([
views.connSettings.render(conn.settings)
, views.input.render()
], 2)
})
this.router.add('/connections/:name/channels/:channelName', (params) => {
const conn = this.connections.get(params.name.toLowerCase())
if (!conn) {
debug('404...cannot find connection %s', params.name)
return
}
const name = params.channelName.toLowerCase()
let chan = conn.channels.get(name)
if (!chan) {
chan = conn.queries.get(name)
if (!chan) {
debug('404...cannot find channel %s %s', conn.name, name)
return
}
}
const cols = this.settings.get('userbar.hidden')
? 2
: 3
this._clearViewBadge(chan)
this.activeModel = chan
this.renderInside([
views.channel.render(chan)
, views.input.render()
], cols)
})
}
App.prototype.renderOutside = function renderOutside(args) {
this.emit('render', args)
}
App.prototype.renderInside = function renderInside(args, cols) {
this.emit('render', this._renderInside(args, cols))
}
App.prototype._renderInside = function _renderInside(args, cols) {
cols = cols || 2
const views = this.views
if (!Array.isArray(args)) args = [args]
return h(`irc-workspace.col-${cols}.pure-g`, [
h('irc-notification.hide', [
h('p', '')
])
, views.serverbar.render()
, h('irc-sidebar.pure-u', [
views.sidebar.render()
])
, h('.container.pure-u-1', args)
])
}
App.prototype._addHandlers = function _addHandlers() {
delegate.on(this.el, 'a.external-url, a.external-url img', 'click', (e) => {
e.preventDefault()
var a = e.target
if (a && a.href) {
this.emit('openUrl', a.href)
} else {
if (a.tagName === 'IMG') {
this.emit('openUrl', a.parentNode.href)
}
}
return false
})
delegate.on(this.el, 'a.internal-url', 'click', (e) => {
e.preventDefault()
const a = e.target
if (a && a.href) {
const u = a.href.replace('file://', '')
this.router.goto(u)
}
return false
})
this.on('scroll', (e) => {
const node = e.target
if (node.scrollHeight <= node.clientHeight + node.scrollTop) {
this._shouldAutoScroll = true
debug('should auto scroll')
} else {
debug('should not auto scroll')
this._shouldAutoScroll = false
}
})
const addConnTooltip = new Tooltip(this.el, {
selector: 'a.add-connection'
, placement: 'right'
, container: 'body'
, viewportPadding: 2
, title: 'Create Connection'
, delay: null
})
this.newConnectionTip = addConnTooltip
this.settings.on('settingChanged', (key, orig, val) => {
// TODO(evanlucas) if key is user.color, maybe go through
// and call the messageFormatter on all messages?
// Not sure if it is worth it to keep the color in sync for previous
// messages though
this.db.settings.put(key, val, (err) => {
if (err) {
console.error('Unable to persist setting changed', key, val, err)
} else {
debug('persisted %s from %j => %j', key, orig, val)
}
})
})
}
App.prototype.load = function load() {
this._loadSettings((err) => {
if (err) {
console.error('settings load error', err)
return
}
const active = this.settings.get('theme.active')
this.themes.load(active, () => {
this.emit('loaded')
this._checkAuth()
})
})
}
App.prototype.persistConn = function persistConn(conn, cb) {
this.checkConnLogging(conn)
this.db.persistConnection(conn.toJSON(), cb)
}
App.prototype.login = function login(opts) {
const conn = new Connection({
name: opts.name
, server: {
host: opts.host
, port: opts.port
}
, user: {
username: opts.username
, nickname: opts.nickname
, realname: opts.realname
, altNick: opts.altnick
, password: opts.password
}
, settings: {
'connect.auto': true
, 'persist.password': true
}
}, this)
this._addConnection(conn)
this.persistConn(conn, (err) => {
if (err) {
console.error('persist error', err)
}
this.router.goto(conn.url)
})
}
App.prototype._loadSettings = function _loadSettings(cb) {
const data = {}
var called = false
const done = (err) => {
if (called) return
called = true
this.settings.load(data)
cb(err)
}
this.db.settings.createReadStream()
.on('data', (item) => {
const o = {}
o[item.key] = item.value
Object.assign(data, o)
})
.on('error', done)
.on('close', done)
}
App.prototype._checkAuth = function _checkAuth() {
this.db.getConnections((err, connections) => {
if (err) {
if (err.notFound) {
debug('cannot find any connections...show login')
this.showLogin()
return
}
console.error(err.stack)
return
}
const len = connections.length
if (!len) {
debug('no saved connections...show login')
this.router.goto('/login')
return
}
// we have saved connections
var active
const settings = this.settings
for (var i = 0; i < len; i++) {
const opts = connections[i]
const user = opts.user
if (user.username && !user.password) {
debug('PASS', auth.getCreds(opts.name, user.username))
opts.user.password = auth.getCreds(opts.name, user.username)
}
opts.messageFormatter = function messageFormatter(msg) {
if (msg.type === 'join' || msg.type === 'part') {
return utils.encode(msg.message)
}
const chan = msg.channel || {}
const m = msg.message
return utils.processMessage(m, chan.colorMap, chan.conn, settings)
}
const conn = new Connection(opts, this)
if (!active) {
active = conn
}
this._addConnection(conn)
this.checkConnLogging(conn)
}
this._isLoading = false
if (active)
this.router.goto(active.url)
})
}
App.prototype._addConnection = function _addConnection(conn) {
debug('add connection %s', conn.name.toLowerCase())
const key = conn.name.toLowerCase()
this.connections.set(key, conn)
const addConnTooltip = new Tooltip(this.el, {
selector: `irc-serverbar a[navtype=connection][navname="${conn.name}"]`
, placement: 'right'
, container: 'body'
, viewportPadding: 2
, title: conn.name
, delay: null
})
this.tooltips.set(key, addConnTooltip)
const evs = [ 'channelUpdated'
, 'whois'
]
evs.forEach((ev) => {
conn.on(ev, () => {
this.needsLayout()
})
})
conn.on('log', (msg) => {
this.needsLayout()
const logger = this.loggers.get(conn)
if (logger) {
const d = new Date(msg.ts)
if (msg.from) {
logger.write(`[${d.toISOString()}] ${msg.from}: ${msg.message}`)
} else {
logger.write(`[${d.toISOString()}] ${msg.message}`)
}
}
})
conn.on('channelLog', (chan, msg) => {
// all mentions should get a sound, a bubble, and a dock icon
// if it is a query and it is to me, get sound, a bubble, and dock icon
// if the window is focused, don't add dock badge
if (msg.mention || msg.to === chan.nick) {
if (this.settings.get('sounds.enabled')) {
this.notifications.playSound('message')
}
if (!this.window.isFocused()) {
this.setBadge()
}
}
const logger = this.loggers.get(chan)
if (logger) {
const d = new Date(msg.ts)
if (msg.from) {
logger.write(`[${d.toISOString()}] ${msg.from}: ${msg.message}`)
} else {
logger.write(`[${d.toISOString()}] ${msg.message}`)
}
}
})
const persistEvents = [
'channelAdded'
, 'channelRemoved'
, 'queryAdded'
, 'queryRemoved'
]
persistEvents.forEach((ev) => {
conn.on(ev, (chan) => {
this.needsLayout()
this.persistConn(conn, (err) => {
if (err) {
debug('failed to persist connection %s', conn.name, err)
} else {
debug('connection persisted %s', conn.name)
}
})
})
})
this.router.goto(conn.url)
conn.settings.on('settingChanged', (key, orig, val) => {
this.persistConn(conn, (err) => {
if (err) {
console.error('cannot persist conn setting', key, orig, val)
} else {
debug('persisted connection setting %s %j => %j', key, orig, val)
}
})
})
}
App.prototype.checkConnLogging = function checkConnLogging(conn) {
debug('checkConnLogging %s', conn.name)
const enabled = conn.settings.get('transcripts.enabled')
const logger = this.loggers.get(conn)
if (enabled && !logger) {
debug('logging enabled, no logger...creating')
const fp = utils.connectionLogLocation(conn)
if (fp) {
debug('%s logging to %s', conn.name, fp)
const l = new Logger({
path: fp
})
l.open()
this.loggers.set(conn, l)
}
} else if (!enabled && logger) {
debug('logger not enabled, but exists...closing')
logger.close()
this.loggers.delete(conn)
} else if (enabled && logger) {
const fp = utils.connectionLogLocation(conn)
if (path.dirname(logger.fp) !== fp) {
debug('logger enabled and exists, diff path %s %s', fp)
logger.close()
}
if (fp) {
const l = new Logger({
path: fp
})
l.open()
this.loggers.set(conn, l)
}
}
// Now, make sure all of the children are correct
for (const chan of conn._panels.values()) {
this.checkChannelLogging(chan)
}
}
App.prototype.checkChannelLogging = function checkChannelLogging(chan) {
debug('check channel logging %s', chan.name)
const enabled = chan.getConnection().settings.get('transcripts.enabled')
const logger = this.loggers.get(chan)
if (enabled && !logger) {
debug('enabled, no logger')
const fp = utils.channelLogLocation(chan)
debug('create logger %s %s', chan.name, fp)
if (fp) {
const l = new Logger({
path: fp
})
l.open()
this.loggers.set(chan, l)
}
} else if (!enabled && logger) {
logger.close()
this.loggers.delete(chan)
} else if (enabled && logger) {
// check that the path is the same, otherwise, close and reopen
const fp = utils.channelLogLocation(chan)
if (path.dirname(logger.fp) !== fp) {
logger.close()
}
if (fp) {
const l = new Logger({
path: fp
})
l.open()
this.loggers.set(chan, l)
}
}
}
App.prototype.removeConnection = function removeConnection(conn) {
const key = conn.name.toLowerCase()
debug('removeConnection %s', key, this.connections.keys())
this.connections.delete(key)
this.tooltips.get(key).destroy()
this.tooltips.delete(key)
this.checkConnLogging(conn)
if (this.connections.size) {
this.router.goto('/connection')
} else {
this.router.goto('/login')
}
}
App.prototype.renameConnection = function renameConnection(conn, prev) {
this.removeConnection(prev)
this._addConnection(conn)
}
App.prototype.needsLayout = function needsLayout() {
this.router.goto(this.url)
}
App.prototype.getActiveConnection = function getActiveConnection() {
if (this.activeModel) {
if (this.activeModel.getConnection) {
return this.activeModel.getConnection()
}
}
return null
}