Files: 612b9afbdd5bbeaf56306ee43dd7e0d3d6599a16 / scuttlebot.js
2189 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 | |
6 | var config = require('./config')() |
7 | var createClient = require('ssb-client') |
8 | var keys = require('./keys') |
9 | |
10 | var CACHE = {} |
11 | |
12 | var rec = reconnect(function (isConn) { |
13 | function notify (value) { |
14 | isConn(value) |
15 | } |
16 | |
17 | createClient(keys, { |
18 | manifest: require('./manifest.json'), |
19 | remote: config.remote, |
20 | caps: config.caps |
21 | }, function (err, _sbot) { |
22 | if(err) |
23 | return notify(err) |
24 | |
25 | sbot = _sbot |
26 | sbot.on('closed', function () { |
27 | sbot = null |
28 | notify(new Error('closed')) |
29 | }) |
30 | |
31 | notify() |
32 | }) |
33 | }) |
34 | |
35 | module.exports = { |
36 | createLogStream: rec.source(function (opts) { |
37 | return pull( |
38 | sbot.createLogStream(opts), |
39 | pull.through(function (e) { |
40 | CACHE[e.key] = CACHE[e.key] || e.value |
41 | }) |
42 | ) |
43 | }), |
44 | userStream: rec.source(function (config) { |
45 | return pull( |
46 | sbot.createUserStream(config), |
47 | pull.through(function (e) { |
48 | CACHE[e.key] = CACHE[e.key] || e.value |
49 | }) |
50 | ) |
51 | }), |
52 | query: rec.source(function (query) { |
53 | return sbot.query.read(query) |
54 | }), |
55 | get: rec.async(function (key, cb) { |
56 | if('function' !== typeof cb) |
57 | throw new Error('cb must be function') |
58 | if(CACHE[key]) cb(null, CACHE[key]) |
59 | else sbot.get(key, function (err, value) { |
60 | if(err) return cb(err) |
61 | cb(null, CACHE[key] = value) |
62 | }) |
63 | }), |
64 | links: rec.source(function (query) { |
65 | return sbot.links(query) |
66 | }), |
67 | addblob: rec.sink(function (cb) { |
68 | return sbot.blobs.add(cb) |
69 | }), |
70 | publish: rec.async(function (content, cb) { |
71 | if(content.recps) |
72 | content = ssbKeys.box(content, content.recps.map(function (e) { |
73 | return ref.isFeed(e) ? e : e.link |
74 | })) |
75 | else if(content.mentions) |
76 | content.mentions.forEach(function (mention) { |
77 | if(ref.isBlob(mention.link)) { |
78 | sbot.blobs.push(mention.link, function (err) { |
79 | if(err) console.error(err) |
80 | }) |
81 | } |
82 | }) |
83 | feed.add(content, function (err, msg) { |
84 | if(err) console.error(err) |
85 | else if(!cb) console.log(msg) |
86 | cb && cb(err, msg) |
87 | }) |
88 | }) |
89 | |
90 | } |
91 | |
92 |
Built with git-ssb-web