offline mode

This commit is contained in:
Mo Bitar
2017-01-08 00:18:30 -06:00
parent 6823469062
commit 93e3a72d30
12 changed files with 269 additions and 323 deletions

View File

@@ -1,13 +1,6 @@
class BaseCtrl { class BaseCtrl {
constructor($rootScope, modelManager) { constructor($rootScope, modelManager, apiController) {
// $rootScope.resetPasswordSubmit = function() { apiController.getCurrentUser(function(){});
// var new_keys = Neeto.crypto.generateEncryptionKeysForUser($rootScope.resetData.password, $rootScope.resetData.email);
// var data = _.clone($rootScope.resetData);
// data.password = new_keys.pw;
// data.password_confirmation = new_keys.pw;
// $auth.updatePassword(data);
// apiController.setMk(new_keys.mk);
// }
} }
} }

View File

@@ -5,8 +5,7 @@ angular.module('app.frontend')
scope: { scope: {
save: "&", save: "&",
remove: "&", remove: "&",
note: "=", note: "="
user: "="
}, },
templateUrl: 'frontend/editor.html', templateUrl: 'frontend/editor.html',
replace: true, replace: true,
@@ -124,6 +123,11 @@ angular.module('app.frontend')
statusTimeout = $timeout(function(){ statusTimeout = $timeout(function(){
this.noteStatus = "All changes saved" this.noteStatus = "All changes saved"
}.bind(this), 200) }.bind(this), 200)
} else {
if(statusTimeout) $timeout.cancel(statusTimeout);
statusTimeout = $timeout(function(){
this.noteStatus = "(Offline) — All changes saved"
}.bind(this), 200)
} }
}.bind(this)); }.bind(this));
} }
@@ -138,7 +142,7 @@ angular.module('app.frontend')
this.changesMade = function() { this.changesMade = function() {
this.note.hasChanges = true; this.note.hasChanges = true;
this.note.dummy = false; this.note.dummy = false;
if(this.user.uuid) { if(apiController.isUserSignedIn()) {
// signed out users have local autosave, dont need draft saving // signed out users have local autosave, dont need draft saving
apiController.saveDraftToDisk(this.note); apiController.saveDraftToDisk(this.note);
} }

View File

@@ -3,7 +3,6 @@ angular.module('app.frontend')
return { return {
restrict: 'E', restrict: 'E',
scope: { scope: {
user: "=",
logout: "&" logout: "&"
}, },
templateUrl: 'frontend/header.html', templateUrl: 'frontend/header.html',
@@ -21,6 +20,7 @@ angular.module('app.frontend')
}) })
.controller('HeaderCtrl', function ($state, apiController, modelManager, serverSideValidation, $timeout, extensionManager) { .controller('HeaderCtrl', function ($state, apiController, modelManager, serverSideValidation, $timeout, extensionManager) {
this.user = apiController.user;
this.extensionManager = extensionManager; this.extensionManager = extensionManager;
this.changePasswordPressed = function() { this.changePasswordPressed = function() {
@@ -104,7 +104,7 @@ angular.module('app.frontend')
return; return;
} }
apiController.changePassword(this.user, this.passwordChangeData.current_password, this.passwordChangeData.new_password, function(response){ apiController.changePassword(this.passwordChangeData.current_password, this.passwordChangeData.new_password, function(response){
}) })
@@ -210,7 +210,6 @@ angular.module('app.frontend')
} }
this.onAuthSuccess = function(user) { this.onAuthSuccess = function(user) {
this.user.uuid = user.uuid;
// if(this.user.shouldMerge && this.hasLocalData()) { // if(this.user.shouldMerge && this.hasLocalData()) {
// apiController.mergeLocalDataRemotely(this.user, function(){ // apiController.mergeLocalDataRemotely(this.user, function(){

View File

@@ -2,30 +2,17 @@ angular.module('app.frontend')
.controller('HomeCtrl', function ($scope, $rootScope, $timeout, apiController, modelManager) { .controller('HomeCtrl', function ($scope, $rootScope, $timeout, apiController, modelManager) {
$rootScope.bodyClass = "app-body-class"; $rootScope.bodyClass = "app-body-class";
var onUserSet = function() { apiController.loadLocalItems();
apiController.setUser($scope.defaultUser); $scope.allTag = new Tag({all: true});
$scope.allTag = new Tag({all: true}); $scope.allTag.title = "All";
$scope.allTag.title = "All"; $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 () {
apiController.sync(null); apiController.sync(null);
// refresh every 30s }, 30000);
setInterval(function () {
apiController.sync(null);
}, 30000);
}
apiController.getCurrentUser(function(user){
if(user) {
$scope.defaultUser = user;
$rootScope.title = "Notes — Standard Notes";
onUserSet();
} else {
$scope.defaultUser = new User(apiController.loadLocalItemsAndUser());
onUserSet();
}
});
/* /*
Tags Ctrl Callbacks Tags Ctrl Callbacks
@@ -115,7 +102,10 @@ angular.module('app.frontend')
apiController.sync(function(response){ apiController.sync(function(response){
if(response && response.error) { if(response && response.error) {
alert("There was an error saving your note. Please try again."); if(!$scope.didShowErrorAlert) {
$scope.didShowErrorAlert = true;
alert("There was an error saving your note. Please try again.");
}
callback(false); callback(false);
} else { } else {
note.hasChanges = false; note.hasChanges = false;
@@ -147,8 +137,7 @@ angular.module('app.frontend')
*/ */
$scope.headerLogout = function() { $scope.headerLogout = function() {
$scope.defaultUser = apiController.loadLocalItemsAndUser(); apiController.clearLocalStorage();
$scope.tags = $scope.defaultUser.tags;
} }

View File

@@ -6,7 +6,6 @@ angular.module('app.frontend')
selectionMade: "&", selectionMade: "&",
remove: "&", remove: "&",
tag: "=", tag: "=",
user: "=",
removeTag: "&" removeTag: "&"
}, },
@@ -71,7 +70,7 @@ angular.module('app.frontend')
this.selectedTagShare = function() { this.selectedTagShare = function() {
this.showMenu = false; this.showMenu = false;
if(!this.user.uuid) { if(!apiController.isUserSignedIn()) {
alert("You must be signed in to share a tag."); alert("You must be signed in to share a tag.");
return; return;
} }

View File

@@ -9,7 +9,6 @@ angular.module('app.frontend')
save: "&", save: "&",
tags: "=", tags: "=",
allTag: "=", allTag: "=",
user: "=",
updateNoteTag: "&" updateNoteTag: "&"
}, },
templateUrl: 'frontend/tags.html', templateUrl: 'frontend/tags.html',

View File

@@ -1,11 +1,10 @@
angular.module('app.frontend') angular.module('app.frontend')
.controller('UsernameModalCtrl', function ($scope, apiController, Restangular, user, callback, $timeout) { .controller('UsernameModalCtrl', function ($scope, apiController, Restangular, callback, $timeout) {
$scope.formData = {}; $scope.formData = {};
$scope.saveUsername = function() { $scope.saveUsername = function() {
apiController.setUsername(user, $scope.formData.username, function(response){ apiController.setUsername($scope.formData.username, function(response){
var username = response.username; var username = response.username;
user.username = username;
callback(username); callback(username);
$scope.closeThisDialog(); $scope.closeThisDialog();
}) })

View File

@@ -1,5 +0,0 @@
class User {
constructor(json_obj) {
_.merge(this, json_obj);
}
}

View File

@@ -26,9 +26,8 @@ angular.module('app.frontend')
function ApiController($rootScope, Restangular, modelManager, ngDialog) { function ApiController($rootScope, Restangular, modelManager, ngDialog) {
this.setUser = function(user) { this.user = {};
this.user = user; this.syncToken = localStorage.getItem("syncToken");
}
/* /*
Config Config
@@ -58,7 +57,11 @@ angular.module('app.frontend')
*/ */
this.isUserSignedIn = function() { this.isUserSignedIn = function() {
return this.user.email && this.retrieveMk(); return localStorage.getItem("jwt");
}
this.userId = function() {
return localStorage.getItem("uuid");
} }
this.getAuthParamsForEmail = function(email, callback) { this.getAuthParamsForEmail = function(email, callback) {
@@ -79,7 +82,8 @@ angular.module('app.frontend')
} }
Restangular.one("users/current").get().then(function(response){ Restangular.one("users/current").get().then(function(response){
var user = response.plain(); var user = response.plain();
callback(user); _.merge(this.user, user);
callback();
}.bind(this)) }.bind(this))
.catch(function(response){ .catch(function(response){
console.log("Error getting current user", response); console.log("Error getting current user", response);
@@ -100,6 +104,7 @@ angular.module('app.frontend')
_.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);
localStorage.setItem("uuid", response.uuid);
callback(response); callback(response);
}) })
.catch(function(response){ .catch(function(response){
@@ -118,6 +123,7 @@ angular.module('app.frontend')
_.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);
localStorage.setItem("uuid", response.uuid);
callback(response); callback(response);
}) })
.catch(function(response){ .catch(function(response){
@@ -126,46 +132,46 @@ angular.module('app.frontend')
}.bind(this)); }.bind(this));
} }
this.changePassword = function(user, current_password, new_password) { // this.changePassword = function(current_password, new_password) {
this.getAuthParamsForEmail(email, function(authParams){ // this.getAuthParamsForEmail(email, function(authParams){
if(!authParams) { // if(!authParams) {
callback(null); // callback(null);
return; // return;
} // }
Neeto.crypto.computeEncryptionKeysForUser(_.merge({password: current_password, email: user.email}, authParams), function(currentKeys) { // Neeto.crypto.computeEncryptionKeysForUser(_.merge({password: current_password, email: user.email}, authParams), function(currentKeys) {
Neeto.crypto.computeEncryptionKeysForUser(_.merge({password: new_password, email: user.email}, authParams), function(newKeys){ // Neeto.crypto.computeEncryptionKeysForUser(_.merge({password: new_password, email: user.email}, authParams), function(newKeys){
var data = {}; // var data = {};
data.current_password = currentKeys.pw; // data.current_password = currentKeys.pw;
data.password = newKeys.pw; // data.password = newKeys.pw;
data.password_confirmation = newKeys.pw; // data.password_confirmation = newKeys.pw;
//
var user = this.user; // var user = this.user;
//
this._performPasswordChange(currentKeys, newKeys, function(response){ // this._performPasswordChange(currentKeys, newKeys, function(response){
if(response && !response.error) { // if(response && !response.error) {
// this.showNewPasswordForm = false; // // this.showNewPasswordForm = false;
// reencrypt data with new mk // // reencrypt data with new mk
this.reencryptAllItemsAndSave(user, newKeys.mk, currentKeys.mk, function(success){ // this.reencryptAllItemsAndSave(user, newKeys.mk, currentKeys.mk, function(success){
if(success) { // if(success) {
this.setMk(newKeys.mk); // this.setMk(newKeys.mk);
alert("Your password has been changed and your data re-encrypted."); // alert("Your password has been changed and your data re-encrypted.");
} else { // } else {
// rollback password // // rollback password
this._performPasswordChange(newKeys, currentKeys, function(response){ // this._performPasswordChange(newKeys, currentKeys, function(response){
alert("There was an error changing your password. Your password has been rolled back."); // alert("There was an error changing your password. Your password has been rolled back.");
window.location.reload(); // window.location.reload();
}) // })
} // }
}.bind(this)); // }.bind(this));
} else { // } else {
// this.showNewPasswordForm = false; // // this.showNewPasswordForm = false;
alert("There was an error changing your password. Please try again."); // alert("There was an error changing your password. Please try again.");
} // }
}.bind(this)) // }.bind(this))
}.bind(this)); // }.bind(this));
}.bind(this)); // }.bind(this));
}.bind(this)); // }.bind(this));
} // }
this._performPasswordChange = function(email, current_keys, new_keys, callback) { this._performPasswordChange = function(email, current_keys, new_keys, callback) {
var request = Restangular.one("auth"); var request = Restangular.one("auth");
@@ -181,53 +187,37 @@ angular.module('app.frontend')
User User
*/ */
this.setUsername = function(user, username, callback) { this.setUsername = function(username, callback) {
var request = Restangular.one("users", user.uuid); var request = Restangular.one("users", this.userId());
request.username = username; request.username = username;
request.patch().then(function(response){ request.patch().then(function(response){
this.user.username = response.username;
callback(response.plain()); callback(response.plain());
})
}
/*
Ensures that if encryption is disabled, all local items are uncrypted,
and that if it's enabled, that all local items are encrypted
*/
this.verifyEncryptionStatusOfAllItems = function(user, callback) {
var allItems = user.filteredItems();
var itemsNeedingUpdate = [];
allItems.forEach(function(item){
if(!item.isPublic()) {
if(item.encryptionEnabled() && !item.isEncrypted()) {
itemsNeedingUpdate.push(item);
}
} else {
if(item.isEncrypted()) {
itemsNeedingUpdate.push(item);
}
}
}.bind(this)) }.bind(this))
if(itemsNeedingUpdate.length > 0) {
console.log("verifying encryption, items need updating", itemsNeedingUpdate);
this.saveBatchItems(user, itemsNeedingUpdate, callback)
}
} }
/* /*
Items Items
*/ */
this.setSyncToken = function(syncToken) {
this.syncToken = syncToken;
localStorage.setItem("syncToken", this.syncToken);
}
this.syncWithOptions = function(callback, options = {}) { this.syncWithOptions = function(callback, options = {}) {
if(!this.user.uuid) { this.writeAllItemsToLocalStorage(function(responseItems){
this.writeItemsToLocalStorage(function(responseItems){ if(!this.isUserSignedIn()) {
// is not signed in
modelManager.clearDirtyItems(); modelManager.clearDirtyItems();
if(callback) { if(callback) {
callback(); callback();
} }
}.bind(this)) }
}.bind(this))
if(!this.isUserSignedIn()) {
return; return;
} }
@@ -245,7 +235,7 @@ angular.module('app.frontend')
request.post().then(function(response) { request.post().then(function(response) {
modelManager.clearDirtyItems(); modelManager.clearDirtyItems();
this.syncToken = response.sync_token; this.setSyncToken(response.sync_token);
$rootScope.$broadcast("sync:updated_token", this.syncToken); $rootScope.$broadcast("sync:updated_token", this.syncToken);
this.handleItemsResponse(response.retrieved_items, null); this.handleItemsResponse(response.retrieved_items, null);
@@ -253,13 +243,17 @@ angular.module('app.frontend')
var omitFields = ["content", "enc_item_key", "auth_hash"]; var omitFields = ["content", "enc_item_key", "auth_hash"];
this.handleItemsResponse(response.saved_items, omitFields); this.handleItemsResponse(response.saved_items, omitFields);
this.writeAllItemsToLocalStorage();
if(callback) { if(callback) {
callback(response); callback(response);
} }
}.bind(this)) }.bind(this))
.catch(function(response){ .catch(function(response){
console.log("Sync error: ", response); console.log("Sync error: ", response);
callback({error: "Sync error"}); if(callback) {
callback({error: "Sync error"});
}
}) })
} }
@@ -314,7 +308,7 @@ angular.module('app.frontend')
} }
this.shareItem = function(item, callback) { this.shareItem = function(item, callback) {
if(!this.user.uuid) { if(!this.isUserSignedIn()) {
alert("You must be signed in to share."); alert("You must be signed in to share.");
return; return;
} }
@@ -333,7 +327,6 @@ angular.module('app.frontend')
template: 'frontend/modals/username.html', template: 'frontend/modals/username.html',
controller: 'UsernameModalCtrl', controller: 'UsernameModalCtrl',
resolve: { resolve: {
user: function() {return this.user}.bind(this),
callback: function() { callback: function() {
return shareFn; return shareFn;
} }
@@ -407,52 +400,56 @@ angular.module('app.frontend')
/* /*
Merging Merging
*/ */
this.mergeLocalDataRemotely = function(user, callback) { // this.mergeLocalDataRemotely = function(user, callback) {
var request = Restangular.one("users", user.uuid).one("merge"); // var request = Restangular.one("users", this.userId()).one("merge");
var tags = user.tags; // var tags = user.tags;
request.items = user.items; // request.items = user.items;
request.items.forEach(function(item){ // request.items.forEach(function(item){
if(item.tag_id) { // if(item.tag_id) {
var tag = tags.filter(function(tag){return tag.uuid == item.tag_id})[0]; // var tag = tags.filter(function(tag){return tag.uuid == item.tag_id})[0];
item.tag_name = tag.title; // item.tag_name = tag.title;
} // }
}) // })
request.post().then(function(response){ // request.post().then(function(response){
callback(); // callback();
localStorage.removeItem('user'); // localStorage.removeItem('user');
}) // })
// }
this.clearLocalStorage = function() {
localStorage.removeItem("items");
localStorage.removeItem("mk");
localStorage.removeItem("jwt");
localStorage.removeItem("uuid");
localStorage.removeItem("syncToken");
} }
this.staticifyObject = function(object) { this.staticifyObject = function(object) {
return JSON.parse(JSON.stringify(object)); return JSON.parse(JSON.stringify(object));
} }
this.writeItemsToLocalStorage = function(callback) { this.writeAllItemsToLocalStorage = function(callback) {
var items = _.map(modelManager.allItems, function(item){ var items = _.map(modelManager.allItems, function(item){
return this.paramsForItem(item, false, ["created_at", "updated_at"], false) return this.paramsForItem(item, this.isUserSignedIn(), ["created_at", "updated_at"], false)
}.bind(this)); }.bind(this));
console.log("Writing items to local", items); // console.log("Writing items to local", items);
this.writeToLocalStorage('items', items); this.writeToLocalStorage('items', items);
callback(items); if(callback) {
callback(items);
}
} }
this.writeToLocalStorage = function(key, value) { this.writeToLocalStorage = function(key, value) {
localStorage.setItem(key, angular.toJson(value)); localStorage.setItem(key, angular.toJson(value));
} }
this.loadLocalItemsAndUser = function() { this.loadLocalItems = function() {
var user = {};
var items = JSON.parse(localStorage.getItem('items')) || []; var items = JSON.parse(localStorage.getItem('items')) || [];
items = this.handleItemsResponse(items, null); items = this.handleItemsResponse(items, null);
Item.sortItemsByDate(items); Item.sortItemsByDate(items);
user.items = items;
user.shouldMerge = true;
return user;
} }
/* /*
@@ -551,7 +548,7 @@ angular.module('app.frontend')
} }
this.reencryptAllItemsAndSave = function(user, newMasterKey, oldMasterKey, callback) { this.reencryptAllItemsAndSave = function(user, newMasterKey, oldMasterKey, callback) {
var items = user.filteredItems(); var items = modelManager.allItems();
items.forEach(function(item){ items.forEach(function(item){
if(item.content.substring(0, 3) == "001" && item.enc_item_key) { if(item.content.substring(0, 3) == "001" && item.enc_item_key) {
// first decrypt item_key with old key // first decrypt item_key with old key

View File

@@ -5,7 +5,7 @@
"tags" => "tags", "user" => "defaultUser", "update-note-tag" => "tagsUpdateNoteTag"} "tags" => "tags", "user" => "defaultUser", "update-note-tag" => "tagsUpdateNoteTag"}
%notes-section{"remove-tag" => "notesRemoveTag", "user" => "defaultUser", "add-new" => "notesAddNew", "selection-made" => "notesSelectionMade", %notes-section{"remove-tag" => "notesRemoveTag", "user" => "defaultUser", "add-new" => "notesAddNew", "selection-made" => "notesSelectionMade",
"tag" => "selectedTag", "user-id" => "defaultUser.uuid", "remove" => "deleteNote"} "tag" => "selectedTag", "remove" => "deleteNote"}
%editor-section{"ng-if" => "selectedNote", "note" => "selectedNote", "remove" => "deleteNote", %editor-section{"ng-if" => "selectedNote", "note" => "selectedNote", "remove" => "deleteNote",
"user" => "defaultUser", "save" => "saveNote"} "user" => "defaultUser", "save" => "saveNote"}

View File

@@ -438,17 +438,10 @@ angular.module('app.frontend', ['ui.router', 'restangular', 'ngDialog']).config(
$locationProvider.html5Mode(true); $locationProvider.html5Mode(true);
}); });
; ;
var BaseCtrl = function BaseCtrl($rootScope, modelManager) { var BaseCtrl = function BaseCtrl($rootScope, modelManager, apiController) {
// $rootScope.resetPasswordSubmit = function() {
// var new_keys = Neeto.crypto.generateEncryptionKeysForUser($rootScope.resetData.password, $rootScope.resetData.email);
// var data = _.clone($rootScope.resetData);
// data.password = new_keys.pw;
// data.password_confirmation = new_keys.pw;
// $auth.updatePassword(data);
// apiController.setMk(new_keys.mk);
// }
_classCallCheck(this, BaseCtrl); _classCallCheck(this, BaseCtrl);
apiController.getCurrentUser(function () {});
}; };
angular.module('app.frontend').controller('BaseCtrl', BaseCtrl); angular.module('app.frontend').controller('BaseCtrl', BaseCtrl);
@@ -458,8 +451,7 @@ angular.module('app.frontend').controller('BaseCtrl', BaseCtrl);
scope: { scope: {
save: "&", save: "&",
remove: "&", remove: "&",
note: "=", note: "="
user: "="
}, },
templateUrl: 'frontend/editor.html', templateUrl: 'frontend/editor.html',
replace: true, replace: true,
@@ -576,6 +568,11 @@ angular.module('app.frontend').controller('BaseCtrl', BaseCtrl);
statusTimeout = $timeout(function () { statusTimeout = $timeout(function () {
this.noteStatus = "All changes saved"; this.noteStatus = "All changes saved";
}.bind(this), 200); }.bind(this), 200);
} else {
if (statusTimeout) $timeout.cancel(statusTimeout);
statusTimeout = $timeout(function () {
this.noteStatus = "(Offline) — All changes saved";
}.bind(this), 200);
} }
}.bind(this)); }.bind(this));
}; };
@@ -590,7 +587,7 @@ angular.module('app.frontend').controller('BaseCtrl', BaseCtrl);
this.changesMade = function () { this.changesMade = function () {
this.note.hasChanges = true; this.note.hasChanges = true;
this.note.dummy = false; this.note.dummy = false;
if (this.user.uuid) { if (apiController.isUserSignedIn()) {
// signed out users have local autosave, dont need draft saving // signed out users have local autosave, dont need draft saving
apiController.saveDraftToDisk(this.note); apiController.saveDraftToDisk(this.note);
} }
@@ -720,7 +717,6 @@ angular.module('app.frontend').controller('BaseCtrl', BaseCtrl);
return { return {
restrict: 'E', restrict: 'E',
scope: { scope: {
user: "=",
logout: "&" logout: "&"
}, },
templateUrl: 'frontend/header.html', templateUrl: 'frontend/header.html',
@@ -737,6 +733,7 @@ angular.module('app.frontend').controller('BaseCtrl', BaseCtrl);
}; };
}).controller('HeaderCtrl', function ($state, apiController, modelManager, serverSideValidation, $timeout, extensionManager) { }).controller('HeaderCtrl', function ($state, apiController, modelManager, serverSideValidation, $timeout, extensionManager) {
this.user = apiController.user;
this.extensionManager = extensionManager; this.extensionManager = extensionManager;
this.changePasswordPressed = function () { this.changePasswordPressed = function () {
@@ -820,7 +817,7 @@ angular.module('app.frontend').controller('BaseCtrl', BaseCtrl);
return; return;
} }
apiController.changePassword(this.user, this.passwordChangeData.current_password, this.passwordChangeData.new_password, function (response) {}); apiController.changePassword(this.passwordChangeData.current_password, this.passwordChangeData.new_password, function (response) {});
}.bind(this)); }.bind(this));
}; };
@@ -923,7 +920,6 @@ angular.module('app.frontend').controller('BaseCtrl', BaseCtrl);
}; };
this.onAuthSuccess = function (user) { this.onAuthSuccess = function (user) {
this.user.uuid = user.uuid;
// if(this.user.shouldMerge && this.hasLocalData()) { // if(this.user.shouldMerge && this.hasLocalData()) {
// apiController.mergeLocalDataRemotely(this.user, function(){ // apiController.mergeLocalDataRemotely(this.user, function(){
@@ -940,30 +936,17 @@ angular.module('app.frontend').controller('BaseCtrl', BaseCtrl);
;angular.module('app.frontend').controller('HomeCtrl', function ($scope, $rootScope, $timeout, apiController, modelManager) { ;angular.module('app.frontend').controller('HomeCtrl', function ($scope, $rootScope, $timeout, apiController, modelManager) {
$rootScope.bodyClass = "app-body-class"; $rootScope.bodyClass = "app-body-class";
var onUserSet = function onUserSet() { apiController.loadLocalItems();
apiController.setUser($scope.defaultUser); $scope.allTag = new Tag({ all: true });
$scope.allTag = new Tag({ all: true }); $scope.allTag.title = "All";
$scope.allTag.title = "All"; $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 () {
apiController.sync(null); apiController.sync(null);
// refresh every 30s }, 30000);
setInterval(function () {
apiController.sync(null);
}, 30000);
};
apiController.getCurrentUser(function (user) {
if (user) {
$scope.defaultUser = user;
$rootScope.title = "Notes — Standard Notes";
onUserSet();
} else {
$scope.defaultUser = new User(apiController.loadLocalItemsAndUser());
onUserSet();
}
});
/* /*
Tags Ctrl Callbacks Tags Ctrl Callbacks
@@ -1053,7 +1036,10 @@ angular.module('app.frontend').controller('BaseCtrl', BaseCtrl);
apiController.sync(function (response) { apiController.sync(function (response) {
if (response && response.error) { if (response && response.error) {
alert("There was an error saving your note. Please try again."); if (!$scope.didShowErrorAlert) {
$scope.didShowErrorAlert = true;
alert("There was an error saving your note. Please try again.");
}
callback(false); callback(false);
} else { } else {
note.hasChanges = false; note.hasChanges = false;
@@ -1085,8 +1071,7 @@ angular.module('app.frontend').controller('BaseCtrl', BaseCtrl);
*/ */
$scope.headerLogout = function () { $scope.headerLogout = function () {
$scope.defaultUser = apiController.loadLocalItemsAndUser(); apiController.clearLocalStorage();
$scope.tags = $scope.defaultUser.tags;
}; };
}); });
;angular.module('app.frontend').directive("notesSection", function () { ;angular.module('app.frontend').directive("notesSection", function () {
@@ -1096,7 +1081,6 @@ angular.module('app.frontend').controller('BaseCtrl', BaseCtrl);
selectionMade: "&", selectionMade: "&",
remove: "&", remove: "&",
tag: "=", tag: "=",
user: "=",
removeTag: "&" removeTag: "&"
}, },
@@ -1160,7 +1144,7 @@ angular.module('app.frontend').controller('BaseCtrl', BaseCtrl);
this.selectedTagShare = function () { this.selectedTagShare = function () {
this.showMenu = false; this.showMenu = false;
if (!this.user.uuid) { if (!apiController.isUserSignedIn()) {
alert("You must be signed in to share a tag."); alert("You must be signed in to share a tag.");
return; return;
} }
@@ -1232,7 +1216,6 @@ angular.module('app.frontend').controller('BaseCtrl', BaseCtrl);
save: "&", save: "&",
tags: "=", tags: "=",
allTag: "=", allTag: "=",
user: "=",
updateNoteTag: "&" updateNoteTag: "&"
}, },
templateUrl: 'frontend/tags.html', templateUrl: 'frontend/tags.html',
@@ -1329,13 +1312,12 @@ angular.module('app.frontend').controller('BaseCtrl', BaseCtrl);
this.updateNoteTag()(note, newTag, this.selectedTag); this.updateNoteTag()(note, newTag, this.selectedTag);
}.bind(this); }.bind(this);
}); });
;angular.module('app.frontend').controller('UsernameModalCtrl', function ($scope, apiController, Restangular, user, callback, $timeout) { ;angular.module('app.frontend').controller('UsernameModalCtrl', function ($scope, apiController, Restangular, callback, $timeout) {
$scope.formData = {}; $scope.formData = {};
$scope.saveUsername = function () { $scope.saveUsername = function () {
apiController.setUsername(user, $scope.formData.username, function (response) { apiController.setUsername($scope.formData.username, function (response) {
var username = response.username; var username = response.username;
user.username = username;
callback(username); callback(username);
$scope.closeThisDialog(); $scope.closeThisDialog();
}); });
@@ -1889,13 +1871,6 @@ var Tag = function (_Item3) {
return Tag; return Tag;
}(Item); }(Item);
;
var User = function User(json_obj) {
_classCallCheck(this, User);
_.merge(this, json_obj);
};
;angular.module('app.frontend').provider('apiController', function () { ;angular.module('app.frontend').provider('apiController', function () {
function domainName() { function domainName() {
@@ -1922,9 +1897,8 @@ var User = function User(json_obj) {
function ApiController($rootScope, Restangular, modelManager, ngDialog) { function ApiController($rootScope, Restangular, modelManager, ngDialog) {
this.setUser = function (user) { this.user = {};
this.user = user; this.syncToken = localStorage.getItem("syncToken");
};
/* /*
Config Config
@@ -1953,7 +1927,11 @@ var User = function User(json_obj) {
*/ */
this.isUserSignedIn = function () { this.isUserSignedIn = function () {
return this.user.email && this.retrieveMk(); return localStorage.getItem("jwt");
};
this.userId = function () {
return localStorage.getItem("uuid");
}; };
this.getAuthParamsForEmail = function (email, callback) { this.getAuthParamsForEmail = function (email, callback) {
@@ -1973,7 +1951,8 @@ var User = function User(json_obj) {
} }
Restangular.one("users/current").get().then(function (response) { Restangular.one("users/current").get().then(function (response) {
var user = response.plain(); var user = response.plain();
callback(user); _.merge(this.user, user);
callback();
}.bind(this)).catch(function (response) { }.bind(this)).catch(function (response) {
console.log("Error getting current user", response); console.log("Error getting current user", response);
callback(response.data); callback(response.data);
@@ -1993,6 +1972,7 @@ var User = function User(json_obj) {
_.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);
localStorage.setItem("uuid", response.uuid);
callback(response); callback(response);
}).catch(function (response) { }).catch(function (response) {
callback(response.data); callback(response.data);
@@ -2010,6 +1990,7 @@ var User = function User(json_obj) {
_.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);
localStorage.setItem("uuid", response.uuid);
callback(response); callback(response);
}).catch(function (response) { }).catch(function (response) {
callback(response.data); callback(response.data);
@@ -2017,46 +1998,46 @@ var User = function User(json_obj) {
}.bind(this)); }.bind(this));
}; };
this.changePassword = function (user, current_password, new_password) { // this.changePassword = function(current_password, new_password) {
this.getAuthParamsForEmail(email, function (authParams) { // this.getAuthParamsForEmail(email, function(authParams){
if (!authParams) { // if(!authParams) {
callback(null); // callback(null);
return; // return;
} // }
Neeto.crypto.computeEncryptionKeysForUser(_.merge({ password: current_password, email: user.email }, authParams), function (currentKeys) { // Neeto.crypto.computeEncryptionKeysForUser(_.merge({password: current_password, email: user.email}, authParams), function(currentKeys) {
Neeto.crypto.computeEncryptionKeysForUser(_.merge({ password: new_password, email: user.email }, authParams), function (newKeys) { // Neeto.crypto.computeEncryptionKeysForUser(_.merge({password: new_password, email: user.email}, authParams), function(newKeys){
var data = {}; // var data = {};
data.current_password = currentKeys.pw; // data.current_password = currentKeys.pw;
data.password = newKeys.pw; // data.password = newKeys.pw;
data.password_confirmation = newKeys.pw; // data.password_confirmation = newKeys.pw;
//
var user = this.user; // var user = this.user;
//
this._performPasswordChange(currentKeys, newKeys, function (response) { // this._performPasswordChange(currentKeys, newKeys, function(response){
if (response && !response.error) { // if(response && !response.error) {
// this.showNewPasswordForm = false; // // this.showNewPasswordForm = false;
// reencrypt data with new mk // // reencrypt data with new mk
this.reencryptAllItemsAndSave(user, newKeys.mk, currentKeys.mk, function (success) { // this.reencryptAllItemsAndSave(user, newKeys.mk, currentKeys.mk, function(success){
if (success) { // if(success) {
this.setMk(newKeys.mk); // this.setMk(newKeys.mk);
alert("Your password has been changed and your data re-encrypted."); // alert("Your password has been changed and your data re-encrypted.");
} else { // } else {
// rollback password // // rollback password
this._performPasswordChange(newKeys, currentKeys, function (response) { // this._performPasswordChange(newKeys, currentKeys, function(response){
alert("There was an error changing your password. Your password has been rolled back."); // alert("There was an error changing your password. Your password has been rolled back.");
window.location.reload(); // window.location.reload();
}); // })
} // }
}.bind(this)); // }.bind(this));
} else { // } else {
// this.showNewPasswordForm = false; // // this.showNewPasswordForm = false;
alert("There was an error changing your password. Please try again."); // alert("There was an error changing your password. Please try again.");
} // }
}.bind(this)); // }.bind(this))
}.bind(this)); // }.bind(this));
}.bind(this)); // }.bind(this));
}.bind(this)); // }.bind(this));
}; // }
this._performPasswordChange = function (email, current_keys, new_keys, callback) { this._performPasswordChange = function (email, current_keys, new_keys, callback) {
var request = Restangular.one("auth"); var request = Restangular.one("auth");
@@ -2071,53 +2052,38 @@ var User = function User(json_obj) {
User User
*/ */
this.setUsername = function (user, username, callback) { this.setUsername = function (username, callback) {
var request = Restangular.one("users", user.uuid); var request = Restangular.one("users", this.userId());
request.username = username; request.username = username;
request.patch().then(function (response) { request.patch().then(function (response) {
this.user.username = response.username;
callback(response.plain()); callback(response.plain());
});
};
/*
Ensures that if encryption is disabled, all local items are uncrypted,
and that if it's enabled, that all local items are encrypted
*/
this.verifyEncryptionStatusOfAllItems = function (user, callback) {
var allItems = user.filteredItems();
var itemsNeedingUpdate = [];
allItems.forEach(function (item) {
if (!item.isPublic()) {
if (item.encryptionEnabled() && !item.isEncrypted()) {
itemsNeedingUpdate.push(item);
}
} else {
if (item.isEncrypted()) {
itemsNeedingUpdate.push(item);
}
}
}.bind(this)); }.bind(this));
if (itemsNeedingUpdate.length > 0) {
console.log("verifying encryption, items need updating", itemsNeedingUpdate);
this.saveBatchItems(user, itemsNeedingUpdate, callback);
}
}; };
/* /*
Items Items
*/ */
this.setSyncToken = function (syncToken) {
this.syncToken = syncToken;
localStorage.setItem("syncToken", this.syncToken);
};
this.syncWithOptions = function (callback) { this.syncWithOptions = function (callback) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (!this.user.uuid) { this.writeAllItemsToLocalStorage(function (responseItems) {
this.writeItemsToLocalStorage(function (responseItems) { if (!this.isUserSignedIn()) {
// is not signed in
modelManager.clearDirtyItems(); modelManager.clearDirtyItems();
if (callback) { if (callback) {
callback(); callback();
} }
}.bind(this)); }
}.bind(this));
if (!this.isUserSignedIn()) {
return; return;
} }
@@ -2135,7 +2101,7 @@ var User = function User(json_obj) {
request.post().then(function (response) { request.post().then(function (response) {
modelManager.clearDirtyItems(); modelManager.clearDirtyItems();
this.syncToken = response.sync_token; this.setSyncToken(response.sync_token);
$rootScope.$broadcast("sync:updated_token", this.syncToken); $rootScope.$broadcast("sync:updated_token", this.syncToken);
this.handleItemsResponse(response.retrieved_items, null); this.handleItemsResponse(response.retrieved_items, null);
@@ -2143,12 +2109,16 @@ var User = function User(json_obj) {
var omitFields = ["content", "enc_item_key", "auth_hash"]; var omitFields = ["content", "enc_item_key", "auth_hash"];
this.handleItemsResponse(response.saved_items, omitFields); this.handleItemsResponse(response.saved_items, omitFields);
this.writeAllItemsToLocalStorage();
if (callback) { if (callback) {
callback(response); callback(response);
} }
}.bind(this)).catch(function (response) { }.bind(this)).catch(function (response) {
console.log("Sync error: ", response); console.log("Sync error: ", response);
callback({ error: "Sync error" }); if (callback) {
callback({ error: "Sync error" });
}
}); });
}; };
@@ -2202,7 +2172,7 @@ var User = function User(json_obj) {
}; };
this.shareItem = function (item, callback) { this.shareItem = function (item, callback) {
if (!this.user.uuid) { if (!this.isUserSignedIn()) {
alert("You must be signed in to share."); alert("You must be signed in to share.");
return; return;
} }
@@ -2221,9 +2191,6 @@ var User = function User(json_obj) {
template: 'frontend/modals/username.html', template: 'frontend/modals/username.html',
controller: 'UsernameModalCtrl', controller: 'UsernameModalCtrl',
resolve: { resolve: {
user: function () {
return this.user;
}.bind(this),
callback: function callback() { callback: function callback() {
return shareFn; return shareFn;
} }
@@ -2295,49 +2262,54 @@ var User = function User(json_obj) {
/* /*
Merging Merging
*/ */
this.mergeLocalDataRemotely = function (user, callback) { // this.mergeLocalDataRemotely = function(user, callback) {
var request = Restangular.one("users", user.uuid).one("merge"); // var request = Restangular.one("users", this.userId()).one("merge");
var tags = user.tags; // var tags = user.tags;
request.items = user.items; // request.items = user.items;
request.items.forEach(function (item) { // request.items.forEach(function(item){
if (item.tag_id) { // if(item.tag_id) {
var tag = tags.filter(function (tag) { // var tag = tags.filter(function(tag){return tag.uuid == item.tag_id})[0];
return tag.uuid == item.tag_id; // item.tag_name = tag.title;
})[0]; // }
item.tag_name = tag.title; // })
} // request.post().then(function(response){
}); // callback();
request.post().then(function (response) { // localStorage.removeItem('user');
callback(); // })
localStorage.removeItem('user'); // }
});
this.clearLocalStorage = function () {
localStorage.removeItem("items");
localStorage.removeItem("mk");
localStorage.removeItem("jwt");
localStorage.removeItem("uuid");
localStorage.removeItem("syncToken");
}; };
this.staticifyObject = function (object) { this.staticifyObject = function (object) {
return JSON.parse(JSON.stringify(object)); return JSON.parse(JSON.stringify(object));
}; };
this.writeItemsToLocalStorage = function (callback) { this.writeAllItemsToLocalStorage = function (callback) {
var items = _.map(modelManager.allItems, function (item) { var items = _.map(modelManager.allItems, function (item) {
return this.paramsForItem(item, false, ["created_at", "updated_at"], false); return this.paramsForItem(item, this.isUserSignedIn(), ["created_at", "updated_at"], false);
}.bind(this)); }.bind(this));
console.log("Writing items to local", items); // console.log("Writing items to local", items);
this.writeToLocalStorage('items', items); this.writeToLocalStorage('items', items);
callback(items); if (callback) {
callback(items);
}
}; };
this.writeToLocalStorage = function (key, value) { this.writeToLocalStorage = function (key, value) {
localStorage.setItem(key, angular.toJson(value)); localStorage.setItem(key, angular.toJson(value));
}; };
this.loadLocalItemsAndUser = function () { this.loadLocalItems = function () {
var user = {};
var items = JSON.parse(localStorage.getItem('items')) || []; var items = JSON.parse(localStorage.getItem('items')) || [];
items = this.handleItemsResponse(items, null); items = this.handleItemsResponse(items, null);
Item.sortItemsByDate(items); Item.sortItemsByDate(items);
user.items = items;
user.shouldMerge = true;
return user;
}; };
/* /*
@@ -2456,7 +2428,7 @@ var User = function User(json_obj) {
}; };
this.reencryptAllItemsAndSave = function (user, newMasterKey, oldMasterKey, callback) { this.reencryptAllItemsAndSave = function (user, newMasterKey, oldMasterKey, callback) {
var items = user.filteredItems(); var items = modelManager.allItems();
items.forEach(function (item) { items.forEach(function (item) {
if (item.content.substring(0, 3) == "001" && item.enc_item_key) { if (item.content.substring(0, 3) == "001" && item.enc_item_key) {
// first decrypt item_key with old key // first decrypt item_key with old key

File diff suppressed because one or more lines are too long