Files: a5417350593eb41e7c3b56c8edf96aa895411fb2 / api / index.js
3557 bytesRaw
1 | var pull = require('pull-stream') |
2 | var ssbKeys = require('ssb-keys') |
3 | var ref = require('ssb-ref') |
4 | var LikeCache = require('./like-cache') |
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 | |
24 | var createFeed = require('ssb-feed') |
25 | var cache = CACHE = {} |
26 | |
27 | module.exports = function (sbot, opts) { |
28 | var connection_status = [] |
29 | var keys = opts.keys |
30 | var likeCache = LikeCache() |
31 | |
32 | var internal = { |
33 | getLatest: function (id, cb) { |
34 | sbot.getLatest(id, cb) |
35 | }, |
36 | add: function (msg, cb) { |
37 | sbot.add(msg, cb) |
38 | } |
39 | } |
40 | |
41 | var feed = createFeed(internal, keys, {remote: true}) |
42 | |
43 | setImmediate((x) => { |
44 | connection_status.forEach(fn => fn()) |
45 | }) |
46 | |
47 | return { |
48 | connection_status: connection_status, |
49 | get_id: function () { |
50 | return sbot.id |
51 | }, |
52 | get_likes: function (id) { |
53 | return likeCache.get(id) |
54 | }, |
55 | sbot_blobs_add: function (cb) { |
56 | return pull( |
57 | Hash(function (err, id) { |
58 | if(err) return cb(err) |
59 | //completely UGLY hack to tell when the blob has been sucessfully written... |
60 | var start = Date.now(), n = 5 |
61 | ;(function next () { |
62 | setTimeout(function () { |
63 | sbot.blobs.has(id, function (err, has) { |
64 | if(has) return cb(null, id) |
65 | if(n--) next() |
66 | else cb(new Error('write failed')) |
67 | }) |
68 | }, Date.now() - start) |
69 | })() |
70 | }), |
71 | sbot.blobs.add() |
72 | ) |
73 | }, |
74 | sbot_links: function (query) { |
75 | return sbot.links(query) |
76 | }, |
77 | sbot_links2: function (query) { |
78 | return sbot.links2.read(query) |
79 | }, |
80 | sbot_query: function (query) { |
81 | return sbot.query.read(query) |
82 | }, |
83 | sbot_log: function (opts) { |
84 | return pull( |
85 | sbot.createLogStream(opts), |
86 | pull.through(function (e) { |
87 | CACHE[e.key] = CACHE[e.key] || e.value |
88 | likeCache.updateFrom(e) |
89 | }) |
90 | ) |
91 | }, |
92 | sbot_user_feed: function (opts) { |
93 | return sbot.createUserStream(opts) |
94 | }, |
95 | sbot_get: function (key, cb) { |
96 | if(CACHE[key]) cb(null, CACHE[key]) |
97 | else sbot.get(key, function (err, value) { |
98 | if(err) return cb(err) |
99 | cb(null, CACHE[key] = value) |
100 | }) |
101 | }, |
102 | sbot_gossip_peers: function (cb) { |
103 | sbot.gossip.peers(cb) |
104 | }, |
105 | //liteclient won't have permissions for this |
106 | sbot_gossip_connect: function (opts, cb) { |
107 | sbot.gossip.connect(opts, cb) |
108 | }, |
109 | sbot_publish: function (content, cb) { |
110 | if(content.recps) |
111 | content = ssbKeys.box(content, content.recps.map(function (e) { |
112 | return ref.isFeed(e) ? e : e.link |
113 | })) |
114 | else if(content.mentions) |
115 | content.mentions.forEach(function (mention) { |
116 | if(ref.isBlob(mention.link)) { |
117 | sbot.blobs.push(mention.link, function (err) { |
118 | if(err) console.error(err) |
119 | }) |
120 | } |
121 | }) |
122 | |
123 | feed.add(content, function (err, msg) { |
124 | if(err) console.error(err) |
125 | else if(!cb) console.log(msg) |
126 | cb && cb(err, msg) |
127 | }) |
128 | }, |
129 | sbot_whoami: function (cb) { |
130 | sbot.whoami(cb) |
131 | } |
132 | } |
133 | } |
134 |
Built with git-ssb-web