Improvements to native ext mgr and password wizard

This commit is contained in:
Mo Bitar
2020-02-17 12:08:21 -06:00
parent 6fc53f3920
commit c6198a4612
12 changed files with 544 additions and 594 deletions

View File

@@ -4,16 +4,14 @@ import {
Environments, Environments,
platformFromString platformFromString
} from 'snjs'; } from 'snjs';
import angular from 'angular';
import { getPlatformString } from '@/utils'; import { getPlatformString } from '@/utils';
import { AlertManager } from '@/services/alertManager'; import { AlertManager } from '@/services/alertManager';
import { WebDeviceInterface } from '@/web_device_interface'; import { WebDeviceInterface } from '@/web_device_interface';
export class Application extends SNApplication { export class Application extends SNApplication {
/* @ngInject */ /* @ngInject */
constructor($compile, $timeout, $rootScope) { constructor($timeout) {
const deviceInterface = new WebDeviceInterface({timeout: $timeout}); const deviceInterface = new WebDeviceInterface({ timeout: $timeout });
super({ super({
environment: Environments.Web, environment: Environments.Web,
platform: platformFromString(getPlatformString()), platform: platformFromString(getPlatformString()),
@@ -27,28 +25,6 @@ export class Application extends SNApplication {
} }
] ]
}); });
this.$compile = $compile;
this.$rootScope = $rootScope;
deviceInterface.setApplication(this); deviceInterface.setApplication(this);
this.overrideComponentManagerFunctions();
}
overrideComponentManagerFunctions() {
function openModalComponent(component) {
const scope = this.$rootScope.$new(true);
scope.component = component;
const el = this.$compile("<component-modal component='component' class='sk-modal'></component-modal>")(scope);
angular.element(document.body).append(el);
}
function presentPermissionsDialog(dialog) {
const scope = this.$rootScope.$new(true);
scope.permissionsString = dialog.permissionsString;
scope.component = dialog.component;
scope.callback = dialog.callback;
const el = this.$compile("<permissions-modal component='component' permissions-string='permissionsString' callback='callback' class='sk-modal'></permissions-modal>")(scope);
angular.element(document.body).append(el);
}
this.componentManager.openModalComponent = openModalComponent.bind(this);
this.componentManager.presentPermissionsDialog = presentPermissionsDialog.bind(this);
} }
} }

View File

@@ -14,7 +14,7 @@ export class PureCtrl {
this.$timeout = $timeout; this.$timeout = $timeout;
this.appState = appState; this.appState = appState;
this.application = application; this.application = application;
this.state = {}; this.state = this.getInitialState();
this.props = {}; this.props = {};
$scope.$on('$destroy', () => { $scope.$on('$destroy', () => {
this.unsubApp(); this.unsubApp();
@@ -29,8 +29,13 @@ export class PureCtrl {
/** @private */ /** @private */
async resetState() { async resetState() {
this.state = {}; this.state = this.getInitialState();
await this.setState({}); await this.setState(this.state);
}
/** @override */
getInitialState() {
return {};
} }
async setState(state) { async setState(state) {

View File

@@ -63,17 +63,6 @@ class EditorCtrl extends PureCtrl {
this.desktopManager = desktopManager; this.desktopManager = desktopManager;
this.keyboardManager = keyboardManager; this.keyboardManager = keyboardManager;
this.preferencesManager = preferencesManager; this.preferencesManager = preferencesManager;
this.state = {
componentStack: [],
editorDebounce: EDITOR_DEBOUNCE,
isDesktop: isDesktopApplication(),
spellcheck: true,
mutable: {
tagsString: ''
}
};
this.leftPanelPuppet = { this.leftPanelPuppet = {
onReady: () => this.reloadPreferences() onReady: () => this.reloadPreferences()
}; };
@@ -88,6 +77,19 @@ class EditorCtrl extends PureCtrl {
this.prefKeyMarginResizers = PrefKeys.EditorResizersEnabled; this.prefKeyMarginResizers = PrefKeys.EditorResizersEnabled;
} }
/** @override */
getInitialState() {
return {
componentStack: [],
editorDebounce: EDITOR_DEBOUNCE,
isDesktop: isDesktopApplication(),
spellcheck: true,
mutable: {
tagsString: ''
}
};
}
onAppLaunch() { onAppLaunch() {
super.onAppLaunch(); super.onAppLaunch();
this.streamItems(); this.streamItems();
@@ -214,6 +216,9 @@ class EditorCtrl extends PureCtrl {
noteStatus: null noteStatus: null
}); });
if (!note) { if (!note) {
this.setState({
noteReady: false
});
return; return;
} }
const associatedEditor = this.editorForNote(note); const associatedEditor = this.editorForNote(note);

View File

@@ -2,6 +2,7 @@ import { Challenges, ChallengeResponse } from 'snjs';
import { getPlatformString } from '@/utils'; import { getPlatformString } from '@/utils';
import template from '%/root.pug'; import template from '%/root.pug';
import { AppStateEvents } from '@/state'; import { AppStateEvents } from '@/state';
import angular from 'angular';
import { import {
PANEL_NAME_NOTES, PANEL_NAME_NOTES,
PANEL_NAME_TAGS PANEL_NAME_TAGS
@@ -16,6 +17,7 @@ import { PureCtrl } from './abstract/pure_ctrl';
class RootCtrl extends PureCtrl { class RootCtrl extends PureCtrl {
/* @ngInject */ /* @ngInject */
constructor( constructor(
$compile,
$location, $location,
$scope, $scope,
$rootScope, $rootScope,
@@ -31,6 +33,7 @@ class RootCtrl extends PureCtrl {
super($scope, $timeout, application, appState); super($scope, $timeout, application, appState);
this.$location = $location; this.$location = $location;
this.$rootScope = $rootScope; this.$rootScope = $rootScope;
this.$compile = $compile;
this.desktopManager = desktopManager; this.desktopManager = desktopManager;
this.lockManager = lockManager; this.lockManager = lockManager;
this.statusManager = statusManager; this.statusManager = statusManager;
@@ -46,13 +49,14 @@ class RootCtrl extends PureCtrl {
onAppStart() { onAppStart() {
super.onAppStart(); super.onAppStart();
this.overrideComponentManagerFunctions();
this.application.componentManager.setDesktopManager(this.desktopManager);
this.setState({ ready: true }); this.setState({ ready: true });
} }
onAppLaunch() { onAppLaunch() {
super.onAppLaunch(); super.onAppLaunch();
this.setState({ needsUnlock: false }); this.setState({ needsUnlock: false });
this.application.componentManager.setDesktopManager(this.desktopManager);
this.application.registerService(this.themeManager); this.application.registerService(this.themeManager);
this.handleAutoSignInFromParams(); this.handleAutoSignInFromParams();
} }
@@ -119,6 +123,25 @@ class RootCtrl extends PureCtrl {
} }
} }
overrideComponentManagerFunctions() {
function openModalComponent(component) {
const scope = this.$rootScope.$new(true);
scope.component = component;
const el = this.$compile("<component-modal component='component' class='sk-modal'></component-modal>")(scope);
angular.element(document.body).append(el);
}
function presentPermissionsDialog(dialog) {
const scope = this.$rootScope.$new(true);
scope.permissionsString = dialog.permissionsString;
scope.component = dialog.component;
scope.callback = dialog.callback;
const el = this.$compile("<permissions-modal component='component' permissions-string='permissionsString' callback='callback' class='sk-modal'></permissions-modal>")(scope);
angular.element(document.body).append(el);
}
this.application.componentManager.openModalComponent = openModalComponent.bind(this);
this.application.componentManager.presentPermissionsDialog = presentPermissionsDialog.bind(this);
}
// addSyncStatusObserver() { // addSyncStatusObserver() {
// this.syncStatusObserver = syncManager.registerSyncStatusObserver((status) => { // this.syncStatusObserver = syncManager.registerSyncStatusObserver((status) => {
// if (status.retrievedCount > 20) { // if (status.retrievedCount > 20) {

View File

@@ -12,6 +12,9 @@ class ActionsMenuCtrl extends PureCtrl {
) { ) {
super($scope, $timeout, application, appState); super($scope, $timeout, application, appState);
this.godService = godService; this.godService = godService;
this.state = {
extensions: []
};
} }
$onInit() { $onInit() {
@@ -28,7 +31,10 @@ class ActionsMenuCtrl extends PureCtrl {
}); });
for (const extension of extensions) { for (const extension of extensions) {
extension.loading = true; extension.loading = true;
await this.application.actionsManager.loadExtensionInContextOfItem(extension, this.props.item); await this.application.actionsManager.loadExtensionInContextOfItem(
extension,
this.props.item
);
extension.loading = false; extension.loading = false;
} }
this.setState({ this.setState({
@@ -46,17 +52,22 @@ class ActionsMenuCtrl extends PureCtrl {
return; return;
} }
action.running = true; action.running = true;
const result = await this.application.actionsManager.executeAction( const result = await this.application.actionsManager.runAction({
action, action: action,
extension, item: this.props.item,
this.props.item passwordRequestHandler: () => {
);
}
});
if (action.error) { if (action.error) {
return; return;
} }
action.running = false; action.running = false;
this.handleActionResult(action, result); this.handleActionResult(action, result);
await this.application.actionsManager.loadExtensionInContextOfItem(extension, this.props.item); await this.application.actionsManager.loadExtensionInContextOfItem(
extension,
this.props.item
);
this.setState({ this.setState({
extensions: this.state.extensions extensions: this.state.extensions
}); });

View File

@@ -3,8 +3,8 @@ import { PureCtrl } from '@Controllers';
const DEFAULT_CONTINUE_TITLE = "Continue"; const DEFAULT_CONTINUE_TITLE = "Continue";
const Steps = { const Steps = {
PasswordStep: 3, PasswordStep: 1,
FinishStep: 5 FinishStep: 2
}; };
class PasswordWizardCtrl extends PureCtrl { class PasswordWizardCtrl extends PureCtrl {
@@ -24,10 +24,11 @@ class PasswordWizardCtrl extends PureCtrl {
} }
$onInit() { $onInit() {
super.$onInit();
this.initProps({ this.initProps({
type: this.type, type: this.type,
changePassword: this.props.type === 'change-pw', changePassword: this.type === 'change-pw',
securityUpdate: this.props.type === 'upgrade-security' securityUpdate: this.type === 'upgrade-security'
}); });
this.setState({ this.setState({
formData: {}, formData: {},
@@ -47,52 +48,44 @@ class PasswordWizardCtrl extends PureCtrl {
}); });
} }
titleForStep(step) { resetContinueState() {
switch (step) { this.setState({
case Steps.PasswordStep: showSpinner: false,
return this.props.changePassword continueTitle: DEFAULT_CONTINUE_TITLE
? "Password information" });
: "Enter your current password"; this.isContinuing = false;
case Steps.FinishStep:
return "Success";
default:
return null;
}
} }
async nextStep() { async nextStep() {
if (this.state.lockContinue || this.isContinuing) { if (this.state.lockContinue || this.isContinuing) {
return; return;
} }
this.isContinuing = true; if (this.state.step === Steps.FinishStep) {
if (this.step === Steps.FinishStep) {
this.dismiss(); this.dismiss();
return; return;
} }
if(this.step === Steps.PasswordStep) {
this.isContinuing = true;
this.setState({ this.setState({
showSpinner: true, showSpinner: true,
continueTitle: "Generating Keys..." continueTitle: "Generating Keys..."
}); });
const success = await this.validateCurrentPassword(); const valid = await this.validateCurrentPassword();
this.setState({ if (!valid) {
showSpinner: false, this.resetContinueState();
continueTitle: DEFAULT_CONTINUE_TITLE return;
}); }
if(!success) { const success = await this.processPasswordChange();
if (!success) {
this.resetContinueState();
return; return;
} }
this.isContinuing = false; this.isContinuing = false;
} this.setState({
this.step++; showSpinner: false,
this.initializeStep(this.step); continueTitle: "Finish",
this.isContinuing = false; step: Steps.FinishStep
} });
async initializeStep(step) {
if (step === Steps.FinishStep) {
this.continueTitle = "Finish";
}
} }
async setFormDataState(formData) { async setFormDataState(formData) {
@@ -104,36 +97,6 @@ class PasswordWizardCtrl extends PureCtrl {
}); });
} }
async initializeSyncingStep() {
this.setState({
lockContinue: true,
processing: true
});
this.setFormDataState({
status: "Processing encryption keys..."
});
const passwordSuccess = await this.processPasswordChange();
this.setFormDataState({
statusError: !passwordSuccess,
processing: passwordSuccess
});
if (!passwordSuccess) {
this.setFormDataState({
status: "Unable to process your password. Please try again."
});
return;
}
this.setState({
lockContinue: false,
formData: {
...this.state.formData,
status: this.props.changePassword
? "Successfully changed password."
: "Successfully performed account update."
}
});
}
async validateCurrentPassword() { async validateCurrentPassword() {
const currentPassword = this.state.formData.currentPassword; const currentPassword = this.state.formData.currentPassword;
const newPass = this.props.securityUpdate ? currentPassword : this.state.formData.newPassword; const newPass = this.props.securityUpdate ? currentPassword : this.state.formData.newPassword;
@@ -179,25 +142,47 @@ class PasswordWizardCtrl extends PureCtrl {
} }
async processPasswordChange() { async processPasswordChange() {
this.setState({
lockContinue: true,
processing: true
});
this.setFormDataState({
status: "Processing encryption keys..."
});
const newPassword = this.props.securityUpdate const newPassword = this.props.securityUpdate
? this.state.formData.currentPassword ? this.state.formData.currentPassword
: this.state.formData.newPassword; : this.state.formData.newPassword;
const response = await this.application.changePassword({ const response = await this.application.changePassword({
email: this.application.getUser().email, email: this.application.getUser().email,
currentPassword: this.state.formData.currentPassword, currentPassword: this.state.formData.currentPassword,
newPassword: newPassword newPassword: newPassword
}); });
if (response.error) { const success = !response.error;
this.setFormDataState({
statusError: !success,
processing: success
});
if (!success) {
this.application.alertManager.alert({ this.application.alertManager.alert({
text: response.error.message text: response.error.message
? response.error.message ? response.error.message
: "There was an error changing your password. Please try again." : "There was an error changing your password. Please try again."
}); });
return false; this.setFormDataState({
status: "Unable to process your password. Please try again."
});
} else { } else {
return true; this.setState({
lockContinue: false,
formData: {
...this.state.formData,
status: this.props.changePassword
? "Successfully changed password."
: "Successfully performed account update."
} }
});
}
return success;
} }
dismiss() { dismiss() {

View File

@@ -16,7 +16,6 @@ class RevisionPreviewModalCtrl {
this.$scope = $scope; this.$scope = $scope;
this.$timeout = $timeout; this.$timeout = $timeout;
this.application = application; this.application = application;
this.configure();
$scope.$on('$destroy', () => { $scope.$on('$destroy', () => {
if (this.identifier) { if (this.identifier) {
this.application.componentManager.deregisterHandler(this.identifier); this.application.componentManager.deregisterHandler(this.identifier);
@@ -24,6 +23,10 @@ class RevisionPreviewModalCtrl {
}); });
} }
$onInit() {
this.configure();
}
async configure() { async configure() {
this.note = await this.application.createItem({ this.note = await this.application.createItem({
contentType: ContentTypes.Note, contentType: ContentTypes.Note,
@@ -84,14 +87,14 @@ class RevisionPreviewModalCtrl {
}); });
} else { } else {
const uuid = this.uuid; const uuid = this.uuid;
item = this.application.findItem({uuid: uuid}); item = this.application.findItem({ uuid: uuid });
item.content = Object.assign({}, this.content); item.content = Object.assign({}, this.content);
await this.application.mergeItem({ await this.application.mergeItem({
item: item, item: item,
source: PAYLOAD_SOURCE_REMOTE_ACTION_RETRIEVED source: PAYLOAD_SOURCE_REMOTE_ACTION_RETRIEVED
}); });
} }
this.application.saveItem({item}); this.application.saveItem({ item });
this.dismiss(); this.dismiss();
}; };

View File

@@ -1,11 +1,11 @@
import { EncryptionIntents, ProtectedActions } from 'snjs'; import { EncryptionIntents, ProtectedActions } from 'snjs';
export class ArchiveManager { export class ArchiveManager {
/* @ngInject */ /* @ngInject */
constructor(lockManager, application) { constructor(lockManager, application, godService) {
this.lockManager = lockManager; this.lockManager = lockManager;
this.application = application; this.application = application;
this.godService = godService;
} }
/* /*

View File

@@ -18,27 +18,58 @@ export class NativeExtManager {
this.unsub = application.addSingleEventObserver(ApplicationEvents.Launched, () => { this.unsub = application.addSingleEventObserver(ApplicationEvents.Launched, () => {
this.reload(); this.reload();
this.streamChanges();
}); });
} }
isSystemExtension(extension) { get extManagerPred() {
return this.nativeExtIds.includes(extension.uuid); const extManagerId = 'org.standardnotes.extensions-manager';
return SFPredicate.CompoundPredicate([
new SFPredicate('content_type', '=', ContentTypes.Component),
new SFPredicate('package_info.identifier', '=', extManagerId)
]);
} }
streamChanges() { get batchManagerPred() {
this.application.streamItems({ const batchMgrId = 'org.standardnotes.batch-manager';
contentType: ContentTypes.Component, return SFPredicate.CompoundPredicate([
stream: () => { new SFPredicate('content_type', '=', ContentTypes.Component),
this.reload(); new SFPredicate('package_info.identifier', '=', batchMgrId)
} ]);
});
} }
reload() { reload() {
this.nativeExtIds = []; this.application.singletonManager.registerPredicate(this.extManagerPred);
// this.resolveExtensionsManager(); this.application.singletonManager.registerPredicate(this.batchManagerPred);
// this.resolveBatchManager(); this.resolveExtensionsManager();
this.resolveBatchManager();
}
async resolveExtensionsManager() {
const extensionsManager = await this.application.singletonManager.findOrCreateSingleton({
predicate: this.extManagerPred,
createPayload: this.extensionsManagerTemplatePayload()
});
let needsSync = false;
if (isDesktopApplication()) {
if (!extensionsManager.local_url) {
extensionsManager.local_url = window._extensions_manager_location;
needsSync = true;
}
} else {
if (!extensionsManager.hosted_url) {
extensionsManager.hosted_url = window._extensions_manager_location;
needsSync = true;
}
}
// Handle addition of SN|ExtensionRepo permission
const permission = extensionsManager.content.permissions.find((p) => p.name === STREAM_ITEMS_PERMISSION);
if (!permission.content_types.includes(ContentTypes.ExtensionRepo)) {
permission.content_types.push(ContentTypes.ExtensionRepo);
needsSync = true;
}
if (needsSync) {
this.application.saveItem({ item: extensionsManager });
}
} }
extensionsManagerTemplatePayload() { extensionsManagerTemplatePayload() {
@@ -84,38 +115,6 @@ export class NativeExtManager {
return payload; return payload;
} }
async resolveExtensionsManager() {
const contentTypePredicate = new SFPredicate('content_type', '=', ContentTypes.Component);
const packagePredicate = new SFPredicate('package_info.identifier', '=', this.extManagerId);
const predicate = SFPredicate.CompoundPredicate([contentTypePredicate, packagePredicate]);
const extensionsManager = await this.application.singletonManager.findOrCreateSingleton({
predicate: predicate,
createPayload: this.extensionsManagerTemplatePayload()
});
this.nativeExtIds.push(extensionsManager.uuid);
let needsSync = false;
if (isDesktopApplication()) {
if (!extensionsManager.local_url) {
extensionsManager.local_url = window._extensions_manager_location;
needsSync = true;
}
} else {
if (!extensionsManager.hosted_url) {
extensionsManager.hosted_url = window._extensions_manager_location;
needsSync = true;
}
}
// Handle addition of SN|ExtensionRepo permission
const permission = extensionsManager.content.permissions.find((p) => p.name === STREAM_ITEMS_PERMISSION);
if (!permission.content_types.includes(ContentTypes.ExtensionRepo)) {
permission.content_types.push(ContentTypes.ExtensionRepo);
needsSync = true;
}
if (needsSync) {
this.application.saveItem({ item: extensionsManager });
}
}
batchManagerTemplatePayload() { batchManagerTemplatePayload() {
const url = window._batch_manager_location; const url = window._batch_manager_location;
if (!url) { if (!url) {
@@ -153,14 +152,10 @@ export class NativeExtManager {
} }
async resolveBatchManager() { async resolveBatchManager() {
const contentTypePredicate = new SFPredicate('content_type', '=', ContentTypes.Component);
const packagePredicate = new SFPredicate('package_info.identifier', '=', this.batchManagerId);
const predicate = SFPredicate.CompoundPredicate([contentTypePredicate, packagePredicate]);
const batchManager = await this.application.singletonManager.findOrCreateSingleton({ const batchManager = await this.application.singletonManager.findOrCreateSingleton({
predicate: predicate, predicate: this.batchManagerPred,
createPayload: this.batchManagerTemplatePayload() createPayload: this.batchManagerTemplatePayload()
}); });
this.nativeExtIds.push(batchManager.uuid);
let needsSync = false; let needsSync = false;
if (isDesktopApplication()) { if (isDesktopApplication()) {
if (!batchManager.local_url) { if (!batchManager.local_url) {
@@ -182,6 +177,5 @@ export class NativeExtManager {
if (needsSync) { if (needsSync) {
this.application.saveItem({ item: batchManager }); this.application.saveItem({ item: batchManager });
} }
} }
} }

View File

@@ -8,7 +8,7 @@
.sk-panel-header-title {{ctrl.state.title}} .sk-panel-header-title {{ctrl.state.title}}
a.sk-a.info.close-button(ng-click='ctrl.dismiss()') Close a.sk-a.info.close-button(ng-click='ctrl.dismiss()') Close
.sk-panel-content .sk-panel-content
div(ng-if='ctrl.state.step == 3') .sk-panel-section(ng-if='ctrl.state.step == 1')
.sk-panel-row .sk-panel-row
.sk-panel-column.stretch .sk-panel-column.stretch
form.sk-panel-form form.sk-panel-form
@@ -31,7 +31,7 @@
placeholder='Confirm New Password', placeholder='Confirm New Password',
type='password' type='password'
) )
div(ng-if='ctrl.state.step == 5') .sk-panel-section(ng-if='ctrl.state.step == 2')
div(ng-if='ctrl.props.changePassword') div(ng-if='ctrl.props.changePassword')
p.sk-p.sk-panel-row.info-i Your password has been successfully changed. p.sk-p.sk-panel-row.info-i Your password has been successfully changed.
div(ng-if='ctrl.props.securityUpdate') div(ng-if='ctrl.props.securityUpdate')
@@ -43,7 +43,6 @@
.sk-panel-footer .sk-panel-footer
.empty .empty
a.sk-a.info.right( a.sk-a.info.right(
ng-class="{'disabled' : ctrl.state.lockContinue}",
ng-click='ctrl.nextStep()', ng-click='ctrl.nextStep()',
ng-disabled='ctrl.state.lockContinue') ng-disabled='ctrl.state.lockContinue')
.sk-spinner.small.inline.info.mr-5(ng-if='ctrl.state.showSpinner') .sk-spinner.small.inline.info.mr-5(ng-if='ctrl.state.showSpinner')

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long