fix bug where sent messages would be added to every chat
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import ChatModel, { ChatMessage } from "./chatModel";
|
||||
import StorageModel, { filePaths } from "../Global/storageModel";
|
||||
|
||||
import ChatModel from "./chatModel";
|
||||
import ConnectionModel from "../Global/connectionModel";
|
||||
import { Message } from "udn-frontend";
|
||||
import SettingsModel from "../Global/settingsModel";
|
||||
@@ -16,16 +16,35 @@ export default class ChatListModel {
|
||||
|
||||
// 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 channel: string | undefined = data.messageChannel;
|
||||
const body: string | undefined = data.messageBody;
|
||||
if (channel == undefined) return;
|
||||
if (body == undefined) return;
|
||||
|
||||
const allChannels = channel.split("/");
|
||||
this.routeMessageToCorrectChatModel(channel, (chatModel: ChatModel) =>
|
||||
chatModel.handleMessage(body)
|
||||
);
|
||||
};
|
||||
|
||||
messageSentHandler = (chatMessage: ChatMessage): void => {
|
||||
const channel: string = chatMessage.channel;
|
||||
|
||||
this.routeMessageToCorrectChatModel(channel, (chatModel: ChatModel) =>
|
||||
chatModel.handleMessageSent(chatMessage)
|
||||
);
|
||||
};
|
||||
|
||||
// methods
|
||||
routeMessageToCorrectChatModel = (
|
||||
channel: string,
|
||||
fn: (chatModel: ChatModel) => void
|
||||
): void => {
|
||||
const allChannels = channel.split("/");
|
||||
|
||||
for (const chatModel of this.chatModels) {
|
||||
for (const channel of allChannels) {
|
||||
if (channel != chatModel.info.primaryChannel) continue;
|
||||
chatModel.handleMessage(body);
|
||||
fn(chatModel);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -83,6 +102,9 @@ export default class ChatListModel {
|
||||
this.connectionModel = connectionModel;
|
||||
this.loadChats();
|
||||
|
||||
connectionModel.setMessageHandler(this.messageHandler);
|
||||
connectionModel.messageHandlerManager.addHandler(this.messageHandler);
|
||||
connectionModel.messageSentHandlerManager.addHandler(
|
||||
this.messageSentHandler
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -236,8 +236,6 @@ export default class ChatModel {
|
||||
this.loadColor();
|
||||
this.subscribe();
|
||||
|
||||
connectionModel.setMessageSentHandler(this.handleMessageSent);
|
||||
|
||||
this.fileModel = new FileModel(this, this.storageModel);
|
||||
}
|
||||
|
||||
|
||||
@@ -83,19 +83,17 @@ export default class BoardModel {
|
||||
createBoard = (name: string): BoardInfoFileContent => {
|
||||
const boardInfoFileContent: BoardInfoFileContent =
|
||||
BoardModel.createBoardInfoFileContent(v4(), name, Color.Standard);
|
||||
this.updateBoard(boardInfoFileContent);
|
||||
return boardInfoFileContent;
|
||||
};
|
||||
|
||||
updateBoard = (boardInfoFileContent: BoardInfoFileContent): void => {
|
||||
this.storeBoard(boardInfoFileContent);
|
||||
this.chatModel.sendMessage("", boardInfoFileContent);
|
||||
this.boardHandlerManager.trigger(boardInfoFileContent);
|
||||
};
|
||||
|
||||
storeBoard = (boardInfoFileContent: BoardInfoFileContent): void => {
|
||||
// store info
|
||||
this.fileModel.addFileContent(boardInfoFileContent);
|
||||
this.fileModel.storeFileContent(boardInfoFileContent);
|
||||
|
||||
// add to list
|
||||
const boardDirectoryPath: string[] = this.getBoardDirectoryPath(
|
||||
@@ -104,6 +102,10 @@ export default class BoardModel {
|
||||
this.storageModel.write(boardDirectoryPath, "");
|
||||
};
|
||||
|
||||
updateBoardAndSend = (boardInfoFileContent: BoardInfoFileContent): void => {
|
||||
this.chatModel.sendMessage("", boardInfoFileContent);
|
||||
};
|
||||
|
||||
deleteBoard = (boardId: string): void => {
|
||||
const boardFilePath: string[] = this.getBoardFilePath(boardId);
|
||||
const boardDirectoryPath: string[] = this.getBoardDirectoryPath(boardId);
|
||||
@@ -139,25 +141,22 @@ export default class BoardModel {
|
||||
|
||||
updateTask = (taskFileContent: TaskFileContent): void => {
|
||||
this.storeTask(taskFileContent);
|
||||
this.chatModel.sendMessage("", taskFileContent);
|
||||
};
|
||||
|
||||
storeTask = (taskFileContent: TaskFileContent): void => {
|
||||
// store info
|
||||
this.fileModel.addFileContent(taskFileContent);
|
||||
this.fileModel.storeFileContent(taskFileContent);
|
||||
|
||||
// add to board
|
||||
const taskReferencePath: string[] = this.getTaskReferencePath(
|
||||
taskFileContent.boardId,
|
||||
taskFileContent.fileId
|
||||
);
|
||||
console.log("storing", taskReferencePath);
|
||||
this.storageModel.write(taskReferencePath, "");
|
||||
};
|
||||
|
||||
listTaskIds = (boardId: string): string[] => {
|
||||
const taskContainerPath: string[] = this.getTaskContainerPath(boardId);
|
||||
console.log("listing", taskContainerPath);
|
||||
const fileIds: string[] = this.storageModel.list(taskContainerPath);
|
||||
return fileIds;
|
||||
};
|
||||
|
||||
@@ -68,7 +68,7 @@ export default class FileModel {
|
||||
};
|
||||
|
||||
// methods
|
||||
addFileContent = (fileContent: FileContent<string>): void => {
|
||||
addFileContentAndSend = (fileContent: FileContent<string>): void => {
|
||||
this.handleFileContent(fileContent);
|
||||
this.chatModel.sendMessage("", fileContent);
|
||||
};
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
// this file is responsible for managing UDN connections.
|
||||
|
||||
import { ChatMessage, ChatMessageReference } from "../Chat/chatModel";
|
||||
import { Handler, HandlerManager, stringify } from "../Utility/utility";
|
||||
import StorageModel, { filePaths } from "./storageModel";
|
||||
import UDNFrontend, { Message } from "udn-frontend";
|
||||
|
||||
import { stringify } from "../Utility/utility";
|
||||
|
||||
export default class ConnectionModel {
|
||||
udn: UDNFrontend;
|
||||
storageModel: StorageModel;
|
||||
@@ -19,20 +18,20 @@ export default class ConnectionModel {
|
||||
return this.udn.ws?.url;
|
||||
}
|
||||
|
||||
connectionChangeHandler: () => void = () => {};
|
||||
messageHandler: (data: Message) => void = () => {};
|
||||
messageSentHandler: (chatMessage: ChatMessage) => void = () => {};
|
||||
connectionChangeHandlerManager: HandlerManager<void> = new HandlerManager();
|
||||
messageHandlerManager: HandlerManager<Message> = new HandlerManager();
|
||||
messageSentHandlerManager: HandlerManager<ChatMessage> = new HandlerManager();
|
||||
|
||||
channelsToSubscribe: Set<string> = new Set();
|
||||
|
||||
// handlers
|
||||
handleMessage = (data: Message): void => {
|
||||
this.messageHandler(data);
|
||||
this.messageHandlerManager.trigger(data);
|
||||
};
|
||||
|
||||
handleConnectionChange = (): void => {
|
||||
console.log("connection status:", this.isConnected, this.address);
|
||||
this.connectionChangeHandler();
|
||||
this.connectionChangeHandlerManager.trigger();
|
||||
|
||||
if (this.isConnected == false) return;
|
||||
if (!this.address) return;
|
||||
@@ -70,7 +69,7 @@ export default class ConnectionModel {
|
||||
};
|
||||
|
||||
requestNewMailbox = (): void => {
|
||||
console.trace("requesting mailbox");
|
||||
console.log("requesting mailbox");
|
||||
this.udn.requestMailbox();
|
||||
};
|
||||
|
||||
@@ -167,7 +166,7 @@ export default class ConnectionModel {
|
||||
chatMessage.channel,
|
||||
stringifiedBody
|
||||
);
|
||||
if (isSent) this.messageSentHandler(chatMessage);
|
||||
if (isSent) this.messageSentHandlerManager.trigger(chatMessage);
|
||||
return isSent;
|
||||
};
|
||||
|
||||
@@ -210,22 +209,7 @@ export default class ConnectionModel {
|
||||
const dirPath = this.getPreviousAddressPath();
|
||||
return this.storageModel.list(dirPath);
|
||||
}
|
||||
|
||||
// other
|
||||
setConnectionChangeHandler = (handler: () => void): void => {
|
||||
this.connectionChangeHandler = handler;
|
||||
};
|
||||
|
||||
setMessageHandler = (handler: (data: Message) => void): void => {
|
||||
this.messageHandler = handler;
|
||||
};
|
||||
|
||||
setMessageSentHandler = (
|
||||
handler: (chatMessage: ChatMessage) => void
|
||||
): void => {
|
||||
this.messageSentHandler = handler;
|
||||
};
|
||||
|
||||
|
||||
// init
|
||||
constructor(storageModel: StorageModel) {
|
||||
// create frontend
|
||||
|
||||
Reference in New Issue
Block a user