From 458c490f6fedd5ddfc466b10b49a7ed1b5de3e59 Mon Sep 17 00:00:00 2001 From: Alf Eaton Date: Tue, 14 Dec 2021 10:51:33 +0000 Subject: [PATCH] Add a client id when sending a chat message (#6073) GitOrigin-RevId: 3ddfab6e711de6770b27aafe87491d33e310635c --- .../app/src/Features/Chat/ChatController.js | 3 ++- .../js/features/chat/context/chat-context.js | 20 +++++++------------ .../chat/context/chat-context.test.js | 2 +- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/services/web/app/src/Features/Chat/ChatController.js b/services/web/app/src/Features/Chat/ChatController.js index 0a2a890c8f..79d9047326 100644 --- a/services/web/app/src/Features/Chat/ChatController.js +++ b/services/web/app/src/Features/Chat/ChatController.js @@ -24,7 +24,7 @@ const async = require('async') module.exports = ChatController = { sendMessage(req, res, next) { const { project_id } = req.params - const { content } = req.body + const { content, client_id } = req.body const user_id = SessionManager.getLoggedInUserId(req.session) if (user_id == null) { const err = new Error('no logged-in user') @@ -45,6 +45,7 @@ module.exports = ChatController = { return next(err) } message.user = UserInfoController.formatPersonalInfo(user) + message.clientId = client_id EditorRealTimeController.emitToRoom( project_id, 'new-chat-message', diff --git a/services/web/frontend/js/features/chat/context/chat-context.js b/services/web/frontend/js/features/chat/context/chat-context.js index 76d178a50a..a01fa2e96b 100644 --- a/services/web/frontend/js/features/chat/context/chat-context.js +++ b/services/web/frontend/js/features/chat/context/chat-context.js @@ -18,6 +18,8 @@ import { useLayoutContext } from '../../../shared/context/layout-context' const PAGE_SIZE = 50 +const clientId = uuid() + export function chatReducer(state, action) { switch (action.type) { case 'INITIAL_FETCH_MESSAGES': @@ -55,14 +57,12 @@ export function chatReducer(state, action) { content: action.content, timestamp: Date.now(), }), - messageWasJustSent: true, } case 'RECEIVE_MESSAGE': return { ...state, messages: appendMessage(state.messages, action.message), - messageWasJustSent: false, unreadMessageCount: state.unreadMessageCount + 1, } @@ -93,7 +93,6 @@ const initialState = { initialMessagesLoaded: false, lastTimestamp: null, atEnd: false, - messageWasJustSent: false, unreadMessageCount: 0, error: null, } @@ -198,7 +197,7 @@ export function ChatProvider({ children }) { const url = `/project/${projectId}/messages` postJSON(url, { - body: { content }, + body: { content, client_id: clientId }, }).catch(error => { dispatch({ type: 'ERROR', @@ -219,11 +218,9 @@ export function ChatProvider({ children }) { if (!socket) return function receivedMessage(message) { - // If the message is from the current user and they just sent a message, - // then we are receiving the sent message back from the socket. Ignore it - // to prevent double message - const messageIsFromSelf = message?.user?.id === user?.id - if (messageIsFromSelf && state.messageWasJustSent) return + // If the message is from the current client id, then we are receiving the sent message back from the socket. + // Ignore it to prevent double message. + if (message.clientId === clientId) return dispatch({ type: 'RECEIVE_MESSAGE', message }) @@ -239,10 +236,7 @@ export function ChatProvider({ children }) { socket.removeListener('new-chat-message', receivedMessage) } - // We're adding and removing the socket listener every time we send a - // message (and messageWasJustSent changes). Not great, but no good way - // around it - }, [socket, state.messageWasJustSent, state.unreadMessageCount, user]) + }, [socket]) // Handle unread messages useEffect(() => { diff --git a/services/web/test/frontend/features/chat/context/chat-context.test.js b/services/web/test/frontend/features/chat/context/chat-context.test.js index 009ac3b7fc..75c4c9d653 100644 --- a/services/web/test/frontend/features/chat/context/chat-context.test.js +++ b/services/web/test/frontend/features/chat/context/chat-context.test.js @@ -405,7 +405,7 @@ describe('ChatContext', function () { 'express:/project/:projectId/messages', 'POST' ) - expect(JSON.parse(body)).to.deep.equal({ content: 'sent message' }) + expect(JSON.parse(body)).to.deep.include({ content: 'sent message' }) }) it("doesn't send if the content is empty", function () {