From c07e6281ba86b0ae2104efa96c8b18235ec5dbc7 Mon Sep 17 00:00:00 2001 From: Simon Grim Date: Sun, 17 Jan 2016 02:52:43 +0500 Subject: [PATCH] add removing of old cached avatars and profiles from localStorage, see #339 --- js/interface_home.js | 2 ++ js/tmobile.js | 1 + js/twister_io.js | 37 ++++++++++++++++++++++++++++++++++--- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/js/interface_home.js b/js/interface_home.js index 388e5da..6510b87 100644 --- a/js/interface_home.js +++ b/js/interface_home.js @@ -33,6 +33,8 @@ var InterfaceFunctions = function() { requestTimelineUpdate("latestFirstTime",postsPerRefresh,followingUsers,promotedPostsOnly); }); + cleanupStorage(); + initInterfaceCommon(); initUserSearch(); initInterfaceDirectMsg(); diff --git a/js/tmobile.js b/js/tmobile.js index 3d7b901..1fb1b45 100644 --- a/js/tmobile.js +++ b/js/tmobile.js @@ -101,6 +101,7 @@ var router=new $.mobile.Router( initializeTwister( true, true, function() { if( !$("#home .posts").children().length ) { $.mobile.showPageLoadingMsg(); + cleanupStorage(); getFullname( defaultScreenName, $("#home .rtitle")); $(".mentions-count").attr("href","#mentions?user="+defaultScreenName ); $.MAL.setPostTemplate( $("#post-template-home") ); diff --git a/js/twister_io.js b/js/twister_io.js index c8784ae..e46cfb5 100644 --- a/js/twister_io.js +++ b/js/twister_io.js @@ -315,11 +315,12 @@ function getGroupChatName( groupalias, item ){ // data will only cause new requests to fail. function _getResourceFromStorage(locator) { var storage = $.localStorage; - if( storage.isSet(locator) ) { + if (storage.isSet(locator)) { var storedResource = storage.get(locator); var curTime = new Date().getTime() / 1000; - // avatar is downloaded once per day - if( storedResource.time + 3600*24 > curTime ) { + // avatar is downloaded once per day FIXME why once per day? what about profiles? + // 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; } } @@ -334,6 +335,35 @@ function _putResourceIntoStorage(locator, data) { 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") // TODO rename to getAvatarImgToELem(), move nin theme related stuff to nin's theme_option.js function getAvatar( username, img ){ @@ -351,6 +381,7 @@ function getAvatar( username, img ){ img.attr('src', _avatarMap[username]); } else { var data = _getResourceFromStorage("avatar:" + username); + if( data ) { _avatarMap[username] = data; img.attr('src', data);