From c43b665db39e562005c4e557a18f6520cb093be9 Mon Sep 17 00:00:00 2001 From: Sale Djenic Date: Fri, 20 Jan 2023 09:31:11 +0100 Subject: [PATCH] chore(@desktop/general): map `defined(macosx)` to custom constant A custom `IS_MACOS` constant introduced which represents os defined macosx, cause for devs working on macos is easier to change a single constant in a single place to simulate some how the app would behave in some cases on other platforms. For example if the dev is working on macos, it's not possible to simulate linux only or win only features that way, but it's definitely possible to check if mac only features are not visible on other platforms. --- src/app/global/user_profile.nim | 3 ++- src/app/modules/startup/controller.nim | 5 +++-- .../internal/keycard_enter_pin_state.nim | 2 +- .../internal/keycard_enter_puk_state.nim | 8 ++++++++ .../internal/keycard_pin_set_state.nim | 19 ++++++++++++------- .../internal/keycard_wrong_pin_state.nim | 2 +- .../internal/keycard_wrong_puk_state.nim | 2 +- .../startup/internal/state_factory.nim | 1 + .../internal/user_profile_chat_key_state.nim | 4 ++-- .../user_profile_confirm_password_state.nim | 4 ++-- src/app/modules/startup/module.nim | 3 ++- src/app_service/common/utils.nim | 4 +++- src/app_service/service/accounts/service.nim | 2 +- src/app_service/service/language/service.nim | 4 +++- src/constants.nim | 8 ++++++-- src/nim_status_client.nim | 12 ++++++------ 16 files changed, 54 insertions(+), 29 deletions(-) diff --git a/src/app/global/user_profile.nim b/src/app/global/user_profile.nim index 403803ed99..2a74f63c40 100644 --- a/src/app/global/user_profile.nim +++ b/src/app/global/user_profile.nim @@ -1,5 +1,6 @@ import NimQml +import ../../constants as main_constants import local_account_settings QtObject: @@ -51,7 +52,7 @@ QtObject: read = getIsKeycardUser proc getUsingBiometricLogin*(self: UserProfile): bool {.slot.} = - if(not defined(macosx)): + if(not main_constants.IS_MACOS): return false return self.localAccountSettings.getStoreToKeychainValue() == LS_VALUE_STORE QtProperty[bool] usingBiometricLogin: diff --git a/src/app/modules/startup/controller.nim b/src/app/modules/startup/controller.nim index 13487af958..708f157552 100644 --- a/src/app/modules/startup/controller.nim +++ b/src/app/modules/startup/controller.nim @@ -2,6 +2,7 @@ import Tables, chronicles, strutils, os import uuids import io_interface +import ../../../constants as main_constants import ../../global/global_singleton import ../../core/signals/types import ../../core/eventemitter @@ -400,7 +401,7 @@ proc isSelectedAccountAKeycardAccount*(self: Controller): bool = proc tryToObtainDataFromKeychain*(self: Controller) = # Dealing with Keychain is the MacOS only feature - if(not defined(macosx)): + if not main_constants.IS_MACOS: return let value = singletonInstance.localAccountSettings.getStoreToKeychainValue() if (value != LS_VALUE_STORE): @@ -504,7 +505,7 @@ proc checkForStoringPasswordToKeychain*(self: Controller) = # This proc is called once user is logged in irrespective he is logged in # through the onboarding or login view. # This is MacOS only feature - if not defined(macosx): + if not main_constants.IS_MACOS: return let value = singletonInstance.localAccountSettings.getStoreToKeychainValue() diff --git a/src/app/modules/startup/internal/keycard_enter_pin_state.nim b/src/app/modules/startup/internal/keycard_enter_pin_state.nim index 1f7a45d8ee..32c19ecef4 100644 --- a/src/app/modules/startup/internal/keycard_enter_pin_state.nim +++ b/src/app/modules/startup/internal/keycard_enter_pin_state.nim @@ -55,7 +55,7 @@ method resolveKeycardNextState*(self: KeycardEnterPinState, keycardFlowType: str return createState(StateType.KeycardMaxPairingSlotsReached, self.flowType, self.getBackState) if keycardFlowType == ResponseTypeValueKeycardFlowResult: controller.setKeycardEvent(keycardEvent) - if not defined(macosx): + if not main_constants.IS_MACOS: controller.setupKeycardAccount(false) return nil return createState(StateType.Biometrics, self.flowType, self) \ No newline at end of file diff --git a/src/app/modules/startup/internal/keycard_enter_puk_state.nim b/src/app/modules/startup/internal/keycard_enter_puk_state.nim index 7778caaee9..46b7f8901b 100644 --- a/src/app/modules/startup/internal/keycard_enter_puk_state.nim +++ b/src/app/modules/startup/internal/keycard_enter_puk_state.nim @@ -33,6 +33,14 @@ method resolveKeycardNextState*(self: KeycardEnterPukState, keycardFlowType: str if keycardEvent.pukRetries > 0: return createState(StateType.KeycardWrongPuk, self.flowType, self.getBackState) return createState(StateType.KeycardMaxPukRetriesReached, self.flowType, self.getBackState) + if keycardFlowType == ResponseTypeValueKeycardFlowResult: + controller.setKeycardEvent(keycardEvent) + controller.setPukValid(true) + if not main_constants.IS_MACOS: + controller.setupKeycardAccount(false) + return nil + let backState = findBackStateWithTargetedStateType(self, StateType.RecoverOldUser) + return createState(StateType.Biometrics, self.flowType, backState) if self.flowType == FlowType.AppLogin: if keycardFlowType == ResponseTypeValueEnterNewPIN and keycardEvent.error.len > 0 and diff --git a/src/app/modules/startup/internal/keycard_pin_set_state.nim b/src/app/modules/startup/internal/keycard_pin_set_state.nim index 88d9762355..156c185876 100644 --- a/src/app/modules/startup/internal/keycard_pin_set_state.nim +++ b/src/app/modules/startup/internal/keycard_pin_set_state.nim @@ -19,7 +19,7 @@ method getNextPrimaryState*(self: KeycardPinSetState, controller: Controller): S return createState(StateType.UserProfileCreate, self.flowType, self.getBackState) if self.flowType == FlowType.FirstRunOldUserKeycardImport: if controller.getValidPuk(): - if not defined(macosx): + if not main_constants.IS_MACOS: return nil return createState(StateType.Biometrics, self.flowType, self.getBackState) return createState(StateType.KeycardWrongPuk, self.flowType, self.getBackState) @@ -31,20 +31,25 @@ method getNextPrimaryState*(self: KeycardPinSetState, controller: Controller): S return nil return createState(StateType.KeycardWrongPuk, self.flowType, self.getBackState) if self.flowType == FlowType.LostKeycardReplacement: - if not defined(macosx): + if not main_constants.IS_MACOS: return nil return createState(StateType.Biometrics, self.flowType, self.getBackState) method executePrimaryCommand*(self: KeycardPinSetState, controller: Controller) = - if defined(macosx): - return - # if controller.getValidPuk(): - # controller.setupKeycardAccount(false) - # return + if self.flowType == FlowType.FirstRunOldUserKeycardImport: + if main_constants.IS_MACOS: + return + if controller.getValidPuk(): + controller.setupKeycardAccount(false) if self.flowType == FlowType.AppLogin: if controller.getRecoverUsingSeedPhraseWhileLogin(): controller.startLoginFlowAutomatically(controller.getPin()) + return + if controller.getValidPuk(): + controller.setupKeycardAccount(false) if self.flowType == FlowType.LostKeycardReplacement: + if main_constants.IS_MACOS: + return controller.startLoginFlowAutomatically(controller.getPin()) method resolveKeycardNextState*(self: KeycardPinSetState, keycardFlowType: string, keycardEvent: KeycardEvent, diff --git a/src/app/modules/startup/internal/keycard_wrong_pin_state.nim b/src/app/modules/startup/internal/keycard_wrong_pin_state.nim index f3bb582b8b..3e251e7a2a 100644 --- a/src/app/modules/startup/internal/keycard_wrong_pin_state.nim +++ b/src/app/modules/startup/internal/keycard_wrong_pin_state.nim @@ -47,7 +47,7 @@ method resolveKeycardNextState*(self: KeycardWrongPinState, keycardFlowType: str return createState(StateType.KeycardMaxPairingSlotsReached, self.flowType, self.getBackState) if keycardFlowType == ResponseTypeValueKeycardFlowResult: controller.setKeycardEvent(keycardEvent) - if not defined(macosx): + if not main_constants.IS_MACOS: controller.setupKeycardAccount(false) return nil return createState(StateType.Biometrics, self.flowType, self) \ No newline at end of file diff --git a/src/app/modules/startup/internal/keycard_wrong_puk_state.nim b/src/app/modules/startup/internal/keycard_wrong_puk_state.nim index d369e96a36..f7977b3333 100644 --- a/src/app/modules/startup/internal/keycard_wrong_puk_state.nim +++ b/src/app/modules/startup/internal/keycard_wrong_puk_state.nim @@ -38,7 +38,7 @@ method resolveKeycardNextState*(self: KeycardWrongPukState, keycardFlowType: str if keycardFlowType == ResponseTypeValueKeycardFlowResult: controller.setKeycardEvent(keycardEvent) controller.setPukValid(true) - if not defined(macosx): + if not main_constants.IS_MACOS: controller.setupKeycardAccount(false) return nil return createState(StateType.Biometrics, self.flowType, self.getBackState) diff --git a/src/app/modules/startup/internal/state_factory.nim b/src/app/modules/startup/internal/state_factory.nim index 71ea8df8f1..535af368c9 100644 --- a/src/app/modules/startup/internal/state_factory.nim +++ b/src/app/modules/startup/internal/state_factory.nim @@ -1,4 +1,5 @@ import chronicles +import ../../../../constants as main_constants import ../../../../app_service/service/keycard/constants import ../controller from ../../../../app_service/service/keycard/service import KCSFlowType diff --git a/src/app/modules/startup/internal/user_profile_chat_key_state.nim b/src/app/modules/startup/internal/user_profile_chat_key_state.nim index 3ee71d49a7..51ecdb896f 100644 --- a/src/app/modules/startup/internal/user_profile_chat_key_state.nim +++ b/src/app/modules/startup/internal/user_profile_chat_key_state.nim @@ -9,7 +9,7 @@ proc delete*(self: UserProfileChatKeyState) = self.State.delete method executePrimaryCommand*(self: UserProfileChatKeyState, controller: Controller) = - if defined(macosx): + if main_constants.IS_MACOS: return let storeToKeychain = false # false, cause we don't have keychain support for other than mac os if self.flowType == FlowType.FirstRunNewUserNewKeycardKeys: @@ -26,6 +26,6 @@ method getNextPrimaryState*(self: UserProfileChatKeyState, controller: Controlle if self.flowType == FlowType.FirstRunNewUserNewKeycardKeys or self.flowType == FlowType.FirstRunNewUserImportSeedPhraseIntoKeycard or self.flowType == FlowType.FirstRunOldUserKeycardImport: - if not defined(macosx): + if not main_constants.IS_MACOS: return nil return createState(StateType.Biometrics, self.flowType, self) diff --git a/src/app/modules/startup/internal/user_profile_confirm_password_state.nim b/src/app/modules/startup/internal/user_profile_confirm_password_state.nim index 3fc35e5ea8..7aa12babb6 100644 --- a/src/app/modules/startup/internal/user_profile_confirm_password_state.nim +++ b/src/app/modules/startup/internal/user_profile_confirm_password_state.nim @@ -9,12 +9,12 @@ proc delete*(self: UserProfileConfirmPasswordState) = self.State.delete method getNextPrimaryState*(self: UserProfileConfirmPasswordState, controller: Controller): State = - if not defined(macosx): + if not main_constants.IS_MACOS: return nil return createState(StateType.Biometrics, self.flowType, self) method executePrimaryCommand*(self: UserProfileConfirmPasswordState, controller: Controller) = - if defined(macosx): + if main_constants.IS_MACOS: return let storeToKeychain = false # false, cause we don't have keychain support for other than mac os if self.flowType == FlowType.FirstRunNewUserNewKeys: diff --git a/src/app/modules/startup/module.nim b/src/app/modules/startup/module.nim index 2d8dd288ee..ee394dffcb 100644 --- a/src/app/modules/startup/module.nim +++ b/src/app/modules/startup/module.nim @@ -6,6 +6,7 @@ import internal/[state, state_factory] import models/generated_account_item as gen_acc_item import models/login_account_item as login_acc_item import models/fetching_data_model as fetch_model +import ../../../constants as main_constants import ../../global/global_singleton import ../../global/app_translatable_constants as atc import ../../core/eventemitter @@ -89,7 +90,7 @@ method load*[T](self: Module[T]) = self.view.setGeneratedAccountList(accounts) if(self.controller.shouldStartWithOnboardingScreen()): - if defined(macosx): + if main_constants.IS_MACOS: self.view.setCurrentStartupState(newNotificationState(FlowType.General, nil)) else: self.view.setCurrentStartupState(newWelcomeState(FlowType.General, nil)) diff --git a/src/app_service/common/utils.nim b/src/app_service/common/utils.nim index a1ded3a13a..cb4647d7b1 100644 --- a/src/app_service/common/utils.nim +++ b/src/app_service/common/utils.nim @@ -2,6 +2,8 @@ import json, random, times, strutils, sugar, os, re, chronicles import nimcrypto import signing_phrases +import ../../constants as main_constants + const STATUS_DOMAIN* = ".stateofus.eth" const ETH_DOMAIN* = ".eth" @@ -39,7 +41,7 @@ proc defaultDataDir(): string = parentDir(getAppDir()) elif homeDir == "": getCurrentDir() - elif defined(macosx): + elif main_constants.IS_MACOS: joinPath(homeDir, "Library", "Application Support") elif defined(windows): let targetDir = getEnv("LOCALAPPDATA").string diff --git a/src/app_service/service/accounts/service.nim b/src/app_service/service/accounts/service.nim index dd747ff26b..688de31b4c 100644 --- a/src/app_service/service/accounts/service.nim +++ b/src/app_service/service/accounts/service.nim @@ -340,7 +340,7 @@ QtObject: result["KeyStoreDir"] = newJString(self.keyStoreDir.replace(main_constants.STATUSGODIR, "")) proc setLocalAccountSettingsFile(self: Service) = - if(defined(macosx) and self.getLoggedInAccount.isValid()): + if(main_constants.IS_MACOS and self.getLoggedInAccount.isValid()): singletonInstance.localAccountSettings.setFileName(self.getLoggedInAccount.name) proc addKeycardDetails(self: Service, settingsJson: var JsonNode, accountData: var JsonNode) = diff --git a/src/app_service/service/language/service.nim b/src/app_service/service/language/service.nim index 83a0e64aaa..83858b9ac6 100644 --- a/src/app_service/service/language/service.nim +++ b/src/app_service/service/language/service.nim @@ -1,5 +1,7 @@ import NimQml import json_serialization, chronicles, os, strformat, re + +import ../../../constants as main_constants import ../../../app/global/global_singleton import ../../../app/core/eventemitter import ../../../app/core/signals/types @@ -47,7 +49,7 @@ proc init*(self: Service) = self.i18nPath = joinPath(getAppDir(), "i18n") elif (defined(windows)): self.i18nPath = joinPath(getAppDir(), "../resources/i18n") - elif (defined(macosx)): + elif (main_constants.IS_MACOS): self.i18nPath = joinPath(getAppDir(), "../i18n") elif (defined(linux)): self.i18nPath = joinPath(getAppDir(), "../i18n") diff --git a/src/constants.nim b/src/constants.nim index ac2fc78eb4..908215dd28 100644 --- a/src/constants.nim +++ b/src/constants.nim @@ -3,6 +3,10 @@ import os, sequtils, strutils import # vendor libs confutils +## Added a constant here cause it's easier to check the app how it behaves +## on other platform if we just change the value here +const IS_MACOS* = defined(macosx) + const sep* = when defined(windows): "\\" else: "/" proc defaultDataDir*(): string = @@ -12,7 +16,7 @@ proc defaultDataDir*(): string = parentDir(getAppDir()) elif homeDir == "": getCurrentDir() - elif defined(macosx): + elif IS_MACOS: joinPath(homeDir, "Library", "Application Support") elif defined(windows): let targetDir = getEnv("LOCALAPPDATA").string @@ -46,7 +50,7 @@ type StatusDesktopConfig = object # Credit: https://github.com/bitcoin/bitcoin/blame/b6e34afe9735faf97d6be7a90fafd33ec18c0cbb/src/util/system.cpp#L383-L389 var cliParams = commandLineParams() -if defined(macosx): +if IS_MACOS: cliParams.keepIf(proc(p: string): bool = not p.startsWith("-psn_")) let desktopConfig = StatusDesktopConfig.load(cliParams) diff --git a/src/nim_status_client.nim b/src/nim_status_client.nim index 8549f763b6..88711d20c9 100644 --- a/src/nim_status_client.nim +++ b/src/nim_status_client.nim @@ -3,7 +3,7 @@ import NimQml, chronicles, os, strformat, strutils, times, md5, json import status_go import keycard_go import app/core/main -import constants +import constants as main_constants import app/global/global_singleton import app/boot/app_controller @@ -29,14 +29,14 @@ proc determineOpenUri(): string = proc determineStatusAppIconPath(): string = if defined(production): - if defined(macosx): + if main_constants.IS_MACOS: return "" # not used in macOS - elif defined(windows): + elif main_constants.IS_MACOS: return "/../resources/status.svg" else: return "/../status.svg" else: - if defined(macosx): + if main_constants.IS_MACOS: return "" # not used in macOS else: return "/../status-dev.svg" @@ -78,7 +78,7 @@ proc setupRemoteSignalsHandling() = keycard_go.setSignalEventCallback(callbackKeycardGo) proc mainProc() = - if defined(macosx) and defined(production): + if main_constants.IS_MACOS and defined(production): setCurrentDir(getAppDir()) ensureDirectories(DATADIR, TMPDIR, LOGDIR) @@ -121,7 +121,7 @@ proc mainProc() = let dockShowAppEvent = newStatusDockShowAppEventObject(singletonInstance.engine) let osThemeEvent = newStatusOSThemeEventObject(singletonInstance.engine) - if not defined(macosx): + if not main_constants.IS_MACOS: app.icon(app.applicationDirPath & statusAppIconPath) prepareLogging()