git ssb

0+

Josiah / ssb-tags



Commit 61c923460be3b91b9c4cf9d9b52ae59befefd4a9

Simplify code based on Mix's feedback

- utilize initialState for flume view
- utilize lodash helper functions to clean up code
- remove secondary index in favour of calculating it at runtime
Josiah Witt committed on 1/19/2018, 2:54:02 AM
Parent: 9fc97c76b6c3a194ee7d1ccab6a3fe398866e4b4

Files changed

index.jschanged
package-lock.jsonchanged
package.jsonchanged
index.jsView
@@ -1,56 +1,43 @@
11 var FlumeReduce = require('flumeview-reduce')
22 var ref = require('ssb-ref')
3 +var _ = require('lodash')
34
45 exports.name = 'tags'
56 exports.version = require('./package.json').version
67 exports.manifest = {
78 stream: 'source',
89 get: 'async'
910 }
1011
12 +var initialState = {}
13 +
1114 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))
1316
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
2319
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]
3522
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 + }
4630 }
31 + result = _.merge(result, newTag)
32 + } else if (!tagged && current) {
33 + delete result[author][tag][message]
4734 }
4835
4936 return result
5037 }
5138
52- function map (msg) {
39 + function map(msg) {
5340 // only include your own tags (for now)
5441 if (msg.value.author !== ssb.id) return
5542
5643 // unbox private message
package-lock.jsonView
@@ -1,6 +1,6 @@
11 {
2- "name": "sbot-tags",
2 + "name": "ssb-tags",
33 "version": "0.1.0",
44 "lockfileVersion": 1,
55 "requires": true,
66 "dependencies": {
@@ -55,8 +55,13 @@
5555 "version": "6.2.0",
5656 "resolved": "https://registry.npmjs.org/level-codec/-/level-codec-6.2.0.tgz",
5757 "integrity": "sha1-pLUkS7akwvcj1oodZOmAxTYn2dQ="
5858 },
59 + "lodash": {
60 + "version": "4.17.4",
61 + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz",
62 + "integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4="
63 + },
5964 "obv": {
6065 "version": "0.0.0",
6166 "resolved": "https://registry.npmjs.org/obv/-/obv-0.0.0.tgz",
6267 "integrity": "sha1-7eq4Ro+R1BkzYu1/kdC5bdOaecE="
package.jsonView
@@ -8,8 +8,9 @@
88 "url": "git://github.com/wittjosiah/ssb-tags.git"
99 },
1010 "dependencies": {
1111 "flumeview-reduce": "^1.3.9",
12 + "lodash": "^4.17.4",
1213 "ssb-ref": "^2.7.1"
1314 },
1415 "devDependencies": {},
1516 "scripts": {},

Built with git-ssb-web