Files: 5c65269d9872f15ab192ba3b916511db8505843b / sbot-api.js
4285 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 | function Hash (onHash) { |
7 | var buffers = [] |
8 | return pull.through(function (data) { |
9 | buffers.push('string' === typeof data |
10 | ? new Buffer(data, 'utf8') |
11 | : data |
12 | ) |
13 | }, function (err) { |
14 | if(err && !onHash) throw err |
15 | var b = buffers.length > 1 ? Buffer.concat(buffers) : buffers[0] |
16 | var h = '&'+ssbKeys.hash(b) |
17 | onHash && onHash(err, h) |
18 | }) |
19 | } |
20 | //uncomment this to use from browser... |
21 | //also depends on having ssb-ws installed. |
22 | //var createClient = require('ssb-lite') |
23 | var createClient = require('ssb-client') |
24 | |
25 | var createConfig = require('ssb-config/inject') |
26 | |
27 | var createFeed = require('ssb-feed') |
28 | var keys = require('./keys') |
29 | var ssbKeys = require('ssb-keys') |
30 | |
31 | |
32 | var cache = CACHE = {} |
33 | |
34 | module.exports = function () { |
35 | var opts = createConfig() |
36 | var sbot = null |
37 | var connection_status = [] |
38 | |
39 | |
40 | var rec = Reconnect(function (isConn) { |
41 | // var remote |
42 | // if('undefined' !== typeof localStorage) |
43 | // remote = localStorage.remote |
44 | |
45 | function notify (value) { |
46 | console.log('connection_status', value, connection_status) |
47 | isConn(value); connection_status.forEach(function (fn) { fn(value) }) |
48 | } |
49 | |
50 | createClient(keys, { |
51 | manifest: require('./manifest.json'), |
52 | remote: require('./config')().remote |
53 | }, function (err, _sbot) { |
54 | if(err) |
55 | return notify(err) |
56 | |
57 | sbot = _sbot |
58 | sbot.on('closed', function () { |
59 | sbot = null |
60 | notify(new Error('closed')) |
61 | }) |
62 | |
63 | notify() |
64 | }) |
65 | }) |
66 | |
67 | var internal = { |
68 | getLatest: rec.async(function (id, cb) { |
69 | sbot.getLatest(id, cb) |
70 | }), |
71 | add: rec.async(function (msg, cb) { |
72 | sbot.add(msg, cb) |
73 | }) |
74 | } |
75 | |
76 | var feed = createFeed(internal, keys, {remote: true}) |
77 | |
78 | return { |
79 | connection_status: connection_status, |
80 | sbot_blobs_add: rec.sink(function (cb) { |
81 | return pull( |
82 | Hash(function (err, id) { |
83 | if(err) return cb(err) |
84 | //completely UGLY hack to tell when the blob has been sucessfully written... |
85 | var start = Date.now(), n = 5 |
86 | ;(function next () { |
87 | setTimeout(function () { |
88 | sbot.blobs.has(id, function (err, has) { |
89 | if(has) return cb(null, id) |
90 | if(n--) next() |
91 | else cb(new Error('write failed')) |
92 | }) |
93 | }, Date.now() - start) |
94 | })() |
95 | }), |
96 | sbot.blobs.add() |
97 | ) |
98 | }), |
99 | sbot_links: rec.source(function (query) { |
100 | return sbot.links(query) |
101 | }), |
102 | sbot_links2: rec.source(function (query) { |
103 | return sbot.links2.read(query) |
104 | }), |
105 | sbot_query: rec.source(function (query) { |
106 | return sbot.query.read(query) |
107 | }), |
108 | sbot_log: rec.source(function (opts) { |
109 | return pull( |
110 | sbot.createLogStream(opts), |
111 | pull.through(function (e) { |
112 | CACHE[e.key] = CACHE[e.key] || e.value |
113 | }) |
114 | ) |
115 | }), |
116 | sbot_user_feed: rec.source(function (opts) { |
117 | return sbot.createUserStream(opts) |
118 | }), |
119 | sbot_get: rec.async(function (key, cb) { |
120 | if(CACHE[key]) cb(null, CACHE[key]) |
121 | else sbot.get(key, function (err, value) { |
122 | if(err) return cb(err) |
123 | cb(null, CACHE[key] = value) |
124 | }) |
125 | }), |
126 | sbot_gossip_peers: rec.async(function (cb) { |
127 | sbot.gossip.peers(cb) |
128 | }), |
129 | //liteclient won't have permissions for this |
130 | sbot_gossip_connect: rec.async(function (opts, cb) { |
131 | sbot.gossip.connect(opts, cb) |
132 | }), |
133 | sbot_publish: rec.async(function (content, cb) { |
134 | if(content.recps) |
135 | content = ssbKeys.box(content, content.recps.map(function (e) { |
136 | return ref.isFeed(e) ? e : e.link |
137 | })) |
138 | else if(content.mentions) |
139 | content.mentions.forEach(function (mention) { |
140 | if(ref.isBlob(mention.link)) { |
141 | sbot.blobs.push(mention.link, function (err) { |
142 | if(err) console.error(err) |
143 | }) |
144 | } |
145 | }) |
146 | |
147 | feed.add(content, function (err, msg) { |
148 | if(err) console.error(err) |
149 | else if(!cb) console.log(msg) |
150 | cb && cb(err, msg) |
151 | }) |
152 | }), |
153 | sbot_whoami: rec.async(function (cb) { |
154 | sbot.whoami(cb) |
155 | }) |
156 | } |
157 | } |
158 | |
159 | |
160 | |
161 | |
162 | |
163 | |
164 | |
165 | |
166 | |
167 | |
168 |
Built with git-ssb-web