This repository has been archived by the owner on May 31, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
cabal-web.js
192 lines (165 loc) · 5.17 KB
/
cabal-web.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
const wss = require('websocket-stream')
const Cabal = require('cabal-core')
const pump = require('pump')
const rai = require('random-access-idb')
const crypto = require('hypercore-crypto')
const collect = require('collect-stream')
module.exports = connectCabal
function connectCabal (state, emitter) {
emitter.once(state.events.DOMCONTENTLOADED, function () {
if (state.params && state.params.key) {
// if (state.params.channel) state.channel = state.params.channel
emitter.emit('cabal:load', state.params.key)
}
})
emitter.on('cabal:load', function (key) {
if (key.indexOf('cabal:') > -1) key = key.split('//')[1] // todo: be less lazy =)
emitter.emit('log:info', `loading new cabal ${key}`)
const host = document.location.host
const proto = document.location.protocol === 'https:' ? 'wss' : 'ws'
const wsUrl = `${proto}://${host}/cabal/${key}`
state = Object.assign(state, {
channel: state.channel || 'default',
key: key,
cabal: null,
messages: [],
connected: false,
wsUrl: wsUrl,
forceScroll: true
})
if (!state.cabal) createCabal()
emitter.emit('render')
})
emitter.on('cabal:create', function () {
state.newCabal = true
emitter.emit('cabal:load', crypto.keyPair().publicKey.toString('hex'))
})
emitter.on('cabal:publishMsg', function (msg) {
emitter.emit('log:info', `publishing new message to ${state.channel}`)
state.cabal.publish({
type: 'chat/text',
content: {
channel: state.channel,
text: msg
}
})
})
emitter.on('cabal:publishNick', function (name) {
emitter.emit('log:info', `publishing new name: ${name}`)
state.nickname = name
state.cabal.publishNick(name)
emitter.emit('render')
})
emitter.once('cabal:backlog', function () {
// TODO make this work?
// const opts = {limit: 3, lt: state.messages[0].value.timestamp }
// var rs = state.cabal.messages.read('default', opts)
// collect(rs, function (err, msgs) {
// if (err) return
// // msgs.reverse()
// msgs.forEach(function (msg) {
// state.messages.unshift(msg)
// console.log(msg.value.timestamp)
// })
// emitter.emit('message')
// })
})
function createCabal () {
const storage = rai(`doc-${state.key}`)
const cabal = Cabal(storage, state.key)
let cabalStream
let retries = 0
cabal.db.ready(function () {
state.cabal = cabal
emitter.emit('log:info', 'cabal ready')
emitter.emit('log:info', cabal.key.toString('hex'))
if (state.newCabal) {
cabal.publish({
type: 'chat/text',
content: {
channel: 'default',
text: 'Welcome to your new Cabal on message.land! Share the url or cabal:// link on the bottom to start chatting.'
}
}, replicate)
} else {
replicate()
}
if (!state.params.key) emitter.emit(state.events.PUSHSTATE, `cabal/${state.key}`) /// ${state.channel}`)
emitter.emit('render')
})
function replicate () {
retries++
emitter.emit('log:info', 'starting websocket - ' + state.wsUrl)
const stream = wss(state.wsUrl)
cabalStream = cabal.replicate({ live: true, encrypt: false })
stream.once('connect', function () {
emitter.emit('connected')
emitter.emit('render')
emitter.emit('log:info', 'websockets connected')
getMessages()
})
pump(
stream,
cabalStream,
stream,
err => {
state.connected = false
if (err) {
console.log('Pipe finished', err.message)
if (err.stack) {
console.log(err.stack)
}
} else {
console.log('Pipe finished, no errors')
}
if (retries > 1) {
// first connection isn't getting messages sometimes/?!?!
emitter.emit('log:warn', 'Waiting 3 seconds to reconnect')
setTimeout(replicate, 3000)
} else {
replicate()
}
}
)
}
}
function getMessages () {
console.log('start getting cabal msgs yay')
if (!state.messages) state.messages = []
let pending = 0
function onMessage () {
if (pending > 0) {
pending++
return
}
pending = 1
// From cabal CLI =)
// TODO: wrap this up in a nice interface and expose it via cabal-client
const rs = state.cabal.messages.read(state.channel, { limit: 25, lt: '~' })
collect(rs, function (err, msgs) {
if (err) return
state.messages = []
msgs.reverse()
msgs.forEach(function (msg) {
state.messages.push(msg)
})
emitter.emit('message')
state.cabal.topics.get(state.channel, (err, topic) => {
if (err) return
if (topic) {
state.topic = topic
emitter.emit('render')
}
})
if (pending > 1) {
pending = 0
onMessage()
} else {
pending = 0
}
})
}
state.cabal.messages.events.on(state.channel, onMessage)
onMessage()
}
}