From 3f75c49e3e61434a9ec768c6e24299b3b828a17a Mon Sep 17 00:00:00 2001 From: mossroy Date: Wed, 6 Aug 2014 12:18:27 +0200 Subject: [PATCH 1/4] Issue #30 : Handle the NOT_LOGGED_IN errors that can come from tt-rss backends. It currently asks the user to type the login/password again, which is not ideal. But at least he can access his unread articles again. --- js/App.js | 66 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 27 deletions(-) diff --git a/js/App.js b/js/App.js index 73c4257..60e141c 100644 --- a/js/App.js +++ b/js/App.js @@ -162,35 +162,47 @@ App.prototype.reload = function() { App.prototype.gotUnreadFeeds = function(new_articles) { - if(new_articles == null || !this.validate(new_articles)) { // on error load the saved unread articles. - - var old_articles = localStorage.unread_articles; - if(old_articles) { - this.unread_articles = JSON.parse(old_articles); - } - this.populateList(); - + if(new_articles == null || !this.validate(new_articles)) { + + // Check if we did not get a NOT_LOGGED_IN error, and ask the + // user to login again if it is the case. + // This can happen with TT-RSS backend + if (new_articles.error && new_articles.error === "NOT_LOGGED_IN") { + $("#url").value = localStorage.server_url; + $("#login form").backend[0].checked = true; + alert("Your TinyTinyRSS session has expired. Please login again"); + this.login.log_in(); + } + else { + // On other errors, load the saved unread articles. + var old_articles = localStorage.unread_articles; + if(old_articles) { + this.unread_articles = JSON.parse(old_articles); + } + this.populateList(); + } + } else { + + this.unread_articles = this.unread_articles.concat(new_articles); - this.unread_articles = this.unread_articles.concat(new_articles); - - if(new_articles.length > 0) { - try { - //To check if when it fails it is the same - localStorage.unread_articles = JSON.stringify(this.unread_articles); - var size = parseInt(localStorage.maxDownload); - if(localStorage.unread_articles.length < size) { - var num = parseInt(localStorage.numArticles); - this.backend.getUnreadFeeds(this.gotUnreadFeeds.bind(this), this.unread_articles,num); - } else { - alert("Limit size reached: Downloaded: " + this.unread_articles.length + " articles. Reached: " + localStorage.unread_articles.length +" bytes"); - } - } - catch (e) { - alert("Reached maximum memory by app " + e.name + " " + e.message + ". We will keep working in anycase with: " + localStorage.unread_articles.length); - } - this.populateList(); - } + if(new_articles.length > 0) { + try { + //To check if when it fails it is the same + localStorage.unread_articles = JSON.stringify(this.unread_articles); + var size = parseInt(localStorage.maxDownload); + if(localStorage.unread_articles.length < size) { + var num = parseInt(localStorage.numArticles); + this.backend.getUnreadFeeds(this.gotUnreadFeeds.bind(this), this.unread_articles,num); + } else { + alert("Limit size reached: Downloaded: " + this.unread_articles.length + " articles. Reached: " + localStorage.unread_articles.length +" bytes"); + } + } + catch (e) { + alert("Reached maximum memory by app " + e.name + " " + e.message + ". We will keep working in anycase with: " + localStorage.unread_articles.length); + } + this.populateList(); + } } }; From b2f319af71a51957c72fe1a27efc77b26e1073f3 Mon Sep 17 00:00:00 2001 From: mossroy Date: Wed, 6 Aug 2014 15:22:59 +0200 Subject: [PATCH 2/4] Issue #30 : Keep the username in localStorage, so that the user does not have to type it again in case his session has expired + also fill the login form with the localStorage values when the user chooses to logout --- js/App.js | 3 +-- js/Login.js | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/js/App.js b/js/App.js index 60e141c..c2187d8 100644 --- a/js/App.js +++ b/js/App.js @@ -168,9 +168,8 @@ App.prototype.gotUnreadFeeds = function(new_articles) { // user to login again if it is the case. // This can happen with TT-RSS backend if (new_articles.error && new_articles.error === "NOT_LOGGED_IN") { - $("#url").value = localStorage.server_url; - $("#login form").backend[0].checked = true; alert("Your TinyTinyRSS session has expired. Please login again"); + this.login.fillLoginFormFromLocalSotrage(); this.login.log_in(); } else { diff --git a/js/Login.js b/js/Login.js index 72a9490..b8ed527 100644 --- a/js/Login.js +++ b/js/Login.js @@ -67,6 +67,7 @@ Login.prototype.authenticate = function(e) { if(data.version) { var auth = btoa(user + ':' + password); localStorage.server_url = server_url; + localStorage.username = user; localStorage.session_id = auth; localStorage.backend = "OwnCloud"; _this.app.after_login(localStorage.backend); @@ -83,6 +84,7 @@ Login.prototype.authenticate = function(e) { Pond.login(server_url, user, password, function(data) { if(data.session_token) { localStorage.server_url = server_url; + localStorage.username = user; localStorage.session_id = data.session_token; localStorage.backend = "Pond"; _this.app.after_login(localStorage.backend); @@ -106,6 +108,7 @@ Login.prototype.authenticate = function(e) { } else { localStorage.server_url = server_url; + localStorage.username = user; localStorage.session_id = data.session_id; localStorage.backend = "TinyTinyRSS"; _this.app.after_login(localStorage.backend); @@ -120,7 +123,23 @@ Login.prototype.authenticate = function(e) { return false; }; +Login.prototype.fillLoginFormFromLocalSotrage = function() { + $("#url").value = localStorage.server_url; + $("#un").value = localStorage.username; + var backendName = localStorage.backend; + if (backendName === "TinyTinyRSS") { + $("#login form").backend[0].checked = true; + } + else if (backendName === "OwnCloud") { + $("#login form").backend[1].checked = true; + } + else if (backendName === "Pond") { + $("#login form").backend[2].checked = true; + } +} + Login.prototype.log_out = function() { + this.fillLoginFormFromLocalSotrage(); localStorage.removeItem("server_url"); localStorage.removeItem("session_id"); localStorage.removeItem("unread_articles"); From 568ca8005de07cdda278771c1d427ddee9f85bac Mon Sep 17 00:00:00 2001 From: mossroy Date: Wed, 6 Aug 2014 15:29:34 +0200 Subject: [PATCH 3/4] Issue #30 : Fix a typo in function name --- js/App.js | 2 +- js/Login.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/js/App.js b/js/App.js index c2187d8..520f9ab 100644 --- a/js/App.js +++ b/js/App.js @@ -169,7 +169,7 @@ App.prototype.gotUnreadFeeds = function(new_articles) { // This can happen with TT-RSS backend if (new_articles.error && new_articles.error === "NOT_LOGGED_IN") { alert("Your TinyTinyRSS session has expired. Please login again"); - this.login.fillLoginFormFromLocalSotrage(); + this.login.fillLoginFormFromLocalStorage(); this.login.log_in(); } else { diff --git a/js/Login.js b/js/Login.js index b8ed527..8f8212c 100644 --- a/js/Login.js +++ b/js/Login.js @@ -123,7 +123,7 @@ Login.prototype.authenticate = function(e) { return false; }; -Login.prototype.fillLoginFormFromLocalSotrage = function() { +Login.prototype.fillLoginFormFromLocalStorage = function() { $("#url").value = localStorage.server_url; $("#un").value = localStorage.username; var backendName = localStorage.backend; @@ -139,7 +139,7 @@ Login.prototype.fillLoginFormFromLocalSotrage = function() { } Login.prototype.log_out = function() { - this.fillLoginFormFromLocalSotrage(); + this.fillLoginFormFromLocalStorage(); localStorage.removeItem("server_url"); localStorage.removeItem("session_id"); localStorage.removeItem("unread_articles"); From cc88b341b2fde13e34e0bff7516852b8889f861d Mon Sep 17 00:00:00 2001 From: mossroy Date: Wed, 6 Aug 2014 15:37:08 +0200 Subject: [PATCH 4/4] Issue #30 : Avoid displaying "undefined" for username when it was not yet stored in the localStorage --- js/Login.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/js/Login.js b/js/Login.js index 8f8212c..c786023 100644 --- a/js/Login.js +++ b/js/Login.js @@ -124,8 +124,14 @@ Login.prototype.authenticate = function(e) { }; Login.prototype.fillLoginFormFromLocalStorage = function() { - $("#url").value = localStorage.server_url; - $("#un").value = localStorage.username; + var serverUrl = localStorage.server_url; + if (serverUrl) { + $("#url").value = serverUrl; + } + var userName = localStorage.username; + if (userName) { + $("#un").value = userName; + } var backendName = localStorage.backend; if (backendName === "TinyTinyRSS") { $("#login form").backend[0].checked = true;