Browse Source

add removing of old cached avatars and profiles from localStorage, see #339

readme-update
Simon Grim 9 years ago
parent
commit
c07e6281ba
  1. 2
      js/interface_home.js
  2. 1
      js/tmobile.js
  3. 37
      js/twister_io.js

2
js/interface_home.js

@ -33,6 +33,8 @@ var InterfaceFunctions = function() {
requestTimelineUpdate("latestFirstTime",postsPerRefresh,followingUsers,promotedPostsOnly); requestTimelineUpdate("latestFirstTime",postsPerRefresh,followingUsers,promotedPostsOnly);
}); });
cleanupStorage();
initInterfaceCommon(); initInterfaceCommon();
initUserSearch(); initUserSearch();
initInterfaceDirectMsg(); initInterfaceDirectMsg();

1
js/tmobile.js

@ -101,6 +101,7 @@ var router=new $.mobile.Router(
initializeTwister( true, true, function() { initializeTwister( true, true, function() {
if( !$("#home .posts").children().length ) { if( !$("#home .posts").children().length ) {
$.mobile.showPageLoadingMsg(); $.mobile.showPageLoadingMsg();
cleanupStorage();
getFullname( defaultScreenName, $("#home .rtitle")); getFullname( defaultScreenName, $("#home .rtitle"));
$(".mentions-count").attr("href","#mentions?user="+defaultScreenName ); $(".mentions-count").attr("href","#mentions?user="+defaultScreenName );
$.MAL.setPostTemplate( $("#post-template-home") ); $.MAL.setPostTemplate( $("#post-template-home") );

37
js/twister_io.js

@ -315,11 +315,12 @@ function getGroupChatName( groupalias, item ){
// data will only cause new requests to fail. // data will only cause new requests to fail.
function _getResourceFromStorage(locator) { function _getResourceFromStorage(locator) {
var storage = $.localStorage; var storage = $.localStorage;
if( storage.isSet(locator) ) { if (storage.isSet(locator)) {
var storedResource = storage.get(locator); var storedResource = storage.get(locator);
var curTime = new Date().getTime() / 1000; var curTime = new Date().getTime() / 1000;
// avatar is downloaded once per day // avatar is downloaded once per day FIXME why once per day? what about profiles?
if( storedResource.time + 3600*24 > curTime ) { // FIXME need to check what type of data is requested and what time is allowed for it
if (storedResource.time + 86400 > curTime) { // 3600 * 24
return storedResource.data; return storedResource.data;
} }
} }
@ -334,6 +335,35 @@ function _putResourceIntoStorage(locator, data) {
storage.set(locator, storedResource); storage.set(locator, storedResource);
} }
function cleanupStorage() {
var curTime = new Date().getTime() / 1000;
var storage = $.localStorage, keys = storage.keys(), item = '';
var delAvatars = delProfiles = 0;
for (var i = 0; i < keys.length; i++) {
item = keys[i];
// FIXME need to decide what time for type of data is allowed
if (item.substr(0, 7) === 'avatar:') {
if (storage.get(item).time + 86400 < curTime) { // 3600 * 24 hours
storage.remove(item);
delAvatars++;
//console.log('local storage item \'' + item + '\' was too old, deleted');
}
} else if (item.substr(0, 8) === 'profile:') {
if (storage.get(item).time + 86400 < curTime) { // 3600 * 24 hours
storage.remove(item);
delProfiles++;
//console.log('local storage item \'' + item + '\' was too old, deleted');
}
}
}
console.log('cleaning of storage is completed for ' + (new Date().getTime() / 1000 - curTime) + 's');
if (delAvatars) console.log(' ' + delAvatars + ' cached avatars was too old, deleted');
if (delProfiles) console.log(' ' + delProfiles + ' cached profiles was too old, deleted');
console.log(' ' + 'there was ' + i + ' items in total, now ' + (i - delAvatars - delProfiles));
}
// get avatar and set it in img.attr("src") // get avatar and set it in img.attr("src")
// TODO rename to getAvatarImgToELem(), move nin theme related stuff to nin's theme_option.js // TODO rename to getAvatarImgToELem(), move nin theme related stuff to nin's theme_option.js
function getAvatar( username, img ){ function getAvatar( username, img ){
@ -351,6 +381,7 @@ function getAvatar( username, img ){
img.attr('src', _avatarMap[username]); img.attr('src', _avatarMap[username]);
} else { } else {
var data = _getResourceFromStorage("avatar:" + username); var data = _getResourceFromStorage("avatar:" + username);
if( data ) { if( data ) {
_avatarMap[username] = data; _avatarMap[username] = data;
img.attr('src', data); img.attr('src', data);

Loading…
Cancel
Save