git ssb

7+

dinoworm ๐Ÿ› / patchcore



Tree: 095cf7418aa45150c4a2caa05dadf8e2233aa889

Files: 095cf7418aa45150c4a2caa05dadf8e2233aa889 / sbot.js

6437 bytesRaw
1var pull = require('pull-stream')
2var defer = require('pull-defer')
3var { onceTrue } = require('mutant')
4var ref = require('ssb-ref')
5var Reconnect = require('pull-reconnect')
6var createClient = require('ssb-client')
7var createFeed = require('ssb-feed')
8var nest = require('depnest')
9var Value = require('mutant/value')
10var ssbKeys = require('ssb-keys')
11
12exports.needs = nest({
13 'config.sync.load': 'first',
14 'keys.sync.load': 'first',
15 'sbot.obs.connectionStatus': 'first',
16 'sbot.hook.publish': 'map'
17})
18
19exports.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
48exports.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
239function 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