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