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 StorageModel, { storageKeys } from "./storageModel";
import { ChatModel } from "./chatModel"; import ChatModel from "./chatModel";
import ConnectionModel from "./connectionModel"; import ConnectionModel from "./connectionModel";
import { Message } from "udn-frontend"; import { Message } from "udn-frontend";
import SettingsModel from "./settingsModel"; import SettingsModel from "./settingsModel";
@@ -76,7 +76,7 @@ export default class ChatListModel {
return this.sortedPrimaryChannels.indexOf(primaryChannel); return this.sortedPrimaryChannels.indexOf(primaryChannel);
} }
// restore // load
loadChats = (): void => { loadChats = (): void => {
const chatDir = storageKeys.chats; const chatDir = storageKeys.chats;
const chatIds = this.storageModel.list(chatDir); const chatIds = this.storageModel.list(chatDir);
+11 -11
View File
@@ -73,17 +73,17 @@ export default class ChatModel {
// store & add // store & add
storeInfo = (): void => { storeInfo = (): void => {
this.storageModel.storeStringifiable(this.infoPath, this.info); this.storageModel.writeStringifiable(this.infoPath, this.info);
}; };
storeColor = (): void => { storeColor = (): void => {
this.storageModel.store(this.colorPath, this.color); this.storageModel.write(this.colorPath, this.color);
}; };
addMessage = async (chatMessage: ChatMessage): Promise<void> => { addMessage = async (chatMessage: ChatMessage): Promise<void> => {
await this.decryptMessage(chatMessage); await this.decryptMessage(chatMessage);
const messagePath: string[] = this.getMessagePath(chatMessage.id); const messagePath: string[] = this.getMessagePath(chatMessage.id);
this.storageModel.storeStringifiable(messagePath, chatMessage); this.storageModel.writeStringifiable(messagePath, chatMessage);
this.chatMessageHandler(chatMessage); this.chatMessageHandler(chatMessage);
}; };
@@ -97,9 +97,9 @@ export default class ChatModel {
this.storageModel.removeRecursive(dirPath); this.storageModel.removeRecursive(dirPath);
}; };
// restore // load
restoreInfo = (): void => { loadInfo = (): void => {
const info: any = this.storageModel.restoreStringifiable(this.infoPath); const info: any = this.storageModel.readStringifiable(this.infoPath);
if (info != null) { if (info != null) {
this.info = info; this.info = info;
} else { } else {
@@ -107,9 +107,9 @@ export default class ChatModel {
} }
}; };
restoreColor = (): void => { loadColor = (): void => {
const path: string[] = this.colorPath; const path: string[] = this.colorPath;
const color: string | null = this.storageModel.restore(path); const color: string | null = this.storageModel.read(path);
if (!color) { if (!color) {
this.color = Color.Standard; this.color = Color.Standard;
} else { } else {
@@ -124,7 +124,7 @@ export default class ChatModel {
const messages: ChatMessage[] = []; const messages: ChatMessage[] = [];
for (const messageId of messageIds) { for (const messageId of messageIds) {
const messagePath: string[] = this.getMessagePath(messageId); 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; if (checkIsValidObject(message) == false) continue;
messages.push(message); messages.push(message);
@@ -205,8 +205,8 @@ export default class ChatModel {
this.storageModel = storageModel; this.storageModel = storageModel;
this.chatListModel = chatListModel; this.chatListModel = chatListModel;
this.restoreInfo(); this.loadInfo();
this.restoreColor(); this.loadColor();
this.subscribe(); this.subscribe();
connectionModel.setMessageSentHandler(this.handleMessageSent); connectionModel.setMessageSentHandler(this.handleMessageSent);
+7 -7
View File
@@ -65,7 +65,7 @@ export default class ConnectionModel {
connectMailbox = (): void => { connectMailbox = (): void => {
if (this.address == undefined) return; if (this.address == undefined) return;
const path = [...storageKeys.mailboxes, this.address]; const path = [...storageKeys.mailboxes, this.address];
const mailboxId = this.storageModel.restore(path); const mailboxId = this.storageModel.read(path);
console.log("connecting mailbox", mailboxId) console.log("connecting mailbox", mailboxId)
if (mailboxId == null) return this.requestNewMailbox(); if (mailboxId == null) return this.requestNewMailbox();
@@ -75,7 +75,7 @@ export default class ConnectionModel {
storeMailbox = (mailboxId: string): void => { storeMailbox = (mailboxId: string): void => {
if (this.address == undefined) return; if (this.address == undefined) return;
const path = [...storageKeys.mailboxes, this.address]; const path = [...storageKeys.mailboxes, this.address];
this.storageModel.store(path, mailboxId); this.storageModel.write(path, mailboxId);
}; };
// subscription // subscription
@@ -101,7 +101,7 @@ export default class ConnectionModel {
let messages: ChatMessage[] = []; let messages: ChatMessage[] = [];
for (const messageId of messageIds) { for (const messageId of messageIds) {
const message: any = this.storageModel.restoreStringifiable([ const message: any = this.storageModel.readStringifiable([
...outboxPath, ...outboxPath,
messageId, messageId,
]); ]);
@@ -116,7 +116,7 @@ export default class ConnectionModel {
addToOutbox = (chatMessage: ChatMessage): void => { addToOutbox = (chatMessage: ChatMessage): void => {
const messagePath: string[] = [...storageKeys.outbox, chatMessage.id]; const messagePath: string[] = [...storageKeys.outbox, chatMessage.id];
this.storageModel.storeStringifiable(messagePath, chatMessage); this.storageModel.writeStringifiable(messagePath, chatMessage);
}; };
removeFromOutbox = (chatMessage: ChatMessage): void => { removeFromOutbox = (chatMessage: ChatMessage): void => {
@@ -162,11 +162,11 @@ export default class ConnectionModel {
storeAddress = (address: string): void => { storeAddress = (address: string): void => {
// history // history
const addressPath = this.getAddressPath(address); const addressPath = this.getAddressPath(address);
this.storageModel.store(addressPath, ""); this.storageModel.write(addressPath, "");
// reconnect // reconnect
const reconnectAddressPath: string[] = storageKeys.reconnectAddress; const reconnectAddressPath: string[] = storageKeys.reconnectAddress;
this.storageModel.store(reconnectAddressPath, address); this.storageModel.write(reconnectAddressPath, address);
}; };
removeAddress = (address: string): void => { removeAddress = (address: string): void => {
@@ -227,7 +227,7 @@ export default class ConnectionModel {
// reconnect // reconnect
const reconnectAddressPath: string[] = storageKeys.reconnectAddress; const reconnectAddressPath: string[] = storageKeys.reconnectAddress;
const reconnectAddress: string | null = const reconnectAddress: string | null =
storageModel.restore(reconnectAddressPath); storageModel.read(reconnectAddressPath);
if (reconnectAddress != null) { if (reconnectAddress != null) {
this.connect(reconnectAddress); 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 { export interface File {
dataVersion: "v2"; dataVersion: "v2";
+9 -9
View File
@@ -12,25 +12,25 @@ export default class SettingsModel {
setName(newValue: string): void { setName(newValue: string): void {
this.username = newValue; this.username = newValue;
const path = storageKeys.username; const path = storageKeys.username;
this.storageModel.store(path, newValue); this.storageModel.write(path, newValue);
} }
setFirstDayOfWeek(newValue: number): void { setFirstDayOfWeek(newValue: number): void {
this.firstDayOfWeek = newValue; this.firstDayOfWeek = newValue;
const path = storageKeys.firstDayOfWeek; const path = storageKeys.firstDayOfWeek;
this.storageModel.storeStringifiable(path, newValue); this.storageModel.writeStringifiable(path, newValue);
} }
// restore // load
restoreUsername(): void { loadUsernam(): void {
const path = storageKeys.username; const path = storageKeys.username;
const content = this.storageModel.restore(path); const content = this.storageModel.read(path);
this.username = content ?? ""; this.username = content ?? "";
} }
restoreFirstDayofWeek(): void { loadFirstDayofWeek(): void {
const path = storageKeys.firstDayOfWeek; const path = storageKeys.firstDayOfWeek;
const content = this.storageModel.restoreStringifiable(path); const content = this.storageModel.readStringifiable(path);
this.firstDayOfWeek = content ?? 0; this.firstDayOfWeek = content ?? 0;
} }
@@ -38,7 +38,7 @@ export default class SettingsModel {
constructor(storageModel: StorageModel) { constructor(storageModel: StorageModel) {
this.storageModel = storageModel; this.storageModel = storageModel;
this.restoreUsername(); this.loadUsernam();
this.restoreFirstDayofWeek(); this.loadFirstDayofWeek();
} }
} }
+6 -6
View File
@@ -10,7 +10,7 @@ export default class StorageModel {
storageEntryTree: StorageEntry = {}; storageEntryTree: StorageEntry = {};
// basic // basic
store = (pathComponents: string[], value: string): void => { write = (pathComponents: string[], value: string): void => {
const pathString: string = StorageModel.pathComponentsToString( const pathString: string = StorageModel.pathComponentsToString(
...pathComponents ...pathComponents
); );
@@ -18,7 +18,7 @@ export default class StorageModel {
this.updateTree(...pathComponents); this.updateTree(...pathComponents);
}; };
restore = (pathComponents: string[]): string | null => { read = (pathComponents: string[]): string | null => {
const pathString: string = StorageModel.pathComponentsToString( const pathString: string = StorageModel.pathComponentsToString(
...pathComponents ...pathComponents
); );
@@ -69,13 +69,13 @@ export default class StorageModel {
}; };
// stringifiable // stringifiable
storeStringifiable = (pathComponents: string[], value: any): void => { writeStringifiable = (pathComponents: string[], value: any): void => {
const valueString: string = stringify(value); const valueString: string = stringify(value);
this.store(pathComponents, valueString); this.write(pathComponents, valueString);
}; };
restoreStringifiable = (pathComponents: string[]): any | null => { readStringifiable = (pathComponents: string[]): any | null => {
const valueString: string | null = this.restore(pathComponents); const valueString: string | null = this.read(pathComponents);
if (!valueString) return null; if (!valueString) return null;
return parse(valueString); return parse(valueString);
+3 -3
View File
@@ -68,8 +68,8 @@ export default class ChatListViewModel {
} }
}; };
// restore // load
restoreChats = (): void => { loadChats = (): void => {
this.chatViewModels.clear(); this.chatViewModels.clear();
for (const chatModel of this.chatListModel.chatModels.values()) { for (const chatModel of this.chatListModel.chatModels.values()) {
const chatViewModel = this.createChatViewModel(chatModel); const chatViewModel = this.createChatViewModel(chatModel);
@@ -87,6 +87,6 @@ export default class ChatListViewModel {
this.storageModel = storageModel; this.storageModel = storageModel;
this.settingsViewModel = settingsViewModel; this.settingsViewModel = settingsViewModel;
this.restoreChats(); this.loadChats();
} }
} }
+13 -13
View File
@@ -1,6 +1,6 @@
import * as React from "bloatless-react"; 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 StorageModel, { storageKeys } from "../Model/storageModel";
import ChatListViewModel from "./chatListViewModel"; import ChatListViewModel from "./chatListViewModel";
@@ -63,7 +63,7 @@ export default class ChatViewModel {
// view // view
open = (): void => { open = (): void => {
this.chatListViewModel.openChat(this); this.chatListViewModel.openChat(this);
this.restoreMessages(); this.loadMessages();
}; };
close = (): void => { close = (): void => {
@@ -99,7 +99,7 @@ export default class ChatViewModel {
this.secondaryChannels.add(this.newSecondaryChannelInput.value); this.secondaryChannels.add(this.newSecondaryChannelInput.value);
this.newSecondaryChannelInput.value = ""; this.newSecondaryChannelInput.value = "";
this.storeSecondaryChannels(); this.storeSecondaryChannels();
this.restoreSecondaryChannels(); this.loadSecondaryChannels();
}; };
removeSecondaryChannel = (secondaryChannel: string): void => { removeSecondaryChannel = (secondaryChannel: string): void => {
@@ -128,7 +128,7 @@ export default class ChatViewModel {
remove = (): void => { remove = (): void => {
this.close(); this.close();
this.chatModel.delete(); this.chatModel.delete();
this.chatListViewModel.restoreChats(); this.chatListViewModel.loadChats();
}; };
// messaging // messaging
@@ -152,20 +152,20 @@ export default class ChatViewModel {
messageViewModel.loadData(); messageViewModel.loadData();
}; };
// restore // load
restorePageSelection = (): void => { loadPageSelection = (): void => {
const path: string[] = storageKeys.chatLastUsedPage(this.chatModel.id); 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) { if (lastUsedPage != null) {
this.selectedPage.value = lastUsedPage as any; this.selectedPage.value = lastUsedPage as any;
} }
this.selectedPage.subscribeSilent((newPage) => { this.selectedPage.subscribeSilent((newPage) => {
this.storageModel.store(path, newPage); this.storageModel.write(path, newPage);
}); });
}; };
restoreSecondaryChannels = (): void => { loadSecondaryChannels = (): void => {
this.secondaryChannels.clear(); this.secondaryChannels.clear();
for (const secondaryChannel of this.chatModel.info.secondaryChannels.sort( for (const secondaryChannel of this.chatModel.info.secondaryChannels.sort(
localeCompare localeCompare
@@ -174,7 +174,7 @@ export default class ChatViewModel {
} }
}; };
restoreMessages = (): void => { loadMessages = (): void => {
for (const chatMessage of this.chatModel.messages) { for (const chatMessage of this.chatModel.messages) {
this.addChatMessage(chatMessage); this.addChatMessage(chatMessage);
} }
@@ -198,7 +198,7 @@ export default class ChatViewModel {
this.addChatMessage(chatMessage); this.addChatMessage(chatMessage);
}); });
// restore // load
this.primaryChannel.value = chatModel.info.primaryChannel; this.primaryChannel.value = chatModel.info.primaryChannel;
this.primaryChannelInput.value = chatModel.info.primaryChannel; this.primaryChannelInput.value = chatModel.info.primaryChannel;
@@ -210,8 +210,8 @@ export default class ChatViewModel {
this.color.value = chatModel.color; this.color.value = chatModel.color;
this.restoreSecondaryChannels(); this.loadSecondaryChannels();
this.restorePageSelection(); this.loadPageSelection();
this.updateIndex(); this.updateIndex();
// guards // guards
+1 -1
View File
@@ -20,7 +20,7 @@ export default class StorageViewModel {
// methods // methods
getSelectedItemContent = (): string => { getSelectedItemContent = (): string => {
const path = StorageModel.stringToPathComponents(this.selectedPath.value); const path = StorageModel.stringToPathComponents(this.selectedPath.value);
const content = this.storageModel.restore(path); const content = this.storageModel.read(path);
return ( return (
(content ?? translations.storage.notAFile) || (content ?? translations.storage.notAFile) ||
translations.storage.contentEmpty translations.storage.contentEmpty