fix bug where sent messages would be added to every chat

This commit is contained in:
marlon-erler
2024-07-24 03:02:49 +02:00
parent 7a5513eabb
commit c9a2995842
10 changed files with 97 additions and 112 deletions
+40 -42
View File
@@ -617,21 +617,22 @@
// boards // boards
createBoard = (name) => { createBoard = (name) => {
const boardInfoFileContent = _BoardModel.createBoardInfoFileContent(v4_default(), name, "standard" /* Standard */); const boardInfoFileContent = _BoardModel.createBoardInfoFileContent(v4_default(), name, "standard" /* Standard */);
this.updateBoard(boardInfoFileContent);
return boardInfoFileContent; return boardInfoFileContent;
}; };
updateBoard = (boardInfoFileContent) => { updateBoard = (boardInfoFileContent) => {
this.storeBoard(boardInfoFileContent); this.storeBoard(boardInfoFileContent);
this.chatModel.sendMessage("", boardInfoFileContent);
this.boardHandlerManager.trigger(boardInfoFileContent); this.boardHandlerManager.trigger(boardInfoFileContent);
}; };
storeBoard = (boardInfoFileContent) => { storeBoard = (boardInfoFileContent) => {
this.fileModel.addFileContent(boardInfoFileContent); this.fileModel.storeFileContent(boardInfoFileContent);
const boardDirectoryPath = this.getBoardDirectoryPath( const boardDirectoryPath = this.getBoardDirectoryPath(
boardInfoFileContent.fileId boardInfoFileContent.fileId
); );
this.storageModel.write(boardDirectoryPath, ""); this.storageModel.write(boardDirectoryPath, "");
}; };
updateBoardAndSend = (boardInfoFileContent) => {
this.chatModel.sendMessage("", boardInfoFileContent);
};
deleteBoard = (boardId) => { deleteBoard = (boardId) => {
const boardFilePath = this.getBoardFilePath(boardId); const boardFilePath = this.getBoardFilePath(boardId);
const boardDirectoryPath = this.getBoardDirectoryPath(boardId); const boardDirectoryPath = this.getBoardDirectoryPath(boardId);
@@ -661,20 +662,17 @@
}; };
updateTask = (taskFileContent) => { updateTask = (taskFileContent) => {
this.storeTask(taskFileContent); this.storeTask(taskFileContent);
this.chatModel.sendMessage("", taskFileContent);
}; };
storeTask = (taskFileContent) => { storeTask = (taskFileContent) => {
this.fileModel.addFileContent(taskFileContent); this.fileModel.storeFileContent(taskFileContent);
const taskReferencePath = this.getTaskReferencePath( const taskReferencePath = this.getTaskReferencePath(
taskFileContent.boardId, taskFileContent.boardId,
taskFileContent.fileId taskFileContent.fileId
); );
console.log("storing", taskReferencePath);
this.storageModel.write(taskReferencePath, ""); this.storageModel.write(taskReferencePath, "");
}; };
listTaskIds = (boardId) => { listTaskIds = (boardId) => {
const taskContainerPath = this.getTaskContainerPath(boardId); const taskContainerPath = this.getTaskContainerPath(boardId);
console.log("listing", taskContainerPath);
const fileIds = this.storageModel.list(taskContainerPath); const fileIds = this.storageModel.list(taskContainerPath);
return fileIds; return fileIds;
}; };
@@ -783,7 +781,7 @@
this.fileContentHandlerManager.trigger(fileContent); this.fileContentHandlerManager.trigger(fileContent);
}; };
// methods // methods
addFileContent = (fileContent) => { addFileContentAndSend = (fileContent) => {
this.handleFileContent(fileContent); this.handleFileContent(fileContent);
this.chatModel.sendMessage("", fileContent); this.chatModel.sendMessage("", fileContent);
}; };
@@ -1097,7 +1095,6 @@
this.loadInfo(); this.loadInfo();
this.loadColor(); this.loadColor();
this.subscribe(); this.subscribe();
connectionModel2.setMessageSentHandler(this.handleMessageSent);
this.fileModel = new FileModel(this, this.storageModel); this.fileModel = new FileModel(this, this.storageModel);
} }
// utility // utility
@@ -1162,15 +1159,29 @@
chatModels = /* @__PURE__ */ new Set(); chatModels = /* @__PURE__ */ new Set();
// handlers // handlers
messageHandler = (data) => { messageHandler = (data) => {
for (const chatModel of this.chatModels) {
const channel = data.messageChannel; const channel = data.messageChannel;
const body = data.messageBody; const body = data.messageBody;
if (channel == void 0) return; if (channel == void 0) return;
if (body == void 0) return; if (body == void 0) return;
this.routeMessageToCorrectChatModel(
channel,
(chatModel) => chatModel.handleMessage(body)
);
};
messageSentHandler = (chatMessage) => {
const channel = chatMessage.channel;
this.routeMessageToCorrectChatModel(
channel,
(chatModel) => chatModel.handleMessageSent(chatMessage)
);
};
// methods
routeMessageToCorrectChatModel = (channel, fn) => {
const allChannels = channel.split("/"); const allChannels = channel.split("/");
for (const chatModel of this.chatModels) {
for (const channel2 of allChannels) { for (const channel2 of allChannels) {
if (channel2 != chatModel.info.primaryChannel) continue; if (channel2 != chatModel.info.primaryChannel) continue;
chatModel.handleMessage(body); fn(chatModel);
break; break;
} }
} }
@@ -1216,7 +1227,10 @@
this.settingsModel = settingsModel2; this.settingsModel = settingsModel2;
this.connectionModel = connectionModel2; this.connectionModel = connectionModel2;
this.loadChats(); this.loadChats();
connectionModel2.setMessageHandler(this.messageHandler); connectionModel2.messageHandlerManager.addHandler(this.messageHandler);
connectionModel2.messageSentHandlerManager.addHandler(
this.messageSentHandler
);
} }
}; };
@@ -1633,22 +1647,17 @@
[this.newBoardNameInput], [this.newBoardNameInput],
() => this.newBoardNameInput.value == "" () => this.newBoardNameInput.value == ""
); );
// handlers
handleFileContent = (fileContent) => {
if (checkMatchesObjectStructure(fileContent, BoardInfoFileContentReference) == false)
return;
this.showBoardInList(fileContent);
};
// methods // methods
createBoard = () => { createBoard = () => {
if (this.cannotCreateBoard.value == true) return; if (this.cannotCreateBoard.value == true) return;
const boardInfoFileContent = this.boardModel.createBoard(this.newBoardNameInput.value); const boardInfoFileContent = this.boardModel.createBoard(this.newBoardNameInput.value);
this.newBoardNameInput.value = ""; this.newBoardNameInput.value = "";
this.showBoardInList(boardInfoFileContent); this.showBoardInList(boardInfoFileContent);
this.boardModel.updateBoardAndSend(boardInfoFileContent);
this.updateIndices(); this.updateIndices();
}; };
updateBoard = (boardInfoFileContent) => { updateBoard = (boardInfoFileContent) => {
this.boardModel.updateBoard(boardInfoFileContent); this.boardModel.updateBoardAndSend(boardInfoFileContent);
this.updateIndices(); this.updateIndices();
}; };
deleteBoard = (boardInfoFileContent) => { deleteBoard = (boardInfoFileContent) => {
@@ -2033,7 +2042,7 @@
} }
// src/View/Components/chatMessage.tsx // src/View/Components/chatMessage.tsx
function ChatMessage(chatMessageViewModel) { function ChatMessage2(chatMessageViewModel) {
const statusIcon = createProxyState( const statusIcon = createProxyState(
[chatMessageViewModel.status], [chatMessageViewModel.status],
() => { () => {
@@ -2073,7 +2082,7 @@
); );
} }
var ChatMessageViewModelToView = (chatMessageViewModel) => { var ChatMessageViewModelToView = (chatMessageViewModel) => {
return ChatMessage(chatMessageViewModel); return ChatMessage2(chatMessageViewModel);
}; };
// src/View/ChatPages/messagePage.tsx // src/View/ChatPages/messagePage.tsx
@@ -2689,20 +2698,17 @@
get address() { get address() {
return this.udn.ws?.url; return this.udn.ws?.url;
} }
connectionChangeHandler = () => { connectionChangeHandlerManager = new HandlerManager();
}; messageHandlerManager = new HandlerManager();
messageHandler = () => { messageSentHandlerManager = new HandlerManager();
};
messageSentHandler = () => {
};
channelsToSubscribe = /* @__PURE__ */ new Set(); channelsToSubscribe = /* @__PURE__ */ new Set();
// handlers // handlers
handleMessage = (data) => { handleMessage = (data) => {
this.messageHandler(data); this.messageHandlerManager.trigger(data);
}; };
handleConnectionChange = () => { handleConnectionChange = () => {
console.log("connection status:", this.isConnected, this.address); console.log("connection status:", this.isConnected, this.address);
this.connectionChangeHandler(); this.connectionChangeHandlerManager.trigger();
if (this.isConnected == false) return; if (this.isConnected == false) return;
if (!this.address) return; if (!this.address) return;
this.connectMailbox(); this.connectMailbox();
@@ -2732,7 +2738,7 @@
return mailboxFilePath; return mailboxFilePath;
}; };
requestNewMailbox = () => { requestNewMailbox = () => {
console.trace("requesting mailbox"); console.log("requesting mailbox");
this.udn.requestMailbox(); this.udn.requestMailbox();
}; };
connectMailbox = () => { connectMailbox = () => {
@@ -2807,7 +2813,7 @@
chatMessage.channel, chatMessage.channel,
stringifiedBody stringifiedBody
); );
if (isSent) this.messageSentHandler(chatMessage); if (isSent) this.messageSentHandlerManager.trigger(chatMessage);
return isSent; return isSent;
}; };
// storage // storage
@@ -2841,16 +2847,6 @@
const dirPath = this.getPreviousAddressPath(); const dirPath = this.getPreviousAddressPath();
return this.storageModel.list(dirPath); return this.storageModel.list(dirPath);
} }
// other
setConnectionChangeHandler = (handler) => {
this.connectionChangeHandler = handler;
};
setMessageHandler = (handler) => {
this.messageHandler = handler;
};
setMessageSentHandler = (handler) => {
this.messageSentHandler = handler;
};
// init // init
constructor(storageModel2) { constructor(storageModel2) {
this.udn = new UDNFrontend(); this.udn = new UDNFrontend();
@@ -2944,7 +2940,9 @@
constructor(connectionModel2) { constructor(connectionModel2) {
this.connectionModel = connectionModel2; this.connectionModel = connectionModel2;
this.updatePreviousAddresses(); this.updatePreviousAddresses();
connectionModel2.setConnectionChangeHandler(this.connectionChangeHandler); connectionModel2.connectionChangeHandlerManager.addHandler(
this.connectionChangeHandler
);
} }
}; };
+26 -4
View File
@@ -1,6 +1,6 @@
import ChatModel, { ChatMessage } from "./chatModel";
import StorageModel, { filePaths } from "../Global/storageModel"; import StorageModel, { filePaths } from "../Global/storageModel";
import ChatModel from "./chatModel";
import ConnectionModel from "../Global/connectionModel"; import ConnectionModel from "../Global/connectionModel";
import { Message } from "udn-frontend"; import { Message } from "udn-frontend";
import SettingsModel from "../Global/settingsModel"; import SettingsModel from "../Global/settingsModel";
@@ -16,16 +16,35 @@ export default class ChatListModel {
// handlers // handlers
messageHandler = (data: Message): void => { messageHandler = (data: Message): void => {
for (const chatModel of this.chatModels) {
const channel: string | undefined = data.messageChannel; const channel: string | undefined = data.messageChannel;
const body: string | undefined = data.messageBody; const body: string | undefined = data.messageBody;
if (channel == undefined) return; if (channel == undefined) return;
if (body == undefined) return; if (body == undefined) return;
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("/"); const allChannels = channel.split("/");
for (const chatModel of this.chatModels) {
for (const channel of allChannels) { for (const channel of allChannels) {
if (channel != chatModel.info.primaryChannel) continue; if (channel != chatModel.info.primaryChannel) continue;
chatModel.handleMessage(body); fn(chatModel);
break; break;
} }
} }
@@ -83,6 +102,9 @@ export default class ChatListModel {
this.connectionModel = connectionModel; this.connectionModel = connectionModel;
this.loadChats(); this.loadChats();
connectionModel.setMessageHandler(this.messageHandler); connectionModel.messageHandlerManager.addHandler(this.messageHandler);
connectionModel.messageSentHandlerManager.addHandler(
this.messageSentHandler
);
} }
} }
-2
View File
@@ -236,8 +236,6 @@ export default class ChatModel {
this.loadColor(); this.loadColor();
this.subscribe(); this.subscribe();
connectionModel.setMessageSentHandler(this.handleMessageSent);
this.fileModel = new FileModel(this, this.storageModel); this.fileModel = new FileModel(this, this.storageModel);
} }
+6 -7
View File
@@ -83,19 +83,17 @@ export default class BoardModel {
createBoard = (name: string): BoardInfoFileContent => { createBoard = (name: string): BoardInfoFileContent => {
const boardInfoFileContent: BoardInfoFileContent = const boardInfoFileContent: BoardInfoFileContent =
BoardModel.createBoardInfoFileContent(v4(), name, Color.Standard); BoardModel.createBoardInfoFileContent(v4(), name, Color.Standard);
this.updateBoard(boardInfoFileContent);
return boardInfoFileContent; return boardInfoFileContent;
}; };
updateBoard = (boardInfoFileContent: BoardInfoFileContent): void => { updateBoard = (boardInfoFileContent: BoardInfoFileContent): void => {
this.storeBoard(boardInfoFileContent); this.storeBoard(boardInfoFileContent);
this.chatModel.sendMessage("", boardInfoFileContent);
this.boardHandlerManager.trigger(boardInfoFileContent); this.boardHandlerManager.trigger(boardInfoFileContent);
}; };
storeBoard = (boardInfoFileContent: BoardInfoFileContent): void => { storeBoard = (boardInfoFileContent: BoardInfoFileContent): void => {
// store info // store info
this.fileModel.addFileContent(boardInfoFileContent); this.fileModel.storeFileContent(boardInfoFileContent);
// add to list // add to list
const boardDirectoryPath: string[] = this.getBoardDirectoryPath( const boardDirectoryPath: string[] = this.getBoardDirectoryPath(
@@ -104,6 +102,10 @@ export default class BoardModel {
this.storageModel.write(boardDirectoryPath, ""); this.storageModel.write(boardDirectoryPath, "");
}; };
updateBoardAndSend = (boardInfoFileContent: BoardInfoFileContent): void => {
this.chatModel.sendMessage("", boardInfoFileContent);
};
deleteBoard = (boardId: string): void => { deleteBoard = (boardId: string): void => {
const boardFilePath: string[] = this.getBoardFilePath(boardId); const boardFilePath: string[] = this.getBoardFilePath(boardId);
const boardDirectoryPath: string[] = this.getBoardDirectoryPath(boardId); const boardDirectoryPath: string[] = this.getBoardDirectoryPath(boardId);
@@ -139,25 +141,22 @@ export default class BoardModel {
updateTask = (taskFileContent: TaskFileContent): void => { updateTask = (taskFileContent: TaskFileContent): void => {
this.storeTask(taskFileContent); this.storeTask(taskFileContent);
this.chatModel.sendMessage("", taskFileContent);
}; };
storeTask = (taskFileContent: TaskFileContent): void => { storeTask = (taskFileContent: TaskFileContent): void => {
// store info // store info
this.fileModel.addFileContent(taskFileContent); this.fileModel.storeFileContent(taskFileContent);
// add to board // add to board
const taskReferencePath: string[] = this.getTaskReferencePath( const taskReferencePath: string[] = this.getTaskReferencePath(
taskFileContent.boardId, taskFileContent.boardId,
taskFileContent.fileId taskFileContent.fileId
); );
console.log("storing", taskReferencePath);
this.storageModel.write(taskReferencePath, ""); this.storageModel.write(taskReferencePath, "");
}; };
listTaskIds = (boardId: string): string[] => { listTaskIds = (boardId: string): string[] => {
const taskContainerPath: string[] = this.getTaskContainerPath(boardId); const taskContainerPath: string[] = this.getTaskContainerPath(boardId);
console.log("listing", taskContainerPath);
const fileIds: string[] = this.storageModel.list(taskContainerPath); const fileIds: string[] = this.storageModel.list(taskContainerPath);
return fileIds; return fileIds;
}; };
+1 -1
View File
@@ -68,7 +68,7 @@ export default class FileModel {
}; };
// methods // methods
addFileContent = (fileContent: FileContent<string>): void => { addFileContentAndSend = (fileContent: FileContent<string>): void => {
this.handleFileContent(fileContent); this.handleFileContent(fileContent);
this.chatModel.sendMessage("", fileContent); this.chatModel.sendMessage("", fileContent);
}; };
+8 -24
View File
@@ -1,11 +1,10 @@
// this file is responsible for managing UDN connections. // this file is responsible for managing UDN connections.
import { ChatMessage, ChatMessageReference } from "../Chat/chatModel"; import { ChatMessage, ChatMessageReference } from "../Chat/chatModel";
import { Handler, HandlerManager, stringify } from "../Utility/utility";
import StorageModel, { filePaths } from "./storageModel"; import StorageModel, { filePaths } from "./storageModel";
import UDNFrontend, { Message } from "udn-frontend"; import UDNFrontend, { Message } from "udn-frontend";
import { stringify } from "../Utility/utility";
export default class ConnectionModel { export default class ConnectionModel {
udn: UDNFrontend; udn: UDNFrontend;
storageModel: StorageModel; storageModel: StorageModel;
@@ -19,20 +18,20 @@ export default class ConnectionModel {
return this.udn.ws?.url; return this.udn.ws?.url;
} }
connectionChangeHandler: () => void = () => {}; connectionChangeHandlerManager: HandlerManager<void> = new HandlerManager();
messageHandler: (data: Message) => void = () => {}; messageHandlerManager: HandlerManager<Message> = new HandlerManager();
messageSentHandler: (chatMessage: ChatMessage) => void = () => {}; messageSentHandlerManager: HandlerManager<ChatMessage> = new HandlerManager();
channelsToSubscribe: Set<string> = new Set(); channelsToSubscribe: Set<string> = new Set();
// handlers // handlers
handleMessage = (data: Message): void => { handleMessage = (data: Message): void => {
this.messageHandler(data); this.messageHandlerManager.trigger(data);
}; };
handleConnectionChange = (): void => { handleConnectionChange = (): void => {
console.log("connection status:", this.isConnected, this.address); console.log("connection status:", this.isConnected, this.address);
this.connectionChangeHandler(); this.connectionChangeHandlerManager.trigger();
if (this.isConnected == false) return; if (this.isConnected == false) return;
if (!this.address) return; if (!this.address) return;
@@ -70,7 +69,7 @@ export default class ConnectionModel {
}; };
requestNewMailbox = (): void => { requestNewMailbox = (): void => {
console.trace("requesting mailbox"); console.log("requesting mailbox");
this.udn.requestMailbox(); this.udn.requestMailbox();
}; };
@@ -167,7 +166,7 @@ export default class ConnectionModel {
chatMessage.channel, chatMessage.channel,
stringifiedBody stringifiedBody
); );
if (isSent) this.messageSentHandler(chatMessage); if (isSent) this.messageSentHandlerManager.trigger(chatMessage);
return isSent; return isSent;
}; };
@@ -211,21 +210,6 @@ export default class ConnectionModel {
return this.storageModel.list(dirPath); 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 // init
constructor(storageModel: StorageModel) { constructor(storageModel: StorageModel) {
// create frontend // create frontend
+3 -1
View File
@@ -81,6 +81,8 @@ export default class ConnectionViewModel {
this.connectionModel = connectionModel; this.connectionModel = connectionModel;
this.updatePreviousAddresses(); this.updatePreviousAddresses();
connectionModel.setConnectionChangeHandler(this.connectionChangeHandler); connectionModel.connectionChangeHandlerManager.addHandler(
this.connectionChangeHandler
);
} }
} }
-2
View File
@@ -3,11 +3,9 @@ import * as React from "bloatless-react";
import BoardModel, { import BoardModel, {
BoardInfoFileContent, BoardInfoFileContent,
TaskFileContent, TaskFileContent,
TaskFileContentReference,
} from "../../Model/Files/boardModel"; } from "../../Model/Files/boardModel";
import { Color } from "../../colors"; import { Color } from "../../colors";
import FileModel from "../../Model/Files/fileModel";
import StorageModel from "../../Model/Global/storageModel"; import StorageModel from "../../Model/Global/storageModel";
import TaskPageViewModel from "./taskPageViewModel"; import TaskPageViewModel from "./taskPageViewModel";
import TaskViewModel from "./taskViewModel"; import TaskViewModel from "./taskViewModel";
+3 -18
View File
@@ -1,16 +1,11 @@
import * as React from "bloatless-react"; import * as React from "bloatless-react";
import BoardModel, { import BoardModel, { BoardInfoFileContent } from "../../Model/Files/boardModel";
BoardInfoFileContent,
BoardInfoFileContentReference,
} from "../../Model/Files/boardModel";
import FileModel, { FileContent } from "../../Model/Files/fileModel";
import BoardViewModel from "./boardViewModel"; import BoardViewModel from "./boardViewModel";
import ChatViewModel from "../Chat/chatViewModel"; import ChatViewModel from "../Chat/chatViewModel";
import { IndexManager } from "../../Model/Utility/utility"; import { IndexManager } from "../../Model/Utility/utility";
import StorageModel from "../../Model/Global/storageModel"; import StorageModel from "../../Model/Global/storageModel";
import { checkMatchesObjectStructure } from "../../Model/Utility/typeSafety";
export default class TaskPageViewModel { export default class TaskPageViewModel {
storageModel: StorageModel; storageModel: StorageModel;
@@ -51,17 +46,6 @@ export default class TaskPageViewModel {
() => this.newBoardNameInput.value == "" () => this.newBoardNameInput.value == ""
); );
// handlers
handleFileContent = (fileContent: FileContent<string>): void => {
if (
checkMatchesObjectStructure(fileContent, BoardInfoFileContentReference) ==
false
)
return;
const boardInfo: BoardInfoFileContent = fileContent as BoardInfoFileContent;
this.showBoardInList(boardInfo);
};
// methods // methods
createBoard = (): void => { createBoard = (): void => {
if (this.cannotCreateBoard.value == true) return; if (this.cannotCreateBoard.value == true) return;
@@ -71,11 +55,12 @@ export default class TaskPageViewModel {
this.newBoardNameInput.value = ""; this.newBoardNameInput.value = "";
this.showBoardInList(boardInfoFileContent); this.showBoardInList(boardInfoFileContent);
this.boardModel.updateBoardAndSend(boardInfoFileContent);
this.updateIndices(); this.updateIndices();
}; };
updateBoard = (boardInfoFileContent: BoardInfoFileContent): void => { updateBoard = (boardInfoFileContent: BoardInfoFileContent): void => {
this.boardModel.updateBoard(boardInfoFileContent); this.boardModel.updateBoardAndSend(boardInfoFileContent);
this.updateIndices(); this.updateIndices();
}; };
-1
View File
@@ -2,7 +2,6 @@ import * as React from "bloatless-react";
import BoardModel, { import BoardModel, {
TaskFileContent, TaskFileContent,
TaskFileContentReference,
} from "../../Model/Files/boardModel"; } from "../../Model/Files/boardModel";
import BoardViewModel from "./boardViewModel"; import BoardViewModel from "./boardViewModel";