From f3cf628327e2bc9ecfec6636d6d13d08232536a7 Mon Sep 17 00:00:00 2001 From: amishabaraskar Date: Fri, 29 Nov 2024 20:50:53 +0530 Subject: [PATCH] Adding "isMessageEditableOrDeletable" in sockets/editMessage.js --- client/src/components/Chat.jsx | 1 + client/src/lib/chatSocket.js | 4 ++-- server/sockets/editMessage.js | 27 +++++++++++++++------------ 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/client/src/components/Chat.jsx b/client/src/components/Chat.jsx index 5b238e1a..3467026d 100644 --- a/client/src/components/Chat.jsx +++ b/client/src/components/Chat.jsx @@ -185,6 +185,7 @@ const Chat = () => { chatId: app.currentChatId, newMessage: message, oldMessage, + time:messageObject.time }); updateMessage({ ...editedMessage, room: app.currentChatId }, true); diff --git a/client/src/lib/chatSocket.js b/client/src/lib/chatSocket.js index 2167be3c..e80fa12f 100644 --- a/client/src/lib/chatSocket.js +++ b/client/src/lib/chatSocket.js @@ -54,7 +54,7 @@ export default function useChatUtils(socket) { }); } - function editMessage({ id, chatId, newMessage, oldMessage, isEdited }) { + function editMessage({ id, chatId, newMessage, oldMessage,time }) { return new Promise((resolve, reject) => { if (!socket.connected) { reject(null); @@ -65,7 +65,7 @@ export default function useChatUtils(socket) { .timeout(30000) .emit( NEW_EVENT_EDIT_MESSAGE, - { id, chatId, newMessage, oldMessage, isEdited }, + { id, chatId, newMessage, oldMessage, time }, (err, messageEdited) => { if (err) { reject(err); diff --git a/server/sockets/editMessage.js b/server/sockets/editMessage.js index a8b94844..a5d1363a 100644 --- a/server/sockets/editMessage.js +++ b/server/sockets/editMessage.js @@ -1,25 +1,28 @@ -const { NEW_EVENT_EDIT_MESSAGE } = require('../../constants.json'); +const { NEW_EVENT_EDIT_MESSAGE,FIFTEEN_MINUTES } = require('../../constants.json'); const { getActiveUser, editMessage } = require('../utils/lib'); +const isMessageEditableOrDeletable = (time) => { + return Date.now() - new Date(time).getTime() <= FIFTEEN_MINUTES; +}; + module.exports = (socket) => { socket.on( NEW_EVENT_EDIT_MESSAGE, async ( - { id: messageId, chatId, newMessage, oldMessage }, - messageWasEditedSuccessfully + { id: messageId, chatId, newMessage, oldMessage,time } ) => { - const user = getActiveUser({ + const user = getActiveUser({ socketId: socket.id, }); - - if (!user || !messageId || !chatId) { - messageWasEditedSuccessfully(false); - return; - } + console.log(user) + if (!user || !messageId || !chatId) { + // messageWasEditedSuccessfully(false); + return; + } // Check if message exists and is within the 15-minute editable window - if (!isMessageEditableOrDeletable(chatId, messageId)) { - messageWasEditedSuccessfully(false); + if (!isMessageEditableOrDeletable(time)) { + // messageWasEditedSuccessfully(false); return; } @@ -29,7 +32,7 @@ module.exports = (socket) => { oldMessage, }); socket.broadcast.to(chatId).emit(NEW_EVENT_EDIT_MESSAGE, messageEdited); - messageWasEditedSuccessfully(messageEdited); + // messageWasEditedSuccessfully(messageEdited); } ); };