remodel storage location map

This commit is contained in:
marlon-erler
2024-07-22 03:01:04 +02:00
parent 839e6e6b2a
commit c21d665776
8 changed files with 452 additions and 272 deletions
+52 -14
View File
@@ -1,7 +1,7 @@
// this file is responsible for managing UDN connections.
import { ChatMessage, ChatMessageReference } from "../Chat/chatModel";
import StorageModel, { storageKeys } from "./storageModel";
import StorageModel, { filePaths } from "./storageModel";
import UDNFrontend, { Message } from "udn-frontend";
import { stringify } from "../Utility/utility";
@@ -52,11 +52,23 @@ export default class ConnectionModel {
this.udn.disconnect();
// do not reconnect
const reconnectAddressPath: string[] = storageKeys.reconnectAddress;
const reconnectAddressPath: string[] = StorageModel.getPath(
"connectionModel",
filePaths.connectionModel.reconnectAddress
);
this.storageModel.remove(reconnectAddressPath);
};
// mailbox
getMailboxPath = (address: string): string[] => {
const mailboxDirPath = StorageModel.getPath(
"connectionModel",
filePaths.connectionModel.mailboxes
);
const mailboxFilePath = [...mailboxDirPath, address];
return mailboxFilePath;
};
requestNewMailbox = (): void => {
console.trace("requesting mailbox");
this.udn.requestMailbox();
@@ -64,8 +76,8 @@ export default class ConnectionModel {
connectMailbox = (): void => {
if (this.address == undefined) return;
const path = [...storageKeys.mailboxes, this.address];
const mailboxId = this.storageModel.read(path);
const mailboxId = this.storageModel.read(this.getMailboxPath(this.address));
console.log("connecting mailbox", mailboxId);
if (mailboxId == null) return this.requestNewMailbox();
@@ -74,8 +86,7 @@ export default class ConnectionModel {
storeMailbox = (mailboxId: string): void => {
if (this.address == undefined) return;
const path = [...storageKeys.mailboxes, this.address];
this.storageModel.write(path, mailboxId);
this.storageModel.write(this.getMailboxPath(this.address), mailboxId);
};
// subscription
@@ -95,8 +106,15 @@ export default class ConnectionModel {
};
// outbox
getOutboxPath = (): string[] => {
return StorageModel.getPath(
"connectionModel",
filePaths.connectionModel.outbox
);
};
getOutboxMessags = (): ChatMessage[] => {
const outboxPath: string[] = storageKeys.outbox;
const outboxPath: string[] = this.getOutboxPath();
const messageIds: string[] = this.storageModel.list(outboxPath);
let chatMessages: ChatMessage[] = [];
@@ -115,12 +133,12 @@ export default class ConnectionModel {
};
addToOutbox = (chatMessage: ChatMessage): void => {
const messagePath: string[] = [...storageKeys.outbox, chatMessage.id];
const messagePath: string[] = [...this.getOutboxPath(), chatMessage.id];
this.storageModel.writeStringifiable(messagePath, chatMessage);
};
removeFromOutbox = (chatMessage: ChatMessage): void => {
const messagePath: string[] = [...storageKeys.outbox, chatMessage.id];
const messagePath: string[] = [...this.getOutboxPath(), chatMessage.id];
this.storageModel.remove(messagePath);
};
@@ -154,18 +172,32 @@ export default class ConnectionModel {
};
// storage
private getAddressPath = (address: string): string[] => {
const dirPath = storageKeys.previousAddresses;
getPreviousAddressPath = (): string[] => {
return StorageModel.getPath(
"connectionModel",
filePaths.connectionModel.previousAddresses
);
};
getAddressPath = (address: string): string[] => {
const dirPath = this.getPreviousAddressPath();
return [...dirPath, address];
};
getReconnectAddressPath = (): string[] => {
return StorageModel.getPath(
"connectionModel",
filePaths.connectionModel.reconnectAddress
);
};
storeAddress = (address: string): void => {
// history
const addressPath = this.getAddressPath(address);
this.storageModel.write(addressPath, "");
// reconnect
const reconnectAddressPath: string[] = storageKeys.reconnectAddress;
const reconnectAddressPath: string[] = this.getReconnectAddressPath();
this.storageModel.write(reconnectAddressPath, address);
};
@@ -175,7 +207,7 @@ export default class ConnectionModel {
};
get addresses(): string[] {
const dirPath = storageKeys.previousAddresses;
const dirPath = this.getPreviousAddressPath();
return this.storageModel.list(dirPath);
}
@@ -225,7 +257,7 @@ export default class ConnectionModel {
};
// reconnect
const reconnectAddressPath: string[] = storageKeys.reconnectAddress;
const reconnectAddressPath: string[] = this.getPreviousAddressPath();
const reconnectAddress: string | null =
storageModel.read(reconnectAddressPath);
if (reconnectAddress != null) {
@@ -233,3 +265,9 @@ export default class ConnectionModel {
}
}
}
// paths
export enum settingsModelStorageFiles {
username = "user-name",
firstDayOfWeek = "first-day-of-week",
}
+18 -6
View File
@@ -1,6 +1,6 @@
// this file is responsible for all settings.
import StorageModel, { storageKeys } from "./storageModel";
import StorageModel, { filePaths } from "./storageModel";
export default class SettingsModel {
storageModel: StorageModel;
@@ -9,28 +9,40 @@ export default class SettingsModel {
username: string;
firstDayOfWeek: string;
// set
// storage
setName(newValue: string): void {
this.username = newValue;
const path = storageKeys.username;
const path = StorageModel.getPath(
"settingsModel",
filePaths.settingsModel.username
);
this.storageModel.write(path, newValue);
}
setFirstDayOfWeek(newValue: string): void {
this.firstDayOfWeek = newValue;
const path = storageKeys.firstDayOfWeek;
const path = StorageModel.getPath(
"settingsModel",
filePaths.settingsModel.firstDayOfWeek
);
this.storageModel.write(path, newValue);
}
// load
loadUsernam(): void {
const path = storageKeys.username;
const path = StorageModel.getPath(
"settingsModel",
filePaths.settingsModel.username
);
const content = this.storageModel.read(path);
this.username = content ?? "";
}
loadFirstDayofWeek(): void {
const path = storageKeys.firstDayOfWeek;
const path = StorageModel.getPath(
"settingsModel",
filePaths.settingsModel.firstDayOfWeek
);
const content = this.storageModel.read(path);
this.firstDayOfWeek = content ?? "0";
}
+48 -31
View File
@@ -90,12 +90,7 @@ export default class StorageModel {
return object;
};
// init
constructor() {
this.initializeTree();
}
// utility
// tree
initializeTree = (): void => {
console.log("initializing tree");
@@ -121,6 +116,12 @@ export default class StorageModel {
return stringify(this.storageEntryTree);
};
// init
constructor() {
this.initializeTree();
}
// utility
static getFileName = (pathComponents: string[]): string => {
return pathComponents[pathComponents.length - 1] || "\\";
};
@@ -146,36 +147,52 @@ export default class StorageModel {
}
return StorageModel.pathComponentsToString(...allComponents);
};
static getPath(
locationName: keyof typeof storageLocations,
filePath: string[]
): string[] {
return [DATA_VERSION, storageLocations[locationName], ...filePath];
}
}
// types
export type StorageEntry = { [key: string]: StorageEntry };
// keys
export const storageKeys = {
// connection
socketAddress: [DATA_VERSION, "connection", "socket-address"],
reconnectAddress: [DATA_VERSION, "connection", "reconnect-address"],
outbox: [DATA_VERSION, "connection", "outbox"],
mailboxes: [DATA_VERSION, "connection", "mailboxes"],
// locations
const storageLocations = {
connectionModel: "connection",
// settings
username: [DATA_VERSION, "settings", "user-name"],
firstDayOfWeek: [DATA_VERSION, "settings", "first-day-of-week"],
chat: "chat",
// history
previousAddresses: [DATA_VERSION, "history", "previous-addresses"],
// chat
chats: [DATA_VERSION, "chat"],
chatInfo: (id: string) => [DATA_VERSION, "chat", id, "info"],
chatLastUsedPage: (id: string) => [
DATA_VERSION,
"chat",
id,
"last-used-page",
],
chatColor: (id: string) => [DATA_VERSION, "chat", id, "color"],
chatMessages: (id: string) => [DATA_VERSION, "chat", id, "messages"],
chatFiles: [DATA_VERSION, "chat", "files"],
settingsModel: "settings",
};
export const filePaths = {
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"],
files: (id: string) => [...filePaths.chat.chatBase(id), "files"],
lastUsedPage: (id: string) => [
...filePaths.chat.chatBase(id),
"last-used-page",
],
},
settingsModel: {
username: ["user-name"],
firstDayOfWeek: ["first-day-of-week"],
},
};