Skip to content

Commit

Permalink
Merge pull request #134 from decaf-dev/fix-offline-access
Browse files Browse the repository at this point in the history
Fix offline access
  • Loading branch information
decaf-dev committed Jun 26, 2024
2 parents 1f52eda + 135e3cd commit 33bd590
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 19 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
6 changes: 3 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -50,6 +47,9 @@ export default class VaultExplorerPlugin extends Plugin {
this.layoutReady = true;
});

await loadDeviceId();
await License.getInstance().verifyLicense();

}

private registerEvents() {
Expand Down
9 changes: 9 additions & 0 deletions src/migrations/migrate_1_17_2.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown>) {
License.getInstance().setStoredDeviceRegistered(false);
return data as unknown as Record<string, unknown>;
}
}
79 changes: 66 additions & 13 deletions src/svelte/shared/services/license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export const LICENSE_KEY_LENGTH = 8;

const LOCAL_STORAGE_LICENSE_KEY = "vault-explorer-license-key";

const LOCAL_STORAGE_DEVICE_REGISTERED = "vault-explorer-device-registration";

export default class License {
private isDeviceRegistered: boolean;
private licenseKey: string;
Expand All @@ -16,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) {
Expand All @@ -28,9 +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.updateDeviceRegistered(true);
this.updateLicenseKey(licenseKey);
}
return result;
}
Expand All @@ -41,9 +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.updateLicenseKey("");
this.updateDeviceRegistered(false);
}
return result;
}
Expand All @@ -63,8 +69,9 @@ export default class License {

const result = await this.postVerifyDevice(this.licenseKey, deviceId);
if (result) {
this.isDeviceRegistered = true;
this.isDeviceRegisteredStore.set(true);
this.updateDeviceRegistered(true);
} else {
this.updateDeviceRegistered(false);
}
}

Expand All @@ -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 deviceRegistered = License.getInstance().getIsDeviceRegistered();
Logger.debug({ fileName: "license.ts", functionName: "postVerifyDevice", message: "returning last deviceRegistered state", }, deviceRegistered);
return deviceRegistered;

}
return false;
}
};
Expand Down Expand Up @@ -172,14 +186,53 @@ export default class License {
}
this.responseMessage = message;


Logger.error({ fileName: "license.ts", functionName: "postUnregisterDevice", message: "error unregistering device" }, error.message);
return false;
}
}

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);
}

/**
* 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;
}

setStoredDeviceRegistered(value: boolean) {
Logger.trace({ filename: "license.ts", functionName: "setStoredDeviceRegistered", message: "called" });
localStorage.setItem(LOCAL_STORAGE_DEVICE_REGISTERED, value.toString());
}

getIsDeviceRegistered() {
Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit 33bd590

Please sign in to comment.