Files
CT/src/Model/Chat/chatModel.ts
T

547 lines
14 KiB
TypeScript
Raw Normal View History

2026-04-18 11:55:01 +00:00
// cleanup: Phase A
2024-07-19 19:47:07 +02:00
2026-04-18 11:55:01 +00:00
import { v4 } from "uuid";
import ChatListModel from "./chatListModel";
import {
2026-04-01 08:29:39 +00:00
HandlerManager,
createTimestamp,
localeCompare,
parseValidObject,
stringify,
2024-07-22 02:05:27 +02:00
} from "../Utility/utility";
2026-04-18 11:55:01 +00:00
import {
DATA_VERSION,
ValidObject,
checkMatchesObjectStructure,
} from "../Utility/typeSafety";
import { decryptString, encryptString } from "../Utility/crypto";
2024-07-26 03:14:55 +02:00
import StorageModel, {
2026-04-08 14:02:12 +00:00
StorageModelSubPaths,
2026-04-01 08:29:39 +00:00
filePaths,
2024-07-26 03:14:55 +02:00
} from "../Global/storageModel";
2024-07-22 02:05:27 +02:00
import SettingsModel from "../Global/settingsModel";
2026-04-18 11:55:01 +00:00
import ConnectionModel from "../Global/connectionModel";
import FileModel, { FileContent } from "../Files/fileModel";
import { Colors } from "../../colors";
2024-07-21 20:55:31 +02:00
export default class ChatModel {
2026-04-18 11:55:01 +00:00
// models
2026-04-13 10:58:21 +00:00
readonly connectionModel: ConnectionModel;
readonly storageModel: StorageModel;
readonly settingsModel: SettingsModel;
readonly chatListModel: ChatListModel;
readonly fileModel: FileModel;
2024-07-22 23:33:57 +02:00
2026-04-01 08:29:39 +00:00
// data
2026-04-13 10:58:21 +00:00
readonly id: string;
2026-09-26 14:27:38 +02:00
info: ChatInfoFileContent; /* load function called in constructor */
2024-07-22 02:23:21 +02:00
2026-04-18 11:55:01 +00:00
get secondaryChannels(): string[] {
return this.info.secondaryChannels.sort(localeCompare);
}
2026-09-26 14:34:25 +02:00
get color(): Colors {
const color = this.info.color as Colors;
if ([...Object.values(Colors)].includes(color)) return color;
return Colors.Standard;
}
2026-04-18 11:55:01 +00:00
// handler managers
2026-04-01 08:29:39 +00:00
chatMessageHandlerManager: HandlerManager<ChatMessage> =
new HandlerManager();
2026-04-13 10:58:21 +00:00
readonly reactionHandlerManager: HandlerManager<ChatMessageReaction> =
2026-04-06 20:47:49 +00:00
new HandlerManager();
2024-07-22 02:23:21 +02:00
2026-04-01 08:29:39 +00:00
// paths
2026-04-18 11:55:01 +00:00
readonly getBasePath = (): string[] => {
2026-04-01 08:29:39 +00:00
return StorageModel.getPath(
2026-04-08 14:02:12 +00:00
StorageModelSubPaths.Chat,
2026-04-01 08:29:39 +00:00
filePaths.chat.chatBase(this.id),
);
};
2026-04-18 11:55:01 +00:00
readonly getInfoPath = (): string[] => {
2026-04-01 08:29:39 +00:00
return StorageModel.getPath(
2026-04-08 14:02:12 +00:00
StorageModelSubPaths.Chat,
2026-04-01 08:29:39 +00:00
filePaths.chat.info(this.id),
);
};
2026-04-18 11:55:01 +00:00
readonly getColorPath = (): string[] => {
2026-04-01 08:29:39 +00:00
return StorageModel.getPath(
2026-04-08 14:02:12 +00:00
StorageModelSubPaths.Chat,
2026-04-01 08:29:39 +00:00
filePaths.chat.color(this.id),
);
};
2026-04-18 11:55:01 +00:00
readonly getMessageDirPath = (): string[] => {
2026-04-01 08:29:39 +00:00
return StorageModel.getPath(
2026-04-08 14:02:12 +00:00
StorageModelSubPaths.Chat,
2026-04-01 08:29:39 +00:00
filePaths.chat.messages(this.id),
);
};
2026-04-18 11:55:01 +00:00
readonly getMessagePath = (id: string): string[] => {
2026-04-01 08:29:39 +00:00
return [...this.getMessageDirPath(), id];
};
2026-04-18 11:55:01 +00:00
readonly getReactionDirPath = (): string[] => {
2026-04-06 20:47:49 +00:00
return StorageModel.getPath(
2026-04-08 14:02:12 +00:00
StorageModelSubPaths.Chat,
2026-04-06 20:47:49 +00:00
filePaths.chat.reactions(this.id),
);
};
2026-04-18 11:55:01 +00:00
readonly getReactionPath = (id: string): string[] => {
2026-04-06 20:47:49 +00:00
return [...this.getReactionDirPath(), id];
};
2026-09-24 02:11:03 +02:00
readonly getPreviousFilterPath = (): string[] => {
return [StorageModelSubPaths.Chat, ...filePaths.chat.previousFilter(this.id)];
}
2026-04-01 08:29:39 +00:00
// handlers
2026-04-18 11:55:01 +00:00
readonly handleMessage = (body: string): void => {
2026-04-01 08:29:39 +00:00
const chatMessage: ChatMessage | null = parseValidObject(
body,
ChatMessageReference,
);
if (chatMessage == null) return;
2026-04-08 14:02:12 +00:00
chatMessage.status = ChatMessageStatuses.Received;
2026-04-01 19:00:16 +00:00
2026-04-06 20:47:49 +00:00
this.addMessage(chatMessage);
2026-04-15 10:43:52 +00:00
if (chatMessage.stringifiedFile) return;
2026-04-01 14:16:38 +00:00
this.setReadStatus(true);
2024-07-20 20:53:48 +02:00
};
2026-04-01 08:29:39 +00:00
2026-04-18 11:55:01 +00:00
readonly handleReaction = (reaction: ChatMessageReaction | any): void => {
2026-04-06 20:47:49 +00:00
if (
!checkMatchesObjectStructure(reaction, ChatMessageReactionReference)
)
return;
2026-04-07 18:48:01 +00:00
const reactionPath: string[] = this.getReactionPath(reaction.fileId);
if (reaction.isDeleting == true) {
this.storageModel.remove(reactionPath);
} else {
this.storageModel.writeStringifiable(reactionPath, reaction);
}
2026-04-07 18:48:01 +00:00
this.reactionHandlerManager.trigger(reaction);
2026-04-06 20:47:49 +00:00
};
2026-04-18 11:55:01 +00:00
readonly handleMessageSent = (chatMessage: ChatMessage): void => {
2026-04-08 14:02:12 +00:00
chatMessage.status = ChatMessageStatuses.Sent;
2026-04-01 08:29:39 +00:00
this.addMessage(chatMessage);
2024-07-20 20:53:48 +02:00
};
2026-04-01 08:29:39 +00:00
// settings
readonly setName = (name: string): void => {
this.info.name = name;
2026-05-06 16:34:29 +00:00
this.storeInfo();
this.subscribe();
};
2026-04-01 08:29:39 +00:00
2026-04-18 11:55:01 +00:00
readonly setSecondaryChannels = (secondaryChannels: string[]): void => {
2026-04-01 08:29:39 +00:00
this.info.secondaryChannels = secondaryChannels;
this.storeInfo();
};
2026-04-18 11:55:01 +00:00
readonly setEncryptionKey = (key: string): void => {
2026-04-01 08:29:39 +00:00
this.info.encryptionKey = key;
this.storeInfo();
};
2026-04-18 11:55:01 +00:00
readonly setColor = (color: Colors): void => {
2026-09-26 14:34:25 +02:00
this.info.color = color;
this.storeInfo();
2026-04-01 08:29:39 +00:00
};
// messaging
2026-04-18 11:55:01 +00:00
readonly addMessage = async (chatMessage: ChatMessage): Promise<void> => {
2026-04-01 08:29:39 +00:00
await this.decryptMessage(chatMessage);
// message
if (chatMessage.body != "") {
const messagePath: string[] = this.getMessagePath(chatMessage.id);
this.storageModel.writeStringifiable(messagePath, chatMessage);
this.chatMessageHandlerManager.trigger(chatMessage);
}
// file
this.fileModel.handleStringifiedFileContent(
chatMessage.stringifiedFile,
);
};
2026-04-18 11:55:01 +00:00
readonly getNameAndChannel = (): [string, string] | false => {
2026-04-13 11:23:17 +00:00
const senderName: string = this.settingsModel.username;
2026-04-01 08:29:39 +00:00
if (senderName == "") return false;
const allChannels: string[] = [this.id];
2026-04-01 08:29:39 +00:00
for (const secondaryChannel of this.info.secondaryChannels) {
allChannels.push(secondaryChannel);
}
const combinedChannel: string = allChannels.join("/");
2026-04-06 20:47:49 +00:00
return [senderName, combinedChannel];
};
2026-04-18 11:55:01 +00:00
readonly sendMessage = async (
2026-04-06 20:47:49 +00:00
body: string,
inlineReplyId?: string,
2026-04-06 20:47:49 +00:00
fileContent?: FileContent<string>,
2026-09-26 13:31:36 +02:00
): Promise<string|false> => {
2026-04-06 20:47:49 +00:00
const nameAndChannel = this.getNameAndChannel();
2026-09-09 00:04:27 +02:00
if (nameAndChannel == false) return false;
2026-04-06 20:47:49 +00:00
const [senderName, combinedChannel] = nameAndChannel;
2026-04-01 08:29:39 +00:00
const chatMessage: ChatMessage = await ChatModel.createChatMessage(
combinedChannel,
senderName,
this.info.encryptionKey,
body,
inlineReplyId,
2026-04-01 08:29:39 +00:00
fileContent,
);
this.addMessage(chatMessage);
this.connectionModel.sendMessageOrStore(chatMessage);
2026-09-26 13:31:36 +02:00
return chatMessage.id;
2026-04-01 08:29:39 +00:00
};
2026-04-18 11:55:01 +00:00
readonly decryptMessage = async (
chatMessage: ChatMessage,
): Promise<void> => {
2026-04-01 08:29:39 +00:00
const decryptedBody: string = await decryptString(
chatMessage.body,
this.info.encryptionKey,
);
const decryptedFile: string = await decryptString(
chatMessage.stringifiedFile ?? "",
this.info.encryptionKey,
);
chatMessage.body = decryptedBody;
chatMessage.stringifiedFile = decryptedFile;
};
2026-04-18 11:55:01 +00:00
readonly sendReaction = async (
2026-04-06 20:47:49 +00:00
messageId: string,
2026-04-07 18:39:55 +00:00
content: ReactionSymbols,
2026-09-09 00:04:27 +02:00
isDeleting: boolean = false,
2026-04-06 20:47:49 +00:00
): Promise<void> => {
const nameAndChannel = this.getNameAndChannel();
if (nameAndChannel == false) return;
2026-09-09 00:04:27 +02:00
const [senderName] = nameAndChannel;
2026-04-06 20:47:49 +00:00
const reaction = ChatModel.createMessageReaction(
messageId,
senderName,
content,
2026-04-07 18:48:01 +00:00
isDeleting,
2026-04-06 20:47:49 +00:00
);
2026-04-14 11:28:40 +00:00
this.sendMessage("", undefined, reaction);
2026-04-07 18:48:01 +00:00
this.handleReaction(reaction);
2026-04-06 20:47:49 +00:00
};
2026-04-18 11:55:01 +00:00
readonly subscribe = (): void => {
this.connectionModel.addChannel(this.id);
2026-04-01 08:29:39 +00:00
};
2026-04-18 11:55:01 +00:00
readonly setReadStatus = (hasUnreadMessages: boolean): void => {
2026-04-01 14:16:38 +00:00
this.info.hasUnreadMessages = hasUnreadMessages;
2026-04-01 08:29:39 +00:00
this.storeInfo();
};
// storage
2026-04-18 11:55:01 +00:00
readonly storeInfo = (): void => {
2026-04-01 08:29:39 +00:00
this.storageModel.writeStringifiable(this.getInfoPath(), this.info);
};
2026-09-24 02:11:03 +02:00
readonly storeFilter = (filter: string): void => {
this.storageModel.write(this.getPreviousFilterPath(), filter);
}
readonly getFilter = (): string=> {
return this.storageModel.read(this.getPreviousFilterPath()) || "";
2026-09-24 02:11:03 +02:00
}
2026-04-18 11:55:01 +00:00
readonly delete = () => {
// untrack
this.chatListModel.untrackChat(this);
2026-04-01 08:29:39 +00:00
// delete
const dirPath: string[] = this.getBasePath();
this.storageModel.removeRecursively(dirPath);
2026-04-01 08:29:39 +00:00
};
// load
2026-04-18 11:55:01 +00:00
readonly loadInfo = (): void => {
2026-09-26 14:27:38 +02:00
const info: ChatInfoFileContent | null = this.storageModel.readStringifiable(
this.getInfoPath(),
ChatInfoReference,
);
if (info != null) {
this.info = info;
} else {
2026-09-26 14:34:25 +02:00
this.info = ChatModel.generateChatInfo("0", "0", Colors.Standard );
}
2026-04-01 08:29:39 +00:00
};
get messages(): ChatMessage[] {
const messageIds: string[] = this.storageModel.list(
this.getMessageDirPath(),
);
if (!Array.isArray(messageIds)) return [];
const chatMessages: ChatMessage[] = [];
for (const messageId of messageIds) {
const messagePath: string[] = this.getMessagePath(messageId);
const chatMessage: ChatMessage | null =
this.storageModel.readStringifiable(
messagePath,
ChatMessageReference,
);
if (chatMessage == null) continue;
chatMessages.push(chatMessage);
}
const sorted = chatMessages.sort((a, b) =>
a.dateSent.localeCompare(b.dateSent),
);
return sorted;
}
2024-07-21 01:12:35 +02:00
2026-04-06 20:47:49 +00:00
get reactions(): ChatMessageReaction[] {
const reactionIds: string[] = this.storageModel.list(
this.getReactionDirPath(),
);
if (!Array.isArray(reactionIds)) return [];
const reactions: ChatMessageReaction[] = [];
for (const reactionId of reactionIds) {
const reactionPath: string[] = this.getReactionPath(reactionId);
const reaction: ChatMessageReaction | null =
this.storageModel.readStringifiable(
reactionPath,
ChatMessageReactionReference,
);
if (reaction == null) continue;
reactions.push(reaction);
}
return reactions;
2026-04-06 20:47:49 +00:00
}
2026-04-01 08:29:39 +00:00
// init
constructor(
storageModel: StorageModel,
connectionModel: ConnectionModel,
settingsModel: SettingsModel,
chatListModel: ChatListModel,
chatId: string,
2026-04-01 08:29:39 +00:00
) {
this.id = chatId;
this.connectionModel = connectionModel;
this.settingsModel = settingsModel;
this.storageModel = storageModel;
this.chatListModel = chatListModel;
this.loadInfo();
this.subscribe();
this.fileModel = new FileModel(
this.storageModel,
this.settingsModel,
this,
);
2024-07-21 01:12:35 +02:00
}
2026-04-01 08:29:39 +00:00
// utility
2026-04-01 19:00:16 +00:00
static splitChannel(channelString: string): string[] {
return channelString.split("/");
2026-04-01 19:00:16 +00:00
}
2026-09-26 14:34:25 +02:00
static generateChatInfo = (name: string, id: string, color: Colors): ChatInfoFileContent => {
2026-09-26 14:27:38 +02:00
const file = FileModel.createFileContent(id, "chat-info");
return {
2026-09-26 14:27:38 +02:00
...file,
2026-04-01 08:29:39 +00:00
name,
secondaryChannels: [],
encryptionKey: "",
2026-09-26 14:34:25 +02:00
color,
hasUnreadMessages: false,
};
2026-04-01 08:29:39 +00:00
};
static createChatMessage = async (
channel: string,
sender: string,
encryptionKey: string,
body: string,
inlineReplyId?: string,
fileContent?: FileContent<string>,
2026-04-01 08:29:39 +00:00
): Promise<ChatMessage> => {
const chatMessage: ChatMessage = {
dataVersion: DATA_VERSION,
id: v4(),
channel,
sender,
body,
dateSent: createTimestamp(),
inlineReplyId,
status: ChatMessageStatuses.Outbox,
stringifiedFile: "",
};
if (fileContent != undefined) {
const stringifiedFile: string = stringify(fileContent);
chatMessage.stringifiedFile = stringifiedFile;
}
if (encryptionKey != "") {
chatMessage.body = await encryptString(
chatMessage.body,
encryptionKey,
);
chatMessage.stringifiedFile = await encryptString(
chatMessage.stringifiedFile,
encryptionKey,
);
}
return chatMessage;
2026-04-01 08:29:39 +00:00
};
2026-04-06 20:47:49 +00:00
static createMessageReaction = (
messageId: string,
sender: string,
content: ReactionSymbols,
isDeleting: boolean,
2026-04-06 20:47:49 +00:00
): ChatMessageReaction => {
const fileContent: FileContent<"reaction"> =
FileModel.createFileContent(v4(), "reaction");
const reaction: ChatMessageReaction = {
...fileContent,
fileId: ChatModel.createMessageReactionId(messageId, sender),
messageId,
sender,
content,
isDeleting,
};
return reaction;
2026-04-06 20:47:49 +00:00
};
static createMessageReactionId = (
messageId: string,
sender: string,
): string => {
return messageId + sender;
};
}
2024-07-19 19:47:07 +02:00
// types
2026-04-07 18:39:55 +00:00
export enum ReactionSymbols {
ThumbsUp = "👍",
Check = "✅",
Stop = "🛑",
Attention = "❗️",
DoubleAttention = "‼️",
Question = "❓",
2026-04-07 18:39:55 +00:00
}
2026-04-06 20:47:49 +00:00
2026-09-26 14:27:38 +02:00
export interface ChatInfoFileContent extends FileContent<"chat-info"> {
name: string;
2026-04-01 08:29:39 +00:00
secondaryChannels: string[];
encryptionKey: string;
2024-07-19 20:50:42 +02:00
2026-09-26 14:34:25 +02:00
color: string;
2026-04-01 08:29:39 +00:00
hasUnreadMessages: boolean;
2024-07-19 20:07:14 +02:00
}
2026-04-08 14:02:12 +00:00
export enum ChatMessageStatuses {
2026-04-01 08:29:39 +00:00
Outbox = "outbox",
Sent = "sent",
Received = "received",
Other = "other",
}
2024-07-21 23:32:20 +02:00
export interface ChatMessage extends ValidObject {
2026-04-13 10:58:21 +00:00
readonly id: string;
2024-07-19 20:50:42 +02:00
2026-04-13 10:58:21 +00:00
readonly channel: string;
readonly sender: string;
2026-04-01 08:29:39 +00:00
body: string;
2026-04-13 10:58:21 +00:00
readonly dateSent: string;
2026-04-14 11:28:40 +00:00
readonly inlineReplyId?: string;
2024-07-19 20:50:42 +02:00
2026-04-08 14:02:12 +00:00
status: ChatMessageStatuses;
2026-04-01 08:29:39 +00:00
stringifiedFile: string;
2024-07-21 21:45:18 +02:00
}
2024-07-22 01:16:13 +02:00
2026-04-06 20:47:49 +00:00
export interface ChatMessageReaction
extends ValidObject, FileContent<"reaction"> {
2026-04-13 10:58:21 +00:00
readonly messageId: string;
readonly sender: string;
readonly content: ReactionSymbols | string;
2026-04-07 18:48:01 +00:00
isDeleting: boolean;
2026-04-06 20:47:49 +00:00
}
2024-07-22 01:22:53 +02:00
// references
2026-09-26 14:27:38 +02:00
export const ChatInfoReference: ChatInfoFileContent = {
2026-04-01 08:29:39 +00:00
dataVersion: DATA_VERSION,
2026-09-26 14:27:38 +02:00
fileId: "",
fileContentId: "",
creationDate: "",
type: "chat-info",
2024-07-22 01:22:53 +02:00
name: "",
2026-04-01 08:29:39 +00:00
secondaryChannels: [""],
encryptionKey: "",
2024-07-22 01:22:53 +02:00
2026-09-26 14:34:25 +02:00
color: "",
2026-04-01 08:29:39 +00:00
hasUnreadMessages: true,
2024-07-22 01:22:53 +02:00
};
2024-07-22 01:16:13 +02:00
export const ChatMessageReference: ChatMessage = {
2026-04-01 08:29:39 +00:00
dataVersion: DATA_VERSION,
2024-07-22 01:16:13 +02:00
2026-04-01 08:29:39 +00:00
id: "",
2024-07-22 01:16:13 +02:00
2026-04-01 08:29:39 +00:00
channel: "",
sender: "",
body: "",
dateSent: "",
2024-07-22 01:16:13 +02:00
2026-04-08 14:02:12 +00:00
status: "" as ChatMessageStatuses,
2024-07-22 01:16:13 +02:00
2026-04-01 08:29:39 +00:00
stringifiedFile: "",
2024-07-22 01:16:13 +02:00
};
2026-04-06 20:47:49 +00:00
export const ChatMessageReactionReference: ChatMessageReaction = {
dataVersion: DATA_VERSION,
fileId: "",
fileContentId: "",
creationDate: "",
type: "reaction",
messageId: "",
sender: "",
content: "",
2026-04-07 18:48:01 +00:00
isDeleting: false,
2026-09-24 02:11:03 +02:00
}