2024-07-21 20:50:05 +02:00
|
|
|
// this file is responsible for managing chats.
|
2024-07-19 19:47:07 +02:00
|
|
|
|
2024-07-22 00:52:54 +02:00
|
|
|
import {
|
|
|
|
|
DATA_VERSION,
|
|
|
|
|
ValidObject,
|
|
|
|
|
checkIsValidObject,
|
|
|
|
|
} from "./Utility/typeSafety";
|
2024-07-21 22:42:32 +02:00
|
|
|
import FileModel, { File } from "./fileModel";
|
2024-07-19 20:07:14 +02:00
|
|
|
import StorageModel, { storageKeys } from "./storageModel";
|
2024-07-21 22:42:32 +02:00
|
|
|
import {
|
|
|
|
|
createTimestamp,
|
|
|
|
|
parse,
|
|
|
|
|
parseValidObject,
|
|
|
|
|
stringify,
|
|
|
|
|
} from "./Utility/utility";
|
2024-07-21 03:17:40 +02:00
|
|
|
import { decryptString, encryptString } from "./Utility/crypto";
|
2024-07-19 20:07:14 +02:00
|
|
|
|
2024-07-20 14:16:26 +02:00
|
|
|
import ChatListModel from "./chatListModel";
|
2024-07-20 18:10:42 +02:00
|
|
|
import { Color } from "../ViewModel/colors";
|
2024-07-20 20:42:43 +02:00
|
|
|
import ConnectionModel from "./connectionModel";
|
|
|
|
|
import SettingsModel from "./settingsModel";
|
2024-07-19 20:50:42 +02:00
|
|
|
import { v4 } from "uuid";
|
2024-07-19 19:49:55 +02:00
|
|
|
|
2024-07-21 20:55:31 +02:00
|
|
|
export default class ChatModel {
|
2024-07-20 20:42:43 +02:00
|
|
|
connectionModel: ConnectionModel;
|
2024-07-19 20:50:42 +02:00
|
|
|
storageModel: StorageModel;
|
2024-07-20 20:42:43 +02:00
|
|
|
settingsModel: SettingsModel;
|
2024-07-20 14:16:26 +02:00
|
|
|
chatListModel: ChatListModel;
|
2024-07-20 20:42:43 +02:00
|
|
|
|
2024-07-21 21:45:18 +02:00
|
|
|
fileModel: FileModel;
|
|
|
|
|
|
2024-07-20 20:42:43 +02:00
|
|
|
// data
|
|
|
|
|
id: string;
|
2024-07-19 20:50:42 +02:00
|
|
|
info: ChatInfo;
|
2024-07-20 18:10:42 +02:00
|
|
|
color: Color;
|
2024-07-19 20:07:14 +02:00
|
|
|
|
2024-07-21 01:12:35 +02:00
|
|
|
chatMessageHandler: (chatMessage: ChatMessage) => void = () => {};
|
|
|
|
|
|
2024-07-20 20:42:43 +02:00
|
|
|
// sorting
|
|
|
|
|
get index(): number {
|
|
|
|
|
return this.chatListModel.getIndexOfPrimaryChannel(
|
2024-07-22 00:52:54 +02:00
|
|
|
this.info.primaryChannel ?? ""
|
2024-07-20 20:42:43 +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 18:10:42 +02:00
|
|
|
get colorPath(): string[] {
|
|
|
|
|
return storageKeys.chatColor(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 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-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-21 01:12:35 +02:00
|
|
|
this.subscribe();
|
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;
|
2024-07-20 19:18:19 +02:00
|
|
|
this.storeInfo();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
setEncryptionKey = (key: string): void => {
|
|
|
|
|
this.info.encryptionKey = key;
|
2024-07-19 21:10:31 +02:00
|
|
|
this.storeInfo();
|
2024-07-20 05:10:50 +02:00
|
|
|
};
|
2024-07-19 21:10:31 +02:00
|
|
|
|
2024-07-20 18:10:42 +02:00
|
|
|
setColor = (color: Color): void => {
|
|
|
|
|
this.color = color;
|
|
|
|
|
this.storeColor();
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-19 20:50:42 +02:00
|
|
|
// store & add
|
2024-07-20 05:10:50 +02:00
|
|
|
storeInfo = (): void => {
|
2024-07-21 21:41:19 +02:00
|
|
|
this.storageModel.writeStringifiable(this.infoPath, this.info);
|
2024-07-20 05:10:50 +02:00
|
|
|
};
|
2024-07-19 20:50:42 +02:00
|
|
|
|
2024-07-20 18:10:42 +02:00
|
|
|
storeColor = (): void => {
|
2024-07-21 21:41:19 +02:00
|
|
|
this.storageModel.write(this.colorPath, this.color);
|
2024-07-20 18:10:42 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-21 14:15:26 +02:00
|
|
|
addMessage = async (chatMessage: ChatMessage): Promise<void> => {
|
|
|
|
|
await this.decryptMessage(chatMessage);
|
2024-07-22 00:52:54 +02:00
|
|
|
if (chatMessage.id == undefined) return;
|
2024-07-21 22:42:32 +02:00
|
|
|
|
|
|
|
|
// message
|
|
|
|
|
if (chatMessage.body != "") {
|
|
|
|
|
const messagePath: string[] = this.getMessagePath(chatMessage.id);
|
|
|
|
|
this.storageModel.writeStringifiable(messagePath, chatMessage);
|
|
|
|
|
this.chatMessageHandler(chatMessage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// file
|
2024-07-22 00:52:54 +02:00
|
|
|
this.fileModel.handleStringifiedFile(chatMessage.stringifiedFile ?? "");
|
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
|
|
|
|
|
this.chatListModel.untrackChat(this);
|
|
|
|
|
|
|
|
|
|
// delete
|
2024-07-20 18:10:42 +02:00
|
|
|
const dirPath: string[] = [...storageKeys.chats, this.id];
|
2024-07-20 04:43:16 +02:00
|
|
|
this.storageModel.removeRecursive(dirPath);
|
2024-07-20 05:10:50 +02:00
|
|
|
};
|
2024-07-20 04:43:16 +02:00
|
|
|
|
2024-07-21 21:41:19 +02:00
|
|
|
// load
|
|
|
|
|
loadInfo = (): void => {
|
|
|
|
|
const info: any = this.storageModel.readStringifiable(this.infoPath);
|
2024-07-20 05:10:50 +02:00
|
|
|
if (info != null) {
|
2024-07-20 18:10:42 +02:00
|
|
|
this.info = info;
|
|
|
|
|
} else {
|
2024-07-20 20:53:48 +02:00
|
|
|
this.info = ChatModel.generateChatInfo("0");
|
2024-07-20 18:10:42 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-21 21:41:19 +02:00
|
|
|
loadColor = (): void => {
|
2024-07-20 18:10:42 +02:00
|
|
|
const path: string[] = this.colorPath;
|
2024-07-21 21:41:19 +02:00
|
|
|
const color: string | null = this.storageModel.read(path);
|
2024-07-20 18:10:42 +02:00
|
|
|
if (!color) {
|
|
|
|
|
this.color = Color.Standard;
|
2024-07-20 05:10:50 +02:00
|
|
|
} else {
|
2024-07-20 18:10:42 +02:00
|
|
|
this.color = color as any;
|
2024-07-20 05:10:50 +02:00
|
|
|
}
|
|
|
|
|
};
|
2024-07-19 20:50:42 +02:00
|
|
|
|
2024-07-20 23:40:58 +02:00
|
|
|
get messages(): ChatMessage[] {
|
2024-07-19 20:50:42 +02:00
|
|
|
const messageIds: string[] = this.storageModel.list(this.messageDirPath);
|
2024-07-20 23:40:58 +02:00
|
|
|
if (!Array.isArray(messageIds)) return [];
|
2024-07-19 20:50:42 +02:00
|
|
|
|
2024-07-21 01:56:10 +02:00
|
|
|
const messages: ChatMessage[] = [];
|
2024-07-19 20:50:42 +02:00
|
|
|
for (const messageId of messageIds) {
|
2024-07-20 02:56:44 +02:00
|
|
|
const messagePath: string[] = this.getMessagePath(messageId);
|
2024-07-21 21:41:19 +02:00
|
|
|
const message: any = this.storageModel.readStringifiable(messagePath);
|
2024-07-20 23:40:58 +02:00
|
|
|
if (checkIsValidObject(message) == false) continue;
|
2024-07-19 20:50:42 +02:00
|
|
|
|
2024-07-20 23:40:58 +02:00
|
|
|
messages.push(message);
|
2024-07-19 20:50:42 +02:00
|
|
|
}
|
2024-07-21 01:56:10 +02:00
|
|
|
|
|
|
|
|
const sorted = messages.sort((a, b) =>
|
2024-07-22 00:52:54 +02:00
|
|
|
(a.dateSent ?? "").localeCompare(b.dateSent ?? "")
|
2024-07-21 01:56:10 +02:00
|
|
|
);
|
|
|
|
|
return sorted;
|
2024-07-20 23:40:58 +02:00
|
|
|
}
|
2024-07-19 20:50:42 +02:00
|
|
|
|
2024-07-20 20:42:43 +02:00
|
|
|
// messaging
|
2024-07-21 22:42:32 +02:00
|
|
|
sendMessage = async (body: string, file?: File): Promise<boolean> => {
|
2024-07-20 21:04:41 +02:00
|
|
|
const senderName = this.settingsModel.username;
|
|
|
|
|
if (senderName == "") return false;
|
|
|
|
|
|
2024-07-20 20:53:48 +02:00
|
|
|
const allChannels = [this.info.primaryChannel];
|
2024-07-22 00:52:54 +02:00
|
|
|
for (const secondaryChannel of this.info.secondaryChannels ?? []) {
|
2024-07-20 20:53:48 +02:00
|
|
|
allChannels.push(secondaryChannel);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-21 03:17:40 +02:00
|
|
|
const combinedChannel: string = allChannels.join("/");
|
2024-07-20 20:53:48 +02:00
|
|
|
|
2024-07-21 22:42:32 +02:00
|
|
|
const chatMessage: ChatMessage = await ChatModel.createChatMessage(
|
2024-07-20 20:53:48 +02:00
|
|
|
combinedChannel,
|
2024-07-20 21:04:41 +02:00
|
|
|
senderName,
|
2024-07-22 00:52:54 +02:00
|
|
|
this.info.encryptionKey ?? "",
|
2024-07-21 22:42:32 +02:00
|
|
|
body,
|
|
|
|
|
file
|
2024-07-20 20:53:48 +02:00
|
|
|
);
|
2024-07-20 21:04:41 +02:00
|
|
|
|
2024-07-21 14:15:26 +02:00
|
|
|
this.addMessage(chatMessage);
|
2024-07-20 20:53:48 +02:00
|
|
|
this.connectionModel.sendMessageOrStore(chatMessage);
|
2024-07-20 21:04:41 +02:00
|
|
|
return true;
|
2024-07-20 20:42:43 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-21 14:15:26 +02:00
|
|
|
handleMessage = (body: string): void => {
|
2024-07-21 22:42:32 +02:00
|
|
|
const chatMessage: ChatMessage | null = parseValidObject<ChatMessage>(body);
|
2024-07-21 01:12:35 +02:00
|
|
|
if (chatMessage == null) return;
|
|
|
|
|
|
2024-07-21 14:15:26 +02:00
|
|
|
chatMessage.status = ChatMessageStatus.Received;
|
|
|
|
|
this.addMessage(chatMessage);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handleMessageSent = (chatMessage: ChatMessage): void => {
|
|
|
|
|
chatMessage.status = ChatMessageStatus.Sent;
|
|
|
|
|
this.addMessage(chatMessage);
|
2024-07-21 01:12:35 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-21 03:17:40 +02:00
|
|
|
decryptMessage = async (chatMessage: ChatMessage): Promise<void> => {
|
|
|
|
|
const decryptedBody: string = await decryptString(
|
2024-07-22 00:52:54 +02:00
|
|
|
chatMessage.body ?? "",
|
|
|
|
|
this.info.encryptionKey ?? ""
|
2024-07-21 03:17:40 +02:00
|
|
|
);
|
2024-07-21 22:42:32 +02:00
|
|
|
const decryptedFile: string = await decryptString(
|
|
|
|
|
chatMessage.stringifiedFile ?? "",
|
2024-07-22 00:52:54 +02:00
|
|
|
this.info.encryptionKey ?? ""
|
2024-07-21 22:42:32 +02:00
|
|
|
);
|
2024-07-21 03:17:40 +02:00
|
|
|
chatMessage.body = decryptedBody;
|
2024-07-21 22:42:32 +02:00
|
|
|
chatMessage.stringifiedFile = decryptedFile;
|
2024-07-21 03:17:40 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-21 01:12:35 +02:00
|
|
|
setMessageHandler = (handler: (chatMessage: ChatMessage) => void): void => {
|
|
|
|
|
this.chatMessageHandler = handler;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
subscribe = (): void => {
|
2024-07-22 00:52:54 +02:00
|
|
|
if (this.info.primaryChannel == undefined || this.info.primaryChannel == "")
|
|
|
|
|
return;
|
2024-07-21 01:12:35 +02:00
|
|
|
this.connectionModel.addChannel(this.info.primaryChannel);
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-19 20:50:42 +02:00
|
|
|
// init
|
2024-07-20 18:10:42 +02:00
|
|
|
constructor(
|
|
|
|
|
storageModel: StorageModel,
|
2024-07-20 20:42:43 +02:00
|
|
|
connectionModel: ConnectionModel,
|
|
|
|
|
settingsModel: SettingsModel,
|
2024-07-20 18:10:42 +02:00
|
|
|
chatListModel: ChatListModel,
|
|
|
|
|
chatId: string
|
|
|
|
|
) {
|
2024-07-19 20:50:42 +02:00
|
|
|
this.id = chatId;
|
2024-07-20 20:42:43 +02:00
|
|
|
this.connectionModel = connectionModel;
|
|
|
|
|
this.settingsModel = settingsModel;
|
2024-07-19 20:50:42 +02:00
|
|
|
this.storageModel = storageModel;
|
2024-07-20 14:16:26 +02:00
|
|
|
this.chatListModel = chatListModel;
|
2024-07-19 20:50:42 +02:00
|
|
|
|
2024-07-21 21:41:19 +02:00
|
|
|
this.loadInfo();
|
|
|
|
|
this.loadColor();
|
2024-07-21 01:12:35 +02:00
|
|
|
this.subscribe();
|
2024-07-21 14:15:26 +02:00
|
|
|
|
|
|
|
|
connectionModel.setMessageSentHandler(this.handleMessageSent);
|
2024-07-21 21:45:18 +02:00
|
|
|
|
2024-07-21 23:32:20 +02:00
|
|
|
this.fileModel = new FileModel(this, this.storageModel);
|
2024-07-19 20:50:42 +02:00
|
|
|
}
|
2024-07-19 20:07:14 +02:00
|
|
|
|
2024-07-20 20:53:48 +02:00
|
|
|
// utility
|
|
|
|
|
static generateChatInfo = (primaryChannel: string): ChatInfo => {
|
|
|
|
|
return {
|
2024-07-22 00:37:20 +02:00
|
|
|
dataVersion: DATA_VERSION,
|
2024-07-21 23:32:20 +02:00
|
|
|
|
2024-07-20 20:53:48 +02:00
|
|
|
primaryChannel,
|
|
|
|
|
secondaryChannels: [],
|
|
|
|
|
encryptionKey: "",
|
|
|
|
|
hasUnreadMessages: false,
|
|
|
|
|
};
|
2024-07-19 20:50:42 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-21 22:42:32 +02:00
|
|
|
static createChatMessage = async (
|
2024-07-20 20:53:48 +02:00
|
|
|
channel: string,
|
|
|
|
|
sender: string,
|
2024-07-21 22:42:32 +02:00
|
|
|
encryptionKey: string,
|
|
|
|
|
body: string,
|
|
|
|
|
file?: File
|
|
|
|
|
): Promise<ChatMessage> => {
|
|
|
|
|
const chatMessage: ChatMessage = {
|
2024-07-22 00:37:20 +02:00
|
|
|
dataVersion: DATA_VERSION,
|
2024-07-20 20:53:48 +02:00
|
|
|
|
|
|
|
|
id: v4(),
|
|
|
|
|
|
|
|
|
|
channel,
|
|
|
|
|
sender,
|
|
|
|
|
body,
|
|
|
|
|
dateSent: createTimestamp(),
|
2024-07-21 14:15:26 +02:00
|
|
|
|
|
|
|
|
status: ChatMessageStatus.Outbox,
|
2024-07-21 22:42:32 +02:00
|
|
|
stringifiedFile: "",
|
2024-07-20 20:53:48 +02:00
|
|
|
};
|
2024-07-21 22:42:32 +02:00
|
|
|
if (file != undefined) {
|
|
|
|
|
const stringifiedFile = stringify(file);
|
|
|
|
|
chatMessage.stringifiedFile = stringifiedFile;
|
|
|
|
|
}
|
2024-07-21 01:12:35 +02:00
|
|
|
|
2024-07-21 22:42:32 +02:00
|
|
|
if (encryptionKey != "") {
|
2024-07-22 00:52:54 +02:00
|
|
|
chatMessage.body = await encryptString(
|
|
|
|
|
chatMessage.body ?? "",
|
|
|
|
|
encryptionKey
|
|
|
|
|
);
|
2024-07-21 22:42:32 +02:00
|
|
|
chatMessage.stringifiedFile = await encryptString(
|
2024-07-22 00:52:54 +02:00
|
|
|
chatMessage.stringifiedFile ?? "",
|
2024-07-21 22:42:32 +02:00
|
|
|
encryptionKey
|
|
|
|
|
);
|
2024-07-21 01:12:35 +02:00
|
|
|
}
|
2024-07-21 22:42:32 +02:00
|
|
|
|
|
|
|
|
return chatMessage;
|
2024-07-21 01:12:35 +02:00
|
|
|
};
|
2024-07-19 19:49:55 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-19 19:47:07 +02:00
|
|
|
// types
|
2024-07-21 23:32:20 +02:00
|
|
|
export interface ChatInfo extends ValidObject {
|
2024-07-22 00:52:54 +02:00
|
|
|
primaryChannel?: string;
|
|
|
|
|
secondaryChannels?: string[];
|
|
|
|
|
encryptionKey?: string;
|
2024-07-19 20:50:42 +02:00
|
|
|
|
2024-07-22 00:52:54 +02:00
|
|
|
hasUnreadMessages?: boolean;
|
2024-07-19 20:07:14 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-21 14:15:26 +02:00
|
|
|
export enum ChatMessageStatus {
|
|
|
|
|
Outbox = "outbox",
|
|
|
|
|
Sent = "sent",
|
|
|
|
|
Received = "received",
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-21 23:32:20 +02:00
|
|
|
export interface ChatMessage extends ValidObject {
|
2024-07-22 00:52:54 +02:00
|
|
|
id?: string;
|
2024-07-19 20:50:42 +02:00
|
|
|
|
2024-07-22 00:52:54 +02:00
|
|
|
channel?: string;
|
|
|
|
|
sender?: string;
|
|
|
|
|
body?: string;
|
|
|
|
|
dateSent?: string;
|
2024-07-19 20:50:42 +02:00
|
|
|
|
2024-07-21 14:15:26 +02:00
|
|
|
status?: ChatMessageStatus;
|
|
|
|
|
|
2024-07-22 00:52:54 +02:00
|
|
|
stringifiedFile?: string;
|
2024-07-21 21:45:18 +02:00
|
|
|
}
|