Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix get message dup calls #400

Merged
merged 7 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Fixed
- Fix `ChatSDK.onNewMessages()` calling `createOmnichannelMessages()` twice during polling

## [1.10.7] - 2025-01-30

### Added
Expand Down
2 changes: 1 addition & 1 deletion src/OmnichannelChatSDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,7 @@ class OmnichannelChatSDK {
ChatId: this.chatToken.chatId as string
});

return messages;
return messages as GetMessagesResponse;
} catch {
this.scenarioMarker.failScenario(TelemetryEvent.GetMessages, {
RequestId: this.requestId,
Expand Down
26 changes: 14 additions & 12 deletions src/core/messaging/ACSClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import LiveChatVersion from "../LiveChatVersion";
import OmnichannelMessage from "./OmnichannelMessage";
import createOmnichannelMessage from "../../utils/createOmnichannelMessage";
import { defaultMessageTags } from "./MessageTags";
import ACSGetMessagesOptionalParams from "./ACSClientGetMessagesOptionParams";

enum ACSClientEvent {
InitializeACSClient = 'InitializeACSClient',
Expand Down Expand Up @@ -89,10 +90,10 @@ export class ACSConversation {
this.logger?.completeScenario(ACSClientEvent.InitializeACSConversation);
}

public async getMessages(): Promise<OmnichannelMessage[]> {
public async getMessages(optionsParams: ACSGetMessagesOptionalParams = {}): Promise<OmnichannelMessage[] | ChatMessage[]> {
this.logger?.startScenario(ACSClientEvent.GetMessages);

const messages: OmnichannelMessage[] = [];
const messages = [];

try {
const pagedAsyncIterableIterator = await (this.chatThreadClient as ChatThreadClient).listMessages();
Expand All @@ -106,11 +107,13 @@ export class ACSConversation {
continue;
}

const omnichannelMessage = createOmnichannelMessage(chatMessage as ChatMessage, {
liveChatVersion: LiveChatVersion.V2
});

messages.push(omnichannelMessage);
if (optionsParams?.skipConversion === true) {
messages.push(chatMessage as ChatMessage)
} else {
messages.push(createOmnichannelMessage(chatMessage as ChatMessage, {
liveChatVersion: LiveChatVersion.V2
}));
}

nextMessage = await pagedAsyncIterableIterator.next();
}
Expand All @@ -128,7 +131,7 @@ export class ACSConversation {
throw new Error(ACSClientEvent.GetMessages);
}

return messages;
return (optionsParams?.skipConversion === true) ? messages as ChatMessage[] : messages as OmnichannelMessage[];
}

public async getParticipants(): Promise<ChatParticipant[]> {
Expand Down Expand Up @@ -174,12 +177,11 @@ export class ACSConversation {
}

try {
const messages = await this.getMessages();
const messages = await this.getMessages({skipConversion: true});
for (const message of messages.reverse()) {
try {
const { id, sender } = message;
const customerMessageCondition = sender.displayName === ACSParticipantDisplayName.Customer;

const { id, senderDisplayName } = message as ChatMessage;
const customerMessageCondition = senderDisplayName === ACSParticipantDisplayName.Customer;
// Filter out customer messages
if (customerMessageCondition) {
continue;
Expand Down
3 changes: 3 additions & 0 deletions src/core/messaging/ACSClientGetMessagesOptionParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default interface ACSGetMessagesOptionalParams {
skipConversion?: boolean; // Skip conversion to OmnichanneMessage
}
Loading