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

123 lines
3.5 KiB
TypeScript
Raw Normal View History

2026-04-18 11:55:01 +00:00
// cleanup: Phase A
import { v4 } from "uuid";
import { Message } from "udn-frontend";
import ChatModel, { ChatMessage } from "./chatModel";
2026-04-01 08:29:39 +00:00
import StorageModel, {
2026-04-08 14:02:12 +00:00
StorageModelSubPaths,
2026-04-01 08:29:39 +00:00
filePaths,
} 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";
2024-07-20 04:43:16 +02:00
export default class ChatListModel {
2026-04-18 11:55:01 +00:00
// models
2026-04-13 10:58:21 +00:00
readonly storageModel: StorageModel;
readonly settingsModel: SettingsModel;
readonly connectionModel: ConnectionModel;
2026-04-01 08:29:39 +00:00
// data
2026-04-18 11:55:01 +00:00
readonly chatModels = new Set<ChatModel>();
// chat handling
readonly addChatModel = (chatModel: ChatModel) => {
this.chatModels.add(chatModel);
};
readonly createChat = (primaryChannel: string): ChatModel => {
const id: string = v4();
const chatModel = new ChatModel(
this.storageModel,
this.connectionModel,
this.settingsModel,
this,
id,
);
chatModel.setPrimaryChannel(primaryChannel);
this.addChatModel(chatModel);
return chatModel;
};
readonly untrackChat = (chat: ChatModel): void => {
this.chatModels.delete(chat);
};
2026-04-01 08:29:39 +00:00
2026-04-18 11:55:01 +00:00
// message handlers
readonly messageHandler = (data: Message): void => {
2026-04-01 08:29:39 +00:00
const channel: string | undefined = data.messageChannel;
const body: string | undefined = data.messageBody;
if (channel == undefined) return;
if (body == undefined) return;
this.routeMessageToCorrectChatModel(channel, (chatModel: ChatModel) =>
chatModel.handleMessage(body),
);
};
2026-04-18 11:55:01 +00:00
readonly messageSentHandler = (chatMessage: ChatMessage): void => {
2026-04-01 08:29:39 +00:00
const channel: string = chatMessage.channel;
this.routeMessageToCorrectChatModel(channel, (chatModel: ChatModel) =>
chatModel.handleMessageSent(chatMessage),
);
};
2026-04-18 11:55:01 +00:00
// util
readonly routeMessageToCorrectChatModel = (
2026-04-01 08:29:39 +00:00
channel: string,
fn: (chatModel: ChatModel) => void,
): void => {
2026-04-13 11:23:17 +00:00
const allChannels: string[] = channel.split("/");
2026-04-01 08:29:39 +00:00
for (const chatModel of this.chatModels) {
for (const channel of allChannels) {
2026-05-06 16:34:29 +00:00
if (channel != chatModel.unwrappedPrimaryChannel) continue;
2026-04-01 08:29:39 +00:00
fn(chatModel);
break;
}
}
};
// load
2026-04-18 11:55:01 +00:00
readonly loadChats = (): void => {
2026-04-01 08:29:39 +00:00
const chatDir = StorageModel.getPath(
2026-04-08 14:02:12 +00:00
StorageModelSubPaths.Chat,
2026-04-01 08:29:39 +00:00
filePaths.chat.base,
);
const chatIds = this.storageModel.list(chatDir);
for (const chatId of chatIds) {
const chatModel = new ChatModel(
this.storageModel,
this.connectionModel,
this.settingsModel,
this,
chatId,
);
this.addChatModel(chatModel);
}
};
// init
constructor(
storageModel: StorageModel,
settingsModel: SettingsModel,
connectionModel: ConnectionModel,
) {
this.storageModel = storageModel;
this.settingsModel = settingsModel;
this.connectionModel = connectionModel;
this.loadChats();
connectionModel.messageHandlerManager.setHandler(
"chat-list",
this.messageHandler,
);
connectionModel.messageSentHandlerManager.setHandler(
"chat-list",
2026-04-01 08:29:39 +00:00
this.messageSentHandler,
);
2024-07-21 01:12:35 +02:00
}
2024-07-20 04:43:16 +02:00
}