git ssb

0+

ev / microbay



forked from Dominic / patchbay

Commit 473ab796d9ba662aa1f1665d60c10838dc0c35d9

load names, avatars, digs from cached data

Dominic Tarr committed on 8/3/2016, 11:16:37 PM
Parent: 9abd057463439027a5ac4f40d5f7e5f0316deb6c

Files changed

modules/avatar-image.jschanged
modules/like.jschanged
modules/message.jschanged
modules/names.jschanged
modules/public.jschanged
modules/search-box.jschanged
modules/avatar-image.jsView
@@ -3,25 +3,96 @@
33 var h = require('hyperscript')
44 var ref = require('ssb-ref')
55
66 var plugs = require('../plugs')
7-var sbot_links = plugs.first(exports.sbot_links = [])
7+var sbot_query = plugs.first(exports.sbot_query = [])
88 var blob_url = require('../plugs').first(exports.blob_url = [])
99
10+var pull = require('pull-stream')
11+
1012 var id = require('../keys').id
1113
1214 var default_avatar = '&qjeAs8+uMXLlyovT4JnEpMwTNDx/QXHfOl2nv2u0VCM=.sha256'
1315
16+var avatars = AVATARS = {}
17+
18+function isFunction (f) {
19+ return 'function' === typeof f
20+}
21+
22+var self_id = require('../keys').id
23+
24+var ready = false
25+var waiting = []
26+
27+var last = 0
28+
29+//blah blah
30+setTimeout(function () {
31+pull(
32+ sbot_query({
33+ query: [{
34+ $filter: {
35+ timestamp: {$gt: last || 0 },
36+ value: { content: {
37+ type: "about",
38+ about: {$prefix: "@"},
39+ image: {link: {$prefix: "&"}}
40+ }}
41+ }},
42+ {
43+ $map: {
44+ id: ["value", "content", "about"],
45+ image: ["value", "content", "image", "link"],
46+ by: ["value", "author"],
47+ ts: 'timestamp'
48+ }}],
49+ live: true
50+ }),
51+ pull.drain(function (a) {
52+ if(a.sync) {
53+ ready = true
54+ while(waiting.length) waiting.shift()()
55+ return
56+ }
57+ last = a.ts
58+ //set image for avatar.
59+ //overwrite another avatar
60+ //you picked.
61+ if(
62+ //if there is no avatar
63+ (!avatars[a.id]) ||
64+ //if i chose this avatar
65+ (a.by == self_id) ||
66+ //they chose their own avatar,
67+ //and current avatar was not chosen by me
68+ (a.by === a.id && avatars[a.id].by != self_id)
69+ )
70+ avatars[a.id] = a
71+
72+ })
73+)
74+})
75+
1476 exports.avatar_image = function (author, classes) {
1577 classes = classes || ''
1678 if(classes && 'string' === typeof classes) classes = '.avatar--'+classes
1779
1880 var img = h('img'+classes, {src: blob_url(default_avatar)})
19- getAvatar({links: sbot_links}, id, author, function (err, avatar) {
20- if (err) return console.error(err)
21- if(ref.isBlob(avatar.image))
22- img.src = blob_url(avatar.image)
23- })
81+// getAvatar({links: sbot_links}, id, author, function (err, avatar) {
82+// if (err) return console.error(err)
83+// if(ref.isBlob(avatar.image))
84+// img.src = blob_url(avatar.image)
85+// })
86+
87+ function go () {
88+ if(avatars[author]) img.src = blob_url(avatars[author].image)
89+ }
90+
91+ if(!ready)
92+ waiting.push(go)
93+ else go()
94+
2495 return img
2596 }
2697
2798
modules/like.jsView
@@ -18,21 +18,24 @@
1818 )
1919 }
2020
2121 exports.message_meta = function (msg, sbot) {
22-
2322 var digs = h('a')
2423
25- pull(
26- sbot_links({dest: msg.key, rel: 'vote'}),
27- pull.collect(function (err, votes) {
28- if(votes.length === 1)
29- digs.textContent = ' 1 Dig'
30- if(votes.length > 1)
31- digs.textContent = ' ' + votes.length + ' Digs'
32- })
33- )
24+ var votes = []
25+ for(var k in CACHE) {
26+ if(CACHE[k].content.type == 'vote' &&
27+ (CACHE[k].content.vote == msg.key ||
28+ CACHE[k].content.vote.link == msg.key
29+ ))
30+ votes.push({source: CACHE[k].author, dest: k, rel: 'vote'})
31+ }
3432
33+ if(votes.length === 1)
34+ digs.textContent = ' 1 Dig'
35+ if(votes.length > 1)
36+ digs.textContent = ' ' + votes.length + ' Digs'
37+
3538 return digs
3639 }
3740
3841 exports.message_action = function (msg, sbot) {
modules/message.jsView
@@ -16,22 +16,39 @@
1616 exports.message_render = function (msg, sbot) {
1717 var el = message_content(msg)
1818 if(!el) return
1919
20+ var links = []
21+ for(var k in CACHE) {
22+ var _msg = CACHE[k]
23+ if(_msg.content.type == 'post' && Array.isArray(_msg.content.mentions)) {
24+ for(var i = 0; i < _msg.content.mentions.length; i++)
25+ if(_msg.content.mentions[i].link == msg.key)
26+ links.push(k)
27+ }
28+ }
29+
2030 var backlinks = h('div.backlinks')
31+ if(links.length)
32+ backlinks.appendChild(h('label', 'backlinks:',
33+ h('div', links.map(function (key) {
34+ return message_link(key)
35+ }))
36+ ))
2137
22- pull(
23- sbot_links({dest: msg.key, rel: 'mentions', keys: true}),
24- pull.collect(function (err, links) {
25- if(links.length)
26- backlinks.appendChild(h('label', 'backlinks:',
27- h('div', links.map(function (link) {
28- return message_link(link.key)
29- }))
30- ))
31- })
32- )
3338
39+// pull(
40+// sbot_links({dest: msg.key, rel: 'mentions', keys: true}),
41+// pull.collect(function (err, links) {
42+// if(links.length)
43+// backlinks.appendChild(h('label', 'backlinks:',
44+// h('div', links.map(function (link) {
45+// return message_link(link.key)
46+// }))
47+// ))
48+// })
49+// )
50+
3451 var msg = h('div.message',
3552 h('div.title.row',
3653 h('div.avatar', avatar(msg.value.author, 'thumbnail')),
3754 h('div.message_meta.row', message_meta(msg))
@@ -60,4 +77,7 @@
6077
6178
6279
6380
81+
82+
83+
modules/names.jsView
@@ -57,9 +57,9 @@
5757 }
5858
5959 //union with this query...
6060
61-var names = []
61+var names = NAMES = []
6262 function update(name) {
6363 var n = names.find(function (e) {
6464 return e.id == name.id && e.name == e.name
6565 })
@@ -102,9 +102,9 @@
102102 //in this case: [name, id]
103103 mfr.reduce(merge),
104104 pull.collect(function (err, ary) {
105105 if(!err) {
106- names = ary
106+ NAMES = names = ary
107107 ready = true
108108 while(waiting.length) waiting.shift()()
109109 }
110110 })
modules/public.jsView
@@ -8,8 +8,10 @@
88 var message_render = plugs.first(exports.message_render = [])
99 var message_compose = plugs.first(exports.message_compose = [])
1010 var sbot_log = plugs.first(exports.sbot_log = [])
1111
12+var HighWatermark = require('pull-high-watermark')
13+
1214 exports.screen_view = function (path, sbot) {
1315 if(path === '/public') {
1416
1517 var content = h('div.column.scroller__content')
@@ -27,8 +29,9 @@
2729 )
2830
2931 pull(
3032 u.next(sbot_log, {reverse: true, limit: 100, live: false}),
33+// HighWatermark(100),
3134 Scroller(div, content, message_render, false, false)
3235 )
3336
3437 return div
modules/search-box.jsView
@@ -71,9 +71,9 @@
7171 }
7272 }))
7373 })
7474 }
75- })
75+ }, {})
7676 }, 10)
7777
7878
7979 pull(

Built with git-ssb-web