-
Notifications
You must be signed in to change notification settings - Fork 8
/
ssb-partial-replication.js
44 lines (40 loc) · 1.04 KB
/
ssb-partial-replication.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const pull = require('pull-stream')
const pullCont = require('pull-cont')
const sort = require('ssb-sort')
const { reEncrypt } = require('ssb-db2/indexes/private')
const {
where,
and,
toCallback,
hasRoot,
author,
type,
startFrom,
paginate
} = require('ssb-db2/operators')
exports.manifest = {
getTangle: 'async'
}
exports.permissions = {
anonymous: {allow: Object.keys(exports.manifest)}
}
exports.name = 'partial-replication'
exports.init = function (sbot, config) {
return {
getTangle: function(msgId, cb) {
if (!msgId) return cb("msg not found:" + msgId)
SSB.db.get(msgId, (err, msg) => {
if (err) return cb(err)
if (msg.meta && msg.meta.private === true) return cb(null, [])
SSB.db.query(
where(and(hasRoot(msgId))),
toCallback((err, msgs) => {
if (err) return cb(err)
msgs = msgs.filter(x => !x.meta || x.meta.private !== true)
cb(null, [reEncrypt(msg), ...sort(msgs).map(m => reEncrypt(m.value))])
})
)
})
}
}
}