git ssb

0+

Josiah / ssb-tags



Tree: 6e5e047648b6604b5135ccf67b925ca282463398

Files: 6e5e047648b6604b5135ccf67b925ca282463398 / index.js

1793 bytesRaw
1var FlumeReduce = require('flumeview-reduce')
2var ref = require('ssb-ref')
3var get = require('lodash/get')
4var merge = require('lodash/merge')
5
6exports.name = 'tags'
7exports.version = require('./package.json').version
8exports.manifest = {
9 stream: 'source',
10 get: 'async'
11}
12
13var initialState = {}
14
15exports.init = function (ssb, config) {
16 return ssb._flumeUse('tags', FlumeReduce('test', reduce, map, null, initialState))
17
18 function reduce(result, item) {
19 if (!item) return result
20
21 var { tag, author, message, tagged, timestamp } = item
22 var storedTimestamp = get(result, [author, tag, message])
23
24 if (shouldAddTag()) {
25 var newTag = {
26 [author]: {
27 [tag]: {
28 [message]: timestamp
29 }
30 }
31 }
32 result = merge(result, newTag)
33 } else if (shouldRemoveTag) {
34 delete result[author][tag][message]
35 }
36
37 return result
38
39 function shouldAddTag () {
40 if (tagged !== true) return false
41 return !storedTimestamp || timestamp > storedTimestamp
42 }
43
44 function shouldRemoveTag () {
45 return storedTimestamp && tagged === false
46 }
47 }
48
49 function map(msg) {
50 // only include your own tags (for now)
51 if (msg.value.author !== ssb.id) return
52
53 // unbox private message
54 if (msg.value.content === 'string') {
55 // unbox private message (requires ssb-private plugin)
56 msg = ssb.private.unbox(msg)
57 }
58
59 if (isTag(msg)) {
60 return {
61 tag: msg.key,
62 author: msg.value.author,
63 message: msg.value.content.message,
64 tagged: msg.value.content.tagged,
65 timestamp: msg.value.timestamp
66 }
67 }
68 }
69}
70
71function isTag (msg) {
72 return get(msg, 'value.content.type') === 'tag'
73 && ref.isLink(get(msg, 'value.content.message'))
74}
75

Built with git-ssb-web