From 2f2b87049666c3472142931f96d5e7a89dbb218e Mon Sep 17 00:00:00 2001 From: Mo Bitar Date: Mon, 31 Jul 2017 14:46:38 -0500 Subject: [PATCH] Handle sync conflicts --- .../app/frontend/controllers/home.js | 1 - .../javascripts/app/services/syncManager.js | 47 ++++++++----------- 2 files changed, 20 insertions(+), 28 deletions(-) diff --git a/app/assets/javascripts/app/frontend/controllers/home.js b/app/assets/javascripts/app/frontend/controllers/home.js index 4f7dc8a93..7ea67b38e 100644 --- a/app/assets/javascripts/app/frontend/controllers/home.js +++ b/app/assets/javascripts/app/frontend/controllers/home.js @@ -57,7 +57,6 @@ angular.module('app.frontend') */ $scope.updateTagsForNote = function(note, stringTags) { - console.log("Updating tags", stringTags); var toRemove = []; for(var tag of note.tags) { if(stringTags.indexOf(tag.title) === -1) { diff --git a/app/assets/javascripts/app/services/syncManager.js b/app/assets/javascripts/app/services/syncManager.js index 1a58dd348..0d2e8efef 100644 --- a/app/assets/javascripts/app/services/syncManager.js +++ b/app/assets/javascripts/app/services/syncManager.js @@ -272,6 +272,12 @@ class SyncManager { } } + handleItemsResponse(responseItems, omitFields) { + EncryptionHelper.decryptMultipleItems(responseItems, this.authManager.keys()); + var items = this.modelManager.mapResponseItemsToLocalModelsOmittingFields(responseItems, omitFields); + return items; + } + handleUnsavedItemsResponse(unsaved) { if(unsaved.length == 0) { return; @@ -284,11 +290,25 @@ class SyncManager { if (i < unsaved.length) { var mapping = unsaved[i]; var itemResponse = mapping.item; + EncryptionHelper.decryptMultipleItems([itemResponse], this.authManager.keys()); var item = this.modelManager.findItem(itemResponse.uuid); + if(!item) { + // could be deleted + return; + } var error = mapping.error; if(error.tag == "uuid_conflict") { // uuid conflicts can occur if a user attempts to import an old data archive with uuids from the old account into a new account this.modelManager.alternateUUIDForItem(item, handleNext); + } else if(error.tag === "sync_conflict") { + // create a new item with the same contents of this item if the contents differ + itemResponse.uuid = null; // we want a new uuid for the new item + var dup = this.modelManager.createItem(itemResponse); + if(!itemResponse.deleted && JSON.stringify(item.structureParams()) !== JSON.stringify(dup.structureParams())) { + this.modelManager.addItem(dup); + dup.conflict_of = item.uuid; + dup.setDirty(true); + } } ++i; } else { @@ -299,33 +319,6 @@ class SyncManager { handleNext(); } - handleItemsResponse(responseItems, omitFields) { - EncryptionHelper.decryptMultipleItems(responseItems, this.authManager.keys()); - var items = this.modelManager.mapResponseItemsToLocalModelsOmittingFields(responseItems, omitFields); - this.handleSyncConflicts(items); - return items; - } - - handleSyncConflicts(items) { - var needsSync = false; - for(var item of items) { - if(item.conflict_of) { - var original = this.modelManager.findItem(item.conflict_of); - // check if item contents are equal. If so, automatically delete conflict - if(JSON.stringify(item.structureParams()) === JSON.stringify(original.structureParams())) { - this.modelManager.setItemToBeDeleted(item); - needsSync = true; - } - } - } - - if(needsSync) { - setTimeout(function () { - this.sync(); - }.bind(this), 100); - } - } - clearSyncToken() { localStorage.removeItem("syncToken"); }