Skip to content

Commit

Permalink
Adding "isMessageEditableOrDeletable" in sockets/editMessage.js
Browse files Browse the repository at this point in the history
  • Loading branch information
amishabaraskar committed Nov 29, 2024
1 parent 986b9df commit f3cf628
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
1 change: 1 addition & 0 deletions client/src/components/Chat.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ const Chat = () => {
chatId: app.currentChatId,
newMessage: message,
oldMessage,
time:messageObject.time
});

updateMessage({ ...editedMessage, room: app.currentChatId }, true);
Expand Down
4 changes: 2 additions & 2 deletions client/src/lib/chatSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
27 changes: 15 additions & 12 deletions server/sockets/editMessage.js
Original file line number Diff line number Diff line change
@@ -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;
}

Expand All @@ -29,7 +32,7 @@ module.exports = (socket) => {
oldMessage,
});
socket.broadcast.to(chatId).emit(NEW_EVENT_EDIT_MESSAGE, messageEdited);
messageWasEditedSuccessfully(messageEdited);
// messageWasEditedSuccessfully(messageEdited);
}
);
};

0 comments on commit f3cf628

Please sign in to comment.