Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.
TypeScript debiandeep-videojitsijitsi-meetscalable-video-conferencessfuvideovideo-communicationvideo-conferencingwebrtc
feat(chat): add support for message retraction in group and private chat (#17554)
Alok-work234701c1f1b805
diff --git a/lang/main.json b/lang/main.jsonindex f83bf02512..fe9cfc4600 100644--- a/lang/main.json+++ b/lang/main.json@@ -115,6 +115,9 @@}},"chat": {+ "deleteMessage": "Delete",+ "deletedMessage": "This message was deleted.",+ "deletedMessageByMe": "You deleted this message.","disabled": "Sending chat messages is disabled.","edited": "(edited)","editingMessage": "Editing",diff --git a/react/features/base/conference/reducer.ts b/react/features/base/conference/reducer.tsindex 40e459cf0b..653e5071b5 100644--- a/react/features/base/conference/reducer.ts+++ b/react/features/base/conference/reducer.ts@@ -103,6 +103,7 @@ export interface IConferenceMetadata {export interface IJitsiConference {addCommandListener: Function;addLobbyMessageListener: Function;+ addLobbyMessageRetractionListener: Function;addTrack: Function;authenticateAndUpgradeRole: Function;avModerationApprove: Function;@@ -169,7 +170,9 @@ export interface IJitsiConference {sendFaceLandmarks: (faceLandmarks: FaceLandmarks) => void;sendFeedback: Function;sendLobbyMessage: Function;+ sendLobbyMessageRetraction: Function;sendMessage: Function;+ sendMessageRetraction: Function;sendPrivateTextMessage: Function;sendReaction: Function;sendTextMessage: Function;diff --git a/react/features/chat/actionTypes.ts b/react/features/chat/actionTypes.tsindex 12d2bcce5c..23e56b4517 100644--- a/react/features/chat/actionTypes.ts+++ b/react/features/chat/actionTypes.ts@@ -84,6 +84,17 @@ export const OPEN_CHAT = 'OPEN_CHAT';export const SEND_MESSAGE = 'SEND_MESSAGE';/**+ * The type of the action which signals a request to retract+ * (delete) a previously sent chat message.+ *+ * {+ * type: SEND_MESSAGE_RETRACTION,+ * message: IMessage+ * }+ */+export const SEND_MESSAGE_RETRACTION = 'SEND_MESSAGE_RETRACTION';++/** The type of the action which signals sending an edit for an existing chat message.** {@@ -176,6 +187,15 @@ export const SET_LOBBY_CHAT_ACTIVE_STATE = 'SET_LOBBY_CHAT_ACTIVE_STATE';*/export const REMOVE_LOBBY_CHAT_PARTICIPANT = 'REMOVE_LOBBY_CHAT_PARTICIPANT';+/**+ * The type of action which signals to delete your own message after sending it (self deletion).+ *+ * {+ * type: RETRACT_MESSAGE+ * }+ */+export const RETRACT_MESSAGE = 'RETRACT_MESSAGE';+/*** The type of action which signals to set the width of the chat panel.*diff --git a/react/features/chat/actions.any.ts b/react/features/chat/actions.any.tsindex f323e29d6e..2dc01bd340 100644--- a/react/features/chat/actions.any.ts+++ b/react/features/chat/actions.any.ts@@ -14,8 +14,10 @@ import {NOTIFY_PRIVATE_RECIPIENTS_CHANGED,OPEN_CHAT,REMOVE_LOBBY_CHAT_PARTICIPANT,+ RETRACT_MESSAGE,SEND_MESSAGE,SEND_MESSAGE_EDIT,+ SEND_MESSAGE_RETRACTION,SEND_REACTION,SET_CHAT_SEARCH_MATCH_INDEX,SET_CHAT_SEARCH_QUERY,@@ -25,6 +27,7 @@ import {SET_PRIVATE_MESSAGE_RECIPIENT} from './actionTypes';import { ChatTabs } from './constants';+import { IMessage } from './types';/*** Adds a chat message to the collection of messages.@@ -154,6 +157,19 @@ export function sendMessage(message: string, ignorePrivacy = false) {};}+/**+ * Requests retraction (delete) of a previously sent message.+ *+ * @param {IMessage} message - The message to retract.+ * @returns {Object}+ */+export function sendMessageRetraction(message: IMessage) {+ return {+ type: SEND_MESSAGE_RETRACTION,+ message+ };+}+/*** Sends a reaction to a message.*@@ -258,6 +274,21 @@ export function setPrivateMessageRecipientById(participantId: string) {};}+/**+ * Marks a message as retracted.+ *+ * @param {string} messageId - The retracted message id.+ * @param {string} retractedBy - The participant ID of the user who retracted the message.+ * @returns {Object}+ */+export function retractMessage(messageId: string, retractedBy: string) {+ return {+ type: RETRACT_MESSAGE,+ messageId,+ retractedBy+ };+}+/*** Set the value of the currently focused tab.*diff --git a/react/features/chat/components/native/ChatMessage.tsx b/react/features/chat/components/native/ChatMessage.tsxindex 4e8544874d..240a79c43c 100644--- a/react/features/chat/components/native/ChatMessage.tsx+++ b/react/features/chat/components/native/ChatMessage.tsx@@ -81,7 +81,9 @@ class ChatMessage extends Component<IChatMessageProps> {messageBubbleStyle.push(styles.activeMatchMessageBubble);}- const messageText = getMessageText(this.props.message);+ const messageText = message.isDeleted+ ? this.props.t(localMessage ? 'chat.deletedMessageByMe' : 'chat.deletedMessage')+ : getMessageText(this.props.message);return (<View@@ -97,7 +99,7 @@ class ChatMessage extends Component<IChatMessageProps> {: this._renderMessageTextComponent(messageText) }{ this._renderPrivateNotice() }</View>- { this._renderPrivateReplyButton() }+ { !message.isDeleted && this._renderPrivateReplyButton() }</View>{ this._renderTimestamp() }</View>diff --git a/react/features/chat/components/web/ChatMessage.tsx b/react/features/chat/components/web/ChatMessage.tsxindex 7e2b00c3a2..e83095c53c 100644--- a/react/features/chat/components/web/ChatMessage.tsx+++ b/react/features/chat/components/web/ChatMessage.tsx@@ -186,6 +186,10 @@ const useStyles = makeStyles()((theme: Theme) => {padding: '0 1px'}},+ deletedMessage: {+ fontStyle: 'italic',+ opacity: 0.7+ },privateMessageNotice: {...theme.typography.labelRegular,color: theme.palette.chatPrivateNotice,@@ -472,7 +476,7 @@ const ChatMessage = ({isFileMessage = { isFileMessage(message) }isFromVisitor = { message.isFromVisitor }isLobbyMessage = { message.lobbyChat }- message = { message.message }+ message = { message }onEditMessage = { handleEditMessage }participantId = { message.participantId } />}</div>@@ -491,7 +495,7 @@ const ChatMessage = ({<div className = { classes.replyWrapper }><div className = { cx('messagecontent', classes.messageContent) }>{showDisplayName && _renderDisplayName()}- <div className = { cx('usermessage', classes.userMessage) }>+ <div className = { cx('usermessage', classes.userMessage, message.isDeleted && classes.deletedMessage) }>{isFileMessage(message) ? (<FileMessagemessage = { message }@@ -509,13 +513,21 @@ const ChatMessage = ({: t<string>('chat.messageAccessibleTitle', {user: message.displayName}) }- text = { getMessageText(message) } />+ text = {+ message.isDeleted+ ? (+ message.messageType === MESSAGE_TYPE_LOCAL+ ? t<string>('chat.deletedMessageByMe')+ : t<string>('chat.deletedMessage')+ )+ : getMessageText(message)+ } />)}{(message.privateMessage || (message.lobbyChat && !knocking))&& _renderPrivateNotice()}<div className = { classes.chatMessageFooter }><div className = { classes.chatMessageFooterLeft }>- {message.reactions && message.reactions.size > 0 && (+ {!message.isDeleted && message.reactions && message.reactions.size > 0 && (<>{renderReactions}</>@@ -535,7 +547,7 @@ const ChatMessage = ({{shouldDisplayMenuOnRight && (<div className = { classes.sideBySideContainer }>{!message.privateMessage && !message.lobbyChat- && !message.isReaction && <div>+ && !message.isReaction && !message.isDeleted && <div><div className = { classes.optionsButtonContainer }>{isHovered && <ReactButtonmessageId = { message.messageId }@@ -544,14 +556,14 @@ const ChatMessage = ({</div>}<div><div className = { classes.optionsButtonContainer }>- {isHovered && <MessageMenu+ {isHovered && !message.isDeleted && <MessageMenucanEdit = { canEdit }displayName = { message.displayName }enablePrivateChat = { Boolean(enablePrivateChat) }isFileMessage = { isFileMessage(message) }isFromVisitor = { message.isFromVisitor }isLobbyMessage = { message.lobbyChat }- message = { message.message }+ message = { message }onEditMessage = { handleEditMessage }participantId = { message.participantId } />}</div>diff --git a/react/features/chat/components/web/MessageMenu.tsx b/react/features/chat/components/web/MessageMenu.tsxindex 5653888df1..1a23fed315 100644--- a/react/features/chat/components/web/MessageMenu.tsx+++ b/react/features/chat/components/web/MessageMenu.tsx@@ -11,8 +11,11 @@ import Popover from '../../../base/popover/components/Popover.web';import Button from '../../../base/ui/components/web/Button';import { BUTTON_TYPES } from '../../../base/ui/constants.any';import { copyText } from '../../../base/util/copyText.web';+import { sendMessageRetraction } from '../../actions.any';import { handleLobbyChatInitialized, openChat } from '../../actions.web';+import { MESSAGE_TYPE_LOCAL } from '../../constants';import logger from '../../logger';+import { IMessage } from '../../types';export interface IProps {canEdit?: boolean;@@ -22,7 +25,7 @@ export interface IProps {isFileMessage?: boolean;isFromVisitor?: boolean;isLobbyMessage: boolean;- message: string;+ message: IMessage;onEditMessage?: () => void;participantId: string;}@@ -64,7 +67,7 @@ const useStyles = makeStyles()(theme => {};});-const MessageMenu = ({ canEdit, message, participantId, isFromVisitor, isLobbyMessage, enablePrivateChat, displayName, isFileMessage, onEditMessage }: IProps) => {+const MessageMenu = ({ canEdit, message, isFromVisitor, isLobbyMessage, enablePrivateChat, displayName, isFileMessage, onEditMessage }: IProps) => {const dispatch = useDispatch();const { classes, cx } = useStyles();const { t } = useTranslation();@@ -74,7 +77,7 @@ const MessageMenu = ({ canEdit, message, participantId, isFromVisitor, isLobbyMeleft: 0 });const buttonRef = useRef<HTMLDivElement>(null);- const participant = useSelector((state: IReduxState) => getParticipantById(state, participantId));+ const participant = useSelector((state: IReduxState) => getParticipantById(state, message.participantId));// If no menu items will be shown, don't render the menu button.if (!enablePrivateChat && isFileMessage && !canEdit) {@@ -91,14 +94,14 @@ const MessageMenu = ({ canEdit, message, participantId, isFromVisitor, isLobbyMeconst handlePrivateClick = useCallback(() => {if (isLobbyMessage) {- dispatch(handleLobbyChatInitialized(participantId));+ dispatch(handleLobbyChatInitialized(message.participantId));} else {// For visitor messages, participant will be undefined but we can still open chat// using the participantId which contains the visitor's original JIDif (isFromVisitor) {// Handle visitor participant that doesn't exist in main participant listconst visitorParticipant = {- id: participantId,+ id: message.participantId,name: displayName,isVisitor: true};@@ -109,10 +112,10 @@ const MessageMenu = ({ canEdit, message, participantId, isFromVisitor, isLobbyMe}}handleClose();- }, [ dispatch, isLobbyMessage, participant, participantId, displayName ]);+ }, [ dispatch, isLobbyMessage, participant, message.participantId, displayName ]);const handleCopyClick = useCallback(() => {- copyText(message)+ copyText(message.message).then(success => {if (success) {if (buttonRef.current) {@@ -135,7 +138,13 @@ const MessageMenu = ({ canEdit, message, participantId, isFromVisitor, isLobbyMelogger.error('Error copying text', error);});handleClose();- }, [ message ]);+ }, [ message.message ]);++ const handleDeleteClick = useCallback(() => {+ dispatch(sendMessageRetraction(message));++ handleClose();+ }, [ message, handleClose ]);const handleEditClick = useCallback(() => {onEditMessage?.();@@ -165,6 +174,15 @@ const MessageMenu = ({ canEdit, message, participantId, isFromVisitor, isLobbyMe{t('Copy')}</div>)}+ {message.messageType === MESSAGE_TYPE_LOCAL+ && !message.isDeleted+ && (+ <div+ className = { classes.menuItem }+ onClick = { handleDeleteClick }>+ {t('chat.deleteMessage')}+ </div>+ )}</div>);diff --git a/react/features/chat/middleware.ts b/react/features/chat/middleware.tsindex fa32af9b3e..2b6dd76795 100644--- a/react/features/chat/middleware.ts+++ b/react/features/chat/middleware.ts@@ -47,6 +47,7 @@ import {OPEN_CHAT,SEND_MESSAGE,SEND_MESSAGE_EDIT,+ SEND_MESSAGE_RETRACTION,SEND_REACTION,SET_FOCUSED_TAB} from './actionTypes';@@ -58,6 +59,7 @@ import {editMessage,notifyPrivateRecipientsChanged,openChat,+ retractMessage,setPrivateMessageRecipient} from './actions';import { ChatPrivacyDialog } from './components';@@ -80,6 +82,7 @@ import {isSendGroupChatDisabled,isVisitorChatParticipant} from './functions';+import logger from './logger';import { INCOMING_MSG_SOUND_FILE } from './sounds';import './subscriber';import { IMessage } from './types';@@ -351,11 +354,14 @@ MiddlewareRegistry.register(store => next => action => {}if (isLobbyChatActive && lobbyMessageRecipient) {+ const messageId = uuidv4();+conference.sendLobbyMessage({type: LOBBY_CHAT_MESSAGE,- message: action.message+ message: action.message,+ messageId}, lobbyMessageRecipient.id);- _persistSentPrivateMessage(store, lobbyMessageRecipient, action.message, true);+ _persistSentPrivateMessage(store, lobbyMessageRecipient, action.message, true, messageId);} else if (privateMessageRecipient) {const messageId = uuidv4();@@ -370,6 +376,36 @@ MiddlewareRegistry.register(store => next => action => {break;}+ case SEND_MESSAGE_RETRACTION: {+ const state = store.getState();+ const conference = getCurrentConference(state);++ if (!localParticipant || action.message.participantId !== localParticipant.id) {+ logger.warn('Ignoring retraction request for a message that does not belong to the local participant.');+ break;+ }+ if (conference) {+ if (action.message.lobbyChat) {+ conference.sendLobbyMessageRetraction(action.message.messageId, action.message.recipientId);+ } else {+ conference.sendMessageRetraction(+ action.message.messageId,+ action.message.privateMessage+ ? action.message.recipientId+ : undefined,+ action.message.sentToVisitor+ );+ }++ // For group chat this comes back via the MUC echo, but private+ // messages don't echo back to the sender via the normal path.+ if ((action.message.privateMessage || action.message.lobbyChat)) {+ dispatch(retractMessage(action.message.messageId, localParticipant.id));+ }+ }+ break;+ }+case SEND_MESSAGE_EDIT: {const state = store.getState();const conference = getCurrentConference(state);@@ -584,6 +620,13 @@ function _addChatMsgListener(conference: IJitsiConference, store: IStore) {});+ conference.on(+ JitsiConferenceEvents.MESSAGE_RETRACTED,+ (participantId: string, messageId: string) => {+ _onMessageRetracted(store, participantId, messageId);+ }+ );+conference.on(JitsiConferenceEvents.CONFERENCE_ERROR, (errorType: string, error: Error) => {errorType === JitsiConferenceErrors.CHAT_ERROR && _handleChatError(store, error);@@ -646,6 +689,30 @@ function _onReactionReceived(store: IStore, { participantId, reactionList, messastore.dispatch(addMessageReaction(reactionPayload));}+/**+ * Handles a retracted message.+ *+ * @param {Object} store - Redux store.+ * @param {string} participantId - Id of the participant that sent the message.+ * @param {string} messageId - The id of the message that is retracted.+ * @returns {void}+ */+function _onMessageRetracted(store: IStore, participantId: string, messageId: string) {+ const { messages } = store.getState()['features/chat'];++ const originalMessage = messages.find(message => message.messageId === messageId);++ if (!originalMessage) {+ return;+ }++ if (originalMessage.participantId !== participantId) {+ return;+ }++ store.dispatch(retractMessage(messageId, participantId));+}+/*** Handles a received gif message.*@@ -682,19 +749,45 @@ function _handleChatError({ dispatch }: IStore, error: Error) {** @param {string} message - The message received.* @param {string} participantId - The participant id.+ * @param {string} messageId - The optional message id.* @returns {Function}*/-export function handleLobbyMessageReceived(message: string, participantId: string) {+export function handleLobbyMessageReceived(message: string, participantId: string, messageId?: string) {return async (dispatch: IStore['dispatch'], getState: IStore['getState']) => {_handleReceivedMessage({ dispatch,getState }, { participantId,message,privateMessage: false,lobbyChat: true,+ messageId,timestamp: Date.now() });};}+/**+ * Handles a retracted message received from the lobby room.+ *+ * @param {string} messageId - The id of the message that is retracted.+ * @param {string} participantId - Id of the participant that sent the message.+ * @returns {Function}+ */+export function handleLobbyMessageRetracted(messageId: string, participantId: string) {+ return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {+ const { messages } = getState()['features/chat'];++ const originalMessage = messages.find(message => message.messageId === messageId);++ if (!originalMessage) {+ return;+ }++ if (originalMessage.participantId !== participantId) {+ return;+ }++ dispatch(retractMessage(messageId, participantId));+ };+}/*** Function to get lobby chat user display name.@@ -847,7 +940,7 @@ interface IRecipient {* @param {IRecipient} recipient - The recipient the private message was sent to.* @param {string} message - The sent message.* @param {boolean} isLobbyPrivateMessage - Is a lobby message.- * @param {string} messageId - The unique identifier of the private message used for message editing and tracking.+ * @param {string} messageId - The id of sent message.* @returns {void}*/function _persistSentPrivateMessage({ dispatch, getState }: IStore, recipient: IRecipient,diff --git a/react/features/chat/reducer.ts b/react/features/chat/reducer.tsindex 909a61b033..8bdc690105 100644--- a/react/features/chat/reducer.ts+++ b/react/features/chat/reducer.ts@@ -14,6 +14,7 @@ import {NOTIFY_PRIVATE_RECIPIENTS_CHANGED,OPEN_CHAT,REMOVE_LOBBY_CHAT_PARTICIPANT,+ RETRACT_MESSAGE,SET_CHAT_IS_RESIZING,SET_CHAT_SEARCH_MATCH_INDEX,SET_CHAT_SEARCH_QUERY,@@ -242,6 +243,26 @@ ReducerRegistry.register<IChatState>('features/chat', (state = DEFAULT_STATE, ac};}+ case RETRACT_MESSAGE: {+ const messages = state.messages.map(message => {+ if (message.messageId === action.messageId && message.participantId === action.retractedBy) {+ return {+ ...message,+ message: '',+ isDeleted: true,+ retractedBy: action.retractedBy+ };+ }++ return message;+ });++ return {+ ...state,+ messages+ };+ }+case SET_CHAT_SEARCH_MATCH_INDEX:return {...state,@@ -295,6 +316,7 @@ ReducerRegistry.register<IChatState>('features/chat', (state = DEFAULT_STATE, acisOpen: action.payload || state.isOpen,privateMessageRecipient: undefined};+case REMOVE_LOBBY_CHAT_PARTICIPANT:return {...state,diff --git a/react/features/chat/types.ts b/react/features/chat/types.tsindex 783e080441..80516e0902 100644--- a/react/features/chat/types.ts+++ b/react/features/chat/types.ts@@ -19,6 +19,7 @@ export interface IMessage {editedAt?: number;error?: unknown;fileMetadata?: IFileMetadata;+ isDeleted?: boolean;isEdited?: Boolean;isFromGuest?: boolean;isFromVisitor?: boolean;@@ -36,6 +37,7 @@ export interface IMessage {* When set, XMPP message id of the message this one replies to (XEP-0461), from lib-jitsi-meet.*/replyToMessageId?: string;+ retractedBy?: string;sentToVisitor?: boolean;timestamp: number;}diff --git a/react/features/lobby/actions.any.ts b/react/features/lobby/actions.any.tsindex 4e012dbf61..214f5a1d08 100644--- a/react/features/lobby/actions.any.ts+++ b/react/features/lobby/actions.any.ts@@ -5,7 +5,7 @@ import { getLocalParticipant } from '../base/participants/functions';import { IParticipant } from '../base/participants/types';import { onLobbyChatInitialized, removeLobbyChatParticipant, sendMessage } from '../chat/actions.any';import { LOBBY_CHAT_MESSAGE } from '../chat/constants';-import { handleLobbyMessageReceived } from '../chat/middleware';+import { handleLobbyMessageReceived, handleLobbyMessageRetracted } from '../chat/middleware';import { hideNotification, showNotification } from '../notifications/actions';import { LOBBY_NOTIFICATION_ID } from '../notifications/constants';import { joinConference } from '../prejoin/actions';@@ -415,7 +415,7 @@ export function setLobbyMessageListener() {conference?.addLobbyMessageListener((message: any, participantId: string) => {if (message.type === LOBBY_CHAT_MESSAGE) {- return dispatch(handleLobbyMessageReceived(message.message, participantId));+ return dispatch(handleLobbyMessageReceived(message.message, participantId, message.messageId));}if (message.type === LOBBY_CHAT_INITIALIZED) {return dispatch(handleLobbyChatInitialized(message));@@ -424,6 +424,10 @@ export function setLobbyMessageListener() {return dispatch(updateLobbyParticipantOnLeave(message.moderatorId));}});++ conference?.addLobbyMessageRetractionListener((messageId: string, participantId: string) => {+ dispatch(handleLobbyMessageRetracted(messageId, participantId));+ });};}