Commit d33a5390e43c533592da17b5d705c32b18d1cb4f
Merge branch 'cache'
Dominic Tarr committed on 8/4/2016, 5:07:46 AMParent: dd4bc213e0bccb92a0815d7a4e3c9d1894defd1f
Parent: d3e75c0a69e9cd486c63267e3e63786761c34c90
Files changed
modules/avatar-image.js | changed |
modules/like.js | changed |
modules/message.js | changed |
modules/names.js | changed |
modules/public.js | changed |
modules/search-box.js | changed |
sbot-api.js | changed |
modules/avatar-image.js | ||
---|---|---|
@@ -3,25 +3,96 @@ | ||
3 | 3 | var h = require('hyperscript') |
4 | 4 | var ref = require('ssb-ref') |
5 | 5 | |
6 | 6 | var plugs = require('../plugs') |
7 | -var sbot_links = plugs.first(exports.sbot_links = []) | |
7 | +var sbot_query = plugs.first(exports.sbot_query = []) | |
8 | 8 | var blob_url = require('../plugs').first(exports.blob_url = []) |
9 | 9 | |
10 | +var pull = require('pull-stream') | |
11 | + | |
10 | 12 | var id = require('../keys').id |
11 | 13 | |
12 | 14 | var default_avatar = '&qjeAs8+uMXLlyovT4JnEpMwTNDx/QXHfOl2nv2u0VCM=.sha256' |
13 | 15 | |
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 | + | |
14 | 76 | exports.avatar_image = function (author, classes) { |
15 | 77 | classes = classes || '' |
16 | 78 | if(classes && 'string' === typeof classes) classes = '.avatar--'+classes |
17 | 79 | |
18 | 80 | 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 | + | |
24 | 95 | return img |
25 | 96 | } |
26 | 97 | |
27 | 98 |
modules/like.js | ||
---|---|---|
@@ -18,21 +18,24 @@ | ||
18 | 18 | ) |
19 | 19 | } |
20 | 20 | |
21 | 21 | exports.message_meta = function (msg, sbot) { |
22 | - | |
23 | 22 | var digs = h('a') |
24 | 23 | |
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 | + } | |
34 | 32 | |
33 | + if(votes.length === 1) | |
34 | + digs.textContent = ' 1 Dig' | |
35 | + if(votes.length > 1) | |
36 | + digs.textContent = ' ' + votes.length + ' Digs' | |
37 | + | |
35 | 38 | return digs |
36 | 39 | } |
37 | 40 | |
38 | 41 | exports.message_action = function (msg, sbot) { |
modules/message.js | ||
---|---|---|
@@ -16,22 +16,39 @@ | ||
16 | 16 | exports.message_render = function (msg, sbot) { |
17 | 17 | var el = message_content(msg) |
18 | 18 | if(!el) return |
19 | 19 | |
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 | + | |
20 | 30 | 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 | + )) | |
21 | 37 | |
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 | - ) | |
33 | 38 | |
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 | + | |
34 | 51 | var msg = h('div.message', |
35 | 52 | h('div.title.row', |
36 | 53 | h('div.avatar', avatar(msg.value.author, 'thumbnail')), |
37 | 54 | h('div.message_meta.row', message_meta(msg)) |
@@ -60,4 +77,7 @@ | ||
60 | 77 | |
61 | 78 | |
62 | 79 | |
63 | 80 | |
81 | + | |
82 | + | |
83 | + |
modules/names.js | ||
---|---|---|
@@ -57,9 +57,9 @@ | ||
57 | 57 | } |
58 | 58 | |
59 | 59 | //union with this query... |
60 | 60 | |
61 | -var names = [] | |
61 | +var names = NAMES = [] | |
62 | 62 | function update(name) { |
63 | 63 | var n = names.find(function (e) { |
64 | 64 | return e.id == name.id && e.name == e.name |
65 | 65 | }) |
@@ -102,9 +102,9 @@ | ||
102 | 102 | //in this case: [name, id] |
103 | 103 | mfr.reduce(merge), |
104 | 104 | pull.collect(function (err, ary) { |
105 | 105 | if(!err) { |
106 | - names = ary | |
106 | + NAMES = names = ary | |
107 | 107 | ready = true |
108 | 108 | while(waiting.length) waiting.shift()() |
109 | 109 | } |
110 | 110 | }) |
modules/public.js | ||
---|---|---|
@@ -8,8 +8,10 @@ | ||
8 | 8 | var message_render = plugs.first(exports.message_render = []) |
9 | 9 | var message_compose = plugs.first(exports.message_compose = []) |
10 | 10 | var sbot_log = plugs.first(exports.sbot_log = []) |
11 | 11 | |
12 | +var HighWatermark = require('pull-high-watermark') | |
13 | + | |
12 | 14 | exports.screen_view = function (path, sbot) { |
13 | 15 | if(path === '/public') { |
14 | 16 | |
15 | 17 | var content = h('div.column.scroller__content') |
@@ -27,8 +29,9 @@ | ||
27 | 29 | ) |
28 | 30 | |
29 | 31 | pull( |
30 | 32 | u.next(sbot_log, {reverse: true, limit: 100, live: false}), |
33 | +// HighWatermark(100), | |
31 | 34 | Scroller(div, content, message_render, false, false) |
32 | 35 | ) |
33 | 36 | |
34 | 37 | return div |
modules/search-box.js | ||
---|---|---|
@@ -71,9 +71,9 @@ | ||
71 | 71 | } |
72 | 72 | })) |
73 | 73 | }) |
74 | 74 | } |
75 | - }) | |
75 | + }, {}) | |
76 | 76 | }, 10) |
77 | 77 | |
78 | 78 | |
79 | 79 | pull( |
sbot-api.js | ||
---|---|---|
@@ -27,8 +27,11 @@ | ||
27 | 27 | var createFeed = require('ssb-feed') |
28 | 28 | var keys = require('./keys') |
29 | 29 | var ssbKeys = require('ssb-keys') |
30 | 30 | |
31 | + | |
32 | +var cache = CACHE = {} | |
33 | + | |
31 | 34 | module.exports = function () { |
32 | 35 | var opts = createConfig() |
33 | 36 | var sbot = null |
34 | 37 | var connection_status = [] |
@@ -102,15 +105,24 @@ | ||
102 | 105 | sbot_query: rec.source(function (query) { |
103 | 106 | return sbot.query.read(query) |
104 | 107 | }), |
105 | 108 | sbot_log: rec.source(function (opts) { |
106 | - return sbot.createLogStream(opts) | |
109 | + return pull( | |
110 | + sbot.createLogStream(opts), | |
111 | + pull.through(function (e) { | |
112 | + CACHE[e.key] = CACHE[e.key] || e.value | |
113 | + }) | |
114 | + ) | |
107 | 115 | }), |
108 | 116 | sbot_user_feed: rec.source(function (opts) { |
109 | 117 | return sbot.createUserStream(opts) |
110 | 118 | }), |
111 | 119 | sbot_get: rec.async(function (key, cb) { |
112 | - sbot.get(key, cb) | |
120 | + if(CACHE[key]) cb(null, CACHE[key]) | |
121 | + sbot.get(key, function (err, value) { | |
122 | + if(err) return cb(err) | |
123 | + cb(null, CACHE[key] = value) | |
124 | + }) | |
113 | 125 | }), |
114 | 126 | sbot_publish: rec.async(function (content, cb) { |
115 | 127 | if(content.recps) |
116 | 128 | content = ssbKeys.box(content, content.recps.map(function (e) { |
@@ -136,4 +148,6 @@ | ||
136 | 148 | }) |
137 | 149 | } |
138 | 150 | } |
139 | 151 | |
152 | + | |
153 | + |
Built with git-ssb-web