cleanup path management

This commit is contained in:
marlon-erler
2024-07-25 16:44:08 +02:00
parent 1dc2da2514
commit c9d74972b8
10 changed files with 57 additions and 67 deletions
+19 -24
View File
@@ -584,14 +584,9 @@
return _StorageModel.pathComponentsToString(...allComponents); return _StorageModel.pathComponentsToString(...allComponents);
}; };
static getPath(locationName, filePath) { static getPath(locationName, filePath) {
return [DATA_VERSION, storageLocations[locationName], ...filePath]; return [DATA_VERSION, locationName, ...filePath];
} }
}; };
var storageLocations = {
connectionModel: "connection",
chat: "chat",
settingsModel: "settings"
};
var filePaths = { var filePaths = {
connectionModel: { connectionModel: {
socketAddress: ["socket-address"], socketAddress: ["socket-address"],
@@ -832,7 +827,7 @@
// paths // paths
getBasePath = () => { getBasePath = () => {
return StorageModel.getPath( return StorageModel.getPath(
"chat", "chat" /* Chat */,
filePaths.chat.files(this.chatModel.id) filePaths.chat.files(this.chatModel.id)
); );
}; };
@@ -1030,16 +1025,16 @@
} }
// paths // paths
getBasePath = () => { getBasePath = () => {
return StorageModel.getPath("chat", filePaths.chat.chatBase(this.id)); return StorageModel.getPath("chat" /* Chat */, filePaths.chat.chatBase(this.id));
}; };
getInfoPath = () => { getInfoPath = () => {
return StorageModel.getPath("chat", filePaths.chat.info(this.id)); return StorageModel.getPath("chat" /* Chat */, filePaths.chat.info(this.id));
}; };
getColorPath = () => { getColorPath = () => {
return StorageModel.getPath("chat", filePaths.chat.color(this.id)); return StorageModel.getPath("chat" /* Chat */, filePaths.chat.color(this.id));
}; };
getMessageDirPath = () => { getMessageDirPath = () => {
return StorageModel.getPath("chat", filePaths.chat.messages(this.id)); return StorageModel.getPath("chat" /* Chat */, filePaths.chat.messages(this.id));
}; };
getMessagePath = (id) => { getMessagePath = (id) => {
return [...this.getMessageDirPath(), id]; return [...this.getMessageDirPath(), id];
@@ -1292,7 +1287,7 @@
}; };
// load // load
loadChats = () => { loadChats = () => {
const chatDir = StorageModel.getPath("chat", filePaths.chat.base); const chatDir = StorageModel.getPath("chat" /* Chat */, filePaths.chat.base);
const chatIds = this.storageModel.list(chatDir); const chatIds = this.storageModel.list(chatDir);
for (const chatId of chatIds) { for (const chatId of chatIds) {
const chatModel = new ChatModel( const chatModel = new ChatModel(
@@ -2015,7 +2010,7 @@
// load // load
loadPageSelection = () => { loadPageSelection = () => {
const path = StorageModel.getPath( const path = StorageModel.getPath(
"chat", "chat" /* Chat */,
filePaths.chat.lastUsedPage(this.chatModel.id) filePaths.chat.lastUsedPage(this.chatModel.id)
); );
const lastUsedPage = this.storageModel.read(path); const lastUsedPage = this.storageModel.read(path);
@@ -3434,7 +3429,7 @@
console.log("connection status:", this.isConnected, this.address); console.log("connection status:", this.isConnected, this.address);
this.connectionChangeHandlerManager.trigger(); this.connectionChangeHandlerManager.trigger();
if (this.isConnected == false) return; if (this.isConnected == false) return;
if (!this.address) return; if (this.address == void 0) return;
this.connectMailbox(); this.connectMailbox();
this.storeAddress(this.address); this.storeAddress(this.address);
this.sendSubscriptionRequest(); this.sendSubscriptionRequest();
@@ -3447,7 +3442,7 @@
disconnect = () => { disconnect = () => {
this.udn.disconnect(); this.udn.disconnect();
const reconnectAddressPath = StorageModel.getPath( const reconnectAddressPath = StorageModel.getPath(
"connectionModel", "connection" /* ConnectionModel */,
filePaths.connectionModel.reconnectAddress filePaths.connectionModel.reconnectAddress
); );
this.storageModel.remove(reconnectAddressPath); this.storageModel.remove(reconnectAddressPath);
@@ -3455,14 +3450,14 @@
// mailbox // mailbox
getMailboxPath = (address) => { getMailboxPath = (address) => {
const mailboxDirPath = StorageModel.getPath( const mailboxDirPath = StorageModel.getPath(
"connectionModel", "connection" /* ConnectionModel */,
filePaths.connectionModel.mailboxes filePaths.connectionModel.mailboxes
); );
const mailboxFilePath = [...mailboxDirPath, address]; const mailboxFilePath = [...mailboxDirPath, address];
return mailboxFilePath; return mailboxFilePath;
}; };
requestNewMailbox = () => { requestNewMailbox = () => {
console.log("requesting mailbox"); console.log("requesting new mailbox");
this.udn.requestMailbox(); this.udn.requestMailbox();
}; };
connectMailbox = () => { connectMailbox = () => {
@@ -3491,7 +3486,7 @@
// outbox // outbox
getOutboxPath = () => { getOutboxPath = () => {
return StorageModel.getPath( return StorageModel.getPath(
"connectionModel", "connection" /* ConnectionModel */,
filePaths.connectionModel.outbox filePaths.connectionModel.outbox
); );
}; };
@@ -3543,7 +3538,7 @@
// storage // storage
getPreviousAddressPath = () => { getPreviousAddressPath = () => {
return StorageModel.getPath( return StorageModel.getPath(
"connectionModel", "connection" /* ConnectionModel */,
filePaths.connectionModel.previousAddresses filePaths.connectionModel.previousAddresses
); );
}; };
@@ -3553,7 +3548,7 @@
}; };
getReconnectAddressPath = () => { getReconnectAddressPath = () => {
return StorageModel.getPath( return StorageModel.getPath(
"connectionModel", "connection" /* ConnectionModel */,
filePaths.connectionModel.reconnectAddress filePaths.connectionModel.reconnectAddress
); );
}; };
@@ -3823,7 +3818,7 @@
setName(newValue) { setName(newValue) {
this.username = newValue; this.username = newValue;
const path = StorageModel.getPath( const path = StorageModel.getPath(
"settingsModel", "settings" /* SettingsModel */,
filePaths.settingsModel.username filePaths.settingsModel.username
); );
this.storageModel.write(path, newValue); this.storageModel.write(path, newValue);
@@ -3831,7 +3826,7 @@
setFirstDayOfWeek(newValue) { setFirstDayOfWeek(newValue) {
this.firstDayOfWeek = newValue; this.firstDayOfWeek = newValue;
const path = StorageModel.getPath( const path = StorageModel.getPath(
"settingsModel", "settings" /* SettingsModel */,
filePaths.settingsModel.firstDayOfWeek filePaths.settingsModel.firstDayOfWeek
); );
this.storageModel.write(path, newValue); this.storageModel.write(path, newValue);
@@ -3839,7 +3834,7 @@
// load // load
loadUsernam() { loadUsernam() {
const path = StorageModel.getPath( const path = StorageModel.getPath(
"settingsModel", "settings" /* SettingsModel */,
filePaths.settingsModel.username filePaths.settingsModel.username
); );
const content = this.storageModel.read(path); const content = this.storageModel.read(path);
@@ -3847,7 +3842,7 @@
} }
loadFirstDayofWeek() { loadFirstDayofWeek() {
const path = StorageModel.getPath( const path = StorageModel.getPath(
"settingsModel", "settings" /* SettingsModel */,
filePaths.settingsModel.firstDayOfWeek filePaths.settingsModel.firstDayOfWeek
); );
const content = this.storageModel.read(path); const content = this.storageModel.read(path);
+2 -2
View File
@@ -1,5 +1,5 @@
import ChatModel, { ChatMessage } from "./chatModel"; import ChatModel, { ChatMessage } from "./chatModel";
import StorageModel, { filePaths } from "../Global/storageModel"; import StorageModel, { StorageModelSubPath, filePaths } from "../Global/storageModel";
import ConnectionModel from "../Global/connectionModel"; import ConnectionModel from "../Global/connectionModel";
import { Message } from "udn-frontend"; import { Message } from "udn-frontend";
@@ -77,7 +77,7 @@ export default class ChatListModel {
// load // load
loadChats = (): void => { loadChats = (): void => {
const chatDir = StorageModel.getPath("chat", filePaths.chat.base); const chatDir = StorageModel.getPath(StorageModelSubPath.Chat, filePaths.chat.base);
const chatIds = this.storageModel.list(chatDir); const chatIds = this.storageModel.list(chatDir);
for (const chatId of chatIds) { for (const chatId of chatIds) {
const chatModel = new ChatModel( const chatModel = new ChatModel(
+5 -5
View File
@@ -9,7 +9,7 @@ import {
parseValidObject, parseValidObject,
stringify, stringify,
} from "../Utility/utility"; } from "../Utility/utility";
import StorageModel, { filePaths } from "../Global/storageModel"; import StorageModel, { StorageModelSubPath, filePaths } from "../Global/storageModel";
import { decryptString, encryptString } from "../Utility/crypto"; import { decryptString, encryptString } from "../Utility/crypto";
import ChatListModel from "./chatListModel"; import ChatListModel from "./chatListModel";
@@ -39,19 +39,19 @@ export default class ChatModel {
// paths // paths
getBasePath = (): string[] => { getBasePath = (): string[] => {
return StorageModel.getPath("chat", filePaths.chat.chatBase(this.id)); return StorageModel.getPath(StorageModelSubPath.Chat, filePaths.chat.chatBase(this.id));
}; };
getInfoPath = (): string[] => { getInfoPath = (): string[] => {
return StorageModel.getPath("chat", filePaths.chat.info(this.id)); return StorageModel.getPath(StorageModelSubPath.Chat, filePaths.chat.info(this.id));
}; };
getColorPath = (): string[] => { getColorPath = (): string[] => {
return StorageModel.getPath("chat", filePaths.chat.color(this.id)); return StorageModel.getPath(StorageModelSubPath.Chat, filePaths.chat.color(this.id));
}; };
getMessageDirPath = (): string[] => { getMessageDirPath = (): string[] => {
return StorageModel.getPath("chat", filePaths.chat.messages(this.id)); return StorageModel.getPath(StorageModelSubPath.Chat, filePaths.chat.messages(this.id));
}; };
getMessagePath = (id: string): string[] => { getMessagePath = (id: string): string[] => {
+2 -2
View File
@@ -8,7 +8,7 @@ import {
parseValidObject, parseValidObject,
stringify, stringify,
} from "../Utility/utility"; } from "../Utility/utility";
import StorageModel, { filePaths } from "../Global/storageModel"; import StorageModel, { StorageModelSubPath, filePaths } from "../Global/storageModel";
import BoardsAndTasksModel from "./boardsAndTasksModel"; import BoardsAndTasksModel from "./boardsAndTasksModel";
import ChatModel from "../Chat/chatModel"; import ChatModel from "../Chat/chatModel";
@@ -25,7 +25,7 @@ export default class FileModel {
// paths // paths
getBasePath = (): string[] => { getBasePath = (): string[] => {
return StorageModel.getPath( return StorageModel.getPath(
"chat", StorageModelSubPath.Chat,
filePaths.chat.files(this.chatModel.id) filePaths.chat.files(this.chatModel.id)
); );
}; };
+8 -8
View File
@@ -2,7 +2,7 @@
import { ChatMessage, ChatMessageReference } from "../Chat/chatModel"; import { ChatMessage, ChatMessageReference } from "../Chat/chatModel";
import { Handler, HandlerManager, stringify } from "../Utility/utility"; import { Handler, HandlerManager, stringify } from "../Utility/utility";
import StorageModel, { filePaths } from "./storageModel"; import StorageModel, { StorageModelSubPath, filePaths } from "./storageModel";
import UDNFrontend, { Message } from "udn-frontend"; import UDNFrontend, { Message } from "udn-frontend";
export default class ConnectionModel { export default class ConnectionModel {
@@ -34,7 +34,7 @@ export default class ConnectionModel {
this.connectionChangeHandlerManager.trigger(); this.connectionChangeHandlerManager.trigger();
if (this.isConnected == false) return; if (this.isConnected == false) return;
if (!this.address) return; if (this.address == undefined) return;
this.connectMailbox(); this.connectMailbox();
this.storeAddress(this.address); this.storeAddress(this.address);
@@ -52,7 +52,7 @@ export default class ConnectionModel {
// do not reconnect // do not reconnect
const reconnectAddressPath: string[] = StorageModel.getPath( const reconnectAddressPath: string[] = StorageModel.getPath(
"connectionModel", StorageModelSubPath.ConnectionModel,
filePaths.connectionModel.reconnectAddress filePaths.connectionModel.reconnectAddress
); );
this.storageModel.remove(reconnectAddressPath); this.storageModel.remove(reconnectAddressPath);
@@ -61,7 +61,7 @@ export default class ConnectionModel {
// mailbox // mailbox
getMailboxPath = (address: string): string[] => { getMailboxPath = (address: string): string[] => {
const mailboxDirPath = StorageModel.getPath( const mailboxDirPath = StorageModel.getPath(
"connectionModel", StorageModelSubPath.ConnectionModel,
filePaths.connectionModel.mailboxes filePaths.connectionModel.mailboxes
); );
const mailboxFilePath = [...mailboxDirPath, address]; const mailboxFilePath = [...mailboxDirPath, address];
@@ -69,7 +69,7 @@ export default class ConnectionModel {
}; };
requestNewMailbox = (): void => { requestNewMailbox = (): void => {
console.log("requesting mailbox"); console.log("requesting new mailbox");
this.udn.requestMailbox(); this.udn.requestMailbox();
}; };
@@ -107,7 +107,7 @@ export default class ConnectionModel {
// outbox // outbox
getOutboxPath = (): string[] => { getOutboxPath = (): string[] => {
return StorageModel.getPath( return StorageModel.getPath(
"connectionModel", StorageModelSubPath.ConnectionModel,
filePaths.connectionModel.outbox filePaths.connectionModel.outbox
); );
}; };
@@ -173,7 +173,7 @@ export default class ConnectionModel {
// storage // storage
getPreviousAddressPath = (): string[] => { getPreviousAddressPath = (): string[] => {
return StorageModel.getPath( return StorageModel.getPath(
"connectionModel", StorageModelSubPath.ConnectionModel,
filePaths.connectionModel.previousAddresses filePaths.connectionModel.previousAddresses
); );
}; };
@@ -185,7 +185,7 @@ export default class ConnectionModel {
getReconnectAddressPath = (): string[] => { getReconnectAddressPath = (): string[] => {
return StorageModel.getPath( return StorageModel.getPath(
"connectionModel", StorageModelSubPath.ConnectionModel,
filePaths.connectionModel.reconnectAddress filePaths.connectionModel.reconnectAddress
); );
}; };
+5 -5
View File
@@ -1,6 +1,6 @@
// this file is responsible for all settings. // this file is responsible for all settings.
import StorageModel, { filePaths } from "./storageModel"; import StorageModel, { StorageModelSubPath, filePaths } from "./storageModel";
export default class SettingsModel { export default class SettingsModel {
storageModel: StorageModel; storageModel: StorageModel;
@@ -13,7 +13,7 @@ export default class SettingsModel {
setName(newValue: string): void { setName(newValue: string): void {
this.username = newValue; this.username = newValue;
const path = StorageModel.getPath( const path = StorageModel.getPath(
"settingsModel", StorageModelSubPath.SettingsModel,
filePaths.settingsModel.username filePaths.settingsModel.username
); );
this.storageModel.write(path, newValue); this.storageModel.write(path, newValue);
@@ -22,7 +22,7 @@ export default class SettingsModel {
setFirstDayOfWeek(newValue: string): void { setFirstDayOfWeek(newValue: string): void {
this.firstDayOfWeek = newValue; this.firstDayOfWeek = newValue;
const path = StorageModel.getPath( const path = StorageModel.getPath(
"settingsModel", StorageModelSubPath.SettingsModel,
filePaths.settingsModel.firstDayOfWeek filePaths.settingsModel.firstDayOfWeek
); );
this.storageModel.write(path, newValue); this.storageModel.write(path, newValue);
@@ -31,7 +31,7 @@ export default class SettingsModel {
// load // load
loadUsernam(): void { loadUsernam(): void {
const path = StorageModel.getPath( const path = StorageModel.getPath(
"settingsModel", StorageModelSubPath.SettingsModel,
filePaths.settingsModel.username filePaths.settingsModel.username
); );
const content = this.storageModel.read(path); const content = this.storageModel.read(path);
@@ -40,7 +40,7 @@ export default class SettingsModel {
loadFirstDayofWeek(): void { loadFirstDayofWeek(): void {
const path = StorageModel.getPath( const path = StorageModel.getPath(
"settingsModel", StorageModelSubPath.SettingsModel,
filePaths.settingsModel.firstDayOfWeek filePaths.settingsModel.firstDayOfWeek
); );
const content = this.storageModel.read(path); const content = this.storageModel.read(path);
+8 -13
View File
@@ -1,9 +1,6 @@
// this file is responsible for reading and writing persistent data. // this file is responsible for reading and writing persistent data.
import { import { DATA_VERSION, ValidObject } from "../Utility/typeSafety";
DATA_VERSION,
ValidObject,
} from "../Utility/typeSafety";
import { localeCompare, parseValidObject, stringify } from "../Utility/utility"; import { localeCompare, parseValidObject, stringify } from "../Utility/utility";
export const PATH_COMPONENT_SEPARATOR = "\\"; export const PATH_COMPONENT_SEPARATOR = "\\";
@@ -84,7 +81,6 @@ export default class StorageModel {
const content: string | null = this.read(sourcePathComponents); const content: string | null = this.read(sourcePathComponents);
if (content == null) return false; if (content == null) return false;
this.write(destinationPathComponents, content); this.write(destinationPathComponents, content);
this.remove(sourcePathComponents); this.remove(sourcePathComponents);
@@ -212,10 +208,10 @@ export default class StorageModel {
}; };
static getPath( static getPath(
locationName: keyof typeof storageLocations, locationName: StorageModelSubPath,
filePath: string[] filePath: string[]
): string[] { ): string[] {
return [DATA_VERSION, storageLocations[locationName], ...filePath]; return [DATA_VERSION, locationName, ...filePath];
} }
} }
@@ -223,13 +219,12 @@ export default class StorageModel {
export type StorageEntry = { [key: string]: StorageEntry }; export type StorageEntry = { [key: string]: StorageEntry };
// locations // locations
const storageLocations = { export enum StorageModelSubPath {
connectionModel: "connection", Chat = "chat",
chat: "chat", ConnectionModel = "connection",
SettingsModel = "settings",
settingsModel: "settings", }
};
export const filePaths = { export const filePaths = {
connectionModel: { connectionModel: {
+2 -2
View File
@@ -1,7 +1,7 @@
import * as React from "bloatless-react"; import * as React from "bloatless-react";
import ChatModel, { ChatMessage } from "../../Model/Chat/chatModel"; import ChatModel, { ChatMessage } from "../../Model/Chat/chatModel";
import StorageModel, { filePaths } from "../../Model/Global/storageModel"; import StorageModel, { StorageModelSubPath, filePaths } from "../../Model/Global/storageModel";
import ChatListViewModel from "./chatListViewModel"; import ChatListViewModel from "./chatListViewModel";
import { Color } from "../../colors"; import { Color } from "../../colors";
@@ -62,7 +62,7 @@ export default class ChatViewModel {
// load // load
loadPageSelection = (): void => { loadPageSelection = (): void => {
const path: string[] = StorageModel.getPath( const path: string[] = StorageModel.getPath(
"chat", StorageModelSubPath.Chat,
filePaths.chat.lastUsedPage(this.chatModel.id) filePaths.chat.lastUsedPage(this.chatModel.id)
); );
const lastUsedPage: string | null = this.storageModel.read(path); const lastUsedPage: string | null = this.storageModel.read(path);
+2 -2
View File
@@ -51,7 +51,7 @@ export default class BoardViewModel {
}; };
getLastUsedBoardPath = (): string[] => { getLastUsedBoardPath = (): string[] => {
return [...this.getBasePath(), BoardViewModelSubPaths.LastUsedView]; return [...this.getBasePath(), BoardViewModelSubPath.LastUsedView];
}; };
// settings // settings
@@ -264,7 +264,7 @@ export default class BoardViewModel {
} }
} }
export enum BoardViewModelSubPaths { export enum BoardViewModelSubPath {
LastUsedView = "last-used-view", LastUsedView = "last-used-view",
} }
+2 -2
View File
@@ -29,7 +29,7 @@ export default class TaskPageViewModel {
}; };
getLastUsedBoardPath = (): string[] => { getLastUsedBoardPath = (): string[] => {
return [...this.getBasePath(), TaskPageViewModelSubPaths.LastUsedBoard]; return [...this.getBasePath(), TaskPageViewModelSubPath.LastUsedBoard];
}; };
// state // state
@@ -162,6 +162,6 @@ export default class TaskPageViewModel {
} }
} }
export enum TaskPageViewModelSubPaths { export enum TaskPageViewModelSubPath {
LastUsedBoard = "last-used-board", LastUsedBoard = "last-used-board",
} }