git ssb

16+

Dominic / patchbay



Tree: 70bc98e1fcb76213a7b487d8e8b7a8f49cc45943

Files: 70bc98e1fcb76213a7b487d8e8b7a8f49cc45943 / modules_core / sbot.js

5289 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_fulltext_search: true,
47 sbot_get: true,
48 sbot_log: true,
49 sbot_user_feed: true,
50 sbot_gossip_peers: true,
51 sbot_gossip_connect: true,
52 sbot_progress: true,
53 sbot_publish: true,
54 sbot_whoami: true
55 },
56
57//module.exports = {
58 create: function (api) {
59
60 var opts = createConfig()
61 var sbot = null
62 var connection_status = []
63
64 var rec = Reconnect(function (isConn) {
65 function notify (value) {
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 caps: config.caps
73 }, function (err, _sbot) {
74 if(err)
75 return notify(err)
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
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 return pull(
130 sbot.createLogStream(opts),
131 pull.through(function (e) {
132 CACHE[e.key] = CACHE[e.key] || e.value
133 })
134 )
135 }),
136 sbot_user_feed: rec.source(function (opts) {
137 return sbot.createUserStream(opts)
138 }),
139 sbot_fulltext_search: rec.source(function (opts) {
140 return sbot.fulltext.search(opts)
141 }),
142 sbot_get: rec.async(function (key, cb) {
143 if('function' !== typeof cb)
144 throw new Error('cb must be function')
145 if(CACHE[key]) cb(null, CACHE[key])
146 else sbot.get(key, function (err, value) {
147 if(err) return cb(err)
148 cb(null, CACHE[key] = value)
149 })
150 }),
151 sbot_gossip_peers: rec.async(function (cb) {
152 sbot.gossip.peers(cb)
153 }),
154 //liteclient won't have permissions for this
155 sbot_gossip_connect: rec.async(function (opts, cb) {
156 sbot.gossip.connect(opts, cb)
157 }),
158 sbot_progress: rec.source(function () {
159 return sbot.replicate.changes()
160 }),
161 sbot_publish: rec.async(function (content, cb) {
162 if(content.recps)
163 content = ssbKeys.box(content, content.recps.map(function (e) {
164 return ref.isFeed(e) ? e : e.link
165 }))
166 else if(content.mentions)
167 content.mentions.forEach(function (mention) {
168 if(ref.isBlob(mention.link)) {
169 sbot.blobs.push(mention.link, function (err) {
170 if(err) console.error(err)
171 })
172 }
173 })
174
175 feed.add(content, function (err, msg) {
176 if(err) console.error(err)
177 else if(!cb) console.log(msg)
178 cb && cb(err, msg)
179 })
180 }),
181 sbot_whoami: rec.async(function (cb) {
182 sbot.whoami(cb)
183 })
184 }
185 }
186}
187
188
189
190
191
192
193
194
195

Built with git-ssb-web