Commit 69e982a06262f72e77dcba72ded0074c435d8a2d
add custom about.obs: latestValue, valueFrom, socialValue, groupedValues
Matt McKegg committed on 6/17/2017, 12:59:33 PMParent: e7dda9470a027ac6e1e028d4732e10f03aee5d68
Files changed
about/obs.js | changed |
about/obs.js | ||
---|---|---|
@@ -19,10 +19,12 @@ | ||
19 | 19 … | 'imageUrl', |
20 | 20 … | 'names', |
21 | 21 … | 'images', |
22 | 22 … | 'color', |
23 | - 'value', | |
24 | - 'values' | |
23 … | + 'latestValue', | |
24 … | + 'valueFrom', | |
25 … | + 'socialValue', | |
26 … | + 'groupedValues' | |
25 | 27 … | ] |
26 | 28 … | }) |
27 | 29 … | |
28 | 30 … | exports.create = function (api) { |
@@ -31,33 +33,46 @@ | ||
31 | 33 … | |
32 | 34 … | return nest({ |
33 | 35 … | 'about.obs': { |
34 | 36 … | // 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'), | |
40 | 42 … | color: (id) => computed(id, (id) => colorHash.hex(id)), |
41 | - imageUrl: (id) => computed(value(id, 'image'), (blobId) => { | |
43 … | + imageUrl: (id) => computed(socialValue(id, 'image'), (blobId) => { | |
42 | 44 … | return blobId ? api.blob.sync.url(blobId) : fallbackImageUrl |
43 | 45 … | }), |
44 | 46 … | |
45 | 47 … | // custom abouts (the future!) |
46 | - value, | |
47 | - values | |
48 … | + valueFrom, | |
49 … | + latestValue, | |
50 … | + socialValue, | |
51 … | + groupedValues | |
48 | 52 … | } |
49 | 53 … | }) |
50 | 54 … | |
51 | - function value (id, key, defaultValue) { | |
55 … | + | |
56 … | + function valueFrom (id, key, author) { | |
52 | 57 … | 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!') | |
53 | 68 … | 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) | |
55 | 70 … | } |
56 | 71 … | |
57 | - function values (id, key) { | |
72 … | + function groupedValues (id, key) { | |
58 | 73 … | 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) | |
60 | 75 … | } |
61 | 76 … | |
62 | 77 … | function get (id) { |
63 | 78 … | if (!ref.isLink(id)) throw new Error('About requires an ssb ref!') |
@@ -102,18 +117,18 @@ | ||
102 | 117 … | } |
103 | 118 … | } |
104 | 119 … | } |
105 | 120 … | |
106 | -function socialValue (lookup, key, id, yourId, fallback) { | |
121 … | +function getSocialValue (lookup, key, id, yourId, fallback) { | |
107 | 122 … | var result = lookup[key] ? getValue(lookup[key][yourId]) || getValue(lookup[key][id]) || highestRank(lookup[key]) : null |
108 | 123 … | if (result != null) { |
109 | 124 … | return result |
110 | 125 … | } else { |
111 | 126 … | return fallback || null |
112 | 127 … | } |
113 | 128 … | } |
114 | 129 … | |
115 | -function allValues (lookup, key) { | |
130 … | +function getGroupedValues (lookup, key) { | |
116 | 131 … | var values = {} |
117 | 132 … | for (var author in lookup[key]) { |
118 | 133 … | var value = getValue(lookup[key][author]) |
119 | 134 … | if (value != null) { |
@@ -144,9 +159,35 @@ | ||
144 | 159 … | function getValue (item) { |
145 | 160 … | if (item && item[0]) { |
146 | 161 … | if (typeof item[0] === 'string') { |
147 | 162 … | 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) { | |
149 | 164 … | return item[0].link |
150 | 165 … | } |
151 | 166 … | } |
152 | 167 … | } |
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