Files: adf8a7771064a6d9a18b6005e625fadbd6fb9846 / sbot.js
2741 bytesRaw
1 | var pull = require('pull-stream') |
2 | var ref = require('ssb-ref') |
3 | var Reconnect = require('pull-reconnect') |
4 | var createClient = require('ssb-client') |
5 | var createFeed = require('ssb-feed') |
6 | |
7 | var cache = CACHE = {} |
8 | |
9 | exports.needs = { |
10 | config: 'first', |
11 | keys: 'first', |
12 | connection_status: 'map' |
13 | } |
14 | |
15 | exports.gives = { |
16 | sbot_log: true, |
17 | sbot_get: true, |
18 | sbot_user_feed: true, |
19 | sbot_query: true, |
20 | sbot_publish: true, |
21 | connection_status: true |
22 | } |
23 | |
24 | exports.create = function (api) { |
25 | const config = api.config() |
26 | const keys = api.keys() |
27 | |
28 | var sbot = null |
29 | var connection_status = [] |
30 | |
31 | var rec = { |
32 | sync: () => {}, |
33 | async: () => {}, |
34 | source: () => {} |
35 | } |
36 | |
37 | var rec = Reconnect(function (isConn) { |
38 | function notify (value) { |
39 | isConn(value); api.connection_status(value) //.forEach(function (fn) { fn(value) }) |
40 | } |
41 | |
42 | createClient(keys, { |
43 | manifest: require('./manifest.json'), |
44 | remote: config.remote, |
45 | caps: config.caps |
46 | }, function (err, _sbot) { |
47 | if(err) |
48 | return notify(err) |
49 | |
50 | sbot = _sbot |
51 | sbot.on('closed', function () { |
52 | sbot = null |
53 | notify(new Error('closed')) |
54 | }) |
55 | |
56 | notify() |
57 | }) |
58 | }) |
59 | |
60 | var internal = { |
61 | getLatest: rec.async(function (id, cb) { |
62 | sbot.getLatest(id, cb) |
63 | }), |
64 | add: rec.async(function (msg, cb) { |
65 | sbot.add(msg, cb) |
66 | }) |
67 | } |
68 | |
69 | var feed = createFeed(internal, keys, {remote: true}) |
70 | |
71 | return { |
72 | connection_status: () => connection_status, |
73 | sbot_query: rec.source(query => { |
74 | return sbot.query.read(query) |
75 | }), |
76 | sbot_user_feed: rec.source(opts => { |
77 | return sbot.createUserStream(opts) |
78 | }), |
79 | sbot_get: rec.async(function (key, cb) { |
80 | if('function' !== typeof cb) |
81 | throw new Error('cb must be function') |
82 | if(CACHE[key]) cb(null, CACHE[key]) |
83 | else sbot.get(key, function (err, value) { |
84 | if(err) return cb(err) |
85 | cb(null, CACHE[key] = value) |
86 | }) |
87 | }), |
88 | sbot_publish: rec.async((content, cb) => { |
89 | if(content.recps) |
90 | content = ssbKeys.box(content, content.recps.map(e => { |
91 | return ref.isFeed(e) ? e : e.link |
92 | })) |
93 | else if(content.mentions) |
94 | content.mentions.forEach(mention => { |
95 | if(ref.isBlob(mention.link)) { |
96 | sbot.blobs.push(mention.link, err => { |
97 | if(err) console.error(err) |
98 | }) |
99 | } |
100 | }) |
101 | |
102 | feed.add(content, (err, msg) => { |
103 | if(err) console.error(err) |
104 | else if(!cb) console.log(msg) |
105 | cb && cb(err, msg) |
106 | }) |
107 | }), |
108 | sbot_log: rec.source(opts => { |
109 | return pull( |
110 | sbot.createLogStream(opts), |
111 | pull.through(e => { |
112 | CACHE[e.key] = CACHE[e.key] || e.value |
113 | }) |
114 | ) |
115 | }) |
116 | } |
117 | } |
118 |
Built with git-ssb-web