From 3dbcc9774c8f49bf32d86e0deed0d6ec0ed0b4ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Oestreicher?= Date: Sat, 17 Dec 2016 18:29:03 +0100 Subject: [PATCH] Sync deletions that happened on the server --- qml/js/WallaBase.js | 76 ++++++++++++++++++++++++++++++++++++++++ qml/pages/ServerPage.qml | 6 +++- qml/types/Server.qml | 23 ++++++++++++ 3 files changed, 104 insertions(+), 1 deletion(-) diff --git a/qml/js/WallaBase.js b/qml/js/WallaBase.js index 8458e26..f4feccf 100644 --- a/qml/js/WallaBase.js +++ b/qml/js/WallaBase.js @@ -277,6 +277,82 @@ function _sendAuthRequest( props, cb ) Articles management */ +function syncDeletedArticles( timerSource, props, cb ) +{ + var db = getDatabase(); + + db.readTransaction( + function( tx ) { + var res = tx.executeSql( "SELECT id, url FROM articles WHERE server=?", [ props.id ] ); + var articles = new Array; + + for ( var i = 0; i < res.rows.length; ++i ) { + articles.push( res.rows[i] ); + } + + var working = false; + + function processArticlesList() { + if ( articles.length === 0 ) { + cb(); + } + else { + if ( !working ) + timerSource.setTimeout( _checkNextArticle, 100 ); + timerSource.setTimeout( processArticlesList, 500 ); + } + } + + function _checkNextArticle() { + working = true; + var article = articles.pop(); + + var url = props.url; + if ( url.charAt( url.length - 1 ) !== "/" ) + url += "/"; + url += "api/entries/exists.json"; + + var params = "url="; + params += encodeURIComponent( article.url ); + url += "?" + params; + + var http = new XMLHttpRequest; + + http.onreadystatechange = function() { + if ( http.readyState === XMLHttpRequest.DONE ) { + console.debug( "Checking if article " + article.id + " exists, response status is " + http.status ); + var json = null; + + if ( http.status === 200 ) { + try { + json = JSON.parse( http.responseText ) + } + catch( e ) { + json = null; + } + + if ( !json.exists ) { + console.debug( "Article " + article.id + " has been deleted" ); + deleteArticle( props.id, article.id ); + } + } + // In case of error let's assume that the article exists + + working = false; + } + }; + + http.open( "GET", url, true ); + http.setRequestHeader( "Authorization:", "Bearer " + props.token ); + + http.send(); + } + + timerSource.setTimeout( processArticlesList, 500 ); + } + ); +} + function getArticles( serverId, cb, filter ) { var db = getDatabase(); diff --git a/qml/pages/ServerPage.qml b/qml/pages/ServerPage.qml index 6d351f3..118fe7f 100644 --- a/qml/pages/ServerPage.qml +++ b/qml/pages/ServerPage.qml @@ -113,7 +113,11 @@ Page { text: qsTr( "Refresh" ) onClicked: { articlesModel.loaded = false - server.getUpdatedArticles() + server.syncDeletedArticles( + function() { + server.getUpdatedArticles() + } + ) } } } diff --git a/qml/types/Server.qml b/qml/types/Server.qml index 80a70b2..2012fcc 100644 --- a/qml/types/Server.qml +++ b/qml/types/Server.qml @@ -25,6 +25,8 @@ import harbour.wallaread 1.0 import "../js/WallaBase.js" as WallaBase Item { + id: server + property int serverId: -1 property string name property string url @@ -58,6 +60,14 @@ Item { id: httpRequester } + function setTimeout( cb, ms ) { + var timer = Qt.createQmlObject( "import QtQuick 2.0; Timer {}", server ); + timer.repeat = false + timer.interval = ms + timer.triggered.connect( function() { cb(); timer.destroy(); } ) + timer.start() + } + function onServerLoaded( props, err ) { if ( err !== null ) { error( qsTr( "Failed to load server information: " ) + err ) @@ -101,6 +111,19 @@ Item { return tokenExpiry > Math.floor( (new Date).getTime() / 1000 ) } + function syncDeletedArticles( cb ) { + connect( + function( err ) { + if ( err !== null ) { + error( qsTr( "Failed to connect to server: " ) + err ) + } + else { + WallaBase.syncDeletedArticles( server, { id: serverId, token: accessToken, url: url }, function() { cb(); } ) + } + } + ) + } + function getUpdatedArticles() { connect( function( err ) {