git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: a5da1caabea6be5eed8baecd05ca37431e9ed9eb

Files: a5da1caabea6be5eed8baecd05ca37431e9ed9eb / api / index.js

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

Built with git-ssb-web