Browse Source

fix avatar cache to speed up image/name load time

master
Ev Bogue 1 year ago
parent
commit
b7cbdc9c3c
2 changed files with 52 additions and 43 deletions
  1. +50
    -41
      bog.js
  2. +2
    -2
      render.js

+ 50
- 41
bog.js View File

@ -115,28 +115,20 @@ function getImage (id, keys, classList) {
var image = h('img', {classList: 'avatar'})
}
localforage.getItem('image:' + id).then(cache => {
//if (cache) {
// return image.src = cache
//} else {
bog().then(log => {
if (log) {
for (var i = 0; i < log.length; i++) {
if ((log[i].imaged === id) && (log[i].author === keys.publicKey)) {
// if you've identified someone as something else show that something else
localforage.setItem('image:' + id, log[i].image)
image.src = cache
return image.src = cache
} else if ((log[i].imaged === id) && (log[i].author === id)) {
// else if show the image they gave themselves
localforage.setItem('image:' + id, log[i].image)
image.src = cache
return image.src = cache
}
}
bog().then(log => {
if (log) {
for (var i = 0; i < log.length; i++) {
if ((log[i].imaged === id) && (log[i].author === keys.publicKey)) {
// if you've identified someone as something else show that something else
localforage.setItem('image:' + id, log[i].image)
return image.src = log[i].image
} else if ((log[i].imaged === id) && (log[i].author === id)) {
// else if show the image they gave themselves
localforage.setItem('image:' + id, log[i].image)
return image.src = log[i].image
}
})
//}
}
}
})
return image
}
@ -148,29 +140,46 @@ function getName (id, keys) {
name.textContent = id.substring(0, 10) + '...'
localforage.getItem('name:' + id).then(cache => {
bog().then(log => {
if (log) {
for (var i = 0; i < log.length; i++ ) {
if ((log[i].named === id) && (log[i].author === keys.publicKey)) {
// if you've identified someone as something else show that something else
localforage.setItem('name:' + id, log[i].name)
return name.textContent = '@' + log[i].name
} else if ((log[i].named === id) && (log[i].author === id)) {
// else if show the name they gave themselves
localforage.setItem('name:' + id, log[i].name)
return name.textContent = '@' + log[i].name
}
// there should probably be some sort of sybil attack resiliance here (weight avatar name based on number of times used by individuals), but this will do for now.
}
}
})
return name
}
function getQuickImage (id, keys) {
var image = h('img', {classList: 'avatar'})
localforage.getItem('image:' + id).then(cache => {
if (cache) {
//console.log(cache)
return name.textContent = '@' + cache
} else {
bog().then(log => {
if (log) {
for (var i = 0; i < log.length; i++ ) {
if ((log[i].named === id) && (log[i].author === keys.publicKey)) {
// if you've identified someone as something else show that something else
localforage.setItem('name:' + id, log[i].name)
return name.textContent = '@' + log[i].name
} else if ((log[i].named === id) && (log[i].author === id)) {
// else if show the name they gave themselves
localforage.setItem('name:' + id, log[i].name)
return name.textContent = '@' + log[i].name
}
// there should probably be some sort of sybil attack resiliance here (weight avatar name based on number of times used by individuals), but this will do for now.
}
}
})
image.src = cache
}
})
return image
}
function getQuickName (id, keys) {
var name = h('span', [id.substring(0, 10)])
localforage.getItem('name:' + id).then(cache => {
if (cache) {
name.textContent = '@' + cache
}
})
return name
}


+ 2
- 2
render.js View File

@ -25,8 +25,8 @@ function getHeader (post, keys, mini) {
]),
h('p', [
h('a', {href: '#' + post.author}, [
getImage(post.author, keys),
getName(post.author, keys)
getQuickImage(post.author, keys),
getQuickName(post.author, keys)
]),
mini
])


Loading…
Cancel
Save