-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
52 lines (46 loc) · 1.32 KB
/
index.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
45
46
47
48
49
50
51
52
var FlumeLinks = require('flumeview-links')
var toUrlFriendly = require('base64-url').escape
var emitLinks = require('./emit-links')
var indexes = [
{ key: 'DTS', value: [['dest'], ['timestamp']] },
{ key: 'DTA', value: [['dest'], ['rts']] }, // asserted timestamp
{ key: 'TDT', value: [['value', 'content', 'type'], ['dest'], ['rts']] }
]
var indexVersion = 11
exports.name = 'backlinks'
exports.version = require('./package.json').version
exports.manifest = {
read: 'source'
}
exports.init = function (ssb, config) {
var index = ssb._flumeUse(
`backlinks-${toUrlFriendly(ssb.id.slice(1, 10))}`,
FlumeLinks(indexes, emitLinks, indexVersion)
)
return {
read: function (opts) {
opts.unlinkedValues = true
if (opts.index) {
// backwards compatibility for opts.index sorting
var sort = selectValueByKey(indexes, opts.index)
if (sort) {
opts.query = opts.query ? [].concat(opts.query) : []
opts.query.push({
$sort: sort
})
} else {
throw new Error('Invalid index: ' + opts.index)
}
}
return index.read(opts)
},
close: index.close
}
}
function selectValueByKey (indexes, key) {
for (var i = 0; i < indexes.length; i++) {
if (indexes[i].key === key) {
return indexes[i].value
}
}
}