From b37f1caa669b3f0c924fa39e49cc41586c594aec Mon Sep 17 00:00:00 2001 From: marlon-erler Date: Sun, 21 Jul 2024 21:41:19 +0200 Subject: [PATCH] cleanup --- src/Model/chatListModel.ts | 4 ++-- src/Model/chatModel.ts | 22 +++++++++++----------- src/Model/connectionModel.ts | 14 +++++++------- src/Model/fileModel.ts | 5 +++++ src/Model/settingsModel.ts | 18 +++++++++--------- src/Model/storageModel.ts | 12 ++++++------ src/ViewModel/chatListViewModel.ts | 6 +++--- src/ViewModel/chatViewModel.ts | 26 +++++++++++++------------- src/ViewModel/storageViewModel.ts | 2 +- 9 files changed, 57 insertions(+), 52 deletions(-) diff --git a/src/Model/chatListModel.ts b/src/Model/chatListModel.ts index 8fb8e27e..8bc2fdcd 100644 --- a/src/Model/chatListModel.ts +++ b/src/Model/chatListModel.ts @@ -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); diff --git a/src/Model/chatModel.ts b/src/Model/chatModel.ts index 890edd19..0ba63f57 100644 --- a/src/Model/chatModel.ts +++ b/src/Model/chatModel.ts @@ -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 => { 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); diff --git a/src/Model/connectionModel.ts b/src/Model/connectionModel.ts index 90c79d9c..3650d91f 100644 --- a/src/Model/connectionModel.ts +++ b/src/Model/connectionModel.ts @@ -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); } diff --git a/src/Model/fileModel.ts b/src/Model/fileModel.ts index 78e2d20e..b003cc6a 100644 --- a/src/Model/fileModel.ts +++ b/src/Model/fileModel.ts @@ -1,3 +1,8 @@ +// this file is responsible for managing files within chats. + +export default class FileModel {} + +// types export interface File { dataVersion: "v2"; diff --git a/src/Model/settingsModel.ts b/src/Model/settingsModel.ts index 89167255..4e3ab2ad 100644 --- a/src/Model/settingsModel.ts +++ b/src/Model/settingsModel.ts @@ -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(); } } diff --git a/src/Model/storageModel.ts b/src/Model/storageModel.ts index 0dd6ea31..3a94f724 100644 --- a/src/Model/storageModel.ts +++ b/src/Model/storageModel.ts @@ -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); diff --git a/src/ViewModel/chatListViewModel.ts b/src/ViewModel/chatListViewModel.ts index a550c6c9..121a889d 100644 --- a/src/ViewModel/chatListViewModel.ts +++ b/src/ViewModel/chatListViewModel.ts @@ -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(); } } diff --git a/src/ViewModel/chatViewModel.ts b/src/ViewModel/chatViewModel.ts index 87587f3d..2c73ca98 100644 --- a/src/ViewModel/chatViewModel.ts +++ b/src/ViewModel/chatViewModel.ts @@ -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 diff --git a/src/ViewModel/storageViewModel.ts b/src/ViewModel/storageViewModel.ts index 9a8b5b38..45ccece7 100644 --- a/src/ViewModel/storageViewModel.ts +++ b/src/ViewModel/storageViewModel.ts @@ -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