git ssb

16+

Dominic / patchbay



Tree: 615ba51a344df64e9c77f4a1acd2c6bfb85ff581

Files: 615ba51a344df64e9c77f4a1acd2c6bfb85ff581 / modules_core / sbot.js

4187 bytesRaw
1var pull = require('pull-stream')
2var ssbKeys = require('ssb-keys')
3var ref = require('ssb-ref')
4var Reconnect = require('pull-reconnect')
5var path = require('path')
6var config = require('ssb-config/inject')(process.env.ssb_appname)
7config.keys = ssbKeys.loadOrCreateSync(path.join(config.path, 'secret'))
8
9function Hash (onHash) {
10 var buffers = []
11 return pull.through(function (data) {
12 buffers.push('string' === typeof data
13 ? new Buffer(data, 'utf8')
14 : data
15 )
16 }, function (err) {
17 if(err && !onHash) throw err
18 var b = buffers.length > 1 ? Buffer.concat(buffers) : buffers[0]
19 var h = '&'+ssbKeys.hash(b)
20 onHash && onHash(err, h)
21 })
22}
23//uncomment this to use from browser...
24//also depends on having ssb-ws installed.
25//var createClient = require('ssb-lite')
26var createClient = require('ssb-client')
27
28var createConfig = require('ssb-config/inject')
29
30var createFeed = require('ssb-feed')
31var keys = require('../keys')
32var ssbKeys = require('ssb-keys')
33
34
35var cache = CACHE = {}
36
37var opts = createConfig()
38var sbot = null
39var connection_status = []
40
41var rec = Reconnect(function (isConn) {
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
64var 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
73var feed = createFeed(internal, keys, {remote: true})
74
75module.exports = {
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 pull(
107 sbot.createLogStream(opts),
108 pull.through(function (e) {
109 CACHE[e.key] = CACHE[e.key] || e.value
110 })
111 )
112 }),
113 sbot_user_feed: rec.source(function (opts) {
114 return sbot.createUserStream(opts)
115 }),
116 sbot_get: rec.async(function (key, cb) {
117 if(CACHE[key]) cb(null, CACHE[key])
118 else sbot.get(key, function (err, value) {
119 if(err) return cb(err)
120 cb(null, CACHE[key] = value)
121 })
122 }),
123 sbot_gossip_peers: rec.async(function (cb) {
124 sbot.gossip.peers(cb)
125 }),
126 //liteclient won't have permissions for this
127 sbot_gossip_connect: rec.async(function (opts, cb) {
128 sbot.gossip.connect(opts, cb)
129 }),
130 sbot_progress: rec.source(function () {
131 return sbot.replicate.changes()
132 }),
133 sbot_publish: rec.async(function (content, cb) {
134 if(content.recps)
135 content = ssbKeys.box(content, content.recps.map(function (e) {
136 return ref.isFeed(e) ? e : e.link
137 }))
138 else if(content.mentions)
139 content.mentions.forEach(function (mention) {
140 if(ref.isBlob(mention.link)) {
141 sbot.blobs.push(mention.link, function (err) {
142 if(err) console.error(err)
143 })
144 }
145 })
146
147 feed.add(content, function (err, msg) {
148 if(err) console.error(err)
149 else if(!cb) console.log(msg)
150 cb && cb(err, msg)
151 })
152 }),
153 sbot_whoami: rec.async(function (cb) {
154 sbot.whoami(cb)
155 })
156}
157
158

Built with git-ssb-web