From 9a4147fb60963f890b444044d8c2b66480ea6f14 Mon Sep 17 00:00:00 2001 From: Hannes Mannerheim Date: Wed, 25 Feb 2015 23:46:32 +0100 Subject: [PATCH] cache full text of truncated queets in localStorage --- js/dom-functions.js | 22 ++++++++++++++--- js/misc-functions.js | 57 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 75 insertions(+), 4 deletions(-) diff --git a/js/dom-functions.js b/js/dom-functions.js index 3cf2e67..3fc9ceb 100644 --- a/js/dom-functions.js +++ b/js/dom-functions.js @@ -930,12 +930,21 @@ function expand_queet(q,doScrolling) { // if shortened queet, get full text if(q.children('.queet').find('span.attachment.more').length>0) { var attachmentId = q.children('.queet').find('span.attachment.more').attr('data-attachment-id'); - getFromAPI("attachment/" + attachmentId + ".json",function(data){ + + // get full html for queet, first try localstorage cache + localStorageObjectCache_GET('fullQueetHtml',qid,function(data){ if(data) { - console.log(data); q.children('.queet').find('.queet-text').html($.trim(data.replace(/@/gi,'').replace(/!/gi,'').replace(/#/gi,'').replace(/@/gi,'').replace(/@/gi,'').replace(/!/gi,'').replace(/#/gi,''))); } - }); + else { + getFromAPI("attachment/" + attachmentId + ".json",function(data){ + if(data) { + localStorageObjectCache_STORE('fullQueetHtml',qid,data); + q.children('.queet').find('.queet-text').html($.trim(data.replace(/@/gi,'').replace(/!/gi,'').replace(/#/gi,'').replace(/@/gi,'').replace(/@/gi,'').replace(/!/gi,'').replace(/#/gi,''))); + } + }); + } + }); } // add expanded container @@ -1692,6 +1701,13 @@ function addToFeed(feed, after, extraClasses, isReply) { function buildQueetHtml(obj, idInStream, extraClassesThisRun, requeeted_by, isConversation) { + // if we have the full html for a truncated notice cached in localstorage, we use that + localStorageObjectCache_GET('fullQueetHtml',obj.id,function(data){ + if(data) { + obj.statusnet_html = data; + } + }); + // we don't want to print 'null' in in_reply_to_screen_name-attribute, someone might have that username! var in_reply_to_screen_name = ''; if(obj.in_reply_to_screen_name != null) { diff --git a/js/misc-functions.js b/js/misc-functions.js index 73f23c5..8c5a5d9 100644 --- a/js/misc-functions.js +++ b/js/misc-functions.js @@ -33,6 +33,57 @@ · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · */ + +/* · + · + · Store in localStorage object cache + · + · @param name: the name of this type of object + · @param unique_id: some unique_id – the key in localStorage will be name-unique_id + · @param object: the object to store + · + · · · · · · · · · */ + +function localStorageObjectCache_STORE(name, unique_id, object) { + + if(localStorageIsEnabled()) { + + if(object.length < 1) { + // an empty object means we remove this entry + if(typeof localStorage[name + '-' + unique_id] != 'undefined' && localStorage[name + '-' + unique_id] !== null) { + delete localStorage[name + '-' + unique_id]; + } + } + else { + localStorage[name + '-' + unique_id] = JSON.stringify(object); + } + } + } + +/* · + · + · Get from localStorage object cache + · + · @param name: the name of this type of object + · @param unique_id: some unique_id – the key in localStorage will be name-unique_id + · @param callback: callback function, returns false if not found + · + · · · · · · · · · */ + +function localStorageObjectCache_GET(name, unique_id, callback) { + + if(localStorageIsEnabled()) { + if(typeof localStorage[name + '-' + unique_id] != 'undefined' && localStorage[name + '-' + unique_id] !== null) { + callback(JSON.parse(localStorage[name + '-' + unique_id])); + } + else { + callback(false); + } + } + } + + + /* · · · Display unread notifications @@ -555,7 +606,11 @@ function remove_spinner() { function convertAttachmentMoreHref() { $('a.attachment.more').each(function() { if(typeof $(this).attr('href') != 'undefined') { - $(this).replaceWith($('')); + var attachment_href = $(this).attr('href'); + var attachment_id = attachment_href.substr((~-attachment_href.lastIndexOf("/") >>> 0) + 2); + if(attachment_id.length>0) { + $(this).replaceWith($('')); + } } }); }