Files: e4ce6bb40173e08028c870d5b637e13128c98c83 / scuttlebot.js
2049 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 config = require('./config')() |
6 | var createClient = require('ssb-client') |
7 | var createFeed = require('ssb-feed') |
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 | var internal = { |
36 | getLatest: rec.async(function (id, cb) { |
37 | sbot.getLatest(id, cb) |
38 | }), |
39 | add: rec.async(function (msg, cb) { |
40 | sbot.add(msg, cb) |
41 | }) |
42 | } |
43 | |
44 | var feed = createFeed(internal, keys, {remote: true}) |
45 | |
46 | module.exports = { |
47 | backlinks: rec.source(function (query) { |
48 | return sbot.backlinks.read(query) |
49 | }), |
50 | links: rec.source(function (query) { |
51 | return sbot.links(query) |
52 | }), |
53 | query: rec.source(function (query) { |
54 | return sbot.query.read(query) |
55 | }), |
56 | get: rec.async(function (key, cb) { |
57 | if(CACHE[key]) cb(null, CACHE[key]) |
58 | else sbot.get(key, function (err, value) { |
59 | if(err) return cb(err) |
60 | cb(null, CACHE[key] = value) |
61 | }) |
62 | }), |
63 | addblob: rec.sink(function (cb) { |
64 | return sbot.blobs.add(cb) |
65 | }), |
66 | publish: rec.async(function (content, cb) { |
67 | if(content.recps) |
68 | content = ssbKeys.box(content, content.recps.map(function (e) { |
69 | return ref.isFeed(e) ? e : e.link |
70 | })) |
71 | else if(content.mentions) |
72 | content.mentions.forEach(function (mention) { |
73 | if(ref.isBlob(mention.link)) { |
74 | sbot.blobs.push(mention.link, function (err) { |
75 | if(err) console.error(err) |
76 | }) |
77 | } |
78 | }) |
79 | feed.add(content, function (err, msg) { |
80 | if(err) console.error(err) |
81 | else if(!cb) console.log(msg) |
82 | cb && cb(err, msg) |
83 | }) |
84 | }) |
85 | } |
86 | |
87 |
Built with git-ssb-web