diff --git a/CHANGELOG.md b/CHANGELOG.md index 17a60cf65b..373a73a997 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -329,6 +329,7 @@ Notes: web developers are advised to use [`~` (tilde range)](https://github.com/ - Fixed citation links are not properly matched against markdown links, in PR [#5614](https://github.com/microsoft/BotFramework-WebChat/pull/5614), by [@OEvgeny](https://github.com/OEvgeny) - Fixed `botframework-webchat/decorator` import in legacy CommonJS environments, in [#5616](https://github.com/microsoft/BotFramework-WebChat/pull/5616), by [@OEvgeny](https://github.com/OEvgeny) - Fixed `npm start` for efficiency and reliability, in PR [#5621](https://github.com/microsoft/BotFramework-WebChat/pull/5621) and [#5629](https://github.com/microsoft/BotFramework-WebChat/pull/5629), by [@compulim](https://github.com/compulim) +- Fixed activity sorting introduced in PR [#5622](https://github.com/microsoft/BotFramework-WebChat/pull/5622), part grouping, and livestreaming, by [@compulim](https://github.com/compulim) in PR [#XXX](https://github.com/microsoft/BotFramework-WebChat/pull/XXX) ### Removed diff --git a/__tests__/html2/activityOrdering/liveStreamingAtTheEndOfChatHistory.html b/__tests__/html2/activityOrdering/liveStreamingAtTheEndOfChatHistory.html new file mode 100644 index 0000000000..1635eccdc1 --- /dev/null +++ b/__tests__/html2/activityOrdering/liveStreamingAtTheEndOfChatHistory.html @@ -0,0 +1,135 @@ + + + + + + +
+ + + + diff --git a/__tests__/html2/activityOrdering/liveStreamingAtTheEndOfChatHistory.html.snap-1.png b/__tests__/html2/activityOrdering/liveStreamingAtTheEndOfChatHistory.html.snap-1.png new file mode 100644 index 0000000000..c9a2e66fb3 Binary files /dev/null and b/__tests__/html2/activityOrdering/liveStreamingAtTheEndOfChatHistory.html.snap-1.png differ diff --git a/__tests__/html2/activityOrdering/liveStreamingAtTheEndOfChatHistory.html.snap-2.png b/__tests__/html2/activityOrdering/liveStreamingAtTheEndOfChatHistory.html.snap-2.png new file mode 100644 index 0000000000..6cbff1997a Binary files /dev/null and b/__tests__/html2/activityOrdering/liveStreamingAtTheEndOfChatHistory.html.snap-2.png differ diff --git a/__tests__/html2/activityOrdering/liveStreamingAtTheEndOfChatHistory.html.snap-3.png b/__tests__/html2/activityOrdering/liveStreamingAtTheEndOfChatHistory.html.snap-3.png new file mode 100644 index 0000000000..d145ae6287 Binary files /dev/null and b/__tests__/html2/activityOrdering/liveStreamingAtTheEndOfChatHistory.html.snap-3.png differ diff --git a/__tests__/html2/activityOrdering/liveStreamingAtTheEndOfChatHistory.html.snap-4.png b/__tests__/html2/activityOrdering/liveStreamingAtTheEndOfChatHistory.html.snap-4.png new file mode 100644 index 0000000000..6eb4e68428 Binary files /dev/null and b/__tests__/html2/activityOrdering/liveStreamingAtTheEndOfChatHistory.html.snap-4.png differ diff --git a/__tests__/html2/activityOrdering/liveStreamingAtTheEndOfChatHistory.html.snap-5.png b/__tests__/html2/activityOrdering/liveStreamingAtTheEndOfChatHistory.html.snap-5.png new file mode 100644 index 0000000000..2bd6f3c7d5 Binary files /dev/null and b/__tests__/html2/activityOrdering/liveStreamingAtTheEndOfChatHistory.html.snap-5.png differ diff --git a/__tests__/html2/activityOrdering/partGroupingAtTheEndOfChatHistory.html b/__tests__/html2/activityOrdering/partGroupingAtTheEndOfChatHistory.html new file mode 100644 index 0000000000..9d50764776 --- /dev/null +++ b/__tests__/html2/activityOrdering/partGroupingAtTheEndOfChatHistory.html @@ -0,0 +1,164 @@ + + + + + + +
+ + + + diff --git a/__tests__/html2/activityOrdering/partGroupingAtTheEndOfChatHistory.html.snap-1.png b/__tests__/html2/activityOrdering/partGroupingAtTheEndOfChatHistory.html.snap-1.png new file mode 100644 index 0000000000..dfc4b0d129 Binary files /dev/null and b/__tests__/html2/activityOrdering/partGroupingAtTheEndOfChatHistory.html.snap-1.png differ diff --git a/__tests__/html2/activityOrdering/partGroupingAtTheEndOfChatHistory.html.snap-2.png b/__tests__/html2/activityOrdering/partGroupingAtTheEndOfChatHistory.html.snap-2.png new file mode 100644 index 0000000000..9d54c2cb0b Binary files /dev/null and b/__tests__/html2/activityOrdering/partGroupingAtTheEndOfChatHistory.html.snap-2.png differ diff --git a/__tests__/html2/activityOrdering/partGroupingAtTheEndOfChatHistory.html.snap-3.png b/__tests__/html2/activityOrdering/partGroupingAtTheEndOfChatHistory.html.snap-3.png new file mode 100644 index 0000000000..5c9fecb1da Binary files /dev/null and b/__tests__/html2/activityOrdering/partGroupingAtTheEndOfChatHistory.html.snap-3.png differ diff --git a/__tests__/html2/activityOrdering/partGroupingAtTheEndOfChatHistory.html.snap-4.png b/__tests__/html2/activityOrdering/partGroupingAtTheEndOfChatHistory.html.snap-4.png new file mode 100644 index 0000000000..cb2e320445 Binary files /dev/null and b/__tests__/html2/activityOrdering/partGroupingAtTheEndOfChatHistory.html.snap-4.png differ diff --git a/__tests__/html2/activityOrdering/partGroupingAtTheEndOfChatHistory.html.snap-5.png b/__tests__/html2/activityOrdering/partGroupingAtTheEndOfChatHistory.html.snap-5.png new file mode 100644 index 0000000000..aeefb34501 Binary files /dev/null and b/__tests__/html2/activityOrdering/partGroupingAtTheEndOfChatHistory.html.snap-5.png differ diff --git a/packages/core/src/reducers/createActivitiesReducer.ts b/packages/core/src/reducers/createActivitiesReducer.ts index 0481600dd1..c7feb3e09b 100644 --- a/packages/core/src/reducers/createActivitiesReducer.ts +++ b/packages/core/src/reducers/createActivitiesReducer.ts @@ -165,36 +165,40 @@ function upsertActivityWithSort( // - If not available, it will fallback to `+new Date(timestamp)` // - Outgoing activity will not have `timestamp` field - let indexToInsert = nextActivities.findIndex(activity => { - const { channelData = {} } = activity; - const currentEntityPosition = channelData['webchat:entity-position']; - const currentEntityPartOf = channelData['webchat:entity-part-of']; + let indexToInsert = -1; - const bothHavePosition = typeof currentEntityPosition === 'number' && typeof upsertingEntityPosition === 'number'; - const bothArePartOf = typeof currentEntityPartOf === 'string' && currentEntityPartOf === upsertingPartOf; + if (typeof upsertingEntityPosition === 'number' && typeof upsertingPartOf === 'string') { + const activitiesOfSamePartGrouping = nextActivities.filter( + activity => activity.channelData['webchat:entity-part-of'] === upsertingPartOf + ); - // For activities in the same creative work part, position is primary sort key - if (bothHavePosition && bothArePartOf) { - return currentEntityPosition > upsertingEntityPosition; - } - - return false; - }); + if (activitiesOfSamePartGrouping.length) { + const activityImmediateBeforeInsertion = activitiesOfSamePartGrouping.find( + activity => activity.channelData['webchat:entity-position'] > upsertingEntityPosition + ); - if (!~indexToInsert) { - indexToInsert = nextActivities.findIndex(activity => { - const currentLivestreamingMetadata = getActivityLivestreamingMetadata(activity); - - if ( - upsertingLivestreamingMetadata && - currentLivestreamingMetadata && - upsertingLivestreamingMetadata.sessionId === currentLivestreamingMetadata.sessionId - ) { - return currentLivestreamingMetadata.sequenceNumber > upsertingLivestreamingMetadata.sequenceNumber; - } + indexToInsert = activityImmediateBeforeInsertion + ? nextActivities.indexOf(activityImmediateBeforeInsertion) + : nextActivities.indexOf(activitiesOfSamePartGrouping.at(-1)) + 1; + } + } - return false; - }); + if (!~indexToInsert && upsertingLivestreamingMetadata) { + const upsertingLivestreamingSessionId = upsertingLivestreamingMetadata.sessionId; + const upsertingLivestreamingSequenceNumber = upsertingLivestreamingMetadata.sequenceNumber; + const activitiesOfSameLivestreamSession = nextActivities.filter( + activity => getActivityLivestreamingMetadata(activity)?.sessionId === upsertingLivestreamingSessionId + ); + + if (activitiesOfSameLivestreamSession.length) { + const activityImmediateBeforeInsertion = activitiesOfSameLivestreamSession.find( + activity => getActivityLivestreamingMetadata(activity).sequenceNumber > upsertingLivestreamingSequenceNumber + ); + + indexToInsert = activityImmediateBeforeInsertion + ? nextActivities.indexOf(activityImmediateBeforeInsertion) + : nextActivities.indexOf(activitiesOfSameLivestreamSession.at(-1)) + 1; + } } // If the upserting activity does not have sequence ID or timestamp, always append it.