git ssb

1+

punkmonk.termux / mvd



forked from ev / mvd

Tree: 90dfad001a91b412a745a9cad0dccf4a3ef49dc2

Files: 90dfad001a91b412a745a9cad0dccf4a3ef49dc2 / mvd / scuttlebot.js

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

Built with git-ssb-web