Files: 0b5a32431a545be14a4887d48c7ce5b1eddca8d7 / sbot-api.js
3738 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(b) : b |
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 | module.exports = function () { |
32 | var opts = createConfig() |
33 | var sbot = null |
34 | var connection_status = [] |
35 | |
36 | |
37 | var rec = Reconnect(function (isConn) { |
38 | // var remote |
39 | // if('undefined' !== typeof localStorage) |
40 | // remote = localStorage.remote |
41 | |
42 | function notify (value) { |
43 | console.log('connection_status', value, connection_status) |
44 | isConn(value); connection_status.forEach(function (fn) { fn(value) }) |
45 | } |
46 | |
47 | createClient(keys, { |
48 | manifest: require('./manifest.json'), |
49 | remote: require('./config')().remote |
50 | }, function (err, _sbot) { |
51 | if(err) |
52 | return notify(err) |
53 | |
54 | sbot = _sbot |
55 | sbot.on('closed', function () { |
56 | sbot = null |
57 | notify(new Error('closed')) |
58 | }) |
59 | |
60 | notify() |
61 | }) |
62 | }) |
63 | |
64 | var internal = { |
65 | getLatest: rec.async(function (id, cb) { |
66 | sbot.getLatest(id, cb) |
67 | }), |
68 | add: rec.async(function (msg, cb) { |
69 | sbot.add(msg, cb) |
70 | }) |
71 | } |
72 | |
73 | var feed = createFeed(internal, keys, {remote: true}) |
74 | |
75 | return { |
76 | connection_status: connection_status, |
77 | sbot_blobs_add: rec.sink(function (cb) { |
78 | return pull( |
79 | Hash(function (err, id) { |
80 | if(err) return cb(err) |
81 | //completely UGLY hack to tell when the blob has been sucessfully written... |
82 | var start = Date.now(), n = 5 |
83 | ;(function next () { |
84 | setTimeout(function () { |
85 | sbot.blobs.has(id, function (err, has) { |
86 | if(has) return cb(null, id) |
87 | if(n--) next() |
88 | else cb(new Error('write failed')) |
89 | }) |
90 | }, Date.now() - start) |
91 | })() |
92 | }), |
93 | sbot.blobs.add() |
94 | ) |
95 | }), |
96 | sbot_links: rec.source(function (query) { |
97 | return sbot.links(query) |
98 | }), |
99 | sbot_links2: rec.source(function (query) { |
100 | return sbot.links2.read(query) |
101 | }), |
102 | sbot_query: rec.source(function (query) { |
103 | return sbot.query.read(query) |
104 | }), |
105 | sbot_log: rec.source(function (opts) { |
106 | return sbot.createLogStream(opts) |
107 | }), |
108 | sbot_user_feed: rec.source(function (opts) { |
109 | return sbot.createUserStream(opts) |
110 | }), |
111 | sbot_get: rec.async(function (key, cb) { |
112 | sbot.get(key, cb) |
113 | }), |
114 | sbot_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 | |
128 | feed.add(content, function (err, msg) { |
129 | if(err) console.error(err) |
130 | else if(!cb) console.log(msg) |
131 | cb && cb(err, msg) |
132 | }) |
133 | }), |
134 | sbot_whoami: rec.async(function (cb) { |
135 | sbot.whoami(cb) |
136 | }) |
137 | } |
138 | } |
139 | |
140 |
Built with git-ssb-web