2026-04-18 11:55:01 +00:00
|
|
|
// cleanup: Phase A
|
2024-07-19 18:26:04 +02:00
|
|
|
|
2024-07-22 02:05:27 +02:00
|
|
|
import { localeCompare, parseValidObject, stringify } from "../Utility/utility";
|
2026-04-18 11:55:01 +00:00
|
|
|
import { DATA_VERSION, ValidObject } from "../Utility/typeSafety";
|
2024-07-19 20:50:42 +02:00
|
|
|
|
2024-07-21 18:56:56 +02:00
|
|
|
export const PATH_COMPONENT_SEPARATOR = "\\";
|
2024-07-20 02:56:44 +02:00
|
|
|
|
2024-07-19 19:23:48 +02:00
|
|
|
export default class StorageModel {
|
2026-04-01 08:29:39 +00:00
|
|
|
storageEntryTree: StorageEntry = {};
|
|
|
|
|
|
|
|
|
|
// read
|
2026-04-18 11:55:01 +00:00
|
|
|
readonly read = (pathComponents: string[]): string | null => {
|
2026-04-01 08:29:39 +00:00
|
|
|
const pathString: string = StorageModel.pathComponentsToString(
|
|
|
|
|
...pathComponents,
|
|
|
|
|
);
|
|
|
|
|
return localStorage.getItem(pathString);
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-18 11:55:01 +00:00
|
|
|
readonly list = (pathComponents: string[]): string[] => {
|
2026-04-01 08:29:39 +00:00
|
|
|
let currentParent: StorageEntry = this.storageEntryTree;
|
|
|
|
|
for (const component of pathComponents) {
|
|
|
|
|
const nextParent: StorageEntry | undefined =
|
|
|
|
|
currentParent[component];
|
|
|
|
|
if (nextParent == undefined) return [];
|
|
|
|
|
currentParent = nextParent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [...Object.keys(currentParent).sort(localeCompare)];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// write
|
2026-04-18 11:55:01 +00:00
|
|
|
readonly write = (pathComponents: string[], value: string): void => {
|
2026-04-01 08:29:39 +00:00
|
|
|
const pathString: string = StorageModel.pathComponentsToString(
|
|
|
|
|
...pathComponents,
|
|
|
|
|
);
|
|
|
|
|
localStorage.setItem(pathString, value);
|
|
|
|
|
this.updateTree(...pathComponents);
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-18 11:55:01 +00:00
|
|
|
readonly remove = (
|
2026-04-01 08:29:39 +00:00
|
|
|
pathComponents: string[],
|
|
|
|
|
shouldInitialize: boolean = true,
|
|
|
|
|
): void => {
|
|
|
|
|
const pathString: string = StorageModel.pathComponentsToString(
|
|
|
|
|
...pathComponents,
|
|
|
|
|
);
|
|
|
|
|
localStorage.removeItem(pathString);
|
|
|
|
|
|
|
|
|
|
if (shouldInitialize == true) {
|
|
|
|
|
this.initializeTree();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-18 11:55:01 +00:00
|
|
|
readonly rename = (
|
2026-04-01 08:29:39 +00:00
|
|
|
sourcePathComponents: string[],
|
|
|
|
|
destinationPathComponents: string[],
|
|
|
|
|
shouldInitialize: boolean = true,
|
|
|
|
|
): boolean => {
|
|
|
|
|
const content: string | null = this.read(sourcePathComponents);
|
|
|
|
|
if (content == null) return false;
|
|
|
|
|
|
|
|
|
|
this.write(destinationPathComponents, content);
|
|
|
|
|
this.remove(sourcePathComponents);
|
|
|
|
|
|
|
|
|
|
if (shouldInitialize == true) {
|
|
|
|
|
this.initializeTree();
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// recursion
|
2026-08-29 02:10:26 +02:00
|
|
|
readonly recurse = (
|
|
|
|
|
rootDirectory: string[],
|
|
|
|
|
fn: (path: string[]) => void,
|
|
|
|
|
): void => {
|
2026-04-01 08:29:39 +00:00
|
|
|
loop_over_files: for (const key of Object.keys(localStorage)) {
|
|
|
|
|
// get path of current entity
|
|
|
|
|
const pathComponentsOfCurrentEntity: string[] =
|
|
|
|
|
StorageModel.stringToPathComponents(key);
|
|
|
|
|
|
|
|
|
|
// exit if entity does not match
|
|
|
|
|
loop_over_path_components: for (
|
|
|
|
|
let i = 0;
|
|
|
|
|
i < rootDirectory.length;
|
|
|
|
|
i++
|
|
|
|
|
) {
|
|
|
|
|
if (!pathComponentsOfCurrentEntity[i]) continue loop_over_files;
|
|
|
|
|
if (pathComponentsOfCurrentEntity[i] != rootDirectory[i])
|
|
|
|
|
continue loop_over_files;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// execute
|
|
|
|
|
fn(pathComponentsOfCurrentEntity);
|
|
|
|
|
}
|
|
|
|
|
this.initializeTree();
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-18 11:55:01 +00:00
|
|
|
readonly removeRecursively = (pathComponents: string[]): void => {
|
2026-04-01 08:29:39 +00:00
|
|
|
this.recurse(pathComponents, (path: string[]) =>
|
|
|
|
|
this.remove(path, false),
|
|
|
|
|
);
|
|
|
|
|
this.initializeTree();
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-18 11:55:01 +00:00
|
|
|
readonly renameRecursively = (
|
2026-04-01 08:29:39 +00:00
|
|
|
sourcePathComponents: string[],
|
|
|
|
|
destinationPathComponents: string[],
|
|
|
|
|
): void => {
|
|
|
|
|
this.recurse(sourcePathComponents, (path: string[]) => {
|
|
|
|
|
// assemble destinationPath
|
|
|
|
|
const relativePathOfCurrentEntity = path.slice(
|
|
|
|
|
sourcePathComponents.length,
|
|
|
|
|
);
|
|
|
|
|
const destinationPathComponentsOfCurrentEntity = [
|
|
|
|
|
...destinationPathComponents,
|
|
|
|
|
...relativePathOfCurrentEntity,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// rename
|
|
|
|
|
this.rename(path, destinationPathComponentsOfCurrentEntity, false);
|
|
|
|
|
});
|
|
|
|
|
this.initializeTree();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// stringifiable
|
2026-04-18 11:55:01 +00:00
|
|
|
readonly writeStringifiable = (
|
2026-04-01 08:29:39 +00:00
|
|
|
pathComponents: string[],
|
|
|
|
|
value: ValidObject,
|
|
|
|
|
): void => {
|
|
|
|
|
const valueString: string = stringify(value);
|
|
|
|
|
this.write(pathComponents, valueString);
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-18 11:55:01 +00:00
|
|
|
readonly readStringifiable = <T extends ValidObject>(
|
2026-04-01 08:29:39 +00:00
|
|
|
pathComponents: string[],
|
|
|
|
|
reference: T,
|
|
|
|
|
): T | null => {
|
|
|
|
|
const valueString: string | null = this.read(pathComponents);
|
|
|
|
|
if (!valueString) return null;
|
|
|
|
|
|
|
|
|
|
const object: any | null = parseValidObject(valueString, reference);
|
|
|
|
|
if (object == null) return null;
|
|
|
|
|
|
|
|
|
|
return object;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// cleaning
|
2026-04-18 11:55:01 +00:00
|
|
|
readonly removeJunk = (): void => {
|
2026-04-01 08:29:39 +00:00
|
|
|
this.recurse([], (path: string[]) => {
|
|
|
|
|
if (path[0] == DATA_VERSION) return;
|
|
|
|
|
this.remove(path);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// tree
|
2026-04-18 11:55:01 +00:00
|
|
|
readonly initializeTree = (): void => {
|
2026-04-01 08:29:39 +00:00
|
|
|
console.log("initializing tree");
|
|
|
|
|
|
|
|
|
|
this.storageEntryTree = {};
|
|
|
|
|
for (const key of Object.keys(localStorage)) {
|
|
|
|
|
const components: string[] =
|
|
|
|
|
StorageModel.stringToPathComponents(key);
|
|
|
|
|
this.updateTree(...components);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-18 11:55:01 +00:00
|
|
|
readonly updateTree = (...pathComponents: string[]): void => {
|
2026-04-01 08:29:39 +00:00
|
|
|
let currentParent: StorageEntry = this.storageEntryTree;
|
|
|
|
|
for (const pathPart of pathComponents) {
|
|
|
|
|
if (!currentParent[pathPart]) {
|
|
|
|
|
currentParent[pathPart] = {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentParent = currentParent[pathPart];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-18 11:55:01 +00:00
|
|
|
readonly printTree = (): string => {
|
2026-04-01 08:29:39 +00:00
|
|
|
return stringify(this.storageEntryTree);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// init
|
|
|
|
|
constructor() {
|
|
|
|
|
this.initializeTree();
|
2024-07-22 15:06:01 +02:00
|
|
|
}
|
|
|
|
|
|
2026-04-01 08:29:39 +00:00
|
|
|
// utility
|
|
|
|
|
static getFileName = (pathComponents: string[]): string => {
|
|
|
|
|
return pathComponents[pathComponents.length - 1] || "\\";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static getFileNameFromString = (pathString: string): string => {
|
|
|
|
|
const pathComponents: string[] =
|
|
|
|
|
this.stringToPathComponents(pathString);
|
|
|
|
|
return pathComponents[pathComponents.length - 1] || "\\";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static pathComponentsToString = (...pathComponents: string[]): string => {
|
|
|
|
|
return pathComponents
|
|
|
|
|
.filter((x) => x != "")
|
|
|
|
|
.join(PATH_COMPONENT_SEPARATOR);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static stringToPathComponents = (string: string): string[] => {
|
|
|
|
|
return string.split(PATH_COMPONENT_SEPARATOR).filter((x) => x != "");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static join = (...items: string[]): string => {
|
|
|
|
|
let allComponents: string[] = [];
|
|
|
|
|
for (const item of items) {
|
|
|
|
|
const parts = this.stringToPathComponents(item);
|
|
|
|
|
allComponents.push(...parts);
|
|
|
|
|
}
|
|
|
|
|
return StorageModel.pathComponentsToString(...allComponents);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static getPath(
|
2026-04-08 14:02:12 +00:00
|
|
|
locationName: StorageModelSubPaths,
|
2026-04-01 08:29:39 +00:00
|
|
|
filePath: string[],
|
|
|
|
|
): string[] {
|
|
|
|
|
return [DATA_VERSION, locationName, ...filePath];
|
2024-07-20 04:36:15 +02:00
|
|
|
}
|
2024-07-19 19:23:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// types
|
|
|
|
|
export type StorageEntry = { [key: string]: StorageEntry };
|
|
|
|
|
|
2024-07-22 03:01:04 +02:00
|
|
|
// locations
|
2026-04-08 14:02:12 +00:00
|
|
|
export enum StorageModelSubPaths {
|
2026-04-01 08:29:39 +00:00
|
|
|
Chat = "chat",
|
2024-07-27 16:16:38 +02:00
|
|
|
|
2026-04-01 08:29:39 +00:00
|
|
|
ConnectionModel = "connection",
|
2026-04-01 19:00:16 +00:00
|
|
|
NotificationModel = "notifications",
|
2026-04-01 08:29:39 +00:00
|
|
|
SettingsModel = "settings",
|
2024-07-25 16:44:08 +02:00
|
|
|
}
|
2024-07-22 03:01:04 +02:00
|
|
|
|
|
|
|
|
export const filePaths = {
|
2026-04-01 08:29:39 +00:00
|
|
|
connectionModel: {
|
|
|
|
|
socketAddress: ["socket-address"],
|
|
|
|
|
reconnectAddress: ["reconnect-address"],
|
|
|
|
|
outbox: ["outbox"],
|
|
|
|
|
mailboxes: ["mailboxes"],
|
|
|
|
|
|
|
|
|
|
previousAddresses: ["previous-addresses"],
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
chat: {
|
|
|
|
|
base: [],
|
|
|
|
|
chatBase: (id: string) => [id],
|
|
|
|
|
info: (id: string) => [...filePaths.chat.chatBase(id), "info"],
|
|
|
|
|
color: (id: string) => [...filePaths.chat.chatBase(id), "color"],
|
|
|
|
|
messages: (id: string) => [...filePaths.chat.chatBase(id), "messages"],
|
2026-04-06 20:47:49 +00:00
|
|
|
reactions: (id: string) => [
|
|
|
|
|
...filePaths.chat.chatBase(id),
|
|
|
|
|
"reactions",
|
|
|
|
|
],
|
2026-09-24 02:11:03 +02:00
|
|
|
previousFilter: (id: string) => [...filePaths.chat.chatBase(id), "previous-filter"],
|
2026-04-01 08:29:39 +00:00
|
|
|
lastUsedPage: (id: string) => [
|
|
|
|
|
...filePaths.chat.chatBase(id),
|
|
|
|
|
"last-used-page",
|
|
|
|
|
],
|
|
|
|
|
files: (id: string) => [...filePaths.chat.chatBase(id), "files"],
|
|
|
|
|
},
|
|
|
|
|
|
2026-04-01 19:00:16 +00:00
|
|
|
notificationModel: {
|
|
|
|
|
base: [],
|
|
|
|
|
},
|
|
|
|
|
|
2026-04-01 08:29:39 +00:00
|
|
|
settingsModel: {
|
|
|
|
|
username: ["user-name"],
|
|
|
|
|
firstDayOfWeek: ["first-day-of-week"],
|
2026-04-09 16:48:22 +00:00
|
|
|
language: ["language"],
|
2026-04-09 17:32:17 +00:00
|
|
|
theme: ["theme"],
|
2026-04-01 08:29:39 +00:00
|
|
|
},
|
2024-07-22 12:39:26 +02:00
|
|
|
};
|