From 971f8d3e6dd524154bf77c7c8f3b0e537fcac4d8 Mon Sep 17 00:00:00 2001 From: amolghode Date: Fri, 27 May 2022 16:29:37 +0530 Subject: [PATCH 1/4] Converting JS to TS from app/livechat directory --- apps/meteor/app/livechat/lib/Assets.js | 2 - apps/meteor/app/livechat/lib/Assets.ts | 4 ++ .../lib/{messageTypes.js => messageTypes.ts} | 43 ++++++++++------- .../lib/stream/{constants.js => constants.ts} | 0 apps/meteor/app/ui-utils/lib/MessageTypes.ts | 2 +- .../core-typings/src/IMessage/IMessage.ts | 48 ++++++++++++++++++- 6 files changed, 77 insertions(+), 22 deletions(-) delete mode 100644 apps/meteor/app/livechat/lib/Assets.js create mode 100644 apps/meteor/app/livechat/lib/Assets.ts rename apps/meteor/app/livechat/lib/{messageTypes.js => messageTypes.ts} (73%) rename apps/meteor/app/livechat/lib/stream/{constants.js => constants.ts} (100%) diff --git a/apps/meteor/app/livechat/lib/Assets.js b/apps/meteor/app/livechat/lib/Assets.js deleted file mode 100644 index 32546c6003be..000000000000 --- a/apps/meteor/app/livechat/lib/Assets.js +++ /dev/null @@ -1,2 +0,0 @@ -export const addServerUrlToIndex = (file) => - file.replace('', ``); diff --git a/apps/meteor/app/livechat/lib/Assets.ts b/apps/meteor/app/livechat/lib/Assets.ts new file mode 100644 index 000000000000..963fb073fdb9 --- /dev/null +++ b/apps/meteor/app/livechat/lib/Assets.ts @@ -0,0 +1,4 @@ +export const addServerUrlToIndex = (file: string): string => { + const rootUrl = window.__meteor_runtime_config__.ROOT_URL.replace(/\/$/, ''); + return file.replace('', ``); +}; diff --git a/apps/meteor/app/livechat/lib/messageTypes.js b/apps/meteor/app/livechat/lib/messageTypes.ts similarity index 73% rename from apps/meteor/app/livechat/lib/messageTypes.js rename to apps/meteor/app/livechat/lib/messageTypes.ts index 62bf6c8aeaf2..5b999e67b8fe 100644 --- a/apps/meteor/app/livechat/lib/messageTypes.js +++ b/apps/meteor/app/livechat/lib/messageTypes.ts @@ -2,14 +2,15 @@ import formatDistance from 'date-fns/formatDistance'; import { TAPi18n } from 'meteor/rocketchat:tap-i18n'; import moment from 'moment'; import { escapeHTML } from '@rocket.chat/string-helpers'; +import { IOmnichannelSystemMessage } from '@rocket.chat/core-typings'; -import { MessageTypes } from '../../ui-utils'; +import { MessageTypes } from '../../ui-utils/lib/MessageTypes'; MessageTypes.registerType({ id: 'livechat_navigation_history', system: true, message: 'New_visitor_navigation', - data(message) { + data(message: IOmnichannelSystemMessage) { if (!message.navigation || !message.navigation.page) { return; } @@ -23,7 +24,7 @@ MessageTypes.registerType({ id: 'livechat_transfer_history', system: true, message: 'New_chat_transfer', - data(message) { + data(message: IOmnichannelSystemMessage) { if (!message.transferData) { return; } @@ -33,20 +34,19 @@ MessageTypes.registerType({ const from = message.transferData.transferredBy && (message.transferData.transferredBy.name || message.transferData.transferredBy.username); const transferTypes = { - agent: () => + agent: (): string => TAPi18n.__(`Livechat_transfer_to_agent${commentLabel}`, { from, - to: - message.transferData.transferredTo && (message.transferData.transferredTo.name || message.transferData.transferredTo.username), + to: message?.transferData?.transferredTo?.name || message?.transferData?.transferredTo?.username || '', ...(comment && { comment }), }), - department: () => + department: (): string => TAPi18n.__(`Livechat_transfer_to_department${commentLabel}`, { from, - to: message.transferData.nextDepartment && message.transferData.nextDepartment.name, + to: message?.transferData?.nextDepartment?.name || '', ...(comment && { comment }), }), - queue: () => + queue: (): string => TAPi18n.__('Livechat_transfer_return_to_the_queue', { from, }), @@ -61,21 +61,21 @@ MessageTypes.registerType({ id: 'livechat_transcript_history', system: true, message: 'Livechat_chat_transcript_sent', - data(message) { + data(message: IOmnichannelSystemMessage) { if (!message.requestData) { return; } - const { requestData: { type, visitor = {}, user = {} } = {} } = message; + const { requestData: { type, visitor, user } = { type: 'user' } } = message; const requestTypes = { - visitor: () => + visitor: (): string => TAPi18n.__('Livechat_visitor_transcript_request', { - guest: visitor.name || visitor.username, + guest: visitor?.name || visitor?.username || '', }), - user: () => + user: (): string => TAPi18n.__('Livechat_user_sent_chat_transcript_to_visitor', { - agent: user.name || user.username, - guest: visitor.name || visitor.username, + agent: user?.name || user?.username || '', + guest: visitor?.name || visitor?.username || '', }), }; @@ -105,13 +105,17 @@ MessageTypes.registerType({ } return escapeHTML(message.msg); }, + message: 'room_changed_privacy', }); MessageTypes.registerType({ id: 'omnichannel_placed_chat_on_hold', system: true, message: 'Omnichannel_placed_chat_on_hold', - data(message) { + data(message: IOmnichannelSystemMessage) { + if (!message.comment) { + return; + } return { comment: message.comment, }; @@ -122,7 +126,10 @@ MessageTypes.registerType({ id: 'omnichannel_on_hold_chat_resumed', system: true, message: 'Omnichannel_on_hold_chat_resumed', - data(message) { + data(message: IOmnichannelSystemMessage) { + if (!message.comment) { + return; + } return { comment: message.comment, }; diff --git a/apps/meteor/app/livechat/lib/stream/constants.js b/apps/meteor/app/livechat/lib/stream/constants.ts similarity index 100% rename from apps/meteor/app/livechat/lib/stream/constants.js rename to apps/meteor/app/livechat/lib/stream/constants.ts diff --git a/apps/meteor/app/ui-utils/lib/MessageTypes.ts b/apps/meteor/app/ui-utils/lib/MessageTypes.ts index d7d4d8e3c98d..5f98c8c1b5e9 100644 --- a/apps/meteor/app/ui-utils/lib/MessageTypes.ts +++ b/apps/meteor/app/ui-utils/lib/MessageTypes.ts @@ -9,7 +9,7 @@ export type MessageType = { /* deprecated */ template?: (message: IMessage) => unknown; message: TranslationKey; - data?: (message: IMessage) => Record; + data?: (message: IMessage) => Record | undefined; }; class MessageTypesClass { private types = new Map(); diff --git a/packages/core-typings/src/IMessage/IMessage.ts b/packages/core-typings/src/IMessage/IMessage.ts index 19fc3f51ed09..146293c69b01 100644 --- a/packages/core-typings/src/IMessage/IMessage.ts +++ b/packages/core-typings/src/IMessage/IMessage.ts @@ -7,6 +7,7 @@ import type { IUser } from '../IUser'; import type { IRoom, RoomID } from '../IRoom'; import type { MessageAttachment } from './MessageAttachment/MessageAttachment'; import type { FileProp } from './MessageAttachment/Files/FileProp'; +import type { ILivechatVisitor } from '../ILivechatVisitor'; type MentionType = 'user' | 'team'; @@ -38,7 +39,16 @@ type TeamMessageTypes = | 'user-added-room-to-team' | 'ujt'; -type OmnichannelTypesValues = 'livechat_transfer_history_fallback' | 'livechat-close'; +type LivechatMessageTypes = 'livechat_navigation_history' + | 'livechat_transfer_history' + | 'livechat_transcript_history' + | 'livechat_video_call' + | 'livechat_webrtc_video_call'; + +type OmnichannelTypesValues = 'livechat_transfer_history_fallback' + | 'livechat-close' + | 'omnichannel_placed_chat_on_hold' + | 'omnichannel_on_hold_chat_resumed'; type OtrSystemMessages = 'user_joined_otr' | 'user_requested_otr_key_refresh' | 'user_key_refreshed_successfully'; @@ -70,6 +80,7 @@ export type MessageTypesValues = | 'room-set-read-only' | 'room-allowed-reacting' | 'room-disallowed-reacting' + | LivechatMessageTypes | TeamMessageTypes | VoipMessageTypesValues | OmnichannelTypesValues @@ -199,6 +210,41 @@ export interface IMessageReactionsNormalized extends IMessage { export const isMessageReactionsNormalized = (message: IMessage): message is IMessageReactionsNormalized => Boolean('reactions' in message && message.reactions && message.reactions[0] && 'names' in message.reactions[0]); +export interface IOmnichannelSystemMessage extends IMessage { + navigation?: { + page: { + title: string; + location: { + href: string; + }; + token?: string; + }; + }; + transferData?: { + comment: string; + transferredBy: { + name?: string; + username: string; + }; + transferredTo: { + name?: string; + username: string; + }; + nextDepartment?: { + _id: string; + name?: string; + }; + scope: 'department' | 'agent' | 'queue'; + }; + requestData?: { + type: 'visitor' | 'user'; + visitor?: ILivechatVisitor; + user?: IUser; + }; + webRtcCallEndTs?: Date; + comment?: string; +} + export type IVoipMessage = IMessage & { voipData: { callDuration?: number; From ec472a0cbd09950cfed0d80331790cca40ce931f Mon Sep 17 00:00:00 2001 From: amolghode Date: Fri, 27 May 2022 16:47:45 +0530 Subject: [PATCH 2/4] Adding missing file --- packages/core-typings/src/IMessage/IMessage.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/core-typings/src/IMessage/IMessage.ts b/packages/core-typings/src/IMessage/IMessage.ts index e7157939f395..4959ae26f23f 100644 --- a/packages/core-typings/src/IMessage/IMessage.ts +++ b/packages/core-typings/src/IMessage/IMessage.ts @@ -39,13 +39,15 @@ type TeamMessageTypes = | 'user-added-room-to-team' | 'ujt'; -type LivechatMessageTypes = 'livechat_navigation_history' +type LivechatMessageTypes = + | 'livechat_navigation_history' | 'livechat_transfer_history' | 'livechat_transcript_history' | 'livechat_video_call' | 'livechat_webrtc_video_call'; -type OmnichannelTypesValues = 'livechat_transfer_history_fallback' +type OmnichannelTypesValues = + | 'livechat_transfer_history_fallback' | 'livechat-close' | 'omnichannel_placed_chat_on_hold' | 'omnichannel_on_hold_chat_resumed'; From 665fe3ee84e6d6e603729b996b5d95c3c7e84d19 Mon Sep 17 00:00:00 2001 From: amolghode Date: Fri, 27 May 2022 17:27:08 +0530 Subject: [PATCH 3/4] Handling review comments. --- apps/meteor/app/livechat/lib/messageTypes.ts | 25 +++++++++----------- apps/meteor/app/ui-utils/lib/MessageTypes.ts | 2 +- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/apps/meteor/app/livechat/lib/messageTypes.ts b/apps/meteor/app/livechat/lib/messageTypes.ts index 5b999e67b8fe..096534d83b9d 100644 --- a/apps/meteor/app/livechat/lib/messageTypes.ts +++ b/apps/meteor/app/livechat/lib/messageTypes.ts @@ -11,11 +11,10 @@ MessageTypes.registerType({ system: true, message: 'New_visitor_navigation', data(message: IOmnichannelSystemMessage) { - if (!message.navigation || !message.navigation.page) { - return; - } return { - history: `${(message.navigation.page.title ? `${message.navigation.page.title} - ` : '') + message.navigation.page.location.href}`, + history: message.navigation + ? `${(message.navigation.page.title ? `${message.navigation.page.title} - ` : '') + message.navigation.page.location.href}` + : '', }; }, }); @@ -26,7 +25,9 @@ MessageTypes.registerType({ message: 'New_chat_transfer', data(message: IOmnichannelSystemMessage) { if (!message.transferData) { - return; + return { + transfer: '', + }; } const { comment } = message.transferData; @@ -63,7 +64,9 @@ MessageTypes.registerType({ message: 'Livechat_chat_transcript_sent', data(message: IOmnichannelSystemMessage) { if (!message.requestData) { - return; + return { + transcript: '', + }; } const { requestData: { type, visitor, user } = { type: 'user' } } = message; @@ -113,11 +116,8 @@ MessageTypes.registerType({ system: true, message: 'Omnichannel_placed_chat_on_hold', data(message: IOmnichannelSystemMessage) { - if (!message.comment) { - return; - } return { - comment: message.comment, + comment: message.comment ? message.comment : 'No comment provided', }; }, }); @@ -127,11 +127,8 @@ MessageTypes.registerType({ system: true, message: 'Omnichannel_on_hold_chat_resumed', data(message: IOmnichannelSystemMessage) { - if (!message.comment) { - return; - } return { - comment: message.comment, + comment: message.comment ? message.comment : 'No comment provided', }; }, }); diff --git a/apps/meteor/app/ui-utils/lib/MessageTypes.ts b/apps/meteor/app/ui-utils/lib/MessageTypes.ts index 5f98c8c1b5e9..d7d4d8e3c98d 100644 --- a/apps/meteor/app/ui-utils/lib/MessageTypes.ts +++ b/apps/meteor/app/ui-utils/lib/MessageTypes.ts @@ -9,7 +9,7 @@ export type MessageType = { /* deprecated */ template?: (message: IMessage) => unknown; message: TranslationKey; - data?: (message: IMessage) => Record | undefined; + data?: (message: IMessage) => Record; }; class MessageTypesClass { private types = new Map(); From 568a4e5613c6fe0d18ff45e49ffca428123814ac Mon Sep 17 00:00:00 2001 From: amolghode Date: Fri, 27 May 2022 18:08:31 +0530 Subject: [PATCH 4/4] Fixing possible test case failure. --- apps/meteor/app/livechat/lib/Assets.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/meteor/app/livechat/lib/Assets.ts b/apps/meteor/app/livechat/lib/Assets.ts index 963fb073fdb9..c812cd22a9b3 100644 --- a/apps/meteor/app/livechat/lib/Assets.ts +++ b/apps/meteor/app/livechat/lib/Assets.ts @@ -1,4 +1,4 @@ export const addServerUrlToIndex = (file: string): string => { - const rootUrl = window.__meteor_runtime_config__.ROOT_URL.replace(/\/$/, ''); + const rootUrl = (global as any).__meteor_runtime_config__.ROOT_URL.replace(/\/$/, ''); return file.replace('', ``); };