11/* eslint complexity: ["error", 30] */
22
3- import { EventActivity , MessageActivity , updateMetadata } from 'botframework-webchat-core' ;
3+ import { EventActivity , MessageActivity , ReadBy , updateMetadata } from 'botframework-webchat-core' ;
44
55import ACSChatMessage from '../types/ACSChatMessage' ;
66import Activity from '../types/Activity' ;
77import createDebug from '../utils/debug' ;
8- import styleConsole from '../utils/styleConsole' ;
98import UserProfiles from '../types/UserProfiles' ;
109
1110let debug ;
1211
13- // TODO: Verify this logic with ACS team to see if this is correct.
14- // function isOthersUserId(userId: string) {
15- // return userId.startsWith('8:');
16- // }
17-
1812// "threadId" is undefined for messages from others, so we prefer to use the "threadId" from setup.
1913export default function createACSMessageToWebChatActivityConverter ( {
2014 threadId,
@@ -24,39 +18,32 @@ export default function createACSMessageToWebChatActivityConverter({
2418 threadId : string ;
2519 userId : string ;
2620 userProfiles : UserProfiles ;
27- } ) : ( chatMessage : ACSChatMessage ) => Activity {
21+ } ) : ( acsChatMessage : ACSChatMessage , metadata : { key : string ; readBy : ReadBy ; trackingNumber : string } ) => Activity {
2822 debug ||
2923 ( debug = createDebug ( 'util:acsMessageToWebChatActivity' , {
3024 backgroundColor : 'lightgray' ,
3125 color : 'black'
3226 } ) ) ;
3327
34- return acsChatMessage => {
28+ return (
29+ acsChatMessage : ACSChatMessage ,
30+ metadata : { key : string ; readBy : ReadBy ; trackingNumber : string }
31+ ) : Activity => {
3532 const now = new Date ( ) ;
3633 const { clientMessageId, content, createdOn, id, sender, senderDisplayName, status, type } = acsChatMessage ;
3734
3835 // TODO: Checks with ACS team to see if this is a good strategy to find out "user.kind".
39- const who : 'others' | 'self' | 'service' = sender
40- ? sender . communicationUserId && sender . communicationUserId !== userId
41- ? 'others'
42- : 'self '
43- : 'service ' ;
36+ const who : 'others' | 'self' | 'service' = ! sender
37+ ? 'service'
38+ : sender . communicationUserId && sender . communicationUserId !== userId
39+ ? 'others '
40+ : 'self ' ;
4441
4542 const communicationUserId = who === 'self' ? userId : who === 'service' ? undefined : sender . communicationUserId ;
4643
47- const userProfile = who === 'others' || who === 'self' ? userProfiles [ communicationUserId || userId ] : undefined ;
44+ const userProfile = who === 'others' || who === 'self' ? userProfiles [ communicationUserId ] : undefined ;
4845
49- // TODO: Assert data if it met our expectation.
50- if ( who === 'self' ) {
51- // Assertion: to work properly, every activity must contains an ID during their lifetime, and this ID must not be changed during any moment of its life.
52- if ( ! clientMessageId && ! id ) {
53- // TODO: Check if conversation history contains "clientMessageId".
54- // TODO: After the message is sent and echoed back from ACS, the "clientMessageId" is gone.
55- // This is crucial for the operation of Web Chat, because, if we can't reference the same message, we will be creating new DOM elements.
56- // Creating new DOM elements will hurt accessibility (ARIA live region).
57- debug ( '🔥🔥🔥 %cFor all self messages, "clientMessageId" and/or "id" must be set.%c' , ...styleConsole ( 'red' ) ) ;
58- }
59- }
46+ // TODO: Add assertions here to make sure the data met our expectation.
6047
6148 const senderName = ( userProfile || { } ) . name || senderDisplayName ;
6249
@@ -83,8 +70,10 @@ export default function createACSMessageToWebChatActivityConverter({
8370 } as any ,
8471 {
8572 avatarInitials : ( userProfile || { } ) . initials ,
86- key : clientMessageId || id ,
73+ key : metadata . key ,
74+ readBy : metadata . readBy ,
8775 senderName,
76+ trackingNumber : metadata . trackingNumber ,
8877 who
8978 }
9079 ) ;
@@ -117,9 +106,6 @@ export default function createACSMessageToWebChatActivityConverter({
117106 ...activityContent
118107 } ,
119108 {
120- // deliveryStatus: createdOn ? undefined : 'sending' // If it contains "createdOn", it's sent.
121-
122- // 'delivered' | 'sending' | 'seen' | 'failed'
123109 deliveryStatus : status === 'failed' ? 'error' : status === 'sending' ? status : undefined
124110 }
125111 ) ;
0 commit comments