git ssb

16+

Dominic / patchbay



Tree: bddd616e485c10d52e676f3f90f53d6a6efea64b

Files: bddd616e485c10d52e676f3f90f53d6a6efea64b / modules_core / sbot.js

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

Built with git-ssb-web