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