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);
+3 -3
View File
@@ -68,8 +68,8 @@ export default class ChatListViewModel {
}
};
// restore
restoreChats = (): void => {
// load
loadChats = (): void => {
this.chatViewModels.clear();
for (const chatModel of this.chatListModel.chatModels.values()) {
const chatViewModel = this.createChatViewModel(chatModel);
@@ -87,6 +87,6 @@ export default class ChatListViewModel {
this.storageModel = storageModel;
this.settingsViewModel = settingsViewModel;
this.restoreChats();
this.loadChats();
}
}
+13 -13
View File
@@ -1,6 +1,6 @@
import * as React from "bloatless-react";
import { ChatMessage, ChatModel } from "../Model/chatModel";
import ChatModel, { ChatMessage } from "../Model/chatModel";
import StorageModel, { storageKeys } from "../Model/storageModel";
import ChatListViewModel from "./chatListViewModel";
@@ -63,7 +63,7 @@ export default class ChatViewModel {
// view
open = (): void => {
this.chatListViewModel.openChat(this);
this.restoreMessages();
this.loadMessages();
};
close = (): void => {
@@ -99,7 +99,7 @@ export default class ChatViewModel {
this.secondaryChannels.add(this.newSecondaryChannelInput.value);
this.newSecondaryChannelInput.value = "";
this.storeSecondaryChannels();
this.restoreSecondaryChannels();
this.loadSecondaryChannels();
};
removeSecondaryChannel = (secondaryChannel: string): void => {
@@ -128,7 +128,7 @@ export default class ChatViewModel {
remove = (): void => {
this.close();
this.chatModel.delete();
this.chatListViewModel.restoreChats();
this.chatListViewModel.loadChats();
};
// messaging
@@ -152,20 +152,20 @@ export default class ChatViewModel {
messageViewModel.loadData();
};
// restore
restorePageSelection = (): void => {
// load
loadPageSelection = (): void => {
const path: string[] = storageKeys.chatLastUsedPage(this.chatModel.id);
const lastUsedPage: string | null = this.storageModel.restore(path);
const lastUsedPage: string | null = this.storageModel.read(path);
if (lastUsedPage != null) {
this.selectedPage.value = lastUsedPage as any;
}
this.selectedPage.subscribeSilent((newPage) => {
this.storageModel.store(path, newPage);
this.storageModel.write(path, newPage);
});
};
restoreSecondaryChannels = (): void => {
loadSecondaryChannels = (): void => {
this.secondaryChannels.clear();
for (const secondaryChannel of this.chatModel.info.secondaryChannels.sort(
localeCompare
@@ -174,7 +174,7 @@ export default class ChatViewModel {
}
};
restoreMessages = (): void => {
loadMessages = (): void => {
for (const chatMessage of this.chatModel.messages) {
this.addChatMessage(chatMessage);
}
@@ -198,7 +198,7 @@ export default class ChatViewModel {
this.addChatMessage(chatMessage);
});
// restore
// load
this.primaryChannel.value = chatModel.info.primaryChannel;
this.primaryChannelInput.value = chatModel.info.primaryChannel;
@@ -210,8 +210,8 @@ export default class ChatViewModel {
this.color.value = chatModel.color;
this.restoreSecondaryChannels();
this.restorePageSelection();
this.loadSecondaryChannels();
this.loadPageSelection();
this.updateIndex();
// guards
+1 -1
View File
@@ -20,7 +20,7 @@ export default class StorageViewModel {
// methods
getSelectedItemContent = (): string => {
const path = StorageModel.stringToPathComponents(this.selectedPath.value);
const content = this.storageModel.restore(path);
const content = this.storageModel.read(path);
return (
(content ?? translations.storage.notAFile) ||
translations.storage.contentEmpty