Skip to content

Commit

Permalink
WIP profile manager refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
zunderscore committed Dec 27, 2024
1 parent 2bc5e84 commit c161769
Show file tree
Hide file tree
Showing 11 changed files with 358 additions and 353 deletions.
9 changes: 3 additions & 6 deletions src/backend/app-management/electron/events/will-quit.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { Event, app } from "electron";

async function cleanup() {
const {
handleProfileDeletion,
handleProfileRename
} = require("../../../app-management/profile-tasks");
handleProfileRename();
handleProfileDeletion();
const { ProfileManager } = require("../../../common/profile-manager");
ProfileManager.handleProfileRename();
ProfileManager.handleProfileDeletion();

const eventManager = require("../../../events/EventManager");
await eventManager.triggerEvent("firebot", "before-firebot-closed", {
Expand Down
99 changes: 0 additions & 99 deletions src/backend/app-management/profile-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,105 +4,6 @@ const dataAccess = require("../common/data-access");
const profileManager = require("../common/profile-manager");
const fs = require("fs");

function handleProfileRename() {
if (!profileManager.hasProfileRename()) {
return;
}
const globalSettingsDb = dataAccess.getJsonDbInUserData("./global-settings");

try {
const currentProfileId = profileManager.getLoggedInProfile(),
newProfileId = profileManager.getNewProfileName(),
activeProfiles = globalSettingsDb.getData("./profiles/activeProfiles");

// Stop here if we have no deleted profile info.
if (currentProfileId != null && newProfileId != null && newProfileId !== "") {
// Delete the profile.
logger.warn(`Profile ${currentProfileId} is marked for renaming. Renaming it now.`);

const currentProfilePath = dataAccess.getPathInUserData(`/profiles/${currentProfileId}`);
const renamedProfilePath = dataAccess.getPathInUserData(`/profiles/${newProfileId}`);
logger.warn(currentProfilePath);

try {
fs.renameSync(currentProfilePath, renamedProfilePath);
} catch (err) {
logger.error("Failed to rename profile!", err);
return;
}

// Remove old id from active profiles and add new
const profilePosition = activeProfiles.indexOf(currentProfileId);
activeProfiles[profilePosition] = newProfileId;
globalSettingsDb.push("/profiles/activeProfiles", activeProfiles);

// Update loggedInProfile
globalSettingsDb.push("./profiles/loggedInProfile", newProfileId);

// Let our logger know we successfully deleted a profile.
logger.warn(`Successfully renamed profile "${currentProfileId}" to "${newProfileId}"`);
}
} catch (err) {
logger.error("error while renaming profile!", err);
return;
}
}

function handleProfileDeletion() {
const globalSettingsDb = dataAccess.getJsonDbInUserData("./global-settings");

let deletedProfile, activeProfiles;
try {
deletedProfile = globalSettingsDb.getData("./profiles/deleteProfile");
activeProfiles = globalSettingsDb.getData("./profiles/activeProfiles");
} catch (error) {
if (error.name === 'DatabaseError') {
logger.error("Error loading deleted and active profiles", error);
}
}

// Stop here if we have no deleted profile info.
if (deletedProfile == null) {
return;
}

try {

// Delete the profile.
logger.warn(`Profile ${deletedProfile} is marked for deletion. Removing it now.`);

const profilePath = dataAccess.getPathInUserData(`/profiles/${deletedProfile}`);

logger.warn(profilePath);
dataAccess.deleteFolderRecursive(profilePath);

// Remove it from active profiles.
const profilePosition = activeProfiles.indexOf(deletedProfile);
if (profilePosition > -1) {
activeProfiles.splice(profilePosition, 1);
globalSettingsDb.push("/profiles/activeProfiles", activeProfiles);
}

// Remove loggedInProfile setting and let restart process handle it.
if (activeProfiles.length > 0 && activeProfiles != null) {
// Switch to whatever the first profile is in our new active profiles list.
globalSettingsDb.push("./profiles/loggedInProfile", activeProfiles[0]);
} else {
// We have no more active profiles, delete the loggedInProfile setting.
globalSettingsDb.delete("./profiles/loggedInProfile");
}

// Reset the deleteProfile setting.
globalSettingsDb.delete("./profiles/deleteProfile");

// Let our logger know we successfully deleted a profile.
logger.warn(`Successfully deleted profile: ${deletedProfile}`);

} catch (err) {
logger.error("error while deleting profile: ", err);
return;
}
}

exports.handleProfileRename = handleProfileRename;

Check failure on line 8 in src/backend/app-management/profile-tasks.js

View workflow job for this annotation

GitHub Actions / Lint

'handleProfileRename' is not defined
exports.handleProfileDeletion = handleProfileDeletion;

Check failure on line 9 in src/backend/app-management/profile-tasks.js

View workflow job for this annotation

GitHub Actions / Lint

'handleProfileDeletion' is not defined
28 changes: 2 additions & 26 deletions src/backend/common/common-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ const logger = require("../logwrapper");
exports.setupCommonListeners = () => {

const frontendCommunicator = require("./frontend-communicator");
const profileManager = require("./profile-manager");
const { SettingsManager } = require("./settings-manager");
const { BackupManager } = require("../backup-manager");
const webServer = require("../../server/http-server-manager");
const { restartApp } = require("../app-management/electron/app-helpers");

frontendCommunicator.on("show-twitch-preview", () => {
const windowManagement = require("../app-management/electron/window-management");
Expand Down Expand Up @@ -74,38 +74,14 @@ exports.setupCommonListeners = () => {

// restarts the app
ipcMain.on("restartApp", () => {
const chatModerationManager = require("../chat/moderation/chat-moderation-manager");
chatModerationManager.stopService();
setTimeout(() => {
app.relaunch({ args: process.argv.slice(1).concat(["--relaunch"]) });
app.exit(0);
}, 100);
restartApp();
});

// Opens the firebot backup folder
ipcMain.on("open-backup-folder", () => {
shell.openPath(BackupManager.backupFolderPath);
});

// When we get an event from the renderer to create a new profile.
ipcMain.on("createProfile", (_, profileName) => {
profileManager.createNewProfile(profileName);
});

// When we get an event from the renderer to delete a particular profile.
ipcMain.on("deleteProfile", () => {
profileManager.deleteProfile();
});

// Change profile when we get event from renderer
ipcMain.on("switchProfile", function(_, profileId) {
profileManager.logInProfile(profileId);
});

ipcMain.on("renameProfile", function(_, newProfileId) {
profileManager.renameProfile(newProfileId);
});

// Get Any kind of file Path
// This listens for an event from the front end.
ipcMain.on("getAnyFilePath", (event, data) => {
Expand Down
4 changes: 2 additions & 2 deletions src/backend/common/data-access.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ const createFirebotDataDir = function () {
}
};

const getJsonDbInUserData = function (filePath) {
const getJsonDbInUserData = function (filePath, humanReadable = true) {
const jsonDbPath = path.join(userDataPath, filePath);
return new JsonDB(jsonDbPath, true, true);
return new JsonDB(jsonDbPath, true, humanReadable);
};

const makeDirInUserData = async function (filePath) {
Expand Down
205 changes: 0 additions & 205 deletions src/backend/common/profile-manager.js

This file was deleted.

Loading

0 comments on commit c161769

Please sign in to comment.