apply checkMatchesObjectStructure in models

This commit is contained in:
marlon-erler
2024-07-22 01:38:08 +02:00
parent 3c8245fca0
commit d85ced4e36
5 changed files with 38 additions and 32 deletions
+10 -9
View File
@@ -3,13 +3,11 @@
import {
DATA_VERSION,
ValidObject,
checkIsValidObject,
} from "./Utility/typeSafety";
import FileModel, { File } from "./fileModel";
import StorageModel, { storageKeys } from "./storageModel";
import {
createTimestamp,
parse,
parseValidObject,
stringify,
} from "./Utility/utility";
@@ -113,12 +111,15 @@ export default class ChatModel {
// delete
const dirPath: string[] = [...storageKeys.chats, this.id];
this.storageModel.removeRecursive(dirPath);
this.storageModel.removeRecursively(dirPath);
};
// load
loadInfo = (): void => {
const info: any = this.storageModel.readStringifiable(this.infoPath);
const info: ChatInfo | null = this.storageModel.readStringifiable(
this.infoPath,
ChatInfoReference
);
if (info != null) {
this.info = info;
} else {
@@ -140,16 +141,16 @@ export default class ChatModel {
const messageIds: string[] = this.storageModel.list(this.messageDirPath);
if (!Array.isArray(messageIds)) return [];
const messages: ChatMessage[] = [];
const chatMessages: ChatMessage[] = [];
for (const messageId of messageIds) {
const messagePath: string[] = this.getMessagePath(messageId);
const message: any = this.storageModel.readStringifiable(messagePath);
if (checkIsValidObject(message) == false) continue;
const chatMessage: ChatMessage | any =
this.storageModel.readStringifiable(messagePath, ChatMessageReference);
messages.push(message);
chatMessages.push(chatMessage);
}
const sorted = messages.sort((a, b) =>
const sorted = chatMessages.sort((a, b) =>
a.dateSent.localeCompare(b.dateSent)
);
return sorted;
+11 -12
View File
@@ -1,10 +1,9 @@
// this file is responsible for managing UDN connections.
import { ChatMessage, ChatMessageReference } from "./chatModel";
import StorageModel, { storageKeys } from "./storageModel";
import UDNFrontend, { Message } from "udn-frontend";
import { ChatMessage } from "./chatModel";
import { checkIsValidObject } from "./Utility/typeSafety";
import { stringify } from "./Utility/utility";
export default class ConnectionModel {
@@ -66,7 +65,7 @@ export default class ConnectionModel {
if (this.address == undefined) return;
const path = [...storageKeys.mailboxes, this.address];
const mailboxId = this.storageModel.read(path);
console.log("connecting mailbox", mailboxId)
console.log("connecting mailbox", mailboxId);
if (mailboxId == null) return this.requestNewMailbox();
this.udn.connectMailbox(mailboxId);
@@ -99,19 +98,19 @@ export default class ConnectionModel {
const outboxPath: string[] = storageKeys.outbox;
const messageIds: string[] = this.storageModel.list(outboxPath);
let messages: ChatMessage[] = [];
let chatMessages: ChatMessage[] = [];
for (const messageId of messageIds) {
const message: any = this.storageModel.readStringifiable([
...outboxPath,
messageId,
]);
if (message == undefined) continue;
if (checkIsValidObject(message) == false) continue;
const chatMessage: ChatMessage | null =
this.storageModel.readStringifiable(
[...outboxPath, messageId],
ChatMessageReference
);
messages.push(message);
if (chatMessage == null) continue;
chatMessages.push(chatMessage);
}
return messages;
return chatMessages;
};
addToOutbox = (chatMessage: ChatMessage): void => {
+7 -5
View File
@@ -1,10 +1,10 @@
// this file is responsible for managing files within chats.
import ChatModel, { ChatMessage } from "./chatModel";
import { DATA_VERSION, ValidObject } from "./Utility/typeSafety";
import StorageModel, { storageKeys } from "./storageModel";
import { parseValidObject, stringify } from "./Utility/utility";
import ChatModel from "./chatModel";
import { v4 } from "uuid";
export default class FileModel {
@@ -58,8 +58,10 @@ export default class FileModel {
getFile = (fileId: string): File | null => {
const filePath = FileModel.getFilePath(fileId);
const fileOrNull: File | null =
this.storageModel.readStringifiable<File>(filePath);
const fileOrNull: File | null = this.storageModel.readStringifiable(
filePath,
FileReference
);
return fileOrNull;
};
@@ -73,7 +75,7 @@ export default class FileModel {
): FileContent | null => {
const filePath = FileModel.getFileContentPath(file, fileContentName);
const fileContentOrNull: FileContent | null =
this.storageModel.readStringifiable<FileContent>(filePath);
this.storageModel.readStringifiable(filePath, FileContentReference);
return fileContentOrNull;
};
@@ -138,4 +140,4 @@ export const FileReference: File = {
id: "",
contentVersions: [FileContentReference],
};
};
+9 -5
View File
@@ -1,9 +1,8 @@
// this file is responsible for reading and writing persistent data; all storage shall be handled by this file.
import { DATA_VERSION, ValidObject } from "./Utility/typeSafety";
import { DATA_VERSION, ValidObject, checkMatchesObjectStructure } from "./Utility/typeSafety";
import {
localeCompare,
parse,
parseValidObject,
stringify,
} from "./Utility/utility";
@@ -43,7 +42,7 @@ export default class StorageModel {
}
};
removeRecursive = (pathComponentsOfEntityToDelete: string[]): void => {
removeRecursively = (pathComponentsOfEntityToDelete: string[]): void => {
a: for (const key of Object.keys(localStorage)) {
const pathComponentsOfCurrentEntity: string[] =
StorageModel.stringToPathComponents(key);
@@ -78,11 +77,16 @@ export default class StorageModel {
this.write(pathComponents, valueString);
};
readStringifiable = <T extends ValidObject>(pathComponents: string[]): T | null => {
readStringifiable = <T extends ValidObject>(pathComponents: string[], reference: T): T | null => {
const valueString: string | null = this.read(pathComponents);
if (!valueString) return null;
const object: T | null = parseValidObject(valueString);
const object: any | null = parseValidObject(valueString);
if (object == null) return null;
const doesMatchReference: boolean = checkMatchesObjectStructure(object, reference);
if (doesMatchReference == false) return null;
return object;
};
+1 -1
View File
@@ -31,7 +31,7 @@ export default class StorageViewModel {
const path = StorageModel.stringToPathComponents(this.selectedPath.value);
this.lastDeletedItemPath.value = this.selectedPath.value;
this.storageModel.removeRecursive(path);
this.storageModel.removeRecursively(path);
this.didMakeChanges.value = true;
};