Merge pull request #8 from standardnotes/sync

Sync
This commit is contained in:
Mo Bitar
2016-12-30 13:02:03 -06:00
committed by GitHub
9 changed files with 173 additions and 170 deletions

View File

@@ -207,8 +207,9 @@ angular.module('app.frontend')
var original = this.note.presentation_name; var original = this.note.presentation_name;
this.note.presentation_name = this.url.token; this.note.presentation_name = this.url.token;
modelManager.addDirtyItems([this.note]);
apiController.saveItems([this.note], function(response){ apiController.sync(function(response){
if(!response) { if(!response) {
this.note.presentation_name = original; this.note.presentation_name = original;
this.url.token = original; this.url.token = original;

View File

@@ -1,5 +1,5 @@
angular.module('app.frontend') angular.module('app.frontend')
.directive("header", function(){ .directive("header", function(apiController){
return { return {
restrict: 'E', restrict: 'E',
scope: { scope: {
@@ -13,7 +13,9 @@ angular.module('app.frontend')
bindToController: true, bindToController: true,
link:function(scope, elem, attrs, ctrl) { link:function(scope, elem, attrs, ctrl) {
scope.$on("sync:updated_token", function(){
ctrl.syncUpdated();
})
} }
} }
}) })
@@ -69,14 +71,18 @@ angular.module('app.frontend')
} }
} }
this.getLastRefreshDate = function() { this.refreshData = function() {
return apiController.lastRefreshDate; apiController.sync(function(response){
if(!response) {
alert("There was an error syncing. Please try again. If all else fails, log out and log back in.");
} else {
this.syncUpdated();
}
}.bind(this));
} }
this.refreshData = function() { this.syncUpdated = function() {
apiController.refreshItems(function(items){ this.lastSyncDate = new Date();
})
} }
this.loginSubmitPressed = function() { this.loginSubmitPressed = function() {

View File

@@ -9,16 +9,17 @@ angular.module('app.frontend')
$scope.tags = modelManager.tags; $scope.tags = modelManager.tags;
$scope.allTag.notes = modelManager.notes; $scope.allTag.notes = modelManager.notes;
// setInterval(function () { apiController.sync(null);
// apiController.refreshItems(null); // refresh every 30s
// }, 1000); setInterval(function () {
apiController.sync(null);
}, 30000);
// apiController.verifyEncryptionStatusOfAllItems($scope.defaultUser, function(success){}); // apiController.verifyEncryptionStatusOfAllItems($scope.defaultUser, function(success){});
} }
apiController.getCurrentUser(function(user){ apiController.getCurrentUser(function(user){
if(user) { if(user) {
// console.log("Get user response", user);
$scope.defaultUser = user; $scope.defaultUser = user;
$rootScope.title = "Notes — Standard Notes"; $rootScope.title = "Notes — Standard Notes";
onUserSet(); onUserSet();
@@ -51,7 +52,8 @@ angular.module('app.frontend')
} }
$scope.tagsSave = function(tag, callback) { $scope.tagsSave = function(tag, callback) {
apiController.saveItems([tag], callback); modelManager.addDirtyItems([tag]);
apiController.sync(callback);
} }
/* /*
@@ -65,7 +67,7 @@ angular.module('app.frontend')
modelManager.addTagToNote(newTag, originalNote); modelManager.addTagToNote(newTag, originalNote);
} }
apiController.saveDirtyItems(function(){}); apiController.sync(function(){});
} }
/* /*
@@ -109,7 +111,7 @@ angular.module('app.frontend')
$scope.saveNote = function(note, callback) { $scope.saveNote = function(note, callback) {
modelManager.addDirtyItems(note); modelManager.addDirtyItems(note);
apiController.saveDirtyItems(function(){ apiController.sync(function(){
note.hasChanges = false; note.hasChanges = false;
if(callback) { if(callback) {
@@ -126,14 +128,11 @@ angular.module('app.frontend')
$scope.selectedNote = null; $scope.selectedNote = null;
} }
$scope.updateAllTag();
if(note.dummy) { if(note.dummy) {
return; return;
} }
apiController.deleteItem(note, function(success){}) apiController.deleteItem(note, function(success){})
apiController.saveDirtyItems(function(){});
} }
/* /*

View File

@@ -20,11 +20,11 @@ angular.module('app.frontend')
} }
this.$get = function(Restangular, modelManager, ngDialog) { this.$get = function($rootScope, Restangular, modelManager, ngDialog) {
return new ApiController(Restangular, modelManager, ngDialog); return new ApiController($rootScope, Restangular, modelManager, ngDialog);
} }
function ApiController(Restangular, modelManager, ngDialog) { function ApiController($rootScope, Restangular, modelManager, ngDialog) {
this.setUser = function(user) { this.setUser = function(user) {
this.user = user; this.user = user;
@@ -74,11 +74,7 @@ angular.module('app.frontend')
return; return;
} }
Restangular.one("users/current").get().then(function(response){ Restangular.one("users/current").get().then(function(response){
var plain = response.plain(); var user = response.plain();
var items = plain.items;
this.decryptItems(items);
items = modelManager.mapResponseItemsToLocalModels(items);
var user = _.omit(plain, ["items"]);
callback(user); callback(user);
}.bind(this)) }.bind(this))
.catch(function(response){ .catch(function(response){
@@ -96,7 +92,6 @@ angular.module('app.frontend')
Neeto.crypto.computeEncryptionKeysForUser(_.merge({email: email, password: password}, authParams), function(keys){ Neeto.crypto.computeEncryptionKeysForUser(_.merge({email: email, password: password}, authParams), function(keys){
this.setMk(keys.mk); this.setMk(keys.mk);
var request = Restangular.one("auth/sign_in"); var request = Restangular.one("auth/sign_in");
console.log("sending pw", keys.pw);
var params = {password: keys.pw, email: email}; var params = {password: keys.pw, email: email};
_.merge(request, params); _.merge(request, params);
request.post().then(function(response){ request.post().then(function(response){
@@ -104,7 +99,6 @@ angular.module('app.frontend')
callback(response); callback(response);
}) })
.catch(function(response){ .catch(function(response){
console.log(response.data);
callback(response.data); callback(response.data);
}) })
}.bind(this)); }.bind(this));
@@ -123,7 +117,6 @@ angular.module('app.frontend')
callback(response); callback(response);
}) })
.catch(function(response){ .catch(function(response){
console.log(response.data);
callback(response.data); callback(response.data);
}) })
}.bind(this)); }.bind(this));
@@ -223,48 +216,42 @@ angular.module('app.frontend')
Items Items
*/ */
this.saveDirtyItems = function(callback) { this.syncWithOptions = function(callback, options = {}) {
var dirtyItems = modelManager.dirtyItems;
if(dirtyItems.length == 0) {
callback();
return;
}
this.saveItems(dirtyItems, function(response){
modelManager.clearDirtyItems();
callback();
})
}
this.refreshItems = function(callback) {
var request = Restangular.one("users", this.user.uuid).one("items");
request.get(this.lastRefreshDate ? {"updated_after" : this.lastRefreshDate.toString()} : {})
.then(function(response){
this.lastRefreshDate = new Date();
var items = this.handleItemsResponse(response.items, null);
callback(items);
}.bind(this))
.catch(function(response) {
callback(response.data);
})
}
this.saveItems = function(items, callback) {
if(!this.user.uuid) { if(!this.user.uuid) {
this.writeItemsToLocalStorage(); this.writeItemsToLocalStorage();
callback(); callback();
return; return;
} }
var request = Restangular.one("users", this.user.uuid).one("items");
request.items = _.map(items, function(item){ var dirtyItems = modelManager.dirtyItems;
return this.createRequestParamsForItem(item); var request = Restangular.one("items/sync");
request.items = _.map(dirtyItems, function(item){
return this.createRequestParamsForItem(item, options.additionalFields);
}.bind(this)); }.bind(this));
if(this.syncToken) {
request.sync_token = this.syncToken;
}
request.post().then(function(response) { request.post().then(function(response) {
var omitFields = ["content", "enc_item_key", "auth_hash"]; modelManager.clearDirtyItems();
this.handleItemsResponse(response.items, omitFields); this.syncToken = response.sync_token;
callback(response); $rootScope.$broadcast("sync:updated_token", this.syncToken);
this.handleItemsResponse(response.retrieved_items, null);
// merge only metadata for saved items
this.handleItemsResponse(response.saved_items, ["content", "enc_item_key", "auth_hash"]);
if(callback) {
callback(response);
}
}.bind(this)) }.bind(this))
.catch(function(response){
console.log("Sync error: ", response);
callback(null);
})
}
this.sync = function(callback) {
this.syncWithOptions(callback, undefined);
} }
this.handleItemsResponse = function(responseItems, omitFields) { this.handleItemsResponse = function(responseItems, omitFields) {
@@ -272,14 +259,14 @@ angular.module('app.frontend')
return modelManager.mapResponseItemsToLocalModelsOmittingFields(responseItems, omitFields); return modelManager.mapResponseItemsToLocalModelsOmittingFields(responseItems, omitFields);
} }
this.createRequestParamsForItem = function(item) { this.createRequestParamsForItem = function(item, additionalFields) {
return this.paramsForItem(item, !item.isPublic(), null, false); return this.paramsForItem(item, !item.isPublic(), additionalFields, false);
} }
this.paramsForItem = function(item, encrypted, additionalFields, forExportFile) { this.paramsForItem = function(item, encrypted, additionalFields, forExportFile) {
var itemCopy = _.cloneDeep(item); var itemCopy = _.cloneDeep(item);
var params = {uuid: item.uuid, content_type: item.content_type, presentation_name: item.presentation_name}; var params = {uuid: item.uuid, content_type: item.content_type, presentation_name: item.presentation_name, deleted: item.deleted};
itemCopy.content.references = _.map(itemCopy.content.references, function(reference){ itemCopy.content.references = _.map(itemCopy.content.references, function(reference){
return {uuid: reference.uuid, content_type: reference.content_type}; return {uuid: reference.uuid, content_type: reference.content_type};
@@ -308,15 +295,9 @@ angular.module('app.frontend')
this.deleteItem = function(item, callback) { this.deleteItem = function(item, callback) {
if(!this.user.uuid) { item.deleted = true;
this.writeItemsToLocalStorage(); modelManager.addDirtyItems([item]);
callback(true); this.sync(callback);
} else {
Restangular.one("users", this.user.uuid).one("items", item.uuid).remove()
.then(function(response) {
callback(true);
})
}
} }
this.shareItem = function(item, callback) { this.shareItem = function(item, callback) {
@@ -328,7 +309,8 @@ angular.module('app.frontend')
var shareFn = function() { var shareFn = function() {
item.presentation_name = "_auto_"; item.presentation_name = "_auto_";
var needsUpdate = [item].concat(item.referencesAffectedBySharingChange() || []); var needsUpdate = [item].concat(item.referencesAffectedBySharingChange() || []);
this.saveItems(needsUpdate, function(success){}) modelManager.addDirtyItems(needsUpdate);
this.sync();
}.bind(this) }.bind(this)
if(!this.user.username) { if(!this.user.username) {
@@ -352,7 +334,8 @@ angular.module('app.frontend')
this.unshareItem = function(item, callback) { this.unshareItem = function(item, callback) {
item.presentation_name = null; item.presentation_name = null;
var needsUpdate = [item].concat(item.referencesAffectedBySharingChange() || []); var needsUpdate = [item].concat(item.referencesAffectedBySharingChange() || []);
this.saveItems(needsUpdate, function(success){}) modelManager.addDirtyItems(needsUpdate);
this.sync(null);
} }
/* /*
@@ -361,12 +344,9 @@ angular.module('app.frontend')
this.importJSONData = function(jsonString, callback) { this.importJSONData = function(jsonString, callback) {
var data = JSON.parse(jsonString); var data = JSON.parse(jsonString);
var customModelManager = new ModelManager(); modelManager.mapResponseItemsToLocalModels(data.items);
customModelManager.mapResponseItemsToLocalModels(data.items); modelManager.addDirtyItems(modelManager.items);
console.log("Importing data", JSON.parse(jsonString)); this.syncWithOptions(callback, {additionalFields: ["created_at", "updated_at"]});
this.saveItems(customModelManager.items, function(response){
callback(response);
});
} }
/* /*
@@ -391,7 +371,7 @@ angular.module('app.frontend')
}.bind(this); }.bind(this);
var items = _.map(modelManager.items, function(item){ var items = _.map(modelManager.items, function(item){
return this.paramsForItem(item, false, ["created_at", "updated_at"], true) return _.omit(this.paramsForItem(item, false, ["created_at", "updated_at"], true), ["deleted"]);
}.bind(this)); }.bind(this));
var data = { var data = {

View File

@@ -21,7 +21,6 @@ class ItemManager {
} }
mapResponseItemsToLocalModelsOmittingFields(items, omitFields) { mapResponseItemsToLocalModelsOmittingFields(items, omitFields) {
console.log("map response items", items);
var models = [] var models = []
for (var json_obj of items) { for (var json_obj of items) {
json_obj = _.omit(json_obj, omitFields || []) json_obj = _.omit(json_obj, omitFields || [])

View File

@@ -72,10 +72,22 @@ class ModelManager extends ItemManager {
this.addDirtyItems(dirty); this.addDirtyItems(dirty);
} }
deleteItem(item) {
var dirty = super.deleteItem(item);
if(item.content_type == "Note") {
_.remove(this.notes, item);
} else if(item.content_type == "Tag") {
_.remove(this.tags, item);
}
return dirty;
}
deleteNote(note) { deleteNote(note) {
var dirty = this.deleteItem(note); var dirty = this.deleteItem(note);
_.remove(this.notes, note); _.remove(this.notes, note);
this.addDirtyItems(dirty); if(!note.dummy) {
this.addDirtyItems(dirty);
}
} }
deleteTag(tag) { deleteTag(tag) {

View File

@@ -93,7 +93,7 @@
.menu.right .menu.right
.items .items
.item.last-refreshed{"ng-if" => "ctrl.getLastRefreshDate()"} .item.last-refreshed{"ng-if" => "ctrl.lastSyncDate"}
Last refreshed {{ctrl.getLastRefreshDate() | appDateTime}} Last refreshed {{ctrl.lastSyncDate | appDateTime}}
.item{"ng-click" => "ctrl.refreshData()"} .item{"ng-click" => "ctrl.refreshData()"}
Refresh Refresh

View File

@@ -641,8 +641,9 @@ angular.module('app.frontend').controller('BaseCtrl', BaseCtrl);
var original = this.note.presentation_name; var original = this.note.presentation_name;
this.note.presentation_name = this.url.token; this.note.presentation_name = this.url.token;
modelManager.addDirtyItems([this.note]);
apiController.saveItems([this.note], function (response) { apiController.sync(function (response) {
if (!response) { if (!response) {
this.note.presentation_name = original; this.note.presentation_name = original;
this.url.token = original; this.url.token = original;
@@ -696,7 +697,7 @@ angular.module('app.frontend').controller('BaseCtrl', BaseCtrl);
this.focusEditor(100); this.focusEditor(100);
}; };
}); });
;angular.module('app.frontend').directive("header", function () { ;angular.module('app.frontend').directive("header", function (apiController) {
return { return {
restrict: 'E', restrict: 'E',
scope: { scope: {
@@ -709,7 +710,11 @@ angular.module('app.frontend').controller('BaseCtrl', BaseCtrl);
controllerAs: 'ctrl', controllerAs: 'ctrl',
bindToController: true, bindToController: true,
link: function link(scope, elem, attrs, ctrl) {} link: function link(scope, elem, attrs, ctrl) {
scope.$on("sync:updated_token", function () {
ctrl.syncUpdated();
});
}
}; };
}).controller('HeaderCtrl', function ($state, apiController, modelManager, serverSideValidation, $timeout) { }).controller('HeaderCtrl', function ($state, apiController, modelManager, serverSideValidation, $timeout) {
@@ -760,12 +765,18 @@ angular.module('app.frontend').controller('BaseCtrl', BaseCtrl);
} }
}; };
this.getLastRefreshDate = function () { this.refreshData = function () {
return apiController.lastRefreshDate; apiController.sync(function (response) {
if (!response) {
alert("There was an error syncing. Please try again. If all else fails, log out and log back in.");
} else {
this.syncUpdated();
}
}.bind(this));
}; };
this.refreshData = function () { this.syncUpdated = function () {
apiController.refreshItems(function (items) {}); this.lastSyncDate = new Date();
}; };
this.loginSubmitPressed = function () { this.loginSubmitPressed = function () {
@@ -871,16 +882,17 @@ angular.module('app.frontend').controller('BaseCtrl', BaseCtrl);
$scope.tags = modelManager.tags; $scope.tags = modelManager.tags;
$scope.allTag.notes = modelManager.notes; $scope.allTag.notes = modelManager.notes;
// setInterval(function () { apiController.sync(null);
// apiController.refreshItems(null); // refresh every 30s
// }, 1000); setInterval(function () {
apiController.sync(null);
}, 30000);
// apiController.verifyEncryptionStatusOfAllItems($scope.defaultUser, function(success){}); // apiController.verifyEncryptionStatusOfAllItems($scope.defaultUser, function(success){});
}; };
apiController.getCurrentUser(function (user) { apiController.getCurrentUser(function (user) {
if (user) { if (user) {
// console.log("Get user response", user);
$scope.defaultUser = user; $scope.defaultUser = user;
$rootScope.title = "Notes — Standard Notes"; $rootScope.title = "Notes — Standard Notes";
onUserSet(); onUserSet();
@@ -913,7 +925,8 @@ angular.module('app.frontend').controller('BaseCtrl', BaseCtrl);
}; };
$scope.tagsSave = function (tag, callback) { $scope.tagsSave = function (tag, callback) {
apiController.saveItems([tag], callback); modelManager.addDirtyItems([tag]);
apiController.sync(callback);
}; };
/* /*
@@ -927,7 +940,7 @@ angular.module('app.frontend').controller('BaseCtrl', BaseCtrl);
modelManager.addTagToNote(newTag, originalNote); modelManager.addTagToNote(newTag, originalNote);
} }
apiController.saveDirtyItems(function () {}); apiController.sync(function () {});
}; };
/* /*
@@ -971,7 +984,7 @@ angular.module('app.frontend').controller('BaseCtrl', BaseCtrl);
$scope.saveNote = function (note, callback) { $scope.saveNote = function (note, callback) {
modelManager.addDirtyItems(note); modelManager.addDirtyItems(note);
apiController.saveDirtyItems(function () { apiController.sync(function () {
note.hasChanges = false; note.hasChanges = false;
if (callback) { if (callback) {
@@ -988,14 +1001,11 @@ angular.module('app.frontend').controller('BaseCtrl', BaseCtrl);
$scope.selectedNote = null; $scope.selectedNote = null;
} }
$scope.updateAllTag();
if (note.dummy) { if (note.dummy) {
return; return;
} }
apiController.deleteItem(note, function (success) {}); apiController.deleteItem(note, function (success) {});
apiController.saveDirtyItems(function () {});
}; };
/* /*
@@ -1547,11 +1557,11 @@ var User = function User(json_obj) {
return url; return url;
}; };
this.$get = function (Restangular, modelManager, ngDialog) { this.$get = function ($rootScope, Restangular, modelManager, ngDialog) {
return new ApiController(Restangular, modelManager, ngDialog); return new ApiController($rootScope, Restangular, modelManager, ngDialog);
}; };
function ApiController(Restangular, modelManager, ngDialog) { function ApiController($rootScope, Restangular, modelManager, ngDialog) {
this.setUser = function (user) { this.setUser = function (user) {
this.user = user; this.user = user;
@@ -1599,11 +1609,7 @@ var User = function User(json_obj) {
return; return;
} }
Restangular.one("users/current").get().then(function (response) { Restangular.one("users/current").get().then(function (response) {
var plain = response.plain(); var user = response.plain();
var items = plain.items;
this.decryptItems(items);
items = modelManager.mapResponseItemsToLocalModels(items);
var user = _.omit(plain, ["items"]);
callback(user); callback(user);
}.bind(this)).catch(function (response) { }.bind(this)).catch(function (response) {
console.log("Error getting current user", response); console.log("Error getting current user", response);
@@ -1620,14 +1626,12 @@ var User = function User(json_obj) {
Neeto.crypto.computeEncryptionKeysForUser(_.merge({ email: email, password: password }, authParams), function (keys) { Neeto.crypto.computeEncryptionKeysForUser(_.merge({ email: email, password: password }, authParams), function (keys) {
this.setMk(keys.mk); this.setMk(keys.mk);
var request = Restangular.one("auth/sign_in"); var request = Restangular.one("auth/sign_in");
console.log("sending pw", keys.pw);
var params = { password: keys.pw, email: email }; var params = { password: keys.pw, email: email };
_.merge(request, params); _.merge(request, params);
request.post().then(function (response) { request.post().then(function (response) {
localStorage.setItem("jwt", response.token); localStorage.setItem("jwt", response.token);
callback(response); callback(response);
}).catch(function (response) { }).catch(function (response) {
console.log(response.data);
callback(response.data); callback(response.data);
}); });
}.bind(this)); }.bind(this));
@@ -1645,7 +1649,6 @@ var User = function User(json_obj) {
localStorage.setItem("jwt", response.token); localStorage.setItem("jwt", response.token);
callback(response); callback(response);
}).catch(function (response) { }).catch(function (response) {
console.log(response.data);
callback(response.data); callback(response.data);
}); });
}.bind(this)); }.bind(this));
@@ -1742,46 +1745,43 @@ var User = function User(json_obj) {
Items Items
*/ */
this.saveDirtyItems = function (callback) { this.syncWithOptions = function (callback) {
var dirtyItems = modelManager.dirtyItems; var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (dirtyItems.length == 0) {
callback();
return;
}
this.saveItems(dirtyItems, function (response) {
modelManager.clearDirtyItems();
callback();
});
};
this.refreshItems = function (callback) {
var request = Restangular.one("users", this.user.uuid).one("items");
request.get(this.lastRefreshDate ? { "updated_after": this.lastRefreshDate.toString() } : {}).then(function (response) {
this.lastRefreshDate = new Date();
var items = this.handleItemsResponse(response.items, null);
callback(items);
}.bind(this)).catch(function (response) {
callback(response.data);
});
};
this.saveItems = function (items, callback) {
if (!this.user.uuid) { if (!this.user.uuid) {
this.writeItemsToLocalStorage(); this.writeItemsToLocalStorage();
callback(); callback();
return; return;
} }
var request = Restangular.one("users", this.user.uuid).one("items");
request.items = _.map(items, function (item) { var dirtyItems = modelManager.dirtyItems;
return this.createRequestParamsForItem(item); var request = Restangular.one("items/sync");
request.items = _.map(dirtyItems, function (item) {
return this.createRequestParamsForItem(item, options.additionalFields);
}.bind(this)); }.bind(this));
if (this.syncToken) {
request.sync_token = this.syncToken;
}
request.post().then(function (response) { request.post().then(function (response) {
var omitFields = ["content", "enc_item_key", "auth_hash"]; modelManager.clearDirtyItems();
this.handleItemsResponse(response.items, omitFields); this.syncToken = response.sync_token;
callback(response); $rootScope.$broadcast("sync:updated_token", this.syncToken);
}.bind(this)); this.handleItemsResponse(response.retrieved_items, null);
// merge only metadata for saved items
this.handleItemsResponse(response.saved_items, ["content", "enc_item_key", "auth_hash"]);
if (callback) {
callback(response);
}
}.bind(this)).catch(function (response) {
console.log("Sync error: ", response);
callback(null);
});
};
this.sync = function (callback) {
this.syncWithOptions(callback, undefined);
}; };
this.handleItemsResponse = function (responseItems, omitFields) { this.handleItemsResponse = function (responseItems, omitFields) {
@@ -1789,14 +1789,14 @@ var User = function User(json_obj) {
return modelManager.mapResponseItemsToLocalModelsOmittingFields(responseItems, omitFields); return modelManager.mapResponseItemsToLocalModelsOmittingFields(responseItems, omitFields);
}; };
this.createRequestParamsForItem = function (item) { this.createRequestParamsForItem = function (item, additionalFields) {
return this.paramsForItem(item, !item.isPublic(), null, false); return this.paramsForItem(item, !item.isPublic(), additionalFields, false);
}; };
this.paramsForItem = function (item, encrypted, additionalFields, forExportFile) { this.paramsForItem = function (item, encrypted, additionalFields, forExportFile) {
var itemCopy = _.cloneDeep(item); var itemCopy = _.cloneDeep(item);
var params = { uuid: item.uuid, content_type: item.content_type, presentation_name: item.presentation_name }; var params = { uuid: item.uuid, content_type: item.content_type, presentation_name: item.presentation_name, deleted: item.deleted };
itemCopy.content.references = _.map(itemCopy.content.references, function (reference) { itemCopy.content.references = _.map(itemCopy.content.references, function (reference) {
return { uuid: reference.uuid, content_type: reference.content_type }; return { uuid: reference.uuid, content_type: reference.content_type };
@@ -1823,14 +1823,9 @@ var User = function User(json_obj) {
}; };
this.deleteItem = function (item, callback) { this.deleteItem = function (item, callback) {
if (!this.user.uuid) { item.deleted = true;
this.writeItemsToLocalStorage(); modelManager.addDirtyItems([item]);
callback(true); this.sync(callback);
} else {
Restangular.one("users", this.user.uuid).one("items", item.uuid).remove().then(function (response) {
callback(true);
});
}
}; };
this.shareItem = function (item, callback) { this.shareItem = function (item, callback) {
@@ -1842,7 +1837,8 @@ var User = function User(json_obj) {
var shareFn = function () { var shareFn = function () {
item.presentation_name = "_auto_"; item.presentation_name = "_auto_";
var needsUpdate = [item].concat(item.referencesAffectedBySharingChange() || []); var needsUpdate = [item].concat(item.referencesAffectedBySharingChange() || []);
this.saveItems(needsUpdate, function (success) {}); modelManager.addDirtyItems(needsUpdate);
this.sync();
}.bind(this); }.bind(this);
if (!this.user.username) { if (!this.user.username) {
@@ -1868,7 +1864,8 @@ var User = function User(json_obj) {
this.unshareItem = function (item, callback) { this.unshareItem = function (item, callback) {
item.presentation_name = null; item.presentation_name = null;
var needsUpdate = [item].concat(item.referencesAffectedBySharingChange() || []); var needsUpdate = [item].concat(item.referencesAffectedBySharingChange() || []);
this.saveItems(needsUpdate, function (success) {}); modelManager.addDirtyItems(needsUpdate);
this.sync(null);
}; };
/* /*
@@ -1877,12 +1874,9 @@ var User = function User(json_obj) {
this.importJSONData = function (jsonString, callback) { this.importJSONData = function (jsonString, callback) {
var data = JSON.parse(jsonString); var data = JSON.parse(jsonString);
var customModelManager = new ModelManager(); modelManager.mapResponseItemsToLocalModels(data.items);
customModelManager.mapResponseItemsToLocalModels(data.items); modelManager.addDirtyItems(modelManager.items);
console.log("Importing data", JSON.parse(jsonString)); this.syncWithOptions(callback, { additionalFields: ["created_at", "updated_at"] });
this.saveItems(customModelManager.items, function (response) {
callback(response);
});
}; };
/* /*
@@ -1907,7 +1901,7 @@ var User = function User(json_obj) {
}.bind(this); }.bind(this);
var items = _.map(modelManager.items, function (item) { var items = _.map(modelManager.items, function (item) {
return this.paramsForItem(item, false, ["created_at", "updated_at"], true); return _.omit(this.paramsForItem(item, false, ["created_at", "updated_at"], true), ["deleted"]);
}.bind(this)); }.bind(this));
var data = { var data = {
@@ -2468,7 +2462,6 @@ var ItemManager = function () {
}, { }, {
key: 'mapResponseItemsToLocalModelsOmittingFields', key: 'mapResponseItemsToLocalModelsOmittingFields',
value: function mapResponseItemsToLocalModelsOmittingFields(items, omitFields) { value: function mapResponseItemsToLocalModelsOmittingFields(items, omitFields) {
console.log("map response items", items);
var models = []; var models = [];
var _iteratorNormalCompletion3 = true; var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false; var _didIteratorError3 = false;
@@ -2700,12 +2693,25 @@ var ModelManager = function (_ItemManager) {
var dirty = this.removeReferencesBetweenItems(tag, note); var dirty = this.removeReferencesBetweenItems(tag, note);
this.addDirtyItems(dirty); this.addDirtyItems(dirty);
} }
}, {
key: 'deleteItem',
value: function deleteItem(item) {
var dirty = _get(ModelManager.prototype.__proto__ || Object.getPrototypeOf(ModelManager.prototype), 'deleteItem', this).call(this, item);
if (item.content_type == "Note") {
_.remove(this.notes, item);
} else if (item.content_type == "Tag") {
_.remove(this.tags, item);
}
return dirty;
}
}, { }, {
key: 'deleteNote', key: 'deleteNote',
value: function deleteNote(note) { value: function deleteNote(note) {
var dirty = this.deleteItem(note); var dirty = this.deleteItem(note);
_.remove(this.notes, note); _.remove(this.notes, note);
this.addDirtyItems(dirty); if (!note.dummy) {
this.addDirtyItems(dirty);
}
} }
}, { }, {
key: 'deleteTag', key: 'deleteTag',

File diff suppressed because one or more lines are too long