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 { import {
DATA_VERSION, DATA_VERSION,
ValidObject, ValidObject,
checkIsValidObject,
} from "./Utility/typeSafety"; } from "./Utility/typeSafety";
import FileModel, { File } from "./fileModel"; import FileModel, { File } from "./fileModel";
import StorageModel, { storageKeys } from "./storageModel"; import StorageModel, { storageKeys } from "./storageModel";
import { import {
createTimestamp, createTimestamp,
parse,
parseValidObject, parseValidObject,
stringify, stringify,
} from "./Utility/utility"; } from "./Utility/utility";
@@ -113,12 +111,15 @@ export default class ChatModel {
// delete // delete
const dirPath: string[] = [...storageKeys.chats, this.id]; const dirPath: string[] = [...storageKeys.chats, this.id];
this.storageModel.removeRecursive(dirPath); this.storageModel.removeRecursively(dirPath);
}; };
// load // load
loadInfo = (): void => { loadInfo = (): void => {
const info: any = this.storageModel.readStringifiable(this.infoPath); const info: ChatInfo | null = this.storageModel.readStringifiable(
this.infoPath,
ChatInfoReference
);
if (info != null) { if (info != null) {
this.info = info; this.info = info;
} else { } else {
@@ -140,16 +141,16 @@ export default class ChatModel {
const messageIds: string[] = this.storageModel.list(this.messageDirPath); const messageIds: string[] = this.storageModel.list(this.messageDirPath);
if (!Array.isArray(messageIds)) return []; if (!Array.isArray(messageIds)) return [];
const messages: ChatMessage[] = []; const chatMessages: 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.readStringifiable(messagePath); const chatMessage: ChatMessage | any =
if (checkIsValidObject(message) == false) continue; 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) a.dateSent.localeCompare(b.dateSent)
); );
return sorted; return sorted;
+11 -12
View File
@@ -1,10 +1,9 @@
// this file is responsible for managing UDN connections. // this file is responsible for managing UDN connections.
import { ChatMessage, ChatMessageReference } from "./chatModel";
import StorageModel, { storageKeys } from "./storageModel"; import StorageModel, { storageKeys } from "./storageModel";
import UDNFrontend, { Message } from "udn-frontend"; import UDNFrontend, { Message } from "udn-frontend";
import { ChatMessage } from "./chatModel";
import { checkIsValidObject } from "./Utility/typeSafety";
import { stringify } from "./Utility/utility"; import { stringify } from "./Utility/utility";
export default class ConnectionModel { export default class ConnectionModel {
@@ -66,7 +65,7 @@ export default class ConnectionModel {
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.read(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();
this.udn.connectMailbox(mailboxId); this.udn.connectMailbox(mailboxId);
@@ -99,19 +98,19 @@ export default class ConnectionModel {
const outboxPath: string[] = storageKeys.outbox; const outboxPath: string[] = storageKeys.outbox;
const messageIds: string[] = this.storageModel.list(outboxPath); const messageIds: string[] = this.storageModel.list(outboxPath);
let messages: ChatMessage[] = []; let chatMessages: ChatMessage[] = [];
for (const messageId of messageIds) { for (const messageId of messageIds) {
const message: any = this.storageModel.readStringifiable([ const chatMessage: ChatMessage | null =
...outboxPath, this.storageModel.readStringifiable(
messageId, [...outboxPath, messageId],
]); ChatMessageReference
if (message == undefined) continue; );
if (checkIsValidObject(message) == false) continue;
messages.push(message); if (chatMessage == null) continue;
chatMessages.push(chatMessage);
} }
return messages; return chatMessages;
}; };
addToOutbox = (chatMessage: ChatMessage): void => { addToOutbox = (chatMessage: ChatMessage): void => {
+7 -5
View File
@@ -1,10 +1,10 @@
// this file is responsible for managing files within chats. // this file is responsible for managing files within chats.
import ChatModel, { ChatMessage } from "./chatModel";
import { DATA_VERSION, ValidObject } from "./Utility/typeSafety"; import { DATA_VERSION, ValidObject } from "./Utility/typeSafety";
import StorageModel, { storageKeys } from "./storageModel"; import StorageModel, { storageKeys } from "./storageModel";
import { parseValidObject, stringify } from "./Utility/utility"; import { parseValidObject, stringify } from "./Utility/utility";
import ChatModel from "./chatModel";
import { v4 } from "uuid"; import { v4 } from "uuid";
export default class FileModel { export default class FileModel {
@@ -58,8 +58,10 @@ export default class FileModel {
getFile = (fileId: string): File | null => { getFile = (fileId: string): File | null => {
const filePath = FileModel.getFilePath(fileId); const filePath = FileModel.getFilePath(fileId);
const fileOrNull: File | null = const fileOrNull: File | null = this.storageModel.readStringifiable(
this.storageModel.readStringifiable<File>(filePath); filePath,
FileReference
);
return fileOrNull; return fileOrNull;
}; };
@@ -73,7 +75,7 @@ export default class FileModel {
): FileContent | null => { ): FileContent | null => {
const filePath = FileModel.getFileContentPath(file, fileContentName); const filePath = FileModel.getFileContentPath(file, fileContentName);
const fileContentOrNull: FileContent | null = const fileContentOrNull: FileContent | null =
this.storageModel.readStringifiable<FileContent>(filePath); this.storageModel.readStringifiable(filePath, FileContentReference);
return fileContentOrNull; return fileContentOrNull;
}; };
@@ -138,4 +140,4 @@ export const FileReference: File = {
id: "", id: "",
contentVersions: [FileContentReference], 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. // 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 { import {
localeCompare, localeCompare,
parse,
parseValidObject, parseValidObject,
stringify, stringify,
} from "./Utility/utility"; } 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)) { a: for (const key of Object.keys(localStorage)) {
const pathComponentsOfCurrentEntity: string[] = const pathComponentsOfCurrentEntity: string[] =
StorageModel.stringToPathComponents(key); StorageModel.stringToPathComponents(key);
@@ -78,11 +77,16 @@ export default class StorageModel {
this.write(pathComponents, valueString); 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); const valueString: string | null = this.read(pathComponents);
if (!valueString) return null; 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; return object;
}; };
+1 -1
View File
@@ -31,7 +31,7 @@ export default class StorageViewModel {
const path = StorageModel.stringToPathComponents(this.selectedPath.value); const path = StorageModel.stringToPathComponents(this.selectedPath.value);
this.lastDeletedItemPath.value = this.selectedPath.value; this.lastDeletedItemPath.value = this.selectedPath.value;
this.storageModel.removeRecursive(path); this.storageModel.removeRecursively(path);
this.didMakeChanges.value = true; this.didMakeChanges.value = true;
}; };