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')
.directive("header", function(){
.directive("header", function(apiController){
return {
restrict: 'E',
scope: {
@@ -13,7 +13,9 @@ angular.module('app.frontend')
bindToController: true,
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() {
return apiController.lastRefreshDate;
this.refreshData = function() {
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() {
apiController.sync(null);
this.syncUpdated = function() {
this.lastSyncDate = new Date();
}
this.loginSubmitPressed = function() {

View File

@@ -9,16 +9,17 @@ angular.module('app.frontend')
$scope.tags = modelManager.tags;
$scope.allTag.notes = modelManager.notes;
apiController.sync(null);
// refresh every 30s
setInterval(function () {
apiController.sync(null);
}, 1000);
}, 30000);
// apiController.verifyEncryptionStatusOfAllItems($scope.defaultUser, function(success){});
}
apiController.getCurrentUser(function(user){
if(user) {
// console.log("Get user response", user);
$scope.defaultUser = user;
$rootScope.title = "Notes — Standard Notes";
onUserSet();

View File

@@ -20,11 +20,11 @@ angular.module('app.frontend')
}
this.$get = function(Restangular, modelManager, ngDialog) {
return new ApiController(Restangular, modelManager, ngDialog);
this.$get = function($rootScope, 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.user = user;
@@ -74,11 +74,7 @@ angular.module('app.frontend')
return;
}
Restangular.one("users/current").get().then(function(response){
var plain = response.plain();
var items = plain.items;
this.decryptItems(items);
items = modelManager.mapResponseItemsToLocalModels(items);
var user = _.omit(plain, ["items"]);
var user = response.plain();
callback(user);
}.bind(this))
.catch(function(response){
@@ -236,19 +232,23 @@ angular.module('app.frontend')
return this.createRequestParamsForItem(item);
}.bind(this));
if(this.lastRefreshDate) {
request.updated_after = this.lastRefreshDate.toString();
if(this.syncToken) {
request.sync_token = this.syncToken;
}
request.post().then(function(response) {
modelManager.clearDirtyItems();
var lastUpdated = new Date(response.last_updated);
this.lastRefreshDate = lastUpdated;
this.syncToken = response.sync_token;
$rootScope.$broadcast("sync:updated_token", this.syncToken);
this.handleItemsResponse(response.retrieved_items, null);
if(callback) {
callback(response);
}
}.bind(this))
.catch(function(response){
console.log("Sync error: ", response);
callback(null);
})
}
this.handleItemsResponse = function(responseItems, omitFields) {

View File

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

View File

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

View File

@@ -697,7 +697,7 @@ angular.module('app.frontend').controller('BaseCtrl', BaseCtrl);
this.focusEditor(100);
};
});
;angular.module('app.frontend').directive("header", function () {
;angular.module('app.frontend').directive("header", function (apiController) {
return {
restrict: 'E',
scope: {
@@ -710,7 +710,11 @@ angular.module('app.frontend').controller('BaseCtrl', BaseCtrl);
controllerAs: 'ctrl',
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) {
@@ -761,12 +765,18 @@ angular.module('app.frontend').controller('BaseCtrl', BaseCtrl);
}
};
this.getLastRefreshDate = function () {
return apiController.lastRefreshDate;
this.refreshData = function () {
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 () {
apiController.sync(null);
this.syncUpdated = function () {
this.lastSyncDate = new Date();
};
this.loginSubmitPressed = function () {
@@ -872,16 +882,17 @@ angular.module('app.frontend').controller('BaseCtrl', BaseCtrl);
$scope.tags = modelManager.tags;
$scope.allTag.notes = modelManager.notes;
apiController.sync(null);
// refresh every 30s
setInterval(function () {
apiController.sync(null);
}, 1000);
}, 30000);
// apiController.verifyEncryptionStatusOfAllItems($scope.defaultUser, function(success){});
};
apiController.getCurrentUser(function (user) {
if (user) {
// console.log("Get user response", user);
$scope.defaultUser = user;
$rootScope.title = "Notes — Standard Notes";
onUserSet();
@@ -1549,11 +1560,11 @@ var User = function User(json_obj) {
return url;
};
this.$get = function (Restangular, modelManager, ngDialog) {
return new ApiController(Restangular, modelManager, ngDialog);
this.$get = function ($rootScope, 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.user = user;
@@ -1601,11 +1612,7 @@ var User = function User(json_obj) {
return;
}
Restangular.one("users/current").get().then(function (response) {
var plain = response.plain();
var items = plain.items;
this.decryptItems(items);
items = modelManager.mapResponseItemsToLocalModels(items);
var user = _.omit(plain, ["items"]);
var user = response.plain();
callback(user);
}.bind(this)).catch(function (response) {
console.log("Error getting current user", response);
@@ -1757,19 +1764,22 @@ var User = function User(json_obj) {
return this.createRequestParamsForItem(item);
}.bind(this));
if (this.lastRefreshDate) {
request.updated_after = this.lastRefreshDate.toString();
if (this.syncToken) {
request.sync_token = this.syncToken;
}
request.post().then(function (response) {
modelManager.clearDirtyItems();
var lastUpdated = new Date(response.last_updated);
this.lastRefreshDate = lastUpdated;
this.syncToken = response.sync_token;
$rootScope.$broadcast("sync:updated_token", this.syncToken);
this.handleItemsResponse(response.retrieved_items, null);
if (callback) {
callback(response);
}
}.bind(this));
}.bind(this)).catch(function (response) {
console.log("Sync error: ", response);
callback(null);
});
};
this.handleItemsResponse = function (responseItems, omitFields) {
@@ -2457,7 +2467,6 @@ var ItemManager = function () {
}, {
key: 'mapResponseItemsToLocalModelsOmittingFields',
value: function mapResponseItemsToLocalModelsOmittingFields(items, omitFields) {
console.log("map response items", items);
var models = [];
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;

File diff suppressed because one or more lines are too long