first attempt, login working, getting some data working

This commit is contained in:
Jeena 2013-09-18 01:58:35 +02:00
parent 876cf8f268
commit 6caee6de79
6 changed files with 276 additions and 33 deletions

View file

@ -11,6 +11,7 @@
<script src="js/App.js"></script>
<script src="js/TinyTinyRSS.js"></script>
<script src="js/OwnCloud.js"></script>
<script src="js/Login.js"></script>
<script src="js/jester.js"></script>
</head>

View file

@ -13,7 +13,7 @@ App.prototype.authenticate = function() {
};
App.prototype.after_login = function() {
App.prototype.after_login = function(backend) {
var request = window.navigator.mozApps.getSelf();
request.onsuccess = function() {
@ -92,12 +92,16 @@ App.prototype.after_login = function() {
this.changeToPage("#list");
this.ttrss = new TinyTinyRSS(this, localStorage.server_url, localStorage.session_id);
if(backend == "TinyTinyRSS") {
this.backend = new TinyTinyRSS(this, localStorage.server_url, localStorage.session_id);
} else if(backend == "OwnCloud") {
this.backend = new OwnCloud(this, localStorage.server_url, localStorage.session_id);
}
this.reload();
};
App.prototype.logout = function() {
this.ttrss.logOut();
this.backend.logOut();
this.unread_articles = [];
this.populateList();
this.login.log_out();
@ -131,11 +135,13 @@ App.prototype.setColor = function(color) {
App.prototype.reload = function() {
this.unread_articles = [];
$("#all-read").innerHTML = "❌";
this.ttrss.getUnreadFeeds(this.gotUnreadFeeds.bind(this));
this.backend.getUnreadFeeds(this.gotUnreadFeeds.bind(this));
};
App.prototype.gotUnreadFeeds = function(new_articles) {
console.log(JSON.stringify(new_articles.length))
if(new_articles == null || !this.validate(new_articles)) { // on error load the saved unread articles.
var old_articles = localStorage.unread_articles;
@ -149,7 +155,7 @@ App.prototype.gotUnreadFeeds = function(new_articles) {
this.unread_articles = this.unread_articles.concat(new_articles);
if(new_articles.length > 0) {
this.ttrss.getUnreadFeeds(this.gotUnreadFeeds.bind(this), this.unread_articles.length);
this.backend.getUnreadFeeds(this.gotUnreadFeeds.bind(this), this.unread_articles);
} else {
localStorage.unread_articles = JSON.stringify(this.unread_articles);
this.populateList();
@ -312,7 +318,7 @@ App.prototype.setCurrentRead = function() {
if(!article.set_unread) {
article.unread = false;
this.updateList();
this.ttrss.setArticleRead(article.id);
this.backend.setArticleRead(article.id);
}
article.set_unread = false;
@ -335,7 +341,7 @@ App.prototype.toggleCurrentUnread = function() {
}
this.updateList();
this.ttrss.setArticleUnread(article.id);
this.backend.setArticleUnread(article.id);
};
App.prototype.toggleAllRead = function() {
@ -353,7 +359,7 @@ App.prototype.toggleAllRead = function() {
this.updateList();
this.ttrss.setArticleRead(ids.join(","));
this.backend.setArticleRead(ids.join(","));
} else {
@ -367,7 +373,7 @@ App.prototype.toggleAllRead = function() {
$("#all-read").innerHTML = "&#10060;";
this.updateList();
this.ttrss.setArticleUnread(ids.join(","));
this.backend.setArticleUnread(ids.join(","));
}
};
@ -379,13 +385,13 @@ App.prototype.toggleStarred = function() {
if(!article.marked) {
article.marked = true;
this.updateList();
this.ttrss.setArticleStarred(article.id);
this.backend.setArticleStarred(article.id);
$("#setstarred").innerHTML = "&#9733;";
}
else {
article.marked = false;
this.updateList();
this.ttrss.setArticleUnStarred(article.id);
this.backend.setArticleUnStarred(article.id);
$("#setstarred").innerHTML = "&#9734;";
}

View file

@ -1,11 +1,10 @@
function Login(app) {
this.app = app;
if(!this.is_logged_in()) {
this.log_in();
if(!this.onLine()) alert("You need to be on line to log in to your server.");
}
else this.app.after_login();
else this.app.after_login(localStorage.backend);
};
Login.prototype.onLine = function() {
@ -13,7 +12,7 @@ Login.prototype.onLine = function() {
};
Login.prototype.is_logged_in = function() {
return localStorage.server_url && localStorage.session_id;
return localStorage.backend && localStorage.server_url && localStorage.session_id;
};
Login.prototype.log_in = function() {
@ -45,6 +44,24 @@ Login.prototype.authenticate = function(e) {
}
var _this = this;
if(true) {
OwnCloud.login(server_url, user, password, function(data) {
if(data.version) {
var auth = btoa(user + ':' + password);
localStorage.server_url = server_url;
localStorage.session_id = auth;
localStorage.backend = "OwnCloud";
_this.app.after_login(localStorage.backend);
$("#url").value = "";
$("#un").value = "";
$("#pw").value = "";
} else {
alert("Something went wrong, please check every input field and try again.");
}
});
} else {
TinyTinyRSS.login(server_url, user, password, function(data) {
if(data.error) {
if(data.error == "API_DISABLED") {
@ -58,13 +75,15 @@ Login.prototype.authenticate = function(e) {
} else {
localStorage.server_url = server_url;
localStorage.session_id = data.session_id;
_this.app.after_login();
localStorage.backend = "TinyTinyRSS";
_this.app.after_login(localStorage.backend);
$("#url").value = "";
$("#un").value = "";
$("#pw").value = "";
}
});
}
return false;
};

200
js/OwnCloud.js Normal file
View file

@ -0,0 +1,200 @@
function OwnCloud(app, server_url, user_pass_btoa) {
this.app = app;
this.server_url = server_url;
this.session_id = user_pass_btoa;
window.addEventListener("offline", this.onoffline.bind(this));
window.addEventListener("online", this.ononline.bind(this));
}
OwnCloud.prototype.onoffline = function() {
// Do nothing
};
OwnCloud.prototype.ononline = function() {
var read_articles = localStorage.read_articles;
if (read_articles ) {
read_articles = JSON.parse(localStorage.read_articles);
this.setArticleRead(read_articles.join(","), function() {
localStorage.read_articles = null;
});
}
var unread_articles = localStorage.unread_articles;
if (unread_articles) {
unread_articles = JSON.parse(unread_articles);
this.setArticleUnread(unread_articles.join(","), function() {
localStorage.unread_articles();
});
}
};
OwnCloud.prototype.doOperation = function(method, operation, new_options, callback) {
if(!navigator.onLine) {
callback(null);
return;
}
var url = this.server_url + "/index.php/apps/news/api/v1-2/" + operation;
var options = {};
for (var key in new_options) {
options[key] = new_options[key];
}
if(method == "GET" || method == "HEAD") {
var a = [];
for(var key in options) {
a.push(key + "=" + options[key]);
}
url += "?" + a.join("&");
}
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if(xhr.readyState == 4) {
if(xhr.status == 200) {
if(callback)
callback(JSON.parse(xhr.responseText));
} else {
if(xhr.status != 0) alert("error: " + xhr.status + " " + xhr.statusText);
if(callback) callback(null);
}
}
}
xhr.open(method, url, true);
xhr.withCredentials = true;
//var auth = btoa(user + ':' + password);
xhr.setRequestHeader('Authorization', 'Basic ' + this.session_id);
xhr.send(JSON.stringify(options));
}
OwnCloud.prototype.getUnreadFeeds = function(callback, skip) {
if(skip) {
skip = skip[skip.length - 1].id;
}
var options = {
batchSize: 700,
offset: skip || 0,
type: 3,
id: 0,
getRead: false
};
var _this = this;
this.doOperation("GET", "items", options, function(data) {
var items = data.items;
// FIXME
var feeds = {};
for (var i = 0; i < data.feeds.length; i++) {
var feed = data.feeds[i];
feeds[feed.id] = feed;
}
callback(items.map(_this.normalize_article, feeds));
});
}
OwnCloud.prototype.setArticleRead = function(article_id) {
var options = {
article_ids: article_id,
mode: 0,
field: 2
};
if (navigator.onLine) {
this.doOperation("updateArticle", options);
} else {
var read_articles = localStorage.read_articles;
if(typeof read_articles !== "undefined") read_articles = JSON.parse(read_articles);
else read_articles = [];
read_articles.push(article_id);
localStorage.read_articles = JSON.stringify(read_articles);
}
};
OwnCloud.prototype.setArticleStarred = function(article_id) {
var options = {
article_ids: article_id,
mode: 1,
field: 0
};
if (navigator.onLine) {
this.doOperation("updateArticle", options);
}
};
OwnCloud.prototype.setArticleUnStarred = function(article_id) {
var options = {
article_ids: article_id,
mode: 0,
field: 0
};
if (navigator.onLine) {
this.doOperation("updateArticle", options);
}
};
OwnCloud.prototype.setArticleUnread = function(article_id) {
var options = {
article_ids: article_id,
mode: 1,
field: 2
};
if (navigator.onLine) this.doOperation("updateArticle", options);
else {
var unread_articles = localStorage.unread_articles;
if (typeof unread_articles !== "undefined") unread_articles = JSON.parse(unread_articles);
else unread_articles = [];
unread_articles.push(article_id);
localStorage.unread_articles = JSON.stringify(unread_articles);
}
};
OwnCloud.prototype.normalize_article = function(article) {
var feeds = this;
return {
id: article.id,
title: article.title,
content: article.body,
feed_title: feeds[article.feedId].title,
excerpt: article.body.stripHTML().substring(0, 50),
updated: article.pubDate,
link: article.link,
marked: article.starred,
unread: article.unread
}
};
OwnCloud.prototype.logOut = function() {
this.doOperation("logout");
};
OwnCloud.login = function(server_url, user, password, callback) {
var url = server_url + "/index.php/apps/news/api/v1-2/version";
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
console.log(xhr.status)
if(xhr.readyState == 4) {
if(xhr.status == 200) {
callback(JSON.parse(xhr.responseText))
} else {
alert("error: " + xhr.status + " " + xhr.statusText)
}
}
}
xhr.open("GET", url, true);
xhr.withCredentials = true;
var auth = btoa(user + ':' + password);
xhr.setRequestHeader('Authorization', 'Basic ' + auth);
xhr.send();
}

View file

@ -63,6 +63,7 @@ TinyTinyRSS.prototype.doOperation = function(operation, new_options, callback) {
}
TinyTinyRSS.prototype.getUnreadFeeds = function(callback, skip) {
skip = skip.length;
var options = {
show_excerpt: false,
view_mode: "unread",

View file

@ -34,5 +34,21 @@ Node.prototype.removeClass = function(cls) {
}
};
var __entityMap = {
"&": "&amp;",
"<": "&lt;",
">": "&gt;"
};
String.prototype.escapeHTML = function() {
return String(this).replace(/[&<>]/g, function (s) {
return __entityMap[s];
});
}
String.prototype.stripHTML = function() {
return this.replace(/(<([^>]+)>)/ig, "");
}
if(!window.app) window.app = new App();