2024-07-20 04:43:16 +02:00
|
|
|
import StorageModel, { storageKeys } from "./storageModel";
|
|
|
|
|
|
2024-07-21 21:41:19 +02:00
|
|
|
import ChatModel from "./chatModel";
|
2024-07-20 20:42:43 +02:00
|
|
|
import ConnectionModel from "./connectionModel";
|
2024-07-21 01:12:35 +02:00
|
|
|
import { Message } from "udn-frontend";
|
2024-07-20 20:42:43 +02:00
|
|
|
import SettingsModel from "./settingsModel";
|
2024-07-20 18:58:25 +02:00
|
|
|
import { localeCompare } from "./Utility/utility";
|
2024-07-20 04:43:16 +02:00
|
|
|
import { v4 } from "uuid";
|
|
|
|
|
|
|
|
|
|
export default class ChatListModel {
|
|
|
|
|
storageModel: StorageModel;
|
2024-07-20 20:42:43 +02:00
|
|
|
settingsModel: SettingsModel;
|
|
|
|
|
connectionModel: ConnectionModel;
|
2024-07-20 04:43:16 +02:00
|
|
|
|
|
|
|
|
// data
|
2024-07-20 16:38:06 +02:00
|
|
|
chatModels = new Set<ChatModel>();
|
2024-07-20 16:49:01 +02:00
|
|
|
sortedPrimaryChannels: string[] = [];
|
2024-07-20 04:43:16 +02:00
|
|
|
|
2024-07-21 01:12:35 +02:00
|
|
|
// handlers
|
|
|
|
|
messageHandler = (data: Message): void => {
|
|
|
|
|
for (const chatModel of this.chatModels) {
|
|
|
|
|
const channel: string | undefined = data.messageChannel;
|
|
|
|
|
const body: string | undefined = data.messageBody;
|
|
|
|
|
if (channel == undefined) return;
|
|
|
|
|
if (body == undefined) return;
|
|
|
|
|
|
|
|
|
|
const allChannels = channel.split("/");
|
|
|
|
|
for (const channel of allChannels) {
|
|
|
|
|
if (channel != chatModel.info.primaryChannel) continue;
|
2024-07-21 03:17:40 +02:00
|
|
|
chatModel.handleMessage(body);
|
2024-07-21 01:12:35 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-20 04:43:16 +02:00
|
|
|
// store & add
|
2024-07-20 05:10:50 +02:00
|
|
|
addChatModel = (chatModel: ChatModel) => {
|
2024-07-20 16:38:06 +02:00
|
|
|
this.chatModels.add(chatModel);
|
2024-07-20 16:49:01 +02:00
|
|
|
this.updateIndices();
|
2024-07-20 05:10:50 +02:00
|
|
|
};
|
2024-07-20 04:43:16 +02:00
|
|
|
|
2024-07-20 05:10:50 +02:00
|
|
|
createChat = (primaryChannel: string): ChatModel => {
|
2024-07-20 04:43:16 +02:00
|
|
|
const id: string = v4();
|
|
|
|
|
|
2024-07-20 20:42:43 +02:00
|
|
|
const chatModel = new ChatModel(
|
|
|
|
|
this.storageModel,
|
|
|
|
|
this.connectionModel,
|
|
|
|
|
this.settingsModel,
|
|
|
|
|
this,
|
|
|
|
|
id
|
|
|
|
|
);
|
2024-07-20 04:43:16 +02:00
|
|
|
chatModel.setPrimaryChannel(primaryChannel);
|
|
|
|
|
|
|
|
|
|
this.addChatModel(chatModel);
|
2024-07-20 05:10:50 +02:00
|
|
|
return chatModel;
|
2024-07-20 04:43:16 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-20 05:10:50 +02:00
|
|
|
untrackChat = (chat: ChatModel): void => {
|
2024-07-20 16:38:06 +02:00
|
|
|
this.chatModels.delete(chat);
|
2024-07-20 16:49:01 +02:00
|
|
|
this.updateIndices();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// sorting
|
|
|
|
|
updateIndices = () => {
|
|
|
|
|
this.sortedPrimaryChannels = [];
|
|
|
|
|
|
|
|
|
|
let allChannels: string[] = [];
|
|
|
|
|
for (const chatModel of this.chatModels) {
|
|
|
|
|
allChannels.push(chatModel.info.primaryChannel);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-20 18:58:25 +02:00
|
|
|
this.sortedPrimaryChannels = allChannels.sort(localeCompare);
|
2024-07-20 04:43:16 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-20 20:42:43 +02:00
|
|
|
getIndexOfPrimaryChannel(primaryChannel: string): number {
|
|
|
|
|
return this.sortedPrimaryChannels.indexOf(primaryChannel);
|
|
|
|
|
}
|
2024-07-21 01:12:35 +02:00
|
|
|
|
2024-07-21 21:41:19 +02:00
|
|
|
// load
|
2024-07-20 04:43:16 +02:00
|
|
|
loadChats = (): void => {
|
|
|
|
|
const chatDir = storageKeys.chats;
|
2024-07-20 05:10:50 +02:00
|
|
|
const chatIds = this.storageModel.list(chatDir);
|
|
|
|
|
for (const chatId of chatIds) {
|
2024-07-20 20:42:43 +02:00
|
|
|
const chatModel = new ChatModel(
|
|
|
|
|
this.storageModel,
|
|
|
|
|
this.connectionModel,
|
|
|
|
|
this.settingsModel,
|
|
|
|
|
this,
|
|
|
|
|
chatId
|
|
|
|
|
);
|
2024-07-20 04:43:16 +02:00
|
|
|
this.addChatModel(chatModel);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// init
|
2024-07-21 01:12:35 +02:00
|
|
|
constructor(
|
|
|
|
|
storageModel: StorageModel,
|
|
|
|
|
settingsModel: SettingsModel,
|
|
|
|
|
connectionModel: ConnectionModel
|
|
|
|
|
) {
|
2024-07-20 04:43:16 +02:00
|
|
|
this.storageModel = storageModel;
|
2024-07-20 20:42:43 +02:00
|
|
|
this.settingsModel = settingsModel;
|
|
|
|
|
this.connectionModel = connectionModel;
|
2024-07-20 05:10:50 +02:00
|
|
|
this.loadChats();
|
2024-07-21 01:12:35 +02:00
|
|
|
|
|
|
|
|
connectionModel.setMessageHandler(this.messageHandler);
|
2024-07-20 04:43:16 +02:00
|
|
|
}
|
|
|
|
|
}
|