git ssb

0+

ev / microbay



forked from Dominic / patchbay

Tree: 84d01396e7c8c55181da6c71061acdb78090dc4c

Files: 84d01396e7c8c55181da6c71061acdb78090dc4c / sbot-api.js

4038 bytesRaw
1var pull = require('pull-stream')
2var ssbKeys = require('ssb-keys')
3var ref = require('ssb-ref')
4var Reconnect = require('pull-reconnect')
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')
23var createClient = require('ssb-client')
24
25var createConfig = require('ssb-config/inject')
26
27var createFeed = require('ssb-feed')
28var keys = require('./keys')
29var ssbKeys = require('ssb-keys')
30
31
32var cache = CACHE = {}
33
34module.exports = function () {
35 var opts = createConfig()
36 var sbot = null
37 var connection_status = []
38
39
40 var rec = Reconnect(function (isConn) {
41// var remote
42// if('undefined' !== typeof localStorage)
43// remote = localStorage.remote
44
45 function notify (value) {
46 console.log('connection_status', value, connection_status)
47 isConn(value); connection_status.forEach(function (fn) { fn(value) })
48 }
49
50 createClient(keys, {
51 manifest: require('./manifest.json'),
52 remote: require('./config')().remote
53 }, function (err, _sbot) {
54 if(err)
55 return notify(err)
56
57 sbot = _sbot
58 sbot.on('closed', function () {
59 sbot = null
60 notify(new Error('closed'))
61 })
62
63 notify()
64 })
65 })
66
67 var internal = {
68 getLatest: rec.async(function (id, cb) {
69 sbot.getLatest(id, cb)
70 }),
71 add: rec.async(function (msg, cb) {
72 sbot.add(msg, cb)
73 })
74 }
75
76 var feed = createFeed(internal, keys, {remote: true})
77
78 return {
79 connection_status: connection_status,
80 sbot_blobs_add: rec.sink(function (cb) {
81 return pull(
82 Hash(function (err, id) {
83 if(err) return cb(err)
84 //completely UGLY hack to tell when the blob has been sucessfully written...
85 var start = Date.now(), n = 5
86 ;(function next () {
87 setTimeout(function () {
88 sbot.blobs.has(id, function (err, has) {
89 if(has) return cb(null, id)
90 if(n--) next()
91 else cb(new Error('write failed'))
92 })
93 }, Date.now() - start)
94 })()
95 }),
96 sbot.blobs.add()
97 )
98 }),
99 sbot_links: rec.source(function (query) {
100 return sbot.links(query)
101 }),
102 sbot_links2: rec.source(function (query) {
103 return sbot.links2.read(query)
104 }),
105 sbot_query: rec.source(function (query) {
106 return sbot.query.read(query)
107 }),
108 sbot_log: rec.source(function (opts) {
109 return pull(
110 sbot.createLogStream(opts),
111 pull.through(function (e) {
112 CACHE[e.key] = CACHE[e.key] || e.value
113 })
114 )
115 }),
116 sbot_user_feed: rec.source(function (opts) {
117 return sbot.createUserStream(opts)
118 }),
119 sbot_get: rec.async(function (key, cb) {
120 if(CACHE[key]) cb(null, CACHE[key])
121 sbot.get(key, function (err, value) {
122 if(err) return cb(err)
123 cb(null, CACHE[key] = value)
124 })
125 }),
126 sbot_publish: rec.async(function (content, cb) {
127 if(content.recps)
128 content = ssbKeys.box(content, content.recps.map(function (e) {
129 return ref.isFeed(e) ? e : e.link
130 }))
131 else if(content.mentions)
132 content.mentions.forEach(function (mention) {
133 if(ref.isBlob(mention.link)) {
134 sbot.blobs.push(mention.link, function (err) {
135 if(err) console.error(err)
136 })
137 }
138 })
139
140 feed.add(content, function (err, msg) {
141 if(err) console.error(err)
142 else if(!cb) console.log(msg)
143 cb && cb(err, msg)
144 })
145 }),
146 sbot_whoami: rec.async(function (cb) {
147 sbot.whoami(cb)
148 })
149 }
150}
151
152
153
154

Built with git-ssb-web