git ssb

10+

Matt McKegg / patchwork



Tree: 33a48d4a8febf6f3e24bfec414ddd8c70db320e0

Files: 33a48d4a8febf6f3e24bfec414ddd8c70db320e0 / api / index.js

3631 bytesRaw
1var pull = require('pull-stream')
2var ssbKeys = require('ssb-keys')
3var ref = require('ssb-ref')
4var LikeCache = require('./like-cache')
5
6function 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
24var createFeed = require('ssb-feed')
25var cache = CACHE = {}
26
27module.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 update_likes: function (msg) {
56 likeCache.updateFrom(msg)
57 },
58 sbot_blobs_add: function (cb) {
59 return pull(
60 Hash(function (err, id) {
61 if(err) return cb(err)
62 //completely UGLY hack to tell when the blob has been sucessfully written...
63 var start = Date.now(), n = 5
64 ;(function next () {
65 setTimeout(function () {
66 sbot.blobs.has(id, function (err, has) {
67 if(has) return cb(null, id)
68 if(n--) next()
69 else cb(new Error('write failed'))
70 })
71 }, Date.now() - start)
72 })()
73 }),
74 sbot.blobs.add()
75 )
76 },
77 sbot_links: function (query) {
78 return sbot.links(query)
79 },
80 sbot_links2: function (query) {
81 return sbot.links2.read(query)
82 },
83 sbot_query: function (query) {
84 return sbot.query.read(query)
85 },
86 sbot_log: function (opts) {
87 return pull(
88 sbot.createLogStream(opts),
89 pull.through(function (e) {
90 CACHE[e.key] = CACHE[e.key] || e.value
91 likeCache.updateFrom(e)
92 })
93 )
94 },
95 sbot_user_feed: function (opts) {
96 return sbot.createUserStream(opts)
97 },
98 sbot_get: function (key, cb) {
99 if(CACHE[key]) cb(null, CACHE[key])
100 else sbot.get(key, function (err, value) {
101 if(err) return cb(err)
102 cb(null, CACHE[key] = value)
103 })
104 },
105 sbot_gossip_peers: function (cb) {
106 sbot.gossip.peers(cb)
107 },
108 //liteclient won't have permissions for this
109 sbot_gossip_connect: function (opts, cb) {
110 sbot.gossip.connect(opts, cb)
111 },
112 sbot_publish: function (content, cb) {
113 if(content.recps)
114 content = ssbKeys.box(content, content.recps.map(function (e) {
115 return ref.isFeed(e) ? e : e.link
116 }))
117 else if(content.mentions)
118 content.mentions.forEach(function (mention) {
119 if(ref.isBlob(mention.link)) {
120 sbot.blobs.push(mention.link, function (err) {
121 if(err) console.error(err)
122 })
123 }
124 })
125
126 feed.add(content, function (err, msg) {
127 if(err) console.error(err)
128 else if(!cb) console.log(msg)
129 cb && cb(err, msg)
130 })
131 },
132 sbot_whoami: function (cb) {
133 sbot.whoami(cb)
134 }
135 }
136}
137

Built with git-ssb-web