Files: abc981ed003091aaeb68aab6d203e6356ea262a8 / modules_embedded / sbot.js
3821 bytesRaw
1 | var pull = require('pull-stream') |
2 | var ssbKeys = require('ssb-keys') |
3 | var ref = require('ssb-ref') |
4 | var Reconnect = require('pull-reconnect') |
5 | var path = require('path') |
6 | var config = require('ssb-config/inject')(process.env.ssb_appname) |
7 | config.keys = ssbKeys.loadOrCreateSync(path.join(config.path, 'secret')) |
8 | |
9 | function 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') |
26 | var createClient = require('ssb-client') |
27 | |
28 | var createConfig = require('ssb-config/inject') |
29 | |
30 | var createFeed = require('ssb-feed') |
31 | var keys = require('../keys') |
32 | var ssbKeys = require('ssb-keys') |
33 | |
34 | |
35 | var cache = CACHE = {} |
36 | |
37 | var opts = createConfig() |
38 | var sbot = null |
39 | var connection_status = [] |
40 | |
41 | var createSbot = require('scuttlebot') |
42 | .use(require('scuttlebot/plugins/gossip')) |
43 | .use(require('scuttlebot/plugins/friends')) |
44 | .use(require('scuttlebot/plugins/replicate')) |
45 | .use(require('ssb-blobs')) |
46 | .use(require('scuttlebot/plugins/invite')) |
47 | .use(require('scuttlebot/plugins/block')) |
48 | .use(require('scuttlebot/plugins/local')) |
49 | .use(require('ssb-ws')) |
50 | .use(require('ssb-query')) |
51 | .use(require('ssb-links')) |
52 | var sbot = createSbot(config) |
53 | |
54 | |
55 | var rec = Reconnect(function (isConn) { |
56 | |
57 | function notify (value) { |
58 | console.log('connection_status', value, connection_status) |
59 | isConn(value); connection_status.forEach(function (fn) { fn(value) }) |
60 | } |
61 | |
62 | return setTimeout(notify) |
63 | }) |
64 | |
65 | var internal = { |
66 | getLatest: sbot.getLatest, |
67 | add: sbot.add, |
68 | } |
69 | |
70 | var feed = createFeed(internal, keys, {remote: true}) |
71 | |
72 | module.exports = { |
73 | connection_status: connection_status, |
74 | sbot_blobs_add: function (cb) { |
75 | return pull( |
76 | Hash(function (err, id) { |
77 | if(err) return cb(err) |
78 | //completely UGLY hack to tell when the blob has been sucessfully written... |
79 | var start = Date.now(), n = 5 |
80 | ;(function next () { |
81 | setTimeout(function () { |
82 | sbot.blobs.has(id, function (err, has) { |
83 | if(has) return cb(null, id) |
84 | if(n--) next() |
85 | else cb(new Error('write failed')) |
86 | }) |
87 | }, Date.now() - start) |
88 | })() |
89 | }), |
90 | sbot.blobs.add() |
91 | ) |
92 | }, |
93 | sbot_links: sbot.links, |
94 | sbot_links2: sbot.links2.read, |
95 | sbot_query: sbot.query.read, |
96 | sbot_log: function (opts) { |
97 | return pull( |
98 | sbot.createLogStream(opts), |
99 | pull.through(function (e) { |
100 | CACHE[e.key] = CACHE[e.key] || e.value |
101 | }) |
102 | ) |
103 | }, |
104 | sbot_user_feed: sbot.createUserStream, |
105 | sbot_get: function (key, cb) { |
106 | if(CACHE[key]) cb(null, CACHE[key]) |
107 | else sbot.get(key, function (err, value) { |
108 | if(err) return cb(err) |
109 | cb(null, CACHE[key] = value) |
110 | }) |
111 | }, |
112 | sbot_gossip_peers: sbot.gossip.peers, |
113 | sbot_gossip_connect: sbot.gossip.connect, |
114 | sbot_progress: sbot.gossip.change, |
115 | sbot_publish: function (content, cb) { |
116 | if(content.recps) |
117 | content = ssbKeys.box(content, content.recps.map(function (e) { |
118 | return ref.isFeed(e) ? e : e.link |
119 | })) |
120 | else if(content.mentions) |
121 | content.mentions.forEach(function (mention) { |
122 | if(ref.isBlob(mention.link)) { |
123 | sbot.blobs.push(mention.link, function (err) { |
124 | if(err) console.error(err) |
125 | }) |
126 | } |
127 | }) |
128 | |
129 | feed.add(content, function (err, msg) { |
130 | if(err) console.error(err) |
131 | else if(!cb) console.log(msg) |
132 | cb && cb(err, msg) |
133 | }) |
134 | }, |
135 | sbot_whoami: sbot.whoami, |
136 | } |
137 | |
138 | |
139 | |
140 |
Built with git-ssb-web