git ssb

16+

Dominic / patchbay



Tree: 4cda013d06b86a002895df3cbed747f6baf1085c

Files: 4cda013d06b86a002895df3cbed747f6baf1085c / modules_core / sbot.js

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

Built with git-ssb-web