2024-07-19 19:47:07 +02:00
|
|
|
// this file is responsible for managing chats and chat messages. objects are delegated to objectModel.ts.
|
|
|
|
|
|
2024-07-19 20:07:14 +02:00
|
|
|
import StorageModel, { storageKeys } from "./storageModel";
|
|
|
|
|
|
2024-07-20 14:16:26 +02:00
|
|
|
import ChatListModel from "./chatListModel";
|
2024-07-19 23:45:07 +02:00
|
|
|
import { checkIsValidObject } from "./Utility/typeSafety";
|
|
|
|
|
import { createTimestamp } from "./Utility/utility";
|
2024-07-19 20:50:42 +02:00
|
|
|
import { v4 } from "uuid";
|
2024-07-19 19:49:55 +02:00
|
|
|
|
2024-07-19 20:07:14 +02:00
|
|
|
export class ChatModel {
|
2024-07-19 20:50:42 +02:00
|
|
|
// data
|
|
|
|
|
id: string;
|
|
|
|
|
storageModel: StorageModel;
|
2024-07-20 14:16:26 +02:00
|
|
|
chatListModel: ChatListModel;
|
2024-07-19 20:50:42 +02:00
|
|
|
info: ChatInfo;
|
2024-07-19 20:07:14 +02:00
|
|
|
|
2024-07-19 20:50:42 +02:00
|
|
|
messages = new Set<ChatMessage>();
|
|
|
|
|
objects = new Map<string, ChatObject>();
|
|
|
|
|
outbox = new Set<ChatMessage>();
|
2024-07-19 20:07:14 +02:00
|
|
|
|
2024-07-19 20:50:42 +02:00
|
|
|
// paths
|
2024-07-20 02:56:44 +02:00
|
|
|
get infoPath(): string[] {
|
2024-07-19 20:50:42 +02:00
|
|
|
return storageKeys.chatInfo(this.id);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-20 02:56:44 +02:00
|
|
|
get messageDirPath(): string[] {
|
2024-07-19 20:50:42 +02:00
|
|
|
return storageKeys.chatMessages(this.id);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-20 02:56:44 +02:00
|
|
|
get objectDirPath(): string[] {
|
2024-07-19 20:50:42 +02:00
|
|
|
return storageKeys.chatObjects(this.id);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-20 02:56:44 +02:00
|
|
|
get outboxDirPath(): string[] {
|
2024-07-19 20:50:42 +02:00
|
|
|
return storageKeys.chatOutbox(this.id);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-20 05:10:50 +02:00
|
|
|
getMessagePath = (id: string): string[] => {
|
2024-07-20 02:56:44 +02:00
|
|
|
return [...this.messageDirPath, id];
|
2024-07-20 05:10:50 +02:00
|
|
|
};
|
2024-07-19 20:50:42 +02:00
|
|
|
|
2024-07-20 05:10:50 +02:00
|
|
|
getObjectPath = (id: string): string[] => {
|
2024-07-20 02:56:44 +02:00
|
|
|
return [...this.objectDirPath, id];
|
2024-07-20 05:10:50 +02:00
|
|
|
};
|
2024-07-19 20:50:42 +02:00
|
|
|
|
2024-07-19 21:10:31 +02:00
|
|
|
// set
|
2024-07-20 05:10:50 +02:00
|
|
|
setPrimaryChannel = (primaryChannel: string): void => {
|
2024-07-19 21:10:31 +02:00
|
|
|
this.info.primaryChannel = primaryChannel;
|
|
|
|
|
this.storeInfo();
|
2024-07-20 17:30:36 +02:00
|
|
|
this.chatListModel.updateIndices();
|
2024-07-20 05:10:50 +02:00
|
|
|
};
|
2024-07-19 21:10:31 +02:00
|
|
|
|
2024-07-20 05:10:50 +02:00
|
|
|
setSecondaryChannels = (secondaryChannels: string[]): void => {
|
2024-07-19 21:10:31 +02:00
|
|
|
this.info.secondaryChannels = secondaryChannels;
|
|
|
|
|
this.storeInfo();
|
2024-07-20 05:10:50 +02:00
|
|
|
};
|
2024-07-19 21:10:31 +02:00
|
|
|
|
2024-07-19 20:50:42 +02:00
|
|
|
// store & add
|
2024-07-20 05:10:50 +02:00
|
|
|
storeInfo = (): void => {
|
2024-07-19 20:50:42 +02:00
|
|
|
this.storageModel.storeStringifiable(this.infoPath, this.info);
|
2024-07-20 05:10:50 +02:00
|
|
|
};
|
2024-07-19 20:50:42 +02:00
|
|
|
|
2024-07-20 05:10:50 +02:00
|
|
|
addMessage = (message: ChatMessage): void => {
|
2024-07-19 20:50:42 +02:00
|
|
|
if (!this.messages.has(message)) {
|
|
|
|
|
this.messages.add(message);
|
2024-07-19 20:07:14 +02:00
|
|
|
}
|
2024-07-20 02:56:44 +02:00
|
|
|
const messagePath: string[] = this.getMessagePath(message.id);
|
2024-07-19 20:50:42 +02:00
|
|
|
this.storageModel.storeStringifiable(messagePath, message);
|
2024-07-20 05:10:50 +02:00
|
|
|
};
|
2024-07-19 20:50:42 +02:00
|
|
|
|
2024-07-20 05:10:50 +02:00
|
|
|
addObject = (object: ChatObject): void => {
|
2024-07-19 20:50:42 +02:00
|
|
|
this.objects.set(object.id, object);
|
2024-07-20 02:56:44 +02:00
|
|
|
const objectPath: string[] = this.getObjectPath(object.id);
|
2024-07-19 20:50:42 +02:00
|
|
|
this.storageModel.storeStringifiable(objectPath, object);
|
2024-07-20 05:10:50 +02:00
|
|
|
};
|
2024-07-19 20:50:42 +02:00
|
|
|
|
2024-07-20 14:16:26 +02:00
|
|
|
// delete
|
|
|
|
|
delete = () => {
|
|
|
|
|
// untrack
|
2024-07-20 04:43:16 +02:00
|
|
|
this.unloadData();
|
2024-07-20 14:16:26 +02:00
|
|
|
this.chatListModel.untrackChat(this);
|
|
|
|
|
|
|
|
|
|
// delete
|
2024-07-20 04:43:16 +02:00
|
|
|
const dirPath = [...storageKeys.chats, this.id];
|
|
|
|
|
this.storageModel.removeRecursive(dirPath);
|
2024-07-20 05:10:50 +02:00
|
|
|
};
|
2024-07-20 04:43:16 +02:00
|
|
|
|
2024-07-19 20:50:42 +02:00
|
|
|
// restore
|
2024-07-20 05:10:50 +02:00
|
|
|
restoreInfo = (): void => {
|
2024-07-19 20:50:42 +02:00
|
|
|
const info: any = this.storageModel.restoreStringifiable(this.infoPath);
|
2024-07-20 05:10:50 +02:00
|
|
|
if (info != null) {
|
|
|
|
|
this.info = info
|
|
|
|
|
} else {
|
|
|
|
|
this.info = generateChatInfo("0")
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-07-19 20:50:42 +02:00
|
|
|
|
2024-07-20 05:10:50 +02:00
|
|
|
restoreMessages = (): void => {
|
2024-07-19 20:50:42 +02:00
|
|
|
const messageIds: string[] = this.storageModel.list(this.messageDirPath);
|
|
|
|
|
if (!Array.isArray(messageIds)) return;
|
|
|
|
|
|
|
|
|
|
for (const messageId of messageIds) {
|
2024-07-20 02:56:44 +02:00
|
|
|
const messagePath: string[] = this.getMessagePath(messageId);
|
2024-07-19 20:50:42 +02:00
|
|
|
const message: any = this.storageModel.restoreStringifiable(messagePath);
|
|
|
|
|
if (checkIsValidObject(message) == false) return;
|
|
|
|
|
|
|
|
|
|
this.messages.add(message);
|
|
|
|
|
}
|
2024-07-20 05:10:50 +02:00
|
|
|
};
|
2024-07-19 20:50:42 +02:00
|
|
|
|
2024-07-20 05:10:50 +02:00
|
|
|
restoreObjects = (): void => {
|
2024-07-19 20:50:42 +02:00
|
|
|
const objectIds: string[] = this.storageModel.list(this.objectDirPath);
|
|
|
|
|
if (!Array.isArray(objectIds)) return;
|
|
|
|
|
|
|
|
|
|
for (const objectId of objectIds) {
|
2024-07-20 02:56:44 +02:00
|
|
|
const objectPathComponents: string[] = this.getObjectPath(objectId);
|
2024-07-20 05:10:50 +02:00
|
|
|
const object: any =
|
|
|
|
|
this.storageModel.restoreStringifiable(objectPathComponents);
|
2024-07-19 20:50:42 +02:00
|
|
|
if (checkIsValidObject(object) == false) return;
|
|
|
|
|
if (object.id != objectId) return;
|
|
|
|
|
|
|
|
|
|
this.objects.set(objectId, object);
|
|
|
|
|
}
|
2024-07-20 05:10:50 +02:00
|
|
|
};
|
2024-07-19 20:50:42 +02:00
|
|
|
|
2024-07-20 04:43:16 +02:00
|
|
|
// memory
|
2024-07-20 05:10:50 +02:00
|
|
|
loadData = (): void => {
|
2024-07-20 04:43:16 +02:00
|
|
|
this.restoreMessages();
|
|
|
|
|
this.restoreObjects();
|
2024-07-20 05:10:50 +02:00
|
|
|
};
|
2024-07-20 04:43:16 +02:00
|
|
|
|
2024-07-20 05:10:50 +02:00
|
|
|
unloadData = (): void => {
|
2024-07-20 04:43:16 +02:00
|
|
|
this.messages.clear();
|
|
|
|
|
this.objects.clear();
|
2024-07-20 05:10:50 +02:00
|
|
|
};
|
2024-07-20 04:43:16 +02:00
|
|
|
|
2024-07-19 20:50:42 +02:00
|
|
|
// init
|
2024-07-20 14:16:26 +02:00
|
|
|
constructor(storageModel: StorageModel, chatListModel: ChatListModel, chatId: string) {
|
2024-07-19 20:50:42 +02:00
|
|
|
this.id = chatId;
|
|
|
|
|
this.storageModel = storageModel;
|
2024-07-20 14:16:26 +02:00
|
|
|
this.chatListModel = chatListModel;
|
2024-07-19 20:50:42 +02:00
|
|
|
|
|
|
|
|
this.restoreInfo();
|
|
|
|
|
}
|
2024-07-19 20:07:14 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-19 19:49:55 +02:00
|
|
|
// creation methods
|
2024-07-19 20:50:42 +02:00
|
|
|
export function generateChatInfo(primaryChannel: string): ChatInfo {
|
|
|
|
|
return {
|
|
|
|
|
primaryChannel,
|
|
|
|
|
secondaryChannels: [],
|
|
|
|
|
encryptionKey: "",
|
|
|
|
|
hasUnreadMessages: false,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function createChatMessage(
|
|
|
|
|
channel: string,
|
|
|
|
|
sender: string,
|
|
|
|
|
body: string
|
|
|
|
|
): ChatMessage {
|
|
|
|
|
return {
|
|
|
|
|
dataVersion: "v2",
|
|
|
|
|
|
|
|
|
|
id: v4(),
|
|
|
|
|
|
|
|
|
|
channel,
|
|
|
|
|
sender,
|
|
|
|
|
body,
|
|
|
|
|
dateSent: createTimestamp(),
|
|
|
|
|
};
|
2024-07-19 19:49:55 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-19 19:47:07 +02:00
|
|
|
// types
|
2024-07-19 20:07:14 +02:00
|
|
|
export interface ChatInfo {
|
2024-07-19 20:50:42 +02:00
|
|
|
primaryChannel: string;
|
|
|
|
|
secondaryChannels: string[];
|
|
|
|
|
encryptionKey: string;
|
|
|
|
|
|
|
|
|
|
hasUnreadMessages: boolean;
|
2024-07-19 20:07:14 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-19 19:47:07 +02:00
|
|
|
export interface ChatMessage {
|
2024-07-19 20:50:42 +02:00
|
|
|
dataVersion: "v2";
|
|
|
|
|
|
|
|
|
|
id: string;
|
|
|
|
|
|
|
|
|
|
channel: string;
|
|
|
|
|
sender: string;
|
|
|
|
|
body: string;
|
|
|
|
|
dateSent: string;
|
|
|
|
|
|
|
|
|
|
stringifiedObject?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface ChatObject {
|
|
|
|
|
dataVersion: "v2";
|
|
|
|
|
|
|
|
|
|
id: string;
|
|
|
|
|
title: string;
|
|
|
|
|
|
|
|
|
|
contentVersions: { [key: string]: ChatObjectContent };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface ChatObjectContent {
|
|
|
|
|
dataVersion: "v2";
|
|
|
|
|
|
|
|
|
|
id: string;
|
|
|
|
|
creationDate: string;
|
|
|
|
|
|
|
|
|
|
noteText?: string;
|
|
|
|
|
priority?: number;
|
|
|
|
|
category?: string;
|
|
|
|
|
status?: string;
|
|
|
|
|
date?: string;
|
|
|
|
|
time?: string;
|
|
|
|
|
}
|