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