git ssb

0+

Josiah / scuttle-tag



Commit 073f7adfddd31252faca6d3c503e202ad1aef088

Handle unused tags and deleting tags better

Josiah Witt committed on 2/22/2018, 3:52:54 AM
Parent: eaa2308c20b5db8566955893f8328f5440f973a9

Files changed

tag/obs/tagged.jschanged
tag/obs/tagged.jsView
@@ -2,9 +2,11 @@
22 var pull = require('pull-stream')
33 var nest = require('depnest')
44 var ref = require('ssb-ref')
55 var set = require('lodash/set')
6 +var unset = require('lodash/unset')
67 var get = require('lodash/get')
8 +var isEmpty = require('lodash/isEmpty')
79
810 exports.needs = nest({
911 'sbot.pull.stream': 'first'
1012 })
@@ -39,9 +41,9 @@
3941 }
4042
4143 function messageTags(msgId) {
4244 if (!ref.isLink(msgId)) throw new Error('Requires an ssb ref!')
43- return withSync(computed(getObs(msgId, messagesCache), Object.keys))
45 + return withSync(computed(getObs(msgId, messagesCache), getMessageTags))
4446 }
4547
4648 function allTagsFrom(author) {
4749 if (!ref.isFeed(author)) throw new Error('Requires an ssb ref!')
@@ -82,13 +84,24 @@
8284 const lastState = state()
8385 var changed = false
8486
8587 for (const tag in values) {
88 + const lastTag = lastState[tag]
89 + const isUnusedTag = isEmpty(values[tag]) && (lastTag === undefined || !isEmpty(lastTag))
90 + if (isUnusedTag) {
91 + set(lastState, [ tag ], {})
92 + changed = true
93 + continue
94 + }
8695 for (const key in values[tag]) {
87- var value = get(values, [ tag, key ])
88- var lastValue = get(lastState, [ tag, key ])
96 + const value = get(values, [ tag, key ])
97 + const lastValue = get(lastState, [ tag, key ])
8998 if (value !== lastValue) {
90- set(lastState, [tag, key], value)
99 + if (value) {
100 + set(lastState, [ tag, key ], value)
101 + } else {
102 + unset(lastState, [ tag, key ])
103 + }
91104 changed = true
92105 }
93106 }
94107 }
@@ -123,13 +136,18 @@
123136
124137 if (!sync()) {
125138 sync.set(true)
126139 }
127- } else if (item && ref.isLink(item.tag) && ref.isFeed(item.author) && ref.isLink(item.message)) {
140 + } else if (item && ref.isLink(item.tagKey) && ref.isFeed(item.author) && ref.isLink(item.message)) {
128141 // handle realtime updates
129- const { tag, author, message, tagged, timestamp } = item
130- update(author, { [tag]: { [message]: timestamp } }, tagsCache)
131- update(message, { [tag]: { [author]: timestamp } }, messagesCache)
142 + const { tagKey, author, message, tagged, timestamp } = item
143 + if (tagged) {
144 + update(author, { [tagKey]: { [message]: timestamp } }, tagsCache)
145 + update(message, { [tagKey]: { [author]: timestamp } }, messagesCache)
146 + } else {
147 + update(author, { [tagKey]: { [message]: false } }, tagsCache)
148 + update(message, { [tagKey]: { [author]: false } }, messagesCache)
149 + }
132150 }
133151 })
134152 )
135153 }
@@ -144,14 +162,13 @@
144162 }
145163 return messages
146164 }
147165
148-function getMessageTags(lookup, tagId) {
149- const tags = {}
150- for (const author in lookup[tagId]) {
151- if (lookup[tagId][author]) {
152- if (!tags[tagId]) tags[tagId] = {}
153- tags[tagId][author] = lookup[tagId][author]
166 +function getMessageTags(lookup) {
167 + const tags = []
168 + for (const tag in lookup) {
169 + if (!isEmpty(lookup[tag])) {
170 + tags.push(tag)
154171 }
155172 }
156173 return tags
157174 }

Built with git-ssb-web