git ssb

0+

dangerousbeans / dangerous_patchbay



forked from Dominic / patchbay

Tree: ebf65e8c8947bcad79b243e4d8cd5674ae1b6777

Files: ebf65e8c8947bcad79b243e4d8cd5674ae1b6777 / modules_core / sbot.js

5148 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 = Reconnect(function (isConn) {
64 function notify (value) {
65 isConn(value); api.connection_status(value) //.forEach(function (fn) { fn(value) })
66 }
67
68 createClient(keys, {
69 manifest: require('../manifest.json'),
70 remote: require('../config')().remote,
71 caps: config.caps
72 }, function (err, _sbot) {
73 if(err)
74 return notify(err)
75
76 sbot = _sbot
77 sbot.on('closed', function () {
78 sbot = null
79 notify(new Error('closed'))
80 })
81
82 notify()
83 })
84 })
85
86 var internal = {
87 getLatest: rec.async(function (id, cb) {
88 sbot.getLatest(id, cb)
89 }),
90 add: rec.async(function (msg, cb) {
91 sbot.add(msg, cb)
92 })
93 }
94
95 var feed = createFeed(internal, keys, {remote: true})
96
97 return {
98 connection_status: connection_status,
99 sbot_blobs_add: rec.sink(function (cb) {
100 return pull(
101 Hash(function (err, id) {
102 if(err) return cb(err)
103 //completely UGLY hack to tell when the blob has been sucessfully written...
104 var start = Date.now(), n = 5
105 ;(function next () {
106 setTimeout(function () {
107 sbot.blobs.has(id, function (err, has) {
108 if(has) return cb(null, id)
109 if(n--) next()
110 else cb(new Error('write failed'))
111 })
112 }, Date.now() - start)
113 })()
114 }),
115 sbot.blobs.add()
116 )
117 }),
118 sbot_links: rec.source(function (query) {
119 return sbot.links(query)
120 }),
121 sbot_links2: rec.source(function (query) {
122 return sbot.links2.read(query)
123 }),
124 sbot_query: rec.source(function (query) {
125 return sbot.query.read(query)
126 }),
127 sbot_log: rec.source(function (opts) {
128 return pull(
129 sbot.createLogStream(opts),
130 pull.through(function (e) {
131 CACHE[e.key] = CACHE[e.key] || e.value
132 })
133 )
134 }),
135 sbot_user_feed: rec.source(function (opts) {
136 return sbot.createUserStream(opts)
137 }),
138 sbot_get: rec.async(function (key, cb) {
139 if('function' !== typeof cb)
140 throw new Error('cb must be function')
141 if(CACHE[key]) cb(null, CACHE[key])
142 else sbot.get(key, function (err, value) {
143 if(err) return cb(err)
144 cb(null, CACHE[key] = value)
145 })
146 }),
147 sbot_gossip_peers: rec.async(function (cb) {
148 sbot.gossip.peers(cb)
149 }),
150 //liteclient won't have permissions for this
151 sbot_gossip_connect: rec.async(function (opts, cb) {
152 sbot.gossip.connect(opts, cb)
153 }),
154 sbot_progress: rec.source(function () {
155 return sbot.replicate.changes()
156 }),
157 sbot_publish: rec.async(function (content, cb) {
158 if(content.recps)
159 content = ssbKeys.box(content, content.recps.map(function (e) {
160 return ref.isFeed(e) ? e : e.link
161 }))
162 else if(content.mentions)
163 content.mentions.forEach(function (mention) {
164 if(ref.isBlob(mention.link)) {
165 sbot.blobs.push(mention.link, function (err) {
166 if(err) console.error(err)
167 })
168 }
169 })
170
171 feed.add(content, function (err, msg) {
172 if(err) console.error(err)
173 else if(!cb) console.log(msg)
174 cb && cb(err, msg)
175 })
176 }),
177 sbot_whoami: rec.async(function (cb) {
178 sbot.whoami(cb)
179 })
180 }
181 }
182}
183
184
185
186
187
188
189
190
191

Built with git-ssb-web