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