-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
75 lines (62 loc) · 2.08 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var pull = require('pull-stream')
var path = require('path')
var FlumeQuery = require('flumeview-query')
var explain = require('explain-error')
function isString(s) {
return 'string' === typeof s
}
exports.name = 'query'
exports.version = require('./package.json').version
exports.manifest = {
read: 'source',
explain: 'sync',
help: 'sync'
}
//what are links used for?
//query follows
//query replies
//query mentions (backlinks & mentions)
//query votes
var INDEX_VERSION = 10
var indexes = [
{key: 'log', value: ['timestamp']},
{key: 'clk', value: [['value', 'author'], ['value', 'sequence']] },
{key: 'typ', value: [['value', 'content', 'type'], ['timestamp']] },
{key: 'tya', value: [['value', 'content', 'type'], ['value', 'timestamp']] },
{key: 'cha', value: [['value', 'content', 'channel'], ['timestamp']] },
{key: 'aty', value: [['value', 'author'], ['value', 'content', 'type'], ['timestamp']]},
{key: 'ata', value: [['value', 'author'], ['value', 'content', 'type'], ['value', 'timestamp']]},
{key: 'art', value: [['value', 'content', 'root'], ['value', 'timestamp']]},
{key: 'lor', value: [['rts']]}
]
function mapRts (msg) {
msg.rts = Math.min(msg.timestamp, msg.value.timestamp) || msg.timestamp
return msg
}
//createHistoryStream( id, seq )
//[{$filter: {author: <id>, sequence: {$gt: <seq>}}}, {$map: true}]
//messagesByType (type)
//[{$filter: {content: {type: <type>}}}, {$map: true}]
exports.init = function (ssb, config) {
var s = ssb._flumeUse('query', FlumeQuery(INDEX_VERSION, {indexes: indexes, map: mapRts}))
var read = s.read
var explain = s.explain
s.explain = function (opts) {
if(!opts) opts = {}
if(isString(opts))
opts = {query: JSON.parse(opts)}
else if(isString(opts.query))
opts.query = JSON.parse(opts.query)
return explain(opts)
}
s.read = function (opts) {
if(!opts) opts = {}
if(isString(opts))
opts = {query: JSON.parse(opts)}
else if(isString(opts.query))
opts.query = JSON.parse(opts.query)
return read(opts)
}
s.help = function () { return require('./help') }
return s
}