Files: 9e984ba9dfe16b18ff2b448acf164d7795d28ee1 / extras.js
1750 bytesRaw
1 | var pull = require('pull-stream') |
2 | var ViewLevel = require('flumeview-level') |
3 | var u = require('./util') |
4 | var stdopts = u.options |
5 | var Format = u.Format |
6 | |
7 | module.exports = function (db, config, keys) { |
8 | db |
9 | .use('time', ViewLevel(2, function (data) { |
10 | return [data.timestamp] |
11 | })) |
12 | .use('feed', require('./indexes/feed')()) |
13 | .use('links', require('./indexes/links')()) |
14 | |
15 | db.createLogStream = function (opts) { |
16 | opts = stdopts(opts) |
17 | if (opts.raw) { return db.stream(opts) } |
18 | |
19 | var keys = opts.keys; delete opts.keys |
20 | var values = opts.values; delete opts.values |
21 | if (opts.gt == null) { opts.gt = 0 } |
22 | |
23 | return pull(db.time.read(opts), Format(keys, values, opts.private)) |
24 | } |
25 | |
26 | // TODO: eventually, this should filter out authors you do not follow. |
27 | db.createFeedStream = db.feed.createFeedStream |
28 | |
29 | db.createUserStream = db.clock.createUserStream |
30 | |
31 | db.latest = db.last.latest |
32 | |
33 | // used by sbot replication plugin |
34 | db.latestSequence = function (id, cb) { |
35 | db.last.get(function (err, val) { |
36 | if (err) cb(err) |
37 | else if (!val || !val[id]) cb(new Error('not found:' + id)) |
38 | else cb(null, val[id].sequence) |
39 | }) |
40 | } |
41 | |
42 | db.getLatest = function (key, cb) { |
43 | db.last.get(function (err, value) { |
44 | if (err || !value || !value[key]) cb() |
45 | // Currently, this retrives the previous message. |
46 | // but, we could rewrite validation to only use |
47 | // data the reduce view, so that no disk read is necessary. |
48 | else { |
49 | db.get(value[key].id, function (err, msg) { |
50 | // will NOT expose private plaintext |
51 | cb(err, { key: value[key].id, value: msg }) |
52 | }) |
53 | } |
54 | }) |
55 | } |
56 | |
57 | db.messagesByType = db.links.messagesByType |
58 | |
59 | db.links = db.links.links |
60 | |
61 | return db |
62 | } |
63 |
Built with git-ssb-web