Skip to content

Commit

Permalink
chore: refactor sort tags, add SortTagManager
Browse files Browse the repository at this point in the history
  • Loading branch information
zunderscore committed Dec 28, 2024
1 parent 5842844 commit 6a9006a
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 96 deletions.
4 changes: 4 additions & 0 deletions src/backend/app-management/electron/events/when-ready.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ exports.whenReady = async () => {
customVariableManager.loadVariablesFromFile();
}

windowManagement.updateSplashScreenStatus("Loading sort tags...");
const { SortTagManager } = require("../../../sort-tags/sort-tag-manager");
SortTagManager.loadSortTags();

// get importer in memory
windowManagement.updateSplashScreenStatus("Loading importers...");

Expand Down
114 changes: 114 additions & 0 deletions src/backend/sort-tags/sort-tag-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import logger from "../logwrapper";
import profileManager from "../common/profile-manager";
import frontendCommunicator from "../common/frontend-communicator";
import { SettingsManager } from "../common/settings-manager";

interface SortTag {
id: string;
name: string;
}

interface SortTagCache {
[context: string]: SortTag[]
}

class SortTagManager {
sortTags: SortTagCache = { };

constructor() {
frontendCommunicator.on("sort-tags:get-sort-tags", () => {
return this.sortTags ?? { };
});

frontendCommunicator.on("sort-tags:save-sort-tags", (request: { context: string, sortTags: SortTag[] }) => {
this.saveSortTagsForContext(request.context, request.sortTags);
});
}

getSortTagsDb() {
return profileManager.getJsonDbInProfile("sort-tags");
}

loadSortTags() {
logger.debug("Attempting to load tags");

try {
const sortTagsData = this.getSortTagsDb().getData("/");

if (sortTagsData) {
this.sortTags = sortTagsData;
}

logger.debug(`Loaded tags.`);
} catch (err) {
logger.warn(`There was an error reading tags file.`, err);
}

this.getLegacyEventAndCommandTags();
}

private getLegacyEventAndCommandTags() {
if (!SettingsManager.getSetting("LegacySortTagsImported")) {
SettingsManager.saveSetting("LegacySortTagsImported", true);

const legacySortTags: SortTagCache = {
commands: [],
events: []
};

try {
const commandsDb = profileManager.getJsonDbInProfile("/chat/commands");

legacySortTags.commands = commandsDb.getData("/sortTags");

commandsDb.delete("/sortTags");
} catch { }

try {
const eventsDb = profileManager.getJsonDbInProfile("/events/events");

legacySortTags.events = eventsDb.getData("/sortTags");

eventsDb.delete("/sortTags");
} catch { }

Object.keys(legacySortTags).forEach((context) => {
if (this.sortTags[context] == null) {
this.sortTags[context] = [];
}

const tags = legacySortTags[context]
.filter(t => this.sortTags[context].every(st => st.id !== t.id));

if (tags.length > 0) {
this.sortTags[context] = [
...this.sortTags[context],
...tags
];
}
});

this.saveAllSortTags();
}
}

saveSortTagsForContext(context: string, sortTags: SortTag[]) {
this.sortTags[context] = sortTags;
this.saveAllSortTags();
}

private saveAllSortTags() {
try {
this.getSortTagsDb().push("/", this.sortTags);

frontendCommunicator.send("sort-tags:updated-sort-tags", this.sortTags);
logger.debug("Saved tags");
} catch (error) {
logger.warn("There was an error saving tags", error);
}
}
}

const sortTagManager = new SortTagManager();

export { sortTagManager as SortTagManager };
113 changes: 17 additions & 96 deletions src/gui/app/services/sort-tags.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,21 @@

angular
.module("firebotApp")
.factory("sortTagsService", function(logger, profileManager,
utilityService, settingsService, backendCommunicator) {
.factory("sortTagsService", function(utilityService, backendCommunicator) {
const service = {};

/**
* @type {Object.<string, SortTag[]>}
* @type {Record<string, SortTag[]>}
*/
let sortTags = {};

/**
* @type {Record<string,SortTag>}
* @type {Record<string, SortTag>}
*/
const selectedSortTags = {};

function getSortTagsDb() {
return profileManager
.getJsonDbInProfile("sort-tags");
}

function saveAllSortTags() {
try {
getSortTagsDb().push("/", sortTags);

logger.debug(`Saved tags.`);
} catch (err) {
logger.warn(`There was an error saving tags.`, err);
}
}

service.loadSortTags = () => {
logger.debug(`Attempting to load tags...`);

try {
const sortTagsData = getSortTagsDb().getData("/");

if (sortTagsData) {
sortTags = sortTagsData;
}

logger.debug(`Loaded tags.`);
} catch (err) {
logger.warn(`There was an error reading tags file.`, err);
}

service.getLegacyEventAndCommandTags();
sortTags = backendCommunicator.fireEventSync("sort-tags:get-sort-tags");
};

/**
Expand Down Expand Up @@ -97,19 +67,23 @@
tags: () => sortTagsForContext
},
closeCallback: (tags) => {
sortTags[context] = tags;
saveAllSortTags();
backendCommunicator.send("sort-tags:save-sort-tags", {
context,
sortTags: tags
});
}
});
saveAllSortTags();
};

/** @param {SortTag} context */
/** @param {string} context */
service.getSelectedSortTag = context => selectedSortTags[context];

/** @param {string} context */
// eslint-disable-next-line no-confusing-arrow
service.getSelectedSortTagDisplay = context => (selectedSortTags[context] != null ? selectedSortTags[context].name : `All ${context}`);
service.getSelectedSortTagDisplay = context => (
selectedSortTags[context] != null
? selectedSortTags[context].name
: `All ${context}`
);

/**
* @param {string} context
Expand All @@ -119,62 +93,9 @@
selectedSortTags[context] = tag;
};

/**
* This asks the backend to give us any old event and/or command tags that need to be
* imported into the new model
*/
service.getLegacyEventAndCommandTags = () => {

if (!settingsService.getSetting("LegacySortTagsImported")) {

settingsService.saveSetting("LegacySortTagsImported", true);

/**@type {Object.<string, SortTag[]>} */
const legacySortTags = {
commands: [],
events: []
};

try {
const commandsDb = profileManager.getJsonDbInProfile("/chat/commands");

legacySortTags.commands = commandsDb.getData("/sortTags");

commandsDb.delete("/sortTags");
} catch (err) {
// silently fail
}

try {
const eventsDb = profileManager.getJsonDbInProfile("/events/events");

legacySortTags.events = eventsDb.getData("/sortTags");

eventsDb.delete("/sortTags");
} catch (err) {
// silently fail
}

Object.keys(legacySortTags).forEach((context) => {

if (sortTags[context] == null) {
sortTags[context] = [];
}

const tags = legacySortTags[context]
.filter(t => sortTags[context].every(st => st.id !== t.id));

if (tags.length > 0) {
sortTags[context] = [
...sortTags[context],
...tags
];
}
});

saveAllSortTags();
}
};
backendCommunicator.on("sort-tags:updated-sort-tags", (updatedSortTags) => {
sortTags = updatedSortTags;
});

return service;
});
Expand Down

0 comments on commit 6a9006a

Please sign in to comment.