git ssb

3+

dangerousbeans / scuttle-vue



Tree: 8dc25143a3f37f1618f7a92b3c46dec512b43bfd

Files: 8dc25143a3f37f1618f7a92b3c46dec512b43bfd / modules / sbot.js

5829 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
24var createClient = require('ssb-client')
25
26var createConfig = require('ssb-config/inject')
27
28var createFeed = require('ssb-feed')
29var keys = require('../keys')
30var ssbKeys = require('ssb-keys')
31
32var CACHE = {}
33var cache = CACHE
34
35module.exports = {
36 needs: {
37 connection_status: 'map'
38 },
39 gives: {
40 raw_sbot: true,
41 sbot_blobs_add: true,
42 sbot_links: true,
43 sbot_links2: true,
44 sbot_query: true,
45 sbot_fulltext_search: true,
46 sbot_get: true,
47 sbot_log: true,
48 sbot_user_feed: true,
49 sbot_gossip_peers: true,
50 sbot_gossip_connect: true,
51 sbot_progress: true,
52 sbot_publish: true,
53 sbot_whoami: true,
54 sbot_stream: true,
55 sbot_friends_get: true,
56 sbot_signs_get: true,
57 sbot_relatedMessages_get: true,
58 sbot_createLogStream: true,
59 sbot_createHotStream: true,
60 sbot_getLatest: true
61 },
62
63 create: function (api) {
64
65 var opts = createConfig()
66 var sbot = null
67 var connection_status = []
68
69 var rec = Reconnect(function (isConn) {
70 function notify (value) {
71 isConn(value); api.connection_status(value) //.forEach(function (fn) { fn(value) })
72 }
73
74 createClient(keys, {
75 manifest: require('../manifest.json'),
76 remote: require('../config')().remote,
77 caps: config.caps
78 }, function (err, _sbot) {
79 if(err)
80 return notify(err)
81
82 sbot = _sbot
83 sbot.on('closed', function () {
84 sbot = null
85 notify(new Error('closed'))
86 })
87
88 notify()
89 })
90 })
91
92 var internal = {
93 getLatest: rec.async(function (id, cb) {
94 sbot.getLatest(id, cb)
95 }),
96 add: rec.async(function (msg, cb) {
97 sbot.add(msg, cb)
98 })
99 }
100
101 var feed = createFeed(internal, keys, {remote: true})
102
103 return {
104 raw_sbot: function() { return sbot },
105 connection_status: connection_status,
106 sbot_blobs_add: rec.sink(function (cb) {
107 return sbot.blobs.add(cb)
108 }),
109 sbot_links: rec.source(function (query) {
110 return sbot.links(query)
111 }),
112 sbot_links2: rec.source(function (query) {
113 return sbot.links2.read(query)
114 }),
115 sbot_query: rec.source(function (query) {
116 return sbot.query.read(query)
117 }),
118 sbot_log: rec.source(function (opts) {
119 return pull(
120 sbot.createLogStream(opts),
121 pull.through(function (e) {
122 CACHE[e.key] = CACHE[e.key] || e.value
123 })
124 )
125 }),
126 sbot_createLogStream: rec.source(function (opts) {
127 return sbot.createLogStream(opts)
128 }),
129 sbot_createHotStream: rec.source(function (opts) {
130 return sbot.createLogStream(opts)
131 }),
132 sbot_getLatest: rec.source(function (opts) {
133 return sbot.getLatest(opts)
134 }),
135 sbot_user_feed: rec.source(function (opts) {
136 console.log("sbot_user_feed")
137 console.log(opts)
138 return sbot.createUserStream(opts)
139 }),
140 sbot_fulltext_search: rec.source(function (opts) {
141 return sbot.fulltext.search(opts)
142 }),
143 sbot_get: rec.async(function (key, cb) {
144 if('function' !== typeof cb)
145 throw new Error('cb must be function')
146 if(CACHE[key]) cb(null, CACHE[key])
147 else sbot.get(key, function (err, value) {
148 if(err) return cb(err)
149 cb(null, CACHE[key] = value)
150 })
151 }),
152 sbot_gossip_peers: rec.async(function (cb) {
153 sbot.gossip.peers(cb)
154 }),
155 //liteclient won't have permissions for this
156 sbot_gossip_connect: rec.async(function (opts, cb) {
157 sbot.gossip.connect(opts, cb)
158 }),
159 sbot_progress: rec.source(function () {
160 return sbot.replicate.changes()
161 }),
162 sbot_publish: rec.async(function (content, cb) {
163 if(content.recps)
164 content = ssbKeys.box(content, content.recps.map(function (e) {
165 return ref.isFeed(e) ? e : e.link
166 }))
167 else if(content.mentions)
168 content.mentions.forEach(function (mention) {
169 if(ref.isBlob(mention.link)) {
170 sbot.blobs.push(mention.link, function (err) {
171 if(err) console.error(err)
172 })
173 }
174 })
175 feed.add(content, function (err, msg) {
176 if(err) console.error(err)
177 else if(!cb) console.log(msg)
178 cb && cb(err, msg)
179 })
180 }),
181 sbot_whoami: rec.async(function (cb) {
182 sbot.whoami(cb)
183 }),
184 sbot_stream: rec.source(function (opts) {
185 return sbot.stream(opts)
186 }),
187 sbot_friends_get: rec.async(function (opts, cb) {
188 return sbot.friends.get(opts, cb)
189 }),
190 sbot_relatedMessages_get: rec.async(function (opts, cb) {
191 // debugger
192 return sbot.relatedMessages(opts, cb)
193 }),
194 // relatedMessages({ id: "%A8H47IPGp2sxPYYFunKU+9mU52R7pvs8eKEsuFzaWXI=.sha256", count: true }, function (err, msgs) {
195
196 // console.log(JSON.stringify(msgs, null, 2))
197
198 // })
199 sbot_signs_get: rec.async(function (opts, cb) {
200 return sbot.signs.get(opts, cb)
201 }),
202 }
203 }
204}
205

Built with git-ssb-web