diff --git a/src/main.ts b/src/main.ts index 363cc4e..686f0ff 100644 --- a/src/main.ts +++ b/src/main.ts @@ -51,6 +51,7 @@ import type { LangType, LangTypeAndAuto, TransItemType } from "./i18n"; import * as origLog from "loglevel"; import { DeletionOnRemote, MetadataOnRemote } from "./metadataOnRemote"; import { SyncAlgoV2Modal } from "./syncAlgoV2Notice"; +import { applyPresetRulesInplace } from "./presetRules"; const log = origLog.getLogger("rs-default"); const DEFAULT_SETTINGS: RemotelySavePluginSettings = { @@ -373,6 +374,7 @@ export default class RemotelySavePlugin extends Plugin { this.currSyncMsg = ""; await this.loadSettings(); + await this.checkIfPresetRulesFollowed(); // lang should be load early, but after settings this.i18n = new I18n(this.settings.lang, async (lang: LangTypeAndAuto) => { @@ -706,6 +708,13 @@ export default class RemotelySavePlugin extends Plugin { } } + async checkIfPresetRulesFollowed() { + const res = applyPresetRulesInplace(this.settings); + if (res.changed) { + await this.saveSettings(); + } + } + async saveSettings() { await this.saveData(normalConfigToMessy(this.settings)); } diff --git a/src/presetRules.ts b/src/presetRules.ts new file mode 100644 index 0000000..4e3cbec --- /dev/null +++ b/src/presetRules.ts @@ -0,0 +1,69 @@ +import type { + RemotelySavePluginSettings, + WebdavConfig, + WebdavDepthType, +} from "./baseTypes"; + +const RULES = { + webdav: { + depth: [ + { + url: "^https://(.+).teracloud.jp/.+", + depth: "auto_1", + manualRecursive: true, + }, + { + url: "^https://dav.jianguoyun.com/dav/", + depth: "auto_1", + manualRecursive: true, + }, + ], + }, +}; + +export const applyWebdavPresetRulesInplace = ( + webdav: Partial | undefined +) => { + if (webdav === undefined) { + return { + changed: false, + webdav: webdav, + }; + } + for (const { url, depth, manualRecursive } of RULES.webdav.depth) { + if ( + webdav.address !== undefined && + new RegExp(url).test(webdav.address) && + webdav.depth !== undefined && + webdav.depth.startsWith("auto_") && + webdav.depth !== depth + ) { + webdav.depth = depth as WebdavDepthType; + webdav.manualRecursive = manualRecursive; + return { + changed: true, + webdav: webdav, + }; + } + } + return { + changed: false, + webdav: webdav, + }; +}; + +export const applyPresetRulesInplace = ( + settings: RemotelySavePluginSettings | undefined +) => { + if (settings === undefined) { + return { + changed: false, + settings: settings, + }; + } + const webdavRes = applyWebdavPresetRulesInplace(settings.webdav); + return { + changed: webdavRes.changed, + settings: settings, + }; +}; diff --git a/src/settings.ts b/src/settings.ts index 54ed2df..3b9250b 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -39,6 +39,7 @@ import type { TransItemType } from "./i18n"; import * as origLog from "loglevel"; import { checkHasSpecialCharForDir } from "./misc"; +import { applyWebdavPresetRulesInplace } from "./presetRules"; const log = origLog.getLogger("rs-default"); class PasswordModal extends Modal { @@ -1323,6 +1324,11 @@ export class RemotelySaveSettingTab extends PluginSettingTab { ) { this.plugin.settings.webdav.depth = "auto_unknown"; } + + // TODO: any more elegant way? + applyWebdavPresetRulesInplace(this.plugin.settings.webdav); + + // normally saved await this.plugin.saveSettings(); }) ); @@ -1420,6 +1426,11 @@ export class RemotelySaveSettingTab extends PluginSettingTab { this.plugin.settings.webdav.depth = "manual_infinity"; this.plugin.settings.webdav.manualRecursive = false; } + + // TODO: any more elegant way? + applyWebdavPresetRulesInplace(this.plugin.settings.webdav); + + // normally save await this.plugin.saveSettings(); }); }); diff --git a/tests/presetRules.test.ts b/tests/presetRules.test.ts new file mode 100644 index 0000000..9505f3b --- /dev/null +++ b/tests/presetRules.test.ts @@ -0,0 +1,111 @@ +import { expect } from "chai"; +import type { WebdavConfig } from "../src/baseTypes"; +import { applyWebdavPresetRulesInplace } from "../src/presetRules"; + +describe("Preset rules tests", () => { + it("should check undefined correctly", () => { + let x: Partial | undefined = undefined; + const y = applyWebdavPresetRulesInplace(x); + expect(y.webdav === undefined); + expect(!y.changed); + }); + + it("should check empty object", () => { + let x: Partial | undefined = {}; + const y = applyWebdavPresetRulesInplace(x); + expect(y.webdav).deep.equals({}); + expect(!y.changed); + }); + + it("should modify depths correctly", () => { + let x: Partial = { + address: "https://example.teracloud.jp/dav/", + depth: "auto_unknown", + }; + let y = applyWebdavPresetRulesInplace(x); + expect(x.depth === "auto_1"); + expect(y.changed); + + x = { + address: "https://example.teracloud.jp/dav/example", + depth: "auto_unknown", + }; + y = applyWebdavPresetRulesInplace(x); + expect(x.depth === "auto_1"); + expect(y.changed); + + x = { + address: "https://dav.jianguoyun.com/dav/", + depth: "auto_unknown", + }; + y = applyWebdavPresetRulesInplace(x); + expect(x.depth === "auto_1"); + expect(y.changed); + + x = { + address: "https://dav.jianguoyun.com/dav/", + depth: "auto_infinity", + }; + y = applyWebdavPresetRulesInplace(x); + expect(x.depth === "auto_1"); + expect(y.changed); + }); + + it("should not modify depths if depths is set automatically correctly", () => { + let x: Partial = { + address: "https://dav.jianguoyun.com/dav/", + depth: "auto_1", + }; + let y = applyWebdavPresetRulesInplace(x); + expect(x.depth === "auto_1"); + expect(!y.changed); + }); + + it("should not modify depths if depths have been set manually", () => { + let x: Partial = { + address: "https://example.teracloud.jp/dav/", + depth: "manual_infinity", + }; + let y = applyWebdavPresetRulesInplace(x); + expect(x.depth === "manual_infinity"); + expect(!y.changed); + + x = { + address: "https://example.teracloud.jp/dav/example", + depth: "manual_1", + }; + y = applyWebdavPresetRulesInplace(x); + expect(x.depth === "manual_1"); + expect(!y.changed); + }); + + it("should not modify depths when urls are not in preset rules", () => { + let x: Partial = { + address: "https://teracloud.jp/dav/", + depth: "auto_unknown", + }; + applyWebdavPresetRulesInplace(x); + expect(x.depth === "auto_unknown"); + + x = { + address: "https://dav.jianguoyun.com/dav_example", + depth: "auto_unknown", + }; + applyWebdavPresetRulesInplace(x); + expect(x.depth === "auto_unknown"); + + x = { + address: "", + depth: "auto_unknown", + }; + applyWebdavPresetRulesInplace(x); + expect(x.depth === "auto_unknown"); + + x = { + address: "https://dav.jianguoyun.com/dav/", + depth: "what" as any, + }; + applyWebdavPresetRulesInplace(x); + expect(x.depth === ("what" as any)); + }); +});