Files: 542d1509520630ddc5a342baeba6e149d931c5ad / difficult / ex1 / index.js
1720 bytesRaw
1 | const flumeView = require('flumeview-level') |
2 | const pull = require('pull-stream') |
3 | const NAME = 'channels' |
4 | const VERSION = 1 |
5 | |
6 | module.exports = { |
7 | name: NAME, |
8 | version: VERSION, |
9 | manifest: { |
10 | read: 'source', |
11 | list: 'async', |
12 | count: 'async' |
13 | }, |
14 | init: function (server, config) { |
15 | const view = server._flumeUse( |
16 | NAME, |
17 | flumeView(VERSION, map) |
18 | ) |
19 | |
20 | return { |
21 | read: view.read, |
22 | list: list, |
23 | count: count |
24 | } |
25 | |
26 | function map (msg) { |
27 | const { author, content } = msg.value |
28 | if (isPost(content) && hasChannel(content)) return [[ content.channel, msg.value.timestamp ]] |
29 | return [] |
30 | |
31 | function isPost (content) { |
32 | return content.type === 'post' |
33 | } |
34 | |
35 | function hasChannel(content) { |
36 | return content.channel |
37 | } |
38 | } |
39 | |
40 | function list (opts, cb) { |
41 | if (typeof opts === 'function') return list({}, opts) |
42 | |
43 | opts = Object.assign({}, opts, { keys: true, values: false, seqs: false }) |
44 | |
45 | pull( |
46 | view.read(opts), |
47 | pull.map(data => data[0]), |
48 | pull.collect((err, msgs) => { |
49 | if (err) cb(err) |
50 | cb(null, Array.from(new Set(msgs.sort()))) |
51 | }) |
52 | ) |
53 | } |
54 | |
55 | function count (opts, cb) { |
56 | if (typeof opts === 'function') return count({}, opts) |
57 | |
58 | opts = Object.assign({}, opts, { keys: true, values: false, seqs: false }) |
59 | |
60 | pull( |
61 | view.read(opts), |
62 | pull.map(data => data[0]), |
63 | pull.collect((err, msgs) => { |
64 | if (err) cb(err) |
65 | var collection = msgs.sort().reduce((coll, msg) => { |
66 | coll[msg] = (coll[msg] || 0) + 1 |
67 | return coll |
68 | }, {}) |
69 | cb(null, collection) |
70 | }) |
71 | ) |
72 | } |
73 | } |
74 | } |
75 | |
76 |
Built with git-ssb-web