mirror of
https://github.com/twisterarmy/twister-html.git
synced 2025-09-13 23:02:09 +00:00
rewrite profiles' and avatars' caching
This commit is contained in:
parent
987090bd27
commit
8c70eae82a
@ -6,6 +6,8 @@
|
|||||||
// Post actions: submit, count characters
|
// Post actions: submit, count characters
|
||||||
|
|
||||||
var twister = {
|
var twister = {
|
||||||
|
profiles: {},
|
||||||
|
avatars: {},
|
||||||
URIs: {}, // shortened URIs are cached here after fetching
|
URIs: {}, // shortened URIs are cached here after fetching
|
||||||
torrentIds: {}, // auto-download torrentIds
|
torrentIds: {}, // auto-download torrentIds
|
||||||
focus: {}, // focused elements are counted here
|
focus: {}, // focused elements are counted here
|
||||||
|
157
js/twister_io.js
157
js/twister_io.js
@ -20,9 +20,6 @@ function twisterRpc(method, params, resultFunc, resultArg, errorFunc, errorArg)
|
|||||||
// join multiple dhtgets to the same resources in this map
|
// join multiple dhtgets to the same resources in this map
|
||||||
var _dhtgetPendingMap = {};
|
var _dhtgetPendingMap = {};
|
||||||
|
|
||||||
// memory cache for profile and avatar
|
|
||||||
var _profileMap = {};
|
|
||||||
var _avatarMap = {};
|
|
||||||
var _pubkeyMap = {};
|
var _pubkeyMap = {};
|
||||||
|
|
||||||
// number of dhtgets in progress (requests to the daemon)
|
// number of dhtgets in progress (requests to the daemon)
|
||||||
@ -228,32 +225,27 @@ function dhtput(peerAlias, resource, multi, value, sig_user, seq, cbFunc, cbReq)
|
|||||||
// get something from profile and store it in elem.text or do callback
|
// get something from profile and store it in elem.text or do callback
|
||||||
function getProfileResource(peerAlias, resource, elem, cbFunc, cbReq) {
|
function getProfileResource(peerAlias, resource, elem, cbFunc, cbReq) {
|
||||||
var profile;
|
var profile;
|
||||||
if (_profileMap[peerAlias]) {
|
if (twister.profiles[peerAlias]) {
|
||||||
profile = _profileMap[peerAlias];
|
profile = twister.profiles[peerAlias];
|
||||||
} else {
|
} else {
|
||||||
profile = _getResourceFromStorage('profile:' + peerAlias);
|
profile = _getResourceFromStorage('profile:' + peerAlias);
|
||||||
|
if (profile)
|
||||||
|
twister.profiles[peerAlias] = profile;
|
||||||
}
|
}
|
||||||
if (profile) {
|
if (profile) {
|
||||||
_profileMap[peerAlias] = profile;
|
|
||||||
if (elem)
|
if (elem)
|
||||||
elem.text(profile[resource]);
|
elem.text(profile[resource]);
|
||||||
if (cbFunc)
|
if (cbFunc)
|
||||||
cbFunc(cbReq, profile[resource]);
|
cbFunc(cbReq, profile[resource]);
|
||||||
} else {
|
} else {
|
||||||
dhtget(peerAlias, 'profile', 's',
|
loadProfile(peerAlias,
|
||||||
function(req, profile) {
|
function (peerAlias, req, res) {
|
||||||
if (profile) {
|
if (req.elem)
|
||||||
_profileMap[req.peerAlias] = profile;
|
req.elem.text(res[req.resource]);
|
||||||
_putResourceIntoStorage('profile:' + peerAlias, profile);
|
if (typeof req.cbFunc === 'function')
|
||||||
if (req.elem)
|
req.cbFunc(req.cbReq, res[req.resource]);
|
||||||
req.elem.text(profile[resource]);
|
},
|
||||||
if (req.cbFunc)
|
{elem: elem, resource: resource, cbFunc: cbFunc, cbReq: cbReq}
|
||||||
req.cbFunc(req.cbReq, profile[resource]);
|
|
||||||
} else {
|
|
||||||
if (req.cbFunc)
|
|
||||||
req.cbFunc(req.cbReq);
|
|
||||||
}
|
|
||||||
}, {peerAlias: peerAlias, elem: elem, cbFunc: cbFunc, cbReq: cbReq}
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -438,59 +430,122 @@ function getAvatar(peerAlias, img) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_avatarMap[peerAlias]) {
|
if (twister.avatars[peerAlias]) {
|
||||||
//img.attr('src', 'data:image/jpg;base64,'+avatarMap[peerAlias]);
|
img.attr('src', twister.avatars[peerAlias].src);
|
||||||
img.attr('src', _avatarMap[peerAlias]);
|
|
||||||
} else {
|
} else {
|
||||||
var data = _getResourceFromStorage('avatar:' + peerAlias);
|
var data = _getResourceFromStorage('avatar:' + peerAlias);
|
||||||
|
|
||||||
if (data) {
|
if (data) {
|
||||||
switch (data.substr(0, 4)) {
|
if (typeof data !== 'object')
|
||||||
|
data = {src: data, version: 0};
|
||||||
|
|
||||||
|
switch (data.src.substr(0, 4)) {
|
||||||
case 'jpg/':
|
case 'jpg/':
|
||||||
data = 'data:image/jpeg;base64,/9j/' + window.btoa(data.slice(4));
|
data.src = 'data:image/jpeg;base64,/9j/' + window.btoa(data.src.slice(4));
|
||||||
break;
|
break;
|
||||||
case 'png/':
|
case 'png/':
|
||||||
data = 'data:image/png;base64,' + window.btoa(data.slice(4));
|
data.src = 'data:image/png;base64,' + window.btoa(data.src.slice(4));
|
||||||
break;
|
break;
|
||||||
case 'gif/':
|
case 'gif/':
|
||||||
data = 'data:image/gif;base64,' + window.btoa(data.slice(4));
|
data.src = 'data:image/gif;base64,' + window.btoa(data.src.slice(4));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
_avatarMap[peerAlias] = data;
|
twister.avatars[peerAlias] = data;
|
||||||
img.attr('src', data);
|
img.attr('src', data.src);
|
||||||
} else {
|
} else {
|
||||||
dhtget(peerAlias, 'avatar', 's',
|
loadAvatar(peerAlias,
|
||||||
function(req, imagedata) {
|
function (peerAlias, req, res) {
|
||||||
if (imagedata && imagedata.length) {
|
req.attr('src', res);
|
||||||
_avatarMap[req.peerAlias] = imagedata;
|
},
|
||||||
if (imagedata !== 'img/genericPerson.png') {
|
img
|
||||||
if (imagedata.substr(0, 27) === 'data:image/jpeg;base64,/9j/')
|
|
||||||
_putResourceIntoStorage('avatar:' + peerAlias, 'jpg/' + window.atob(imagedata.slice(27)));
|
|
||||||
else {
|
|
||||||
var s = imagedata.substr(0, 22);
|
|
||||||
if (s === 'data:image/png;base64,' || s === 'data:image/gif;base64,')
|
|
||||||
_putResourceIntoStorage('avatar:' + peerAlias, imagedata.substr(11, 3) + '/' + window.atob(imagedata.slice(22)));
|
|
||||||
else
|
|
||||||
_putResourceIntoStorage('avatar:' + peerAlias, imagedata);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
req.img.attr('src', imagedata);
|
|
||||||
}
|
|
||||||
}, {peerAlias: peerAlias, img: img}
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function loadProfile(peerAlias, cbFunc, cbReq) {
|
||||||
|
dhtget(peerAlias, 'profile', 's',
|
||||||
|
function(req, res, resRaw) {
|
||||||
|
if (!resRaw || typeof res !== 'object')
|
||||||
|
return;
|
||||||
|
|
||||||
|
res.version = parseInt(resRaw[0].p.seq);
|
||||||
|
|
||||||
|
if (!twister.profiles[req.peerAlias] || !twister.profiles[req.peerAlias].version
|
||||||
|
|| res.version > twister.profiles[req.peerAlias].version) {
|
||||||
|
console.log('got ' + req.peerAlias + '\'s profile version ' + res.version + ' — going to cache and redraw globally');
|
||||||
|
cacheProfile(req.peerAlias, res);
|
||||||
|
redrawProfile(req.peerAlias, res);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof req.cbFunc === 'function')
|
||||||
|
req.cbFunc(req.peerAlias, req.cbReq, res);
|
||||||
|
},
|
||||||
|
{peerAlias: peerAlias, cbFunc: cbFunc, cbReq: cbReq}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadAvatar(peerAlias, cbFunc, cbReq) {
|
||||||
|
dhtget(peerAlias, 'avatar', 's',
|
||||||
|
function(req, res, resRaw) {
|
||||||
|
if (!resRaw)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (!res)
|
||||||
|
res = 'img/genericPerson.png';
|
||||||
|
|
||||||
|
var version = parseInt(resRaw[0].p.seq);
|
||||||
|
|
||||||
|
if (!twister.avatars[req.peerAlias] || !twister.avatars[req.peerAlias].version
|
||||||
|
|| version > twister.avatars[req.peerAlias].version) {
|
||||||
|
console.log('got ' + req.peerAlias + '\'s avatar version ' + version + ' — going to cache and redraw globally');
|
||||||
|
cacheAvatar(req.peerAlias, res, version);
|
||||||
|
redrawAvatar(req.peerAlias, res);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof req.cbFunc === 'function')
|
||||||
|
req.cbFunc(req.peerAlias, req.cbReq, res);
|
||||||
|
},
|
||||||
|
{peerAlias: peerAlias, cbFunc: cbFunc, cbReq: cbReq}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function cacheProfile(peerAlias, req) {
|
||||||
|
var dat = {};
|
||||||
|
for (var i in req)
|
||||||
|
if (req[i])
|
||||||
|
dat[i] = req[i];
|
||||||
|
|
||||||
|
twister.profiles[peerAlias] = dat;
|
||||||
|
|
||||||
|
_putResourceIntoStorage('profile:' + peerAlias, dat);
|
||||||
|
}
|
||||||
|
|
||||||
|
function cacheAvatar(peerAlias, req, version) {
|
||||||
|
twister.avatars[peerAlias] = {src: req, version: version};
|
||||||
|
|
||||||
|
if (req === 'img/genericPerson.png')
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (req.substr(0, 27) === 'data:image/jpeg;base64,/9j/') {
|
||||||
|
req = 'jpg/' + window.atob(req.slice(27));
|
||||||
|
} else {
|
||||||
|
var s = req.substr(0, 22);
|
||||||
|
if (s === 'data:image/png;base64,' || s === 'data:image/gif;base64,')
|
||||||
|
req = req.substr(11, 3) + '/' + window.atob(req.slice(22));
|
||||||
|
}
|
||||||
|
_putResourceIntoStorage('avatar:' + peerAlias, {src: req, version: version});
|
||||||
|
}
|
||||||
|
|
||||||
function clearAvatarAndProfileCache(peerAlias) {
|
function clearAvatarAndProfileCache(peerAlias) {
|
||||||
var storage = $.localStorage;
|
var storage = $.localStorage;
|
||||||
storage.remove('avatar:' + peerAlias);
|
storage.remove('avatar:' + peerAlias);
|
||||||
storage.remove('profile:' + peerAlias);
|
storage.remove('profile:' + peerAlias);
|
||||||
if (_avatarMap[peerAlias]) {
|
if (twister.avatars[peerAlias]) {
|
||||||
delete _avatarMap[peerAlias];
|
delete twister.avatars[peerAlias];
|
||||||
}
|
}
|
||||||
if (_profileMap[peerAlias]) {
|
if (twister.profiles[peerAlias]) {
|
||||||
delete _profileMap[peerAlias];
|
delete twister.profiles[peerAlias];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user