From fa205ec72ef433e2c77e82cb8e017ce4fbdfefa7 Mon Sep 17 00:00:00 2001 From: maver Date: Tue, 11 Jul 2023 21:03:12 +0200 Subject: [PATCH] Validate import data before prompt import --- public/scripts/PromptManager.js | 37 +++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/public/scripts/PromptManager.js b/public/scripts/PromptManager.js index 27e4f1b86..a483a088c 100644 --- a/public/scripts/PromptManager.js +++ b/public/scripts/PromptManager.js @@ -369,7 +369,7 @@ PromptManagerModule.prototype.init = function (moduleConfiguration, serviceSetti // Export user prompts and order for this character this.handleCharacterExport = () => { const characterPrompts = this.getPromptsForCharacter(this.activeCharacter).reduce((userPrompts, prompt) => { - if (false === prompt.system_prompt && false === prompt.marker) userPrompts.push(prompt); + if (false === prompt.system_prompt && !prompt.marker) userPrompts.push(prompt); return userPrompts; }, []); @@ -813,7 +813,7 @@ PromptManagerModule.prototype.getPromptById = function (identifier) { * @returns {number|null} Index of the prompt, or null if not found */ PromptManagerModule.prototype.getPromptIndexById = function (identifier) { - return this.serviceSettings.prompts.findIndex(item => item.position === identifier) ?? null; + return this.serviceSettings.prompts.findIndex(item => item.identifier === identifier) ?? null; } PromptManagerModule.prototype.getSettings = function(settings) { @@ -1302,6 +1302,20 @@ PromptManagerModule.prototype.import = function (importData) { return merged; } + const controlObj = { + version: 1, + type: '', + data: { + prompts: [], + promptList: null + } + } + + if (false === this.validateObject(controlObj, importData)) { + toastr.warning('Could not import prompts. Export failed validation.'); + return; + } + const prompts = mergeKeepNewer(this.serviceSettings.prompts, importData.data.prompts); this.setPrompts(prompts); @@ -1313,9 +1327,28 @@ PromptManagerModule.prototype.import = function (importData) { this.log(`Prompt order import for character ${this.activeCharacter.name} completed`); } + toastr.success('Prompt import complete.'); this.saveServiceSettings().then(() => this.render()); }; +PromptManagerModule.prototype.validateObject = function(controlObj, object) { + for (let key in controlObj) { + if (!object.hasOwnProperty(key)) { + if (controlObj[key] === null) continue; + else return false; + } + + if (typeof controlObj[key] === 'object' && controlObj[key] !== null) { + if (typeof object[key] !== 'object') return false; + if (!this.validateObject(controlObj[key], object[key])) return false; + } else { + if (typeof object[key] !== typeof controlObj[key]) return false; + } + } + + return true; +} + PromptManagerModule.prototype.getFormattedDate = function() { const date = new Date(); let month = String(date.getMonth() + 1);