Files: 10e29232507d7fe07c572a0052f8c19772fbec30 / sbot.js
6437 bytesRaw
1 | var pull = require('pull-stream') |
2 | var defer = require('pull-defer') |
3 | var { onceTrue } = require('mutant') |
4 | var ref = require('ssb-ref') |
5 | var Reconnect = require('pull-reconnect') |
6 | var createClient = require('ssb-client') |
7 | var createFeed = require('ssb-feed') |
8 | var nest = require('depnest') |
9 | var Value = require('mutant/value') |
10 | var ssbKeys = require('ssb-keys') |
11 | |
12 | exports.needs = nest({ |
13 | 'config.sync.load': 'first', |
14 | 'keys.sync.load': 'first', |
15 | 'sbot.obs.connectionStatus': 'first', |
16 | 'sbot.hook.publish': 'map' |
17 | }) |
18 | |
19 | exports.gives = { |
20 | sbot: { |
21 | sync: { |
22 | cache: true |
23 | }, |
24 | async: { |
25 | get: true, |
26 | publish: true, |
27 | addBlob: true, |
28 | gossipConnect: true |
29 | }, |
30 | pull: { |
31 | log: true, |
32 | userFeed: true, |
33 | messagesByType: true, |
34 | feed: true, |
35 | links: true, |
36 | backlinks: true, |
37 | stream: true |
38 | }, |
39 | obs: { |
40 | connectionStatus: true, |
41 | connection: true, |
42 | connectedPeers: true, |
43 | localPeers: true |
44 | } |
45 | } |
46 | } |
47 | |
48 | exports.create = function (api) { |
49 | const config = api.config.sync.load() |
50 | const keys = api.keys.sync.load() |
51 | var cache = {} |
52 | |
53 | var sbot = null |
54 | var connection = Value() |
55 | var connectionStatus = Value() |
56 | var connectedPeers = Value([]) |
57 | var localPeers = Value([]) |
58 | |
59 | setInterval(refreshPeers, 1e3) |
60 | |
61 | var rec = Reconnect(function (isConn) { |
62 | function notify (value) { |
63 | isConn(value); connectionStatus.set(value) |
64 | } |
65 | |
66 | createClient(keys, config, function (err, _sbot) { |
67 | if (err) { |
68 | return notify(err) |
69 | } |
70 | |
71 | sbot = _sbot |
72 | sbot.on('closed', function () { |
73 | sbot = null |
74 | connection.set(null) |
75 | notify(new Error('closed')) |
76 | }) |
77 | |
78 | connection.set(sbot) |
79 | notify() |
80 | refreshPeers() |
81 | }) |
82 | }) |
83 | |
84 | var internal = { |
85 | getLatest: rec.async(function (id, cb) { |
86 | sbot.getLatest(id, cb) |
87 | }), |
88 | add: rec.async(function (msg, cb) { |
89 | sbot.add(msg, cb) |
90 | }) |
91 | } |
92 | |
93 | var feed = createFeed(internal, keys, {remote: true}) |
94 | |
95 | return { |
96 | sbot: { |
97 | sync: { |
98 | cache: () => cache |
99 | }, |
100 | async: { |
101 | get: rec.async(function (key, cb) { |
102 | if (typeof cb !== 'function') { |
103 | throw new Error('cb must be function') |
104 | } |
105 | if (cache[key]) cb(null, cache[key]) |
106 | else { |
107 | sbot.get(key, function (err, value) { |
108 | if (err) return cb(err) |
109 | runHooks({key, value}) |
110 | cb(null, value) |
111 | }) |
112 | } |
113 | }), |
114 | publish: rec.async((content, cb) => { |
115 | if (content.recps) { |
116 | content = ssbKeys.box(content, content.recps.map(e => { |
117 | return ref.isFeed(e) ? e : e.link |
118 | })) |
119 | } else if (content.mentions) { |
120 | content.mentions.forEach(mention => { |
121 | if (ref.isBlob(mention.link)) { |
122 | sbot.blobs.push(mention.link, err => { |
123 | if (err) console.error(err) |
124 | }) |
125 | } |
126 | }) |
127 | } |
128 | |
129 | if (sbot) { |
130 | // instant updating of interface (just incase sbot is busy) |
131 | runHooks({ |
132 | publishing: true, |
133 | timestamp: Date.now(), |
134 | value: { |
135 | timestamp: Date.now(), |
136 | author: keys.id, |
137 | content |
138 | } |
139 | }) |
140 | } |
141 | |
142 | feed.add(content, (err, msg) => { |
143 | if (err) console.error(err) |
144 | else if (!cb) console.log(msg) |
145 | cb && cb(err, msg) |
146 | }) |
147 | }), |
148 | addBlob: rec.async((stream, cb) => { |
149 | return pull( |
150 | stream, |
151 | Hash(function (err, id) { |
152 | if (err) return cb(err) |
153 | // completely UGLY hack to tell when the blob has been sucessfully written... |
154 | var start = Date.now() |
155 | var n = 5 |
156 | next() |
157 | |
158 | function next () { |
159 | setTimeout(function () { |
160 | sbot.blobs.has(id, function (_, has) { |
161 | if (has) return cb(null, id) |
162 | if (n--) next() |
163 | else cb(new Error('write failed')) |
164 | }) |
165 | }, Date.now() - start) |
166 | } |
167 | }), |
168 | sbot.blobs.add() |
169 | ) |
170 | }), |
171 | gossipConnect: rec.async(function (opts, cb) { |
172 | sbot.gossip.connect(opts, cb) |
173 | }) |
174 | }, |
175 | pull: { |
176 | backlinks: rec.source(query => { |
177 | return sbot.backlinks.read(query) |
178 | }), |
179 | userFeed: rec.source(opts => { |
180 | return sbot.createUserStream(opts) |
181 | }), |
182 | messagesByType: rec.source(opts => { |
183 | return sbot.messagesByType(opts) |
184 | }), |
185 | feed: rec.source(function (opts) { |
186 | return pull( |
187 | sbot.createFeedStream(opts), |
188 | pull.through(runHooks) |
189 | ) |
190 | }), |
191 | log: rec.source(opts => { |
192 | return pull( |
193 | sbot.createLogStream(opts), |
194 | pull.through(runHooks) |
195 | ) |
196 | }), |
197 | links: rec.source(function (query) { |
198 | return sbot.links(query) |
199 | }), |
200 | stream: function (fn) { |
201 | var stream = defer.source() |
202 | onceTrue(connection, function (connection) { |
203 | stream.resolve(fn(connection)) |
204 | }) |
205 | return stream |
206 | } |
207 | }, |
208 | obs: { |
209 | connectionStatus: (listener) => connectionStatus(listener), |
210 | connection, |
211 | connectedPeers: () => connectedPeers, |
212 | localPeers: () => localPeers |
213 | } |
214 | } |
215 | } |
216 | |
217 | // scoped |
218 | |
219 | function runHooks (msg) { |
220 | if (msg.publishing) { |
221 | api.sbot.hook.publish(msg) |
222 | } else if (!cache[msg.key]) { |
223 | // cache[msg.key] = msg.value |
224 | // api.sbot.hook.feed(msg) |
225 | } |
226 | } |
227 | |
228 | function refreshPeers () { |
229 | if (sbot) { |
230 | sbot.gossip.peers((err, peers) => { |
231 | if (err) return console.error(err) |
232 | connectedPeers.set(peers.filter(x => x.state === 'connected').map(x => x.key)) |
233 | localPeers.set(peers.filter(x => x.source === 'local').map(x => x.key)) |
234 | }) |
235 | } |
236 | } |
237 | } |
238 | |
239 | function Hash (onHash) { |
240 | var buffers = [] |
241 | return pull.through(function (data) { |
242 | buffers.push(typeof data === 'string' |
243 | ? new Buffer(data, 'utf8') |
244 | : data |
245 | ) |
246 | }, function (err) { |
247 | if (err && !onHash) throw err |
248 | var b = buffers.length > 1 ? Buffer.concat(buffers) : buffers[0] |
249 | var h = '&' + ssbKeys.hash(b) |
250 | onHash && onHash(err, h) |
251 | }) |
252 | } |
253 |
Built with git-ssb-web