sync tokens

This commit is contained in:
Mo Bitar
2016-12-30 09:53:02 -06:00
parent d5d1b2b7f7
commit b2f1077fef
7 changed files with 63 additions and 46 deletions

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,12 +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.sync(null); 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;
apiController.sync(null);
// refresh every 30s
setInterval(function () { setInterval(function () {
apiController.sync(null); apiController.sync(null);
}, 1000); }, 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();

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){
@@ -236,19 +232,23 @@ angular.module('app.frontend')
return this.createRequestParamsForItem(item); return this.createRequestParamsForItem(item);
}.bind(this)); }.bind(this));
if(this.lastRefreshDate) { if(this.syncToken) {
request.updated_after = this.lastRefreshDate.toString(); request.sync_token = this.syncToken;
} }
request.post().then(function(response) { request.post().then(function(response) {
modelManager.clearDirtyItems(); modelManager.clearDirtyItems();
var lastUpdated = new Date(response.last_updated); this.syncToken = response.sync_token;
this.lastRefreshDate = lastUpdated; $rootScope.$broadcast("sync:updated_token", this.syncToken);
this.handleItemsResponse(response.retrieved_items, null); this.handleItemsResponse(response.retrieved_items, null);
if(callback) { if(callback) {
callback(response); callback(response);
} }
}.bind(this)) }.bind(this))
.catch(function(response){
console.log("Sync error: ", response);
callback(null);
})
} }
this.handleItemsResponse = function(responseItems, omitFields) { this.handleItemsResponse = function(responseItems, omitFields) {

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

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

@@ -697,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: {
@@ -710,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) {
@@ -761,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.sync(null); this.lastSyncDate = new Date();
}; };
this.loginSubmitPressed = function () { this.loginSubmitPressed = function () {
@@ -872,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;
apiController.sync(null);
// refresh every 30s
setInterval(function () { setInterval(function () {
apiController.sync(null); apiController.sync(null);
}, 1000); }, 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();
@@ -1549,11 +1560,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;
@@ -1601,11 +1612,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);
@@ -1757,19 +1764,22 @@ var User = function User(json_obj) {
return this.createRequestParamsForItem(item); return this.createRequestParamsForItem(item);
}.bind(this)); }.bind(this));
if (this.lastRefreshDate) { if (this.syncToken) {
request.updated_after = this.lastRefreshDate.toString(); request.sync_token = this.syncToken;
} }
request.post().then(function (response) { request.post().then(function (response) {
modelManager.clearDirtyItems(); modelManager.clearDirtyItems();
var lastUpdated = new Date(response.last_updated); this.syncToken = response.sync_token;
this.lastRefreshDate = lastUpdated; $rootScope.$broadcast("sync:updated_token", this.syncToken);
this.handleItemsResponse(response.retrieved_items, null); this.handleItemsResponse(response.retrieved_items, null);
if (callback) { if (callback) {
callback(response); callback(response);
} }
}.bind(this)); }.bind(this)).catch(function (response) {
console.log("Sync error: ", response);
callback(null);
});
}; };
this.handleItemsResponse = function (responseItems, omitFields) { this.handleItemsResponse = function (responseItems, omitFields) {
@@ -2457,7 +2467,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;

File diff suppressed because one or more lines are too long