From da43c1c87ed82c1e19c2d324d8b6c2caa26f2376 Mon Sep 17 00:00:00 2001 From: Cohee Date: Thu, 13 Jul 2023 19:26:01 +0300 Subject: [PATCH] #699 Delete character without losing chats --- public/script.js | 12 +++++++++--- server.js | 50 ++++++++++++++++++++++++------------------------ 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/public/script.js b/public/script.js index 194ed8742..3253ba6c7 100644 --- a/public/script.js +++ b/public/script.js @@ -7132,9 +7132,11 @@ $(document).ready(function () { " -- Name: " + characters[this_chid].name ); + const delete_chats = !!$("#del_char_checkbox").prop("checked"); const avatar = characters[this_chid].avatar; const name = characters[this_chid].name; - var msg = jQuery("#form_create").serialize(); // ID form + const msg = new FormData($("#form_create").get(0)); // ID form + msg.append("delete_chats", delete_chats); jQuery.ajax({ method: "POST", url: "/deletecharacter", @@ -7142,6 +7144,8 @@ $(document).ready(function () { }, data: msg, cache: false, + contentType: false, + processData: false, success: async function (html) { //RossAscends: New handling of character deletion that avoids page refreshes and should have // fixed char corruption due to cache problems. @@ -7259,8 +7263,10 @@ $(document).ready(function () { callPopup(`

Delete the character?

THIS IS PERMANENT!

- THIS WILL ALSO DELETE ALL
- OF THE CHARACTER'S CHAT FILES.

` +
` ); }); diff --git a/server.js b/server.js index d4ef636ba..c4797e360 100644 --- a/server.js +++ b/server.js @@ -1078,7 +1078,7 @@ app.post("/editcharacterattribute", jsonParser, async function (request, respons } }); -app.post("/deletecharacter", urlencodedParser, function (request, response) { +app.post("/deletecharacter", urlencodedParser, async function (request, response) { if (!request.body || !request.body.avatar_url) { return response.sendStatus(400); } @@ -1102,16 +1102,16 @@ app.post("/deletecharacter", urlencodedParser, function (request, response) { return response.sendStatus(403); } - rimraf(path.join(chatsPath, sanitize(dir_name)), (err) => { - if (err) { - response.send(err); - return console.log(err); - } else { - //response.redirect("/"); - - response.send('ok'); + if (request.body.delete_chats == 'true') { + try { + await fs.promises.rm(path.join(chatsPath, sanitize(dir_name)), { recursive: true, force: true }) + } catch (err) { + console.error(err); + return response.sendStatus(500); } - }); + } + + return response.sendStatus(200); }); async function charaWrite(img_url, data, target_img, response = undefined, mes = 'ok', crop = undefined) { @@ -2768,7 +2768,7 @@ app.post('/poe_suggest', jsonParser, async function (request, response) { * If the folder is called third-party, search for subfolders instead */ app.get('/discover_extensions', jsonParser, function (_, response) { - + // get all folders in the extensions folder, except third-party const extensions = fs .readdirSync(directories.extensions) @@ -4444,7 +4444,7 @@ async function getImageBuffers(zipFilePath) { -/** +/** * This function extracts the extension information from the manifest file. * @param {string} extensionPath - The path of the extension folder * @returns {Object} - Returns the manifest data as an object @@ -4473,7 +4473,7 @@ async function checkIfRepoIsUpToDate(extensionPath) { // Fetch remote repository information const remotes = await git.cwd(extensionPath).getRemotes(true); - + return { isUpToDate: log.total === 0, remoteUrl: remotes[0].refs.fetch, // URL of the remote repository @@ -4488,7 +4488,7 @@ async function checkIfRepoIsUpToDate(extensionPath) { * * @param {Object} request - HTTP Request object, expects a JSON body with a 'url' property. * @param {Object} response - HTTP Response object used to respond to the HTTP request. - * + * * @returns {void} */ app.post('/get_extension', jsonParser, async (request, response) => { @@ -4526,14 +4526,14 @@ app.post('/get_extension', jsonParser, async (request, response) => { }); /** - * HTTP POST handler function to pull the latest updates from a git repository - * based on the extension name provided in the request body. It returns the latest commit hash, - * the path of the extension, the status of the repository (whether it's up-to-date or not), + * HTTP POST handler function to pull the latest updates from a git repository + * based on the extension name provided in the request body. It returns the latest commit hash, + * the path of the extension, the status of the repository (whether it's up-to-date or not), * and the remote URL of the repository. * * @param {Object} request - HTTP Request object, expects a JSON body with an 'extensionName' property. * @param {Object} response - HTTP Response object used to respond to the HTTP request. - * + * * @returns {void} */ app.post('/update_extension', jsonParser, async (request, response) => { @@ -4578,7 +4578,7 @@ app.post('/update_extension', jsonParser, async (request, response) => { * * @param {Object} request - HTTP Request object, expects a JSON body with an 'extensionName' property. * @param {Object} response - HTTP Response object used to respond to the HTTP request. - * + * * @returns {void} */ app.post('/get_extension_version', jsonParser, async (request, response) => { @@ -4586,17 +4586,17 @@ app.post('/get_extension_version', jsonParser, async (request, response) => { if (!request.body.extensionName) { return response.status(400).send('Bad Request: extensionName is required in the request body.'); } - + try { const extensionName = request.body.extensionName; const extensionPath = path.join(directories.extensions, 'third-party', extensionName); - + if (!fs.existsSync(extensionPath)) { return response.status(404).send(`Directory does not exist at ${extensionPath}`); } - + const currentBranch = await git.cwd(extensionPath).branch(); - // get only the working branch + // get only the working branch const currentBranchName = currentBranch.current; await git.cwd(extensionPath).fetch('origin'); const currentCommitHash = await git.cwd(extensionPath).revparse(['HEAD']); @@ -4604,7 +4604,7 @@ app.post('/get_extension_version', jsonParser, async (request, response) => { const { isUpToDate, remoteUrl } = await checkIfRepoIsUpToDate(extensionPath); return response.send({ currentBranchName, currentCommitHash, isUpToDate, remoteUrl }); - + } catch (error) { console.log('Getting extension version failed', error); return response.status(500).send(`Server Error: ${error.message}`); @@ -4617,7 +4617,7 @@ app.post('/get_extension_version', jsonParser, async (request, response) => { * * @param {Object} request - HTTP Request object, expects a JSON body with a 'url' property. * @param {Object} response - HTTP Response object used to respond to the HTTP request. - * + * * @returns {void} */ app.post('/delete_extension', jsonParser, async (request, response) => {