sync tokens
This commit is contained in:
@@ -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() {
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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 || [])
|
||||
|
||||
Reference in New Issue
Block a user