From 9f214fff5d5ae0eebaf29347808d536175db3b1d Mon Sep 17 00:00:00 2001 From: DecafDev <40307803+decaf-dev@users.noreply.github.com> Date: Tue, 25 Jun 2024 20:32:59 -0600 Subject: [PATCH 1/4] fix: don't block plugin UI while waiting for request --- src/main.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main.ts b/src/main.ts index 960b656..ece27d7 100644 --- a/src/main.ts +++ b/src/main.ts @@ -22,9 +22,6 @@ export default class VaultExplorerPlugin extends Plugin { await this.loadSettings(); this.setupLogger(); - await loadDeviceId(); - await License.getInstance().verifyLicense(); - this.registerView( VAULT_EXPLORER_VIEW, (leaf) => new VaultExplorerView(leaf, this) @@ -50,6 +47,9 @@ export default class VaultExplorerPlugin extends Plugin { this.layoutReady = true; }); + await loadDeviceId(); + await License.getInstance().verifyLicense(); + } private registerEvents() { From 9db8b7a25bdfc24013b0c911c1e68cfaeec49c89 Mon Sep 17 00:00:00 2001 From: DecafDev <40307803+decaf-dev@users.noreply.github.com> Date: Tue, 25 Jun 2024 20:33:13 -0600 Subject: [PATCH 2/4] fix: allow offline access --- src/svelte/shared/services/license.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/svelte/shared/services/license.ts b/src/svelte/shared/services/license.ts index cd6986f..48cb668 100644 --- a/src/svelte/shared/services/license.ts +++ b/src/svelte/shared/services/license.ts @@ -7,6 +7,8 @@ export const LICENSE_KEY_LENGTH = 8; const LOCAL_STORAGE_LICENSE_KEY = "vault-explorer-license-key"; +const LOCAL_STORAGE_LICENSE_LAST_VALIDATION = "vault-explorer-license-last-validation"; + export default class License { private isDeviceRegistered: boolean; private licenseKey: string; @@ -31,6 +33,7 @@ export default class License { this.isDeviceRegistered = true; this.isDeviceRegisteredStore.set(true); this.setLicenseKey(licenseKey); + this.setLastValidation(true); } return result; } @@ -44,6 +47,7 @@ export default class License { this.isDeviceRegistered = false; this.isDeviceRegisteredStore.set(false); this.setLicenseKey(""); + this.setLastValidation(false); } return result; } @@ -65,6 +69,9 @@ export default class License { if (result) { this.isDeviceRegistered = true; this.isDeviceRegisteredStore.set(true); + this.setLastValidation(true); + } else { + this.setLastValidation(false); } } @@ -88,6 +95,13 @@ export default class License { } catch (err: unknown) { const error = err as Error; Logger.error({ fileName: "license.ts", functionName: "postVerifyDevice", message: "error verifying device" }, error.message); + + if (error.message.contains("net::ERR_INTERNET_DISCONNECTED")) { + const state = License.getInstance().getLastValidationState(); + Logger.debug({ fileName: "license.ts", functionName: "postVerifyDevice", message: "returning last validation state", }, state); + return state; + + } return false; } }; @@ -172,6 +186,7 @@ export default class License { } this.responseMessage = message; + Logger.error({ fileName: "license.ts", functionName: "postUnregisterDevice", message: "error unregistering device" }, error.message); return false; } @@ -182,6 +197,15 @@ export default class License { this.licenseKey = value; } + private setLastValidation(value: boolean) { + localStorage.setItem(LOCAL_STORAGE_LICENSE_LAST_VALIDATION, value.toString()); + } + + getLastValidationState() { + const value = localStorage.getItem(LOCAL_STORAGE_LICENSE_LAST_VALIDATION) ?? ""; + return value === "true"; + } + getIsDeviceRegistered() { return this.isDeviceRegistered; } From 938d8697fd438291dc4efd5244c4cbf4957425c3 Mon Sep 17 00:00:00 2001 From: DecafDev <40307803+decaf-dev@users.noreply.github.com> Date: Tue, 25 Jun 2024 21:11:04 -0600 Subject: [PATCH 3/4] fix: use last device registered value to handle slow network --- src/migrations/migrate_1_17_2.ts | 9 +++ src/svelte/shared/services/license.ts | 81 ++++++++++++++++++--------- 2 files changed, 64 insertions(+), 26 deletions(-) create mode 100644 src/migrations/migrate_1_17_2.ts diff --git a/src/migrations/migrate_1_17_2.ts b/src/migrations/migrate_1_17_2.ts new file mode 100644 index 0000000..bfc1b35 --- /dev/null +++ b/src/migrations/migrate_1_17_2.ts @@ -0,0 +1,9 @@ +import License from "src/svelte/shared/services/license"; +import MigrationInterface from "./migration_interface"; + +export default class Migrate_1_17_2 implements MigrationInterface { + migrate(data: Record) { + License.getInstance().setStoredDeviceRegistered(false); + return data as unknown as Record; + } +} diff --git a/src/svelte/shared/services/license.ts b/src/svelte/shared/services/license.ts index 48cb668..738eb94 100644 --- a/src/svelte/shared/services/license.ts +++ b/src/svelte/shared/services/license.ts @@ -7,7 +7,7 @@ export const LICENSE_KEY_LENGTH = 8; const LOCAL_STORAGE_LICENSE_KEY = "vault-explorer-license-key"; -const LOCAL_STORAGE_LICENSE_LAST_VALIDATION = "vault-explorer-license-last-validation"; +const LOCAL_STORAGE_DEVICE_REGISTERED = "vault-explorer-device-registration"; export default class License { private isDeviceRegistered: boolean; @@ -18,10 +18,16 @@ export default class License { private static instance: License; constructor() { - this.isDeviceRegistered = false; - this.isDeviceRegisteredStore.set(false); + const storedDeviceRegistered = this.getStoredDeviceRegistered(); + this.isDeviceRegistered = storedDeviceRegistered; + this.isDeviceRegisteredStore.set(storedDeviceRegistered); + Logger.debug({ fileName: "license.ts", functionName: "constructor", message: "loaded storedDeviceRegistered", }, storedDeviceRegistered); + this.responseMessage = ""; - this.licenseKey = localStorage.getItem(LOCAL_STORAGE_LICENSE_KEY) ?? ""; + + const storedKey = this.getStoredLicenseKey(); + this.licenseKey = storedKey; + Logger.debug({ fileName: "license.ts", functionName: "constructor", message: "loaded storedKey" }, storedKey); } async registerDevice(licenseKey: string) { @@ -30,10 +36,8 @@ export default class License { const deviceId = readDeviceId(); const result = await this.postRegisterDevice(licenseKey, deviceId); if (result) { - this.isDeviceRegistered = true; - this.isDeviceRegisteredStore.set(true); - this.setLicenseKey(licenseKey); - this.setLastValidation(true); + this.updateDeviceRegistered(true); + this.updateLicenseKey(licenseKey); } return result; } @@ -44,10 +48,8 @@ export default class License { const deviceId = readDeviceId(); const result = await this.postUnregisterDevice(this.licenseKey, deviceId); if (result) { - this.isDeviceRegistered = false; - this.isDeviceRegisteredStore.set(false); - this.setLicenseKey(""); - this.setLastValidation(false); + this.updateLicenseKey(""); + this.updateDeviceRegistered(false); } return result; } @@ -67,11 +69,9 @@ export default class License { const result = await this.postVerifyDevice(this.licenseKey, deviceId); if (result) { - this.isDeviceRegistered = true; - this.isDeviceRegisteredStore.set(true); - this.setLastValidation(true); + this.updateDeviceRegistered(true); } else { - this.setLastValidation(false); + this.updateDeviceRegistered(false); } } @@ -97,9 +97,9 @@ export default class License { Logger.error({ fileName: "license.ts", functionName: "postVerifyDevice", message: "error verifying device" }, error.message); if (error.message.contains("net::ERR_INTERNET_DISCONNECTED")) { - const state = License.getInstance().getLastValidationState(); - Logger.debug({ fileName: "license.ts", functionName: "postVerifyDevice", message: "returning last validation state", }, state); - return state; + const deviceRegistered = License.getInstance().getIsDeviceRegistered(); + Logger.debug({ fileName: "license.ts", functionName: "postVerifyDevice", message: "returning last deviceRegistered state", }, deviceRegistered); + return deviceRegistered; } return false; @@ -192,18 +192,47 @@ export default class License { } } - private setLicenseKey(value: string) { - localStorage.setItem(LOCAL_STORAGE_LICENSE_KEY, value); + /** + * Sets the class licenseKey and updates local storage + * @param value - The license key + */ + private updateLicenseKey(value: string) { + Logger.trace({ filename: "license.ts", functionName: "updateLicenseKey", message: "called" }); this.licenseKey = value; + this.setStoredLicenseKey(value); } - private setLastValidation(value: boolean) { - localStorage.setItem(LOCAL_STORAGE_LICENSE_LAST_VALIDATION, value.toString()); + /** + * Sets the class registration flag and updates local storage + * @param value - The registration status of the device + */ + private updateDeviceRegistered(value: boolean) { + Logger.trace({ filename: "license.ts", functionName: "updateDeviceRegistered", message: "called" }); + this.isDeviceRegistered = value; + this.isDeviceRegisteredStore.set(value); + this.setStoredDeviceRegistered(value); + } + + private setStoredLicenseKey(value: string) { + Logger.trace({ filename: "license.ts", functionName: "setStoredLicenseKey", message: "called" }); + localStorage.setItem(LOCAL_STORAGE_LICENSE_KEY, value); + } + + private getStoredLicenseKey() { + return localStorage.getItem(LOCAL_STORAGE_LICENSE_KEY) ?? "" + } + + private getStoredDeviceRegistered() { + const value = localStorage.getItem(LOCAL_STORAGE_DEVICE_REGISTERED); + if (value) { + return value === "true"; + } + return false; } - getLastValidationState() { - const value = localStorage.getItem(LOCAL_STORAGE_LICENSE_LAST_VALIDATION) ?? ""; - return value === "true"; + setStoredDeviceRegistered(value: boolean) { + Logger.trace({ filename: "license.ts", functionName: "setStoredDeviceRegistered", message: "called" }); + localStorage.setItem(LOCAL_STORAGE_DEVICE_REGISTERED, value.toString()); } getIsDeviceRegistered() { From 135e3cd5212a6a597cdb4f692dc4234cc44eaddc Mon Sep 17 00:00:00 2001 From: DecafDev <40307803+decaf-dev@users.noreply.github.com> Date: Tue, 25 Jun 2024 21:11:09 -0600 Subject: [PATCH 4/4] chore: bump version --- manifest.json | 2 +- package.json | 2 +- versions.json | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/manifest.json b/manifest.json index ebfba02..30c4268 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "vault-explorer", "name": "Vault Explorer", - "version": "1.17.1", + "version": "1.17.2", "minAppVersion": "1.4.13", "description": "Explore your vault in visual format", "author": "DecafDev", diff --git a/package.json b/package.json index 92f8521..012def3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "obsidian-vault-explorer", - "version": "1.17.1", + "version": "1.17.2", "description": "Explore your vault in visual format", "main": "main.js", "scripts": { diff --git a/versions.json b/versions.json index 1f0c3bd..882a783 100644 --- a/versions.json +++ b/versions.json @@ -74,5 +74,6 @@ "1.15.0": "1.4.13", "1.16.0": "1.4.13", "1.17.0": "1.4.13", - "1.17.1": "1.4.13" + "1.17.1": "1.4.13", + "1.17.2": "1.4.13" }