From 94c280256e0a71fb8078f0d0e4d7ad2b6d3c1462 Mon Sep 17 00:00:00 2001 From: Aman Harwara Date: Thu, 22 Dec 2022 20:07:48 +0530 Subject: [PATCH] fix: Disabled Moments photo capture when app is passcode or biometrics locked (#2114) --- packages/snjs/lib/Application/Application.ts | 8 +++++-- .../Controllers/Moments/MomentsService.ts | 21 ++++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/packages/snjs/lib/Application/Application.ts b/packages/snjs/lib/Application/Application.ts index ce8180ff4..1a602cd72 100644 --- a/packages/snjs/lib/Application/Application.ts +++ b/packages/snjs/lib/Application/Application.ts @@ -178,6 +178,7 @@ export class SNApplication implements ApplicationInterface, AppGroupManagedAppli private launched = false /** Whether the application has been destroyed via .deinit() */ public dealloced = false + private isBiometricsSoftLockEngaged = false private revokingSession = false private handledFullSyncStage = false @@ -940,11 +941,12 @@ export class SNApplication implements ApplicationInterface, AppGroupManagedAppli return this.protocolService.hasPasscode() } - isLocked(): Promise { + async isLocked(): Promise { if (!this.started) { return Promise.resolve(true) } - return this.challengeService.isPasscodeLocked() + const isPasscodeLocked = await this.challengeService.isPasscodeLocked() + return isPasscodeLocked || this.isBiometricsSoftLockEngaged } public async lock(): Promise { @@ -964,10 +966,12 @@ export class SNApplication implements ApplicationInterface, AppGroupManagedAppli void this.promptForCustomChallenge(challenge) + this.isBiometricsSoftLockEngaged = true void this.notifyEvent(ApplicationEvent.BiometricsSoftLockEngaged) this.addChallengeObserver(challenge, { onComplete: () => { + this.isBiometricsSoftLockEngaged = false void this.notifyEvent(ApplicationEvent.BiometricsSoftLockDisengaged) }, }) diff --git a/packages/web/src/javascripts/Controllers/Moments/MomentsService.ts b/packages/web/src/javascripts/Controllers/Moments/MomentsService.ts index f0ce0f4fb..9e4e5b0d5 100644 --- a/packages/web/src/javascripts/Controllers/Moments/MomentsService.ts +++ b/packages/web/src/javascripts/Controllers/Moments/MomentsService.ts @@ -28,7 +28,13 @@ export class MomentsService extends AbstractViewController { if (this.isEnabled) { void this.beginTakingPhotos() } - }, ApplicationEvent.Launched), + }, ApplicationEvent.LocalDataLoaded), + application.addEventObserver(async () => { + this.disableMoments() + }, ApplicationEvent.BiometricsSoftLockEngaged), + application.addEventObserver(async () => { + this.enableMoments() + }, ApplicationEvent.BiometricsSoftLockDisengaged), ) makeObservable(this, { @@ -80,7 +86,13 @@ export class MomentsService extends AbstractViewController { } } - public async takePhoto(): Promise { + public takePhoto = async (): Promise => { + const isAppLocked = await this.application.isLocked() + + if (isAppLocked) { + return + } + const toastId = addToast({ type: ToastType.Loading, message: 'Capturing Moment...', @@ -123,7 +135,10 @@ export class MomentsService extends AbstractViewController { const uploadedFile = await this.filesController.uploadNewFile(file) if (uploadedFile) { - void this.application.linkingController.linkItemToSelectedItem(uploadedFile) + const isAppInForeground = document.visibilityState === 'visible' + if (isAppInForeground) { + void this.application.linkingController.linkItemToSelectedItem(uploadedFile) + } const defaultTag = this.getDefaultTag() if (defaultTag) {