From 71c94a7936cbc3e123fac7ffd1d77b8e458e487c Mon Sep 17 00:00:00 2001 From: haomingli2020 <74380769+haomingli2020@users.noreply.github.com> Date: Mon, 18 Dec 2023 14:32:41 -0800 Subject: [PATCH] fix sendMessageReceipts only if chat has not ended (#206) --- src/core/chatController.js | 14 ++++++++++++- src/core/chatController.spec.js | 37 +++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/core/chatController.js b/src/core/chatController.js index 1650767..0769b95 100644 --- a/src/core/chatController.js +++ b/src/core/chatController.js @@ -49,6 +49,7 @@ class ChatController { }); this.logMetaData = args.logMetaData; this.messageReceiptUtil = new MessageReceiptsUtil(args.logMetaData); + this.hasChatEnded = false; this.logger.info("Browser info:", window.navigator.userAgent); } @@ -132,6 +133,14 @@ class ChatController { .catch(this.handleRequestFailure(metadata, ACPS_METHODS.DOWNLOAD_ATTACHMENT, startTime)); } + sendEventIfChatHasNotEnded(...args) { + if (this.hasChatEnded) { + this.logger.warn("Ignoring sendEvent API bec chat has ended", ...args); + return Promise.resolve(); + } + return this.chatClient.sendEvent(...args); + } + sendEvent(args) { if (!this._validateConnectionStatus('sendEvent')) { return; @@ -153,7 +162,7 @@ class ChatController { }); } // Prioritize and send selective message receipts - return this.messageReceiptUtil.prioritizeAndSendMessageReceipt(this.chatClient, this.chatClient.sendEvent, + return this.messageReceiptUtil.prioritizeAndSendMessageReceipt(this.chatClient, this.sendEventIfChatHasNotEnded.bind(this), connectionToken, args.contentType, content, @@ -259,6 +268,8 @@ class ChatController { } _handleGainedConnection(eventData) { + this.hasChatEnded = false; + this._forwardChatEvent(CHAT_EVENTS.CONNECTION_ESTABLISHED, { data: eventData, chatDetails: this.getChatDetails() @@ -283,6 +294,7 @@ class ChatController { chatDetails: this.getChatDetails() }); if (incomingData.ContentType === CONTENT_TYPE.chatEnded) { + this.hasChatEnded = true; this._forwardChatEvent(CHAT_EVENTS.CHAT_ENDED, { data: null, chatDetails: this.getChatDetails() diff --git a/src/core/chatController.spec.js b/src/core/chatController.spec.js index 1b034d1..d30bfbe 100644 --- a/src/core/chatController.spec.js +++ b/src/core/chatController.spec.js @@ -549,6 +549,40 @@ describe("ChatController", () => { }); }); + test("should not sendEvent for MessageReceipts if chat has ended", done => { + jest.useRealTimers(); + const args = { + metadata: "metadata", + contentType: CONTENT_TYPE.readReceipt, + content: JSON.stringify({ + messageId: "messageId" + }) + }; + const chatController = getChatController(); + chatController.hasChatEnded = false; + chatController.connect().then(() => { + chatClient.sendEvent.mockClear(); + + Promise.all([chatController.sendEvent(args), + chatController.sendEvent(args), + chatController.sendEvent(args), + chatController.sendEvent(args), + chatController.sendEvent(args)]).then(async () => { + expect(chatClient.sendEvent).toHaveBeenCalledTimes(1); + expect(chatClient.sendEvent).toHaveBeenCalledWith("token", CONTENT_TYPE.readReceipt, "{\"messageId\":\"messageId\"}", "INCOMING_READ_RECEIPT", 1000); + + chatController.connectionHelper.$simulateEnding(); + chatClient.sendEvent.mockClear(); + await Utils.delay(1); + await chatController.sendEvent(args); + await chatController.sendEvent(args); + expect(chatClient.sendEvent).toHaveBeenCalledTimes(0); + + done(); + }); + }); + }); + test("should throttle Read and Delivered events for MessageReceipts to only send Read Event", async () => { jest.useRealTimers(); const readArgs = { @@ -566,6 +600,7 @@ describe("ChatController", () => { }) }; const chatController = getChatController(); + chatController.hasChatEnded = false; await chatController.connect(); chatClient.sendEvent.mockClear(); chatController.sendEvent(readArgs); @@ -608,6 +643,7 @@ describe("ChatController", () => { }) }; const chatController = getChatController(); + chatController.hasChatEnded = false; chatController.connect().then(()=>{ chatClient.sendEvent.mockClear(); @@ -651,6 +687,7 @@ describe("ChatController", () => { }) }; const chatController = getChatController(false); + chatController.hasChatEnded = false; try { await chatController.connect(); chatClient.sendEvent.mockClear();