Skip to content

Commit

Permalink
fix sendMessageReceipts only if chat has not ended (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
haomingli2020 authored Dec 18, 2023
1 parent 5af66f7 commit 71c94a7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/core/chatController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -259,6 +268,8 @@ class ChatController {
}

_handleGainedConnection(eventData) {
this.hasChatEnded = false;

this._forwardChatEvent(CHAT_EVENTS.CONNECTION_ESTABLISHED, {
data: eventData,
chatDetails: this.getChatDetails()
Expand All @@ -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()
Expand Down
37 changes: 37 additions & 0 deletions src/core/chatController.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -566,6 +600,7 @@ describe("ChatController", () => {
})
};
const chatController = getChatController();
chatController.hasChatEnded = false;
await chatController.connect();
chatClient.sendEvent.mockClear();
chatController.sendEvent(readArgs);
Expand Down Expand Up @@ -608,6 +643,7 @@ describe("ChatController", () => {
})
};
const chatController = getChatController();
chatController.hasChatEnded = false;

chatController.connect().then(()=>{
chatClient.sendEvent.mockClear();
Expand Down Expand Up @@ -651,6 +687,7 @@ describe("ChatController", () => {
})
};
const chatController = getChatController(false);
chatController.hasChatEnded = false;
try {
await chatController.connect();
chatClient.sendEvent.mockClear();
Expand Down

0 comments on commit 71c94a7

Please sign in to comment.