git ssb

0+

Josiah / ssb-tags



Commit 0e8fddd65bb3d05fbcafb9c66ceea4ea5d9648e5

lodash tweaks

mix irving authored on 1/19/2018, 11:17:17 PM
Josiah Witt committed on 1/25/2018, 3:34:33 AM
Parent: 26cb584b07aa6709f758873f12119382291beab7

Files changed

index.jschanged
index.jsView
@@ -1,7 +1,8 @@
11 var FlumeReduce = require('flumeview-reduce')
22 var ref = require('ssb-ref')
3-var _ = require('lodash')
3 +var get = require('lodash/get')
4 +var merge = require('lodash/merge')
45
56 exports.name = 'tags'
67 exports.version = require('./package.json').version
78 exports.manifest = {
@@ -10,31 +11,40 @@
1011 }
1112
1213 var initialState = {}
1314
14-exports.init = function (ssb, config) {
15 +exports.init = function(ssb, config) {
1516 return ssb._flumeUse('tags', FlumeReduce('test', reduce, map, null, initialState))
1617
1718 function reduce(result, item) {
1819 if (!item) return result
1920
2021 var { tag, author, message, tagged, timestamp } = item
21- var current = _.at(result, `${author}.${tag}.${message}`)[0]
22 + var storedTimestamp = get(result, [author, tag, message])
2223
23- if (tagged && (!current || timestamp > current)) {
24 + if (shouldAddTag()) {
2425 var newTag = {
2526 [author]: {
2627 [tag]: {
2728 [message]: timestamp
2829 }
2930 }
3031 }
31- result = _.merge(result, newTag)
32- } else if (!tagged && current) {
32 + result = merge(result, newTag)
33 + } else if (shouldRemoveTag()) {
3334 delete result[author][tag][message]
3435 }
3536
3637 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 + }
3747 }
3848
3949 function map(msg) {
4050 // only include your own tags (for now)
@@ -45,9 +55,9 @@
4555 // unbox private message (requires ssb-private plugin)
4656 msg = ssb.private.unbox(msg)
4757 }
4858
49- if (msg.value.content && msg.value.content.type === 'tag' && ref.isLink(msg.value.content.message)) {
59 + if (isTag(msg)) {
5060 return {
5161 tag: msg.key,
5262 author: msg.value.author,
5363 message: msg.value.content.message,
@@ -56,4 +66,9 @@
5666 }
5767 }
5868 }
5969 }
70 +
71 +function isTag(msg) {
72 + return get(msg, 'value.content.type') === 'tag'
73 + && ref.isLink(get(msg, 'value.content.message'))
74 +}

Built with git-ssb-web