git ssb

0+

dangerousbeans / patchbay-bootstrap



Tree: 55bcc3082d358b89d7c236bd1899e86e19464fbb

Files: 55bcc3082d358b89d7c236bd1899e86e19464fbb / sbot-api.js

4133 bytesRaw
1var pull = require('pull-stream')
2var ssbKeys = require('ssb-keys')
3var ref = require('ssb-ref')
4var Reconnect = require('pull-reconnect')
5
6function Hash (onHash) {
7 var buffers = []
8 return pull.through(function (data) {
9 buffers.push('string' === typeof data
10 ? new Buffer(data, 'utf8')
11 : data
12 )
13 }, function (err) {
14 if(err && !onHash) throw err
15 var b = buffers.length > 1 ? Buffer.concat(buffers) : buffers[0]
16 var h = '&'+ssbKeys.hash(b)
17 onHash && onHash(err, h)
18 })
19}
20//uncomment this to use from browser...
21//also depends on having ssb-ws installed.
22//var createClient = require('ssb-lite')
23var createClient = require('ssb-client')
24
25var createConfig = require('ssb-config/inject')
26
27var createFeed = require('ssb-feed')
28var keys = require('./keys')
29var ssbKeys = require('ssb-keys')
30
31
32var cache = CACHE = {}
33
34module.exports = function () {
35 var opts = createConfig()
36 var sbot = null
37 var connection_status = []
38
39
40 var rec = Reconnect(function (isConn) {
41// var remote
42// if('undefined' !== typeof localStorage)
43// remote = localStorage.remote
44
45 function notify (value) {
46 console.log('connection_status', value, connection_status)
47 isConn(value); connection_status.forEach(function (fn) { fn(value) })
48 }
49
50 createClient(keys, {
51 manifest: require('./manifest.json'),
52 remote: require('./config')().remote
53 }, function (err, _sbot) {
54 if(err)
55 return notify(err)
56
57 sbot = _sbot
58 sbot.on('closed', function () {
59 sbot = null
60 notify(new Error('closed'))
61 })
62
63 notify()
64 })
65 })
66
67 var internal = {
68 getLatest: rec.async(function (id, cb) {
69 sbot.getLatest(id, cb)
70 }),
71 add: rec.async(function (msg, cb) {
72 sbot.add(msg, cb)
73 })
74 }
75
76 var feed = createFeed(internal, keys, {remote: true})
77
78 return {
79 connection_status: connection_status,
80 sbot_blobs_add: rec.sink(function (cb) {
81 return pull(
82 Hash(function (err, id) {
83 if(err) return cb(err)
84 //completely UGLY hack to tell when the blob has been sucessfully written...
85 var start = Date.now(), n = 5
86 ;(function next () {
87 setTimeout(function () {
88 sbot.blobs.has(id, function (err, has) {
89 if(has) return cb(null, id)
90 if(n--) next()
91 else cb(new Error('write failed'))
92 })
93 }, Date.now() - start)
94 })()
95 }),
96 sbot.blobs.add()
97 )
98 }),
99 sbot_links: rec.source(function (query) {
100 return sbot.links(query)
101 }),
102 sbot_links2: rec.source(function (query) {
103 return sbot.links2.read(query)
104 }),
105 sbot_query: rec.source(function (query) {
106 return sbot.query.read(query)
107 }),
108 sbot_log: rec.source(function (opts) {
109 return pull(
110 sbot.createLogStream(opts),
111 pull.through(function (e) {
112 CACHE[e.key] = CACHE[e.key] || e.value
113 })
114 )
115 }),
116 sbot_user_feed: rec.source(function (opts) {
117 return sbot.createUserStream(opts)
118 }),
119 sbot_get: rec.async(function (key, cb) {
120 if(CACHE[key]) cb(null, CACHE[key])
121 else sbot.get(key, function (err, value) {
122 if(err) return cb(err)
123 cb(null, CACHE[key] = value)
124 })
125 }),
126 sbot_gossip_peers: rec.async(function (cb) {
127 sbot.gossip.peers(cb)
128 }),
129 sbot_publish: rec.async(function (content, cb) {
130 if(content.recps)
131 content = ssbKeys.box(content, content.recps.map(function (e) {
132 return ref.isFeed(e) ? e : e.link
133 }))
134 else if(content.mentions)
135 content.mentions.forEach(function (mention) {
136 if(ref.isBlob(mention.link)) {
137 sbot.blobs.push(mention.link, function (err) {
138 if(err) console.error(err)
139 })
140 }
141 })
142
143 feed.add(content, function (err, msg) {
144 if(err) console.error(err)
145 else if(!cb) console.log(msg)
146 cb && cb(err, msg)
147 })
148 }),
149 sbot_whoami: rec.async(function (cb) {
150 sbot.whoami(cb)
151 })
152 }
153}
154
155
156
157
158
159
160
161
162

Built with git-ssb-web