2024-07-20 04:43:16 +02:00
|
|
|
import StorageModel, { storageKeys } from "./storageModel";
|
|
|
|
|
|
|
|
|
|
import { ChatModel } from "./chatModel";
|
|
|
|
|
import { v4 } from "uuid";
|
|
|
|
|
|
|
|
|
|
export default class ChatListModel {
|
|
|
|
|
storageModel: StorageModel;
|
|
|
|
|
|
|
|
|
|
// data
|
|
|
|
|
chatModels = new Map<string, ChatModel>();
|
|
|
|
|
|
|
|
|
|
// store & add
|
2024-07-20 05:10:50 +02:00
|
|
|
addChatModel = (chatModel: ChatModel) => {
|
2024-07-20 04:43:16 +02:00
|
|
|
this.chatModels.set(chatModel.id, chatModel);
|
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();
|
|
|
|
|
|
|
|
|
|
const chatModel = new ChatModel(this.storageModel, id);
|
|
|
|
|
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
|
|
|
deleteChat = (chat: ChatModel): void => {
|
2024-07-20 04:43:16 +02:00
|
|
|
chat.remove();
|
2024-07-20 05:10:50 +02:00
|
|
|
this.untrackChat(chat);
|
2024-07-20 04:43:16 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-20 05:10:50 +02:00
|
|
|
untrackChat = (chat: ChatModel): void => {
|
|
|
|
|
this.chatModels.delete(chat.id);
|
2024-07-20 04:43:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// restore
|
|
|
|
|
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 04:43:16 +02:00
|
|
|
const chatModel = new ChatModel(this.storageModel, chatId);
|
|
|
|
|
this.addChatModel(chatModel);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// init
|
|
|
|
|
constructor(storageModel: StorageModel) {
|
|
|
|
|
this.storageModel = storageModel;
|
2024-07-20 05:10:50 +02:00
|
|
|
this.loadChats();
|
2024-07-20 04:43:16 +02:00
|
|
|
}
|
|
|
|
|
}
|