This commit is contained in:
marlon-erler
2024-07-21 21:41:19 +02:00
parent 39a86cc486
commit b37f1caa66
9 changed files with 57 additions and 52 deletions
+2 -2
View File
@@ -1,6 +1,6 @@
import StorageModel, { storageKeys } from "./storageModel";
import { ChatModel } from "./chatModel";
import ChatModel from "./chatModel";
import ConnectionModel from "./connectionModel";
import { Message } from "udn-frontend";
import SettingsModel from "./settingsModel";
@@ -76,7 +76,7 @@ export default class ChatListModel {
return this.sortedPrimaryChannels.indexOf(primaryChannel);
}
// restore
// load
loadChats = (): void => {
const chatDir = storageKeys.chats;
const chatIds = this.storageModel.list(chatDir);
+11 -11
View File
@@ -73,17 +73,17 @@ export default class ChatModel {
// store & add
storeInfo = (): void => {
this.storageModel.storeStringifiable(this.infoPath, this.info);
this.storageModel.writeStringifiable(this.infoPath, this.info);
};
storeColor = (): void => {
this.storageModel.store(this.colorPath, this.color);
this.storageModel.write(this.colorPath, this.color);
};
addMessage = async (chatMessage: ChatMessage): Promise<void> => {
await this.decryptMessage(chatMessage);
const messagePath: string[] = this.getMessagePath(chatMessage.id);
this.storageModel.storeStringifiable(messagePath, chatMessage);
this.storageModel.writeStringifiable(messagePath, chatMessage);
this.chatMessageHandler(chatMessage);
};
@@ -97,9 +97,9 @@ export default class ChatModel {
this.storageModel.removeRecursive(dirPath);
};
// restore
restoreInfo = (): void => {
const info: any = this.storageModel.restoreStringifiable(this.infoPath);
// load
loadInfo = (): void => {
const info: any = this.storageModel.readStringifiable(this.infoPath);
if (info != null) {
this.info = info;
} else {
@@ -107,9 +107,9 @@ export default class ChatModel {
}
};
restoreColor = (): void => {
loadColor = (): void => {
const path: string[] = this.colorPath;
const color: string | null = this.storageModel.restore(path);
const color: string | null = this.storageModel.read(path);
if (!color) {
this.color = Color.Standard;
} else {
@@ -124,7 +124,7 @@ export default class ChatModel {
const messages: ChatMessage[] = [];
for (const messageId of messageIds) {
const messagePath: string[] = this.getMessagePath(messageId);
const message: any = this.storageModel.restoreStringifiable(messagePath);
const message: any = this.storageModel.readStringifiable(messagePath);
if (checkIsValidObject(message) == false) continue;
messages.push(message);
@@ -205,8 +205,8 @@ export default class ChatModel {
this.storageModel = storageModel;
this.chatListModel = chatListModel;
this.restoreInfo();
this.restoreColor();
this.loadInfo();
this.loadColor();
this.subscribe();
connectionModel.setMessageSentHandler(this.handleMessageSent);
+7 -7
View File
@@ -65,7 +65,7 @@ export default class ConnectionModel {
connectMailbox = (): void => {
if (this.address == undefined) return;
const path = [...storageKeys.mailboxes, this.address];
const mailboxId = this.storageModel.restore(path);
const mailboxId = this.storageModel.read(path);
console.log("connecting mailbox", mailboxId)
if (mailboxId == null) return this.requestNewMailbox();
@@ -75,7 +75,7 @@ export default class ConnectionModel {
storeMailbox = (mailboxId: string): void => {
if (this.address == undefined) return;
const path = [...storageKeys.mailboxes, this.address];
this.storageModel.store(path, mailboxId);
this.storageModel.write(path, mailboxId);
};
// subscription
@@ -101,7 +101,7 @@ export default class ConnectionModel {
let messages: ChatMessage[] = [];
for (const messageId of messageIds) {
const message: any = this.storageModel.restoreStringifiable([
const message: any = this.storageModel.readStringifiable([
...outboxPath,
messageId,
]);
@@ -116,7 +116,7 @@ export default class ConnectionModel {
addToOutbox = (chatMessage: ChatMessage): void => {
const messagePath: string[] = [...storageKeys.outbox, chatMessage.id];
this.storageModel.storeStringifiable(messagePath, chatMessage);
this.storageModel.writeStringifiable(messagePath, chatMessage);
};
removeFromOutbox = (chatMessage: ChatMessage): void => {
@@ -162,11 +162,11 @@ export default class ConnectionModel {
storeAddress = (address: string): void => {
// history
const addressPath = this.getAddressPath(address);
this.storageModel.store(addressPath, "");
this.storageModel.write(addressPath, "");
// reconnect
const reconnectAddressPath: string[] = storageKeys.reconnectAddress;
this.storageModel.store(reconnectAddressPath, address);
this.storageModel.write(reconnectAddressPath, address);
};
removeAddress = (address: string): void => {
@@ -227,7 +227,7 @@ export default class ConnectionModel {
// reconnect
const reconnectAddressPath: string[] = storageKeys.reconnectAddress;
const reconnectAddress: string | null =
storageModel.restore(reconnectAddressPath);
storageModel.read(reconnectAddressPath);
if (reconnectAddress != null) {
this.connect(reconnectAddress);
}
+5
View File
@@ -1,3 +1,8 @@
// this file is responsible for managing files within chats.
export default class FileModel {}
// types
export interface File {
dataVersion: "v2";
+9 -9
View File
@@ -12,25 +12,25 @@ export default class SettingsModel {
setName(newValue: string): void {
this.username = newValue;
const path = storageKeys.username;
this.storageModel.store(path, newValue);
this.storageModel.write(path, newValue);
}
setFirstDayOfWeek(newValue: number): void {
this.firstDayOfWeek = newValue;
const path = storageKeys.firstDayOfWeek;
this.storageModel.storeStringifiable(path, newValue);
this.storageModel.writeStringifiable(path, newValue);
}
// restore
restoreUsername(): void {
// load
loadUsernam(): void {
const path = storageKeys.username;
const content = this.storageModel.restore(path);
const content = this.storageModel.read(path);
this.username = content ?? "";
}
restoreFirstDayofWeek(): void {
loadFirstDayofWeek(): void {
const path = storageKeys.firstDayOfWeek;
const content = this.storageModel.restoreStringifiable(path);
const content = this.storageModel.readStringifiable(path);
this.firstDayOfWeek = content ?? 0;
}
@@ -38,7 +38,7 @@ export default class SettingsModel {
constructor(storageModel: StorageModel) {
this.storageModel = storageModel;
this.restoreUsername();
this.restoreFirstDayofWeek();
this.loadUsernam();
this.loadFirstDayofWeek();
}
}
+6 -6
View File
@@ -10,7 +10,7 @@ export default class StorageModel {
storageEntryTree: StorageEntry = {};
// basic
store = (pathComponents: string[], value: string): void => {
write = (pathComponents: string[], value: string): void => {
const pathString: string = StorageModel.pathComponentsToString(
...pathComponents
);
@@ -18,7 +18,7 @@ export default class StorageModel {
this.updateTree(...pathComponents);
};
restore = (pathComponents: string[]): string | null => {
read = (pathComponents: string[]): string | null => {
const pathString: string = StorageModel.pathComponentsToString(
...pathComponents
);
@@ -69,13 +69,13 @@ export default class StorageModel {
};
// stringifiable
storeStringifiable = (pathComponents: string[], value: any): void => {
writeStringifiable = (pathComponents: string[], value: any): void => {
const valueString: string = stringify(value);
this.store(pathComponents, valueString);
this.write(pathComponents, valueString);
};
restoreStringifiable = (pathComponents: string[]): any | null => {
const valueString: string | null = this.restore(pathComponents);
readStringifiable = (pathComponents: string[]): any | null => {
const valueString: string | null = this.read(pathComponents);
if (!valueString) return null;
return parse(valueString);