git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: 3dfc64e44061b79b9d06b8b067d711ed62c28896

Files: 3dfc64e44061b79b9d06b8b067d711ed62c28896 / api / index.js

3330 bytesRaw
1var pull = require('pull-stream')
2var ssbKeys = require('ssb-keys')
3var ref = require('ssb-ref')
4
5function Hash (onHash) {
6 var buffers = []
7 return pull.through(function (data) {
8 buffers.push('string' === typeof data
9 ? new Buffer(data, 'utf8')
10 : data
11 )
12 }, function (err) {
13 if(err && !onHash) throw err
14 var b = buffers.length > 1 ? Buffer.concat(buffers) : buffers[0]
15 var h = '&'+ssbKeys.hash(b)
16 onHash && onHash(err, h)
17 })
18}
19//uncomment this to use from browser...
20//also depends on having ssb-ws installed.
21//var createClient = require('ssb-lite')
22
23var createFeed = require('ssb-feed')
24var cache = CACHE = {}
25
26module.exports = function (sbot, opts) {
27 var connection_status = []
28 var keys = opts.keys
29
30 var internal = {
31 getLatest: function (id, cb) {
32 sbot.getLatest(id, cb)
33 },
34 add: function (msg, cb) {
35 sbot.add(msg, cb)
36 }
37 }
38
39 var feed = createFeed(internal, keys, {remote: true})
40
41 setImmediate((x) => {
42 connection_status.forEach(fn => fn())
43 })
44
45 return {
46 connection_status: connection_status,
47 sbot_blobs_add: function (cb) {
48 return pull(
49 Hash(function (err, id) {
50 if(err) return cb(err)
51 //completely UGLY hack to tell when the blob has been sucessfully written...
52 var start = Date.now(), n = 5
53 ;(function next () {
54 setTimeout(function () {
55 sbot.blobs.has(id, function (err, has) {
56 if(has) return cb(null, id)
57 if(n--) next()
58 else cb(new Error('write failed'))
59 })
60 }, Date.now() - start)
61 })()
62 }),
63 sbot.blobs.add()
64 )
65 },
66 sbot_links: function (query) {
67 return sbot.links(query)
68 },
69 sbot_links2: function (query) {
70 return sbot.links2.read(query)
71 },
72 sbot_query: function (query) {
73 return sbot.query.read(query)
74 },
75 sbot_log: function (opts) {
76 return pull(
77 sbot.createLogStream(opts),
78 pull.through(function (e) {
79 CACHE[e.key] = CACHE[e.key] || e.value
80 })
81 )
82 },
83 sbot_user_feed: function (opts) {
84 return sbot.createUserStream(opts)
85 },
86 sbot_get: function (key, cb) {
87 if(CACHE[key]) cb(null, CACHE[key])
88 else sbot.get(key, function (err, value) {
89 if(err) return cb(err)
90 cb(null, CACHE[key] = value)
91 })
92 },
93 sbot_gossip_peers: function (cb) {
94 sbot.gossip.peers(cb)
95 },
96 //liteclient won't have permissions for this
97 sbot_gossip_connect: function (opts, cb) {
98 sbot.gossip.connect(opts, cb)
99 },
100 sbot_publish: function (content, cb) {
101 if(content.recps)
102 content = ssbKeys.box(content, content.recps.map(function (e) {
103 return ref.isFeed(e) ? e : e.link
104 }))
105 else if(content.mentions)
106 content.mentions.forEach(function (mention) {
107 if(ref.isBlob(mention.link)) {
108 sbot.blobs.push(mention.link, function (err) {
109 if(err) console.error(err)
110 })
111 }
112 })
113
114 feed.add(content, function (err, msg) {
115 if(err) console.error(err)
116 else if(!cb) console.log(msg)
117 cb && cb(err, msg)
118 })
119 },
120 sbot_whoami: function (cb) {
121 sbot.whoami(cb)
122 }
123 }
124}
125

Built with git-ssb-web