index.jsView |
---|
1 | 1 … | var FlumeReduce = require('flumeview-reduce') |
|
2 | 2 … | var ref = require('ssb-ref') |
| 3 … | +var _ = require('lodash') |
3 | 4 … | |
4 | 5 … | exports.name = 'tags' |
5 | 6 … | exports.version = require('./package.json').version |
6 | 7 … | exports.manifest = { |
7 | 8 … | stream: 'source', |
8 | 9 … | get: 'async' |
9 | 10 … | } |
10 | 11 … | |
| 12 … | +var initialState = {} |
| 13 … | + |
11 | 14 … | exports.init = function (ssb, config) { |
12 | | - return ssb._flumeUse('tags', FlumeReduce('test', reduce, map)) |
| 15 … | + return ssb._flumeUse('tags', FlumeReduce('test', reduce, map, null, initialState)) |
13 | 16 … | |
14 | | - function reduce (result, item) { |
15 | | - if (!result) result = {} |
16 | | - if (!result.tags) result.tags = {} |
17 | | - if (!result.messages) result.messages = {} |
18 | | - var tags = result.tags |
19 | | - var messages = result.messages |
20 | | - |
21 | | - if (item) { |
22 | | - var { tag, author, message, tagged, timestamp } = item |
| 17 … | + function reduce(result, item) { |
| 18 … | + if (!item) return result |
23 | 19 … | |
24 | | - if (!tags[author]) tags[author] = {} |
25 | | - if (!tags[author][tag]) tags[author][tag] = {} |
26 | | - if ( |
27 | | - tagged && |
28 | | - (!tags[author][tag][message] || |
29 | | - timestamp > tags[author][tag][message].timestamp) |
30 | | - ) { |
31 | | - tags[author][tag][message] = { timestamp, tagged } |
32 | | - } else if (!tagged && tags[author][tag][message]) { |
33 | | - delete tags[author][tag][message] |
34 | | - } |
| 20 … | + var { tag, author, message, tagged, timestamp } = item |
| 21 … | + var current = _.at(result, `${author}.${tag}.${message}`)[0] |
35 | 22 … | |
36 | | - if (!messages[message]) messages[message] = {} |
37 | | - if (!messages[message][tag]) messages[message][tag] = {} |
38 | | - if ( |
39 | | - tagged && |
40 | | - (!messages[message][tag][author] || |
41 | | - timestamp > messages[message][tag][author].timestamp) |
42 | | - ) { |
43 | | - messages[message][tag][author] = { timestamp, tagged } |
44 | | - } else if (!tagged && messages[message][tag][author]) { |
45 | | - delete messages[message][tag][authpr] |
| 23 … | + if (tagged && (!current || timestamp > current)) { |
| 24 … | + var newTag = { |
| 25 … | + [author]: { |
| 26 … | + [tag]: { |
| 27 … | + [message]: timestamp |
| 28 … | + } |
| 29 … | + } |
46 | 30 … | } |
| 31 … | + result = _.merge(result, newTag) |
| 32 … | + } else if (!tagged && current) { |
| 33 … | + delete result[author][tag][message] |
47 | 34 … | } |
48 | 35 … | |
49 | 36 … | return result |
50 | 37 … | } |
51 | 38 … | |
52 | | - function map (msg) { |
| 39 … | + function map(msg) { |
53 | 40 … | |
54 | 41 … | if (msg.value.author !== ssb.id) return |
55 | 42 … | |
56 | 43 … | |