Files: ef33ab337c848fb05b567cdab92bd9a21bedfab5 / scuttlebot.js
3402 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 | acceptInvite: rec.async(function (invite, cb) { |
50 | sbot.invite.accept(invite, cb) |
51 | }), |
52 | createLogStream: rec.source(function (opts) { |
53 | return pull( |
54 | sbot.createLogStream(opts), |
55 | pull.through(function (e) { |
56 | CACHE[e.key] = CACHE[e.key] || e.value |
57 | }) |
58 | ) |
59 | }), |
60 | userStream: rec.source(function (config) { |
61 | return pull( |
62 | sbot.createUserStream(config), |
63 | pull.through(function (e) { |
64 | CACHE[e.key] = CACHE[e.key] || e.value |
65 | }) |
66 | ) |
67 | }), |
68 | backlinks: rec.source(function (query) { |
69 | return sbot.backlinks.read(query) |
70 | }), |
71 | names: { |
72 | get: rec.async(function (opts, cb) { |
73 | sbot.names.get(opts, cb) |
74 | }), |
75 | getImages: rec.async(function (opts, cb) { |
76 | sbot.names.getImages(opts, cb) |
77 | }), |
78 | getImageFor: rec.async(function (opts, cb) { |
79 | return sbot.names.getImageFor(opts, cb) |
80 | if(images[opts]) cb(null, images[opts]) |
81 | else |
82 | sbot.names.getImageFor(opts, function (err, v) { |
83 | if(err) cb(err) |
84 | else cb(null, images[opts]= v) |
85 | }) |
86 | }), |
87 | getSignifier: rec.async(function (opts, cb) { |
88 | sbot.names.getSignifier(opts, cb) |
89 | }), |
90 | getSignifies: rec.async(function (opts, cb) { |
91 | sbot.names.getSignifies(opts, cb) |
92 | }) |
93 | }, |
94 | friends: { |
95 | get: rec.async(function (opts, cb) { |
96 | sbot.friends.get(opts, cb) |
97 | }) |
98 | }, |
99 | query: rec.source(function (query) { |
100 | return sbot.query.read(query) |
101 | }), |
102 | get: rec.async(function (key, cb) { |
103 | if('function' !== typeof cb) |
104 | throw new Error('cb must be function') |
105 | if(CACHE[key]) cb(null, CACHE[key]) |
106 | else sbot.get(key, function (err, value) { |
107 | if(err) return cb(err) |
108 | cb(null, CACHE[key] = value) |
109 | }) |
110 | }), |
111 | links: rec.source(function (query) { |
112 | return sbot.links(query) |
113 | }), |
114 | addblob: rec.sink(function (cb) { |
115 | return sbot.blobs.add(cb) |
116 | }), |
117 | publish: rec.async(function (content, cb) { |
118 | if(content.recps) |
119 | content = ssbKeys.box(content, content.recps.map(function (e) { |
120 | return ref.isFeed(e) ? e : e.link |
121 | })) |
122 | else if(content.mentions) |
123 | content.mentions.forEach(function (mention) { |
124 | if(ref.isBlob(mention.link)) { |
125 | sbot.blobs.push(mention.link, function (err) { |
126 | if(err) console.error(err) |
127 | }) |
128 | } |
129 | }) |
130 | feed.add(content, function (err, msg) { |
131 | if(err) console.error(err) |
132 | else if(!cb) console.log(msg) |
133 | cb && cb(err, msg) |
134 | }) |
135 | }) |
136 | |
137 | } |
138 | |
139 |
Built with git-ssb-web