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,
platformFromString
} from 'snjs';
import angular from 'angular';
import { getPlatformString } from '@/utils';
import { AlertManager } from '@/services/alertManager';
import { WebDeviceInterface } from '@/web_device_interface';
export class Application extends SNApplication {
/* @ngInject */
constructor($compile, $timeout, $rootScope) {
const deviceInterface = new WebDeviceInterface({timeout: $timeout});
constructor($timeout) {
const deviceInterface = new WebDeviceInterface({ timeout: $timeout });
super({
environment: Environments.Web,
platform: platformFromString(getPlatformString()),
@@ -27,28 +25,6 @@ export class Application extends SNApplication {
}
]
});
this.$compile = $compile;
this.$rootScope = $rootScope;
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,14 +14,14 @@ export class PureCtrl {
this.$timeout = $timeout;
this.appState = appState;
this.application = application;
this.state = {};
this.state = this.getInitialState();
this.props = {};
$scope.$on('$destroy', () => {
this.unsubApp();
this.unsubState();
});
}
$onInit() {
this.addAppStateObserver();
this.addAppEventObserver();
@@ -29,8 +29,13 @@ export class PureCtrl {
/** @private */
async resetState() {
this.state = {};
await this.setState({});
this.state = this.getInitialState();
await this.setState(this.state);
}
/** @override */
getInitialState() {
return {};
}
async setState(state) {

View File

@@ -63,8 +63,23 @@ class EditorCtrl extends PureCtrl {
this.desktopManager = desktopManager;
this.keyboardManager = keyboardManager;
this.preferencesManager = preferencesManager;
this.leftPanelPuppet = {
onReady: () => this.reloadPreferences()
};
this.rightPanelPuppet = {
onReady: () => this.reloadPreferences()
};
this.addSyncStatusObserver();
this.registerKeyboardShortcuts();
/** Used by .pug template */
this.prefKeyMonospace = PrefKeys.EditorMonospaceEnabled;
this.prefKeySpellcheck = PrefKeys.EditorSpellcheck;
this.prefKeyMarginResizers = PrefKeys.EditorResizersEnabled;
}
this.state = {
/** @override */
getInitialState() {
return {
componentStack: [],
editorDebounce: EDITOR_DEBOUNCE,
isDesktop: isDesktopApplication(),
@@ -73,21 +88,8 @@ class EditorCtrl extends PureCtrl {
tagsString: ''
}
};
this.leftPanelPuppet = {
onReady: () => this.reloadPreferences()
};
this.rightPanelPuppet = {
onReady: () => this.reloadPreferences()
};
this.addSyncStatusObserver();
this.registerKeyboardShortcuts();
/** Used by .pug template */
this.prefKeyMonospace = PrefKeys.EditorMonospaceEnabled;
this.prefKeySpellcheck = PrefKeys.EditorSpellcheck;
this.prefKeyMarginResizers = PrefKeys.EditorResizersEnabled;
}
onAppLaunch() {
super.onAppLaunch();
this.streamItems();
@@ -214,6 +216,9 @@ class EditorCtrl extends PureCtrl {
noteStatus: null
});
if (!note) {
this.setState({
noteReady: false
});
return;
}
const associatedEditor = this.editorForNote(note);

View File

@@ -2,6 +2,7 @@ import { Challenges, ChallengeResponse } from 'snjs';
import { getPlatformString } from '@/utils';
import template from '%/root.pug';
import { AppStateEvents } from '@/state';
import angular from 'angular';
import {
PANEL_NAME_NOTES,
PANEL_NAME_TAGS
@@ -16,6 +17,7 @@ import { PureCtrl } from './abstract/pure_ctrl';
class RootCtrl extends PureCtrl {
/* @ngInject */
constructor(
$compile,
$location,
$scope,
$rootScope,
@@ -31,6 +33,7 @@ class RootCtrl extends PureCtrl {
super($scope, $timeout, application, appState);
this.$location = $location;
this.$rootScope = $rootScope;
this.$compile = $compile;
this.desktopManager = desktopManager;
this.lockManager = lockManager;
this.statusManager = statusManager;
@@ -46,13 +49,14 @@ class RootCtrl extends PureCtrl {
onAppStart() {
super.onAppStart();
this.overrideComponentManagerFunctions();
this.application.componentManager.setDesktopManager(this.desktopManager);
this.setState({ ready: true });
}
onAppLaunch() {
super.onAppLaunch();
this.setState({ needsUnlock: false });
this.application.componentManager.setDesktopManager(this.desktopManager);
this.application.registerService(this.themeManager);
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() {
// this.syncStatusObserver = syncManager.registerSyncStatusObserver((status) => {
// if (status.retrievedCount > 20) {

View File

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

View File

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

View File

@@ -1,5 +1,5 @@
import {
PAYLOAD_SOURCE_REMOTE_ACTION_RETRIEVED,
import {
PAYLOAD_SOURCE_REMOTE_ACTION_RETRIEVED,
ContentTypes
} from 'snjs';
import template from '%/directives/revision-preview-modal.pug';
@@ -16,7 +16,6 @@ class RevisionPreviewModalCtrl {
this.$scope = $scope;
this.$timeout = $timeout;
this.application = application;
this.configure();
$scope.$on('$destroy', () => {
if (this.identifier) {
this.application.componentManager.deregisterHandler(this.identifier);
@@ -24,6 +23,10 @@ class RevisionPreviewModalCtrl {
});
}
$onInit() {
this.configure();
}
async configure() {
this.note = await this.application.createItem({
contentType: ContentTypes.Note,
@@ -43,7 +46,7 @@ class RevisionPreviewModalCtrl {
* interfere with active editor. Be sure to copy only the content, as the top level
* editor object has non-copyable properties like .window, which cannot be transfered
*/
const editorCopy = await this.application.createItem({
const editorCopy = await this.application.createItem({
contentType: ContentTypes.Component,
content: editorForNote.content
});
@@ -84,14 +87,14 @@ class RevisionPreviewModalCtrl {
});
} else {
const uuid = this.uuid;
item = this.application.findItem({uuid: uuid});
item = this.application.findItem({ uuid: uuid });
item.content = Object.assign({}, this.content);
await this.application.mergeItem({
item: item,
source: PAYLOAD_SOURCE_REMOTE_ACTION_RETRIEVED
});
}
this.application.saveItem({item});
this.application.saveItem({ item });
this.dismiss();
};

View File

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

View File

@@ -18,27 +18,58 @@ export class NativeExtManager {
this.unsub = application.addSingleEventObserver(ApplicationEvents.Launched, () => {
this.reload();
this.streamChanges();
});
}
isSystemExtension(extension) {
return this.nativeExtIds.includes(extension.uuid);
get extManagerPred() {
const extManagerId = 'org.standardnotes.extensions-manager';
return SFPredicate.CompoundPredicate([
new SFPredicate('content_type', '=', ContentTypes.Component),
new SFPredicate('package_info.identifier', '=', extManagerId)
]);
}
streamChanges() {
this.application.streamItems({
contentType: ContentTypes.Component,
stream: () => {
this.reload();
}
});
get batchManagerPred() {
const batchMgrId = 'org.standardnotes.batch-manager';
return SFPredicate.CompoundPredicate([
new SFPredicate('content_type', '=', ContentTypes.Component),
new SFPredicate('package_info.identifier', '=', batchMgrId)
]);
}
reload() {
this.nativeExtIds = [];
// this.resolveExtensionsManager();
// this.resolveBatchManager();
this.application.singletonManager.registerPredicate(this.extManagerPred);
this.application.singletonManager.registerPredicate(this.batchManagerPred);
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() {
@@ -84,38 +115,6 @@ export class NativeExtManager {
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() {
const url = window._batch_manager_location;
if (!url) {
@@ -153,14 +152,10 @@ export class NativeExtManager {
}
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({
predicate: predicate,
predicate: this.batchManagerPred,
createPayload: this.batchManagerTemplatePayload()
});
this.nativeExtIds.push(batchManager.uuid);
let needsSync = false;
if (isDesktopApplication()) {
if (!batchManager.local_url) {
@@ -182,6 +177,5 @@ export class NativeExtManager {
if (needsSync) {
this.application.saveItem({ item: batchManager });
}
}
}

View File

@@ -8,7 +8,7 @@
.sk-panel-header-title {{ctrl.state.title}}
a.sk-a.info.close-button(ng-click='ctrl.dismiss()') Close
.sk-panel-content
div(ng-if='ctrl.state.step == 3')
.sk-panel-section(ng-if='ctrl.state.step == 1')
.sk-panel-row
.sk-panel-column.stretch
form.sk-panel-form
@@ -31,7 +31,7 @@
placeholder='Confirm New 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')
p.sk-p.sk-panel-row.info-i Your password has been successfully changed.
div(ng-if='ctrl.props.securityUpdate')
@@ -43,7 +43,6 @@
.sk-panel-footer
.empty
a.sk-a.info.right(
ng-class="{'disabled' : ctrl.state.lockContinue}",
ng-click='ctrl.nextStep()',
ng-disabled='ctrl.state.lockContinue')
.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