handle uuid conflicts

This commit is contained in:
Mo Bitar
2017-01-29 11:36:20 -06:00
parent 48ea5dc22c
commit 2274e6835d
5 changed files with 39 additions and 21 deletions

View File

@@ -50,10 +50,6 @@ class Item {
}
}
alternateUUID() {
this.uuid = Neeto.crypto.generateUUID();
}
setDirty(dirty) {
this.dirty = dirty;

View File

@@ -98,11 +98,12 @@ class DBManager {
}, null)
}
deleteItem(item) {
deleteItem(item, callback) {
this.openDatabase((db) => {
var request = db.transaction("items", "readwrite").objectStore("items").delete(item.uuid);
request.onsuccess = function(event) {
console.log("Successfully deleted item", item.uuid);
callback(true);
};
}, null)
}

View File

@@ -221,8 +221,10 @@ class AccountMenu {
items: items
}
// auth params are only needed when encrypted with a standard file key
data["auth_params"] = authManager.getAuthParams();
if(ek) {
// auth params are only needed when encrypted with a standard file key
data["auth_params"] = authManager.getAuthParams();
}
return makeTextFile(JSON.stringify(data, null, 2 /* pretty print */));
}

View File

@@ -22,6 +22,18 @@ class ModelManager {
})
}
alternateUUIDForItem(item, callback) {
// we need to clone this item and give it a new uuid, then delete item with old uuid from db (you can't mofidy uuid's in our indexeddb setup)
var newItem = this.createItem(item);
newItem.uuid = Neeto.crypto.generateUUID();
this.removeItemLocally(item, function(){
this.addItem(newItem);
newItem.setDirty(true);
newItem.markAllReferencesDirty();
callback();
}.bind(this));
}
allItemsMatchingTypes(contentTypes) {
return this.items.filter(function(item){
return (_.includes(contentTypes, item.content_type) || _.includes(contentTypes, "*")) && !item.dummy;
@@ -213,7 +225,7 @@ class ModelManager {
item.removeAllRelationships();
}
removeItemLocally(item) {
removeItemLocally(item, callback) {
_.pull(this.items, item);
item.isBeingRemovedLocally();
@@ -227,7 +239,7 @@ class ModelManager {
_.pull(this._extensions, item);
}
this.dbManager.deleteItem(item);
this.dbManager.deleteItem(item, callback);
}
/*

View File

@@ -50,7 +50,7 @@ class SyncManager {
this.modelManager.removeItemLocally(item);
}
}
if(callback) {
callback({success: true});
}
@@ -202,18 +202,25 @@ class SyncManager {
}
console.log("Handle unsaved", unsaved);
for(var mapping of unsaved) {
var itemResponse = mapping.item;
var item = this.modelManager.findItem(itemResponse.uuid);
var error = mapping.error;
if(error.tag == "uuid_conflict") {
item.alternateUUID();
item.setDirty(true);
item.markAllReferencesDirty();
}
}
this.sync(null, {additionalFields: ["created_at", "updated_at"]});
var i = 0;
var handleNext = function() {
if (i < unsaved.length) {
var mapping = unsaved[i];
var itemResponse = mapping.item;
var item = this.modelManager.findItem(itemResponse.uuid);
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 form the old account into a new account
this.modelManager.alternateUUIDForItem(item, handleNext);
}
++i;
} else {
this.sync(null, {additionalFields: ["created_at", "updated_at"]});
}
}.bind(this);
handleNext();
}
handleItemsResponse(responseItems, omitFields) {