Sync deletions that happened on the server

pull/1/head
Grégory Oestreicher 2016-12-17 18:29:03 +01:00
parent 5e7fc2dea1
commit 3dbcc9774c
3 changed files with 104 additions and 1 deletions

View File

@ -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();

View File

@ -113,8 +113,12 @@ Page {
text: qsTr( "Refresh" )
onClicked: {
articlesModel.loaded = false
server.syncDeletedArticles(
function() {
server.getUpdatedArticles()
}
)
}
}
}

View File

@ -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 ) {