git ssb

7+

dinoworm 🐛 / patchcore



Commit 6de90614bba444a5983be5bf651a2f79fcc19e73

huge improvements to about.obs

- now handles other assigned values and collects
- use color-hash instead of visualize-buffer
Matt McKegg committed on 2/16/2017, 4:55:37 AM
Parent: b7c9439125a528967ba78bac1f8d7c0372e3aec6

Files changed

about/html/image.jschanged
about/obs.jschanged
package.jsonchanged
about/html/image.jsView
@@ -1,16 +1,19 @@
11 var h = require('mutant/h')
22 var nest = require('depnest')
33
44 exports.needs = nest({
5- 'about.obs.imageUrl': 'first'
5 + 'about.obs.imageUrl': 'first',
6 + 'about.obs.color': 'first'
67 })
78
89 exports.gives = nest('about.html.image')
910
1011 exports.create = function (api) {
1112 return nest('about.html.image', function (id) {
1213 return h('img', {
14 + className: 'Avatar',
15 + style: { 'background-color': api.about.obs.color(id) },
1316 src: api.about.obs.imageUrl(id)
1417 })
1518 })
1619 }
about/obs.jsView
@@ -1,20 +1,25 @@
1-var {Value, Struct, computed} = require('mutant')
2-var Abortable = require('pull-abortable')
1 +var {Value, Struct, Dict, computed} = require('mutant')
2 +var pullPause = require('pull-pause')
33 var pull = require('pull-stream')
44 var msgs = require('ssb-msgs')
5-var visualize = require('visualize-buffer')
65 var nest = require('depnest')
6 +var colorHash = new (require('color-hash'))()
77
88 exports.needs = nest({
9- 'sbot.pull.userFeed': 'first',
10- 'blob.sync.url': 'first'
9 + 'sbot.pull.links': 'first',
10 + 'blob.sync.url': 'first',
11 + 'keys.sync.id': 'first'
1112 })
1213 exports.gives = nest({
1314 'about.obs': [
1415 'name',
16 + 'description',
1517 'image',
16- 'imageUrl'
18 + 'imageUrl',
19 + 'names',
20 + 'images',
21 + 'color'
1722 ]
1823 })
1924
2025 exports.create = function (api) {
@@ -22,10 +27,15 @@
2227
2328 return nest({
2429 'about.obs': {
2530 name: (id) => get(id).displayName,
31 + description: (id) => get(id).description,
2632 image: (id) => get(id).image,
27- imageUrl: (id) => get(id).imageUrl
33 + imageUrl: (id) => get(id).imageUrl,
34 +
35 + names: (id) => get(id).names,
36 + images: (id) => get(id).images,
37 + color: (id) => computed(id, (id) => colorHash.hex(id))
2838 }
2939 })
3040
3141 function get (id) {
@@ -36,70 +46,94 @@
3646 }
3747 }
3848
3949 function About (api, id) {
40- // naive about that only looks at what a feed asserts about itself
50 + var pauser = pullPause((paused) => {})
4151
42- var fallbackImageUrl = genImage(id)
52 + // transparent image
53 + var fallbackImageUrl = 'data:image/gif;base64,R0lGODlhAQABAPAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='
4354
55 + var sync = Value(false)
56 + var yourId = api.keys.sync.id()
57 +
4458 var obs = Struct({
45- displayName: Value(id.slice(1, 10)),
46- image: Value(genImage(id))
59 + assignedNames: Dict(),
60 + assignedImages: Dict(),
61 + assignedDescriptions: Dict()
62 + }, {
63 + onListen: pauser.resume,
64 + onUnlisten: pauser.pause
4765 })
4866
49- obs.imageUrl = computed(obs.image, (image) => {
50- var obj = msgs.link(image, 'blob')
51- if (obj) {
52- return api.blob.sync.url(obj.link)
67 + obs.sync = computed([sync, obs], (v) => v)
68 + obs.displayName = computed([obs.assignedNames, id, yourId, id.slice(1, 10)], socialValue)
69 + obs.description = computed([obs.assignedDescriptions, id, yourId], socialValue)
70 + obs.image = computed([obs.assignedImages, id, yourId], socialValue)
71 +
72 + obs.names = computed(obs.assignedNames, indexByValue)
73 + obs.images = computed(obs.assignedImages, indexByValue)
74 +
75 + obs.imageUrl = computed(obs.image, (blobId) => {
76 + if (blobId) {
77 + return api.blob.sync.url(blobId)
5378 } else {
5479 return fallbackImageUrl
5580 }
5681 })
5782
58- var hasName = false
59- var hasImage = false
60-
61- var abortable = Abortable()
62-
63- // search history
6483 pull(
65- api.sbot.pull.userFeed({reverse: true, id}),
66- abortable,
67- pull.drain(function (item) {
68- update(item)
69- if (hasName && obs.image()) {
70- abortable.abort()
84 + api.sbot.pull.links({dest: id, rel: 'about', values: true, live: true}),
85 + pauser,
86 + pull.drain(function (msg) {
87 + if (msg.sync) {
88 + sync.set(true)
89 + } else {
90 + if (msg.value.content.name) {
91 + obs.assignedNames.put(msg.value.author, msg.value.content.name)
92 + }
93 + if (msg.value.content.image) {
94 + var obj = msgs.link(msg.value.content.image, 'blob')
95 + if (obj && obj.link) {
96 + obs.assignedImages.put(msg.value.author, obj.link)
97 + }
98 + }
99 + if (msg.value.content.description) {
100 + obs.assignedDescriptions.put(msg.value.author, msg.value.content.description)
101 + }
71102 }
103 + }, () => {
104 + sync.set(true)
72105 })
73106 )
74107
75- // get live changes
76- pull(
77- api.sbot.pull.userFeed({old: false, id}),
78- pull.drain(update)
79- )
80-
81108 return obs
109 +}
82110
83- // scoped
111 +function socialValue (lookup, id, yourId, fallback) {
112 + return lookup[yourId] || lookup[id] || highestRank(lookup) || fallback || null
113 +}
84114
85- function update (item) {
86- if (item.value && item.value.content.type === 'about' && item.value.content.about === id) {
87- if (item.value.content.name) {
88- if (!hasName || hasName < item.value.timestamp) {
89- hasName = item.value.timestamp
90- obs.displayName.set(item.value.content.name)
91- }
92- }
93- if (item.value.content.image) {
94- if (!hasImage || hasImage < item.value.timestamp) {
95- hasImage = item.value.timestamp
96- obs.image.set(item.value.content.image)
97- }
98- }
115 +function highestRank (lookup) {
116 + var indexed = indexByValue(lookup)
117 + var highestCount = 0
118 + var currentHighest = null
119 + Object.keys(indexed).forEach((item) => {
120 + var count = indexed[item].length
121 + if (count > highestCount) {
122 + highestCount = count
123 + currentHighest = item
99124 }
100- }
125 + })
126 + return currentHighest
101127 }
102128
103-function genImage (id) {
104- return visualize(new Buffer(id.substring(1), 'base64'), 256).src
129 +function indexByValue (lookup) {
130 + var result = {}
131 + Object.keys(lookup).forEach((key) => {
132 + var value = lookup[key]
133 + if (!result[value]) {
134 + result[value] = []
135 + }
136 + result[value].push(key)
137 + })
138 + return result
105139 }
package.jsonView
@@ -25,8 +25,9 @@
2525 "homepage": "https://github.com/ssbc/patchcore#readme",
2626 "dependencies": {
2727 "bulk-require": "^1.0.0",
2828 "bulkify": "^1.4.2",
29 + "color-hash": "^1.0.3",
2930 "depnest": "^1.0.2",
3031 "emoji-named-characters": "^1.0.2",
3132 "es2040": "^1.2.4",
3233 "human-time": "0.0.1",
@@ -43,9 +44,8 @@
4344 "ssb-keys": "^7.0.4",
4445 "ssb-markdown": "^3.2.1",
4546 "ssb-ref": "^2.6.2",
4647 "ssb-sort": "^1.0.0",
47- "visualize-buffer": "0.0.1",
4848 "xtend": "^4.0.1"
4949 },
5050 "browserify": {
5151 "transform": [

Built with git-ssb-web