git ssb

7+

dinoworm 🐛 / patchcore



Commit 69e982a06262f72e77dcba72ded0074c435d8a2d

add custom about.obs: latestValue, valueFrom, socialValue, groupedValues

Matt McKegg committed on 6/17/2017, 12:59:33 PM
Parent: e7dda9470a027ac6e1e028d4732e10f03aee5d68

Files changed

about/obs.jschanged
about/obs.jsView
@@ -19,10 +19,12 @@
1919 'imageUrl',
2020 'names',
2121 'images',
2222 'color',
23- 'value',
24- 'values'
23 + 'latestValue',
24 + 'valueFrom',
25 + 'socialValue',
26 + 'groupedValues'
2527 ]
2628 })
2729
2830 exports.create = function (api) {
@@ -31,33 +33,46 @@
3133
3234 return nest({
3335 'about.obs': {
3436 // quick helpers, probably should deprecate!
35- name: (id) => value(id, 'name', id.slice(1, 10)),
36- description: (id) => value(id, 'description'),
37- image: (id) => value(id, 'image'),
38- names: (id) => values(id, 'name'),
39- images: (id) => values(id, 'image'),
37 + name: (id) => socialValue(id, 'name', id.slice(1, 10)),
38 + description: (id) => socialValue(id, 'description'),
39 + image: (id) => socialValue(id, 'image'),
40 + names: (id) => groupedValues(id, 'name'),
41 + images: (id) => groupedValues(id, 'image'),
4042 color: (id) => computed(id, (id) => colorHash.hex(id)),
41- imageUrl: (id) => computed(value(id, 'image'), (blobId) => {
43 + imageUrl: (id) => computed(socialValue(id, 'image'), (blobId) => {
4244 return blobId ? api.blob.sync.url(blobId) : fallbackImageUrl
4345 }),
4446
4547 // custom abouts (the future!)
46- value,
47- values
48 + valueFrom,
49 + latestValue,
50 + socialValue,
51 + groupedValues
4852 }
4953 })
5054
51- function value (id, key, defaultValue) {
55 +
56 + function valueFrom (id, key, author) {
5257 if (!ref.isLink(id)) throw new Error('About requires an ssb ref!')
58 + return computed([get(id), key, author], getValueFrom)
59 + }
60 +
61 + function latestValue (id, key) {
62 + if (!ref.isLink(id)) throw new Error('About requires an ssb ref!')
63 + return computed([get(id), key], getLatestValue)
64 + }
65 +
66 + function socialValue (id, key, defaultValue) {
67 + if (!ref.isLink(id)) throw new Error('About requires an ssb ref!')
5368 var yourId = api.keys.sync.id()
54- return computed([get(id), key, id, yourId, defaultValue], socialValue)
69 + return computed([get(id), key, id, yourId, defaultValue], getSocialValue)
5570 }
5671
57- function values (id, key) {
72 + function groupedValues (id, key) {
5873 if (!ref.isLink(id)) throw new Error('About requires an ssb ref!')
59- return computed([get(id), key], allValues)
74 + return computed([get(id), key], getGroupedValues)
6075 }
6176
6277 function get (id) {
6378 if (!ref.isLink(id)) throw new Error('About requires an ssb ref!')
@@ -102,18 +117,18 @@
102117 }
103118 }
104119 }
105120
106-function socialValue (lookup, key, id, yourId, fallback) {
121 +function getSocialValue (lookup, key, id, yourId, fallback) {
107122 var result = lookup[key] ? getValue(lookup[key][yourId]) || getValue(lookup[key][id]) || highestRank(lookup[key]) : null
108123 if (result != null) {
109124 return result
110125 } else {
111126 return fallback || null
112127 }
113128 }
114129
115-function allValues (lookup, key) {
130 +function getGroupedValues (lookup, key) {
116131 var values = {}
117132 for (var author in lookup[key]) {
118133 var value = getValue(lookup[key][author])
119134 if (value != null) {
@@ -144,9 +159,35 @@
144159 function getValue (item) {
145160 if (item && item[0]) {
146161 if (typeof item[0] === 'string') {
147162 return item[0]
148- } else if (item[0] && item[0].link && ref.isLink(item[0].link)) {
163 + } else if (item[0] && item[0].link && ref.isLink(item[0].link) && !item[0].remove) {
149164 return item[0].link
150165 }
151166 }
152167 }
168 +
169 +function getLatestValue (lookup, key) {
170 + var latestTime = 0
171 + var latestValue = null
172 +
173 + if (lookup[key]) {
174 + for (var author in lookup[key]) {
175 + if (Array.isArray(lookup[key][author])) {
176 + var value = lookup[key][author][0]
177 + var timestamp = lookup[key][author][1]
178 + if (timestamp > latestTime) {
179 + latestTime = timestamp
180 + latestValue = value
181 + }
182 + }
183 + }
184 + }
185 +
186 + return latestValue
187 +}
188 +
189 +function getValueFrom (lookup, key, author) {
190 + if (lookup[key] && Array.isArray(lookup[key][author])) {
191 + return lookup[key][author][0]
192 + }
193 +}

Built with git-ssb-web