git ssb

0+

dangerousbeans / patchbay-bootstrap



Tree: a9b3a6e76ee1656d725b4555cb0327ffd2202f70

Files: a9b3a6e76ee1656d725b4555cb0327ffd2202f70 / sbot-api.js

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

Built with git-ssb-web