Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add 15-minute restriction on message edit/delete options #717

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions client/src/components/Chat/DropDownOption.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { BiDotsVerticalRounded } from 'react-icons/bi';
import Dropdown from 'rsuite/Dropdown';
import PropTypes from 'prop-types';
import React from 'react';
import React, { useMemo } from 'react';
import chatHelper from 'src/lib/chatHelper';
import { socket } from 'src/lib/socketConnection';
import { useApp } from 'src/context/AppContext';
import { useChat } from 'src/context/ChatContext';
import useChatUtils from 'src/lib/chatSocket';
import useCryptoKeys from 'src/hooks/useCryptoKeys';

import { FIFTEEN_MINUTES } from '../../../../constants.json';


const DropDownOptions = ({ id, isSender, inputRef, cancelEdit, setEditing, setReplyId }) => {
const { app } = useApp();

Expand All @@ -17,7 +20,17 @@ const DropDownOptions = ({ id, isSender, inputRef, cancelEdit, setEditing, setRe
const { getMessage, messageExists, handleCopyToClipBoard } = chatHelper(state, app);
const { deleteMessage } = useChatUtils(socket);

const message = getMessage(id, state, app);

const isWithin15Minutes = useMemo(() => {
return Date.now() - new Date(message.time).getTime() <= FIFTEEN_MINUTES;
}, [message.time])

const handleEdit = (id) => {
if (!isWithin15Minutes) {
return;
}

inputRef.current.focus();
const { message } = getMessage(id, state, app);

Expand All @@ -31,7 +44,7 @@ const DropDownOptions = ({ id, isSender, inputRef, cancelEdit, setEditing, setRe
};

const handleDelete = async (id) => {
if (!messageExists(id)) {
if (!messageExists(id) || !isWithin15Minutes) {
return;
}

Expand Down Expand Up @@ -78,13 +91,22 @@ const DropDownOptions = ({ id, isSender, inputRef, cancelEdit, setEditing, setRe
renderToggle={renderIconButton}
NoCaret
>
<Dropdown.Item onClick={() => handleEdit(id)}>Edit</Dropdown.Item>
{
isWithin15Minutes && (
<Dropdown.Item onClick={() => handleEdit(id)}>Edit</Dropdown.Item>
)
}

<Dropdown.Item onClick={() => handleCopyToClipBoard(id, importedPrivateKey)}>
Copy
</Dropdown.Item>
<Dropdown.Item onClick={() => setReplyId(id)}>Reply</Dropdown.Item>
<Dropdown.Item onClick={() => handleDelete(id)}>Delete</Dropdown.Item>

{
isWithin15Minutes && (
<Dropdown.Item onClick={() => handleDelete(id)}>Delete</Dropdown.Item>
)
}
</Dropdown>
);
} else if (!isSender) {
Expand Down
4 changes: 3 additions & 1 deletion constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@
"NEW_EVENT_CREATE_ROOM": "createRoom",
"NEW_EVENT_READ_MESSAGE": "read_message",
"NEW_EVENT_ONLINE_STATUS": "online_status",
"NEW_EVENT_REQUEST_PUBLIC_KEY": "requestPublicKey"
"NEW_EVENT_REQUEST_PUBLIC_KEY": "requestPublicKey",

"FIFTEEN_MINUTES": 900000
}
8 changes: 7 additions & 1 deletion server/sockets/deleteMessage.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { NEW_EVENT_DELETE_MESSAGE } = require('../../constants.json');
const { getActiveUser, removeMessage } = require('../utils/lib');
const { getActiveUser, removeMessage, isMessageEditableOrDeletable } = require('../utils/lib');

module.exports = (socket) => {
socket.on(
Expand All @@ -14,6 +14,12 @@ module.exports = (socket) => {
return;
}

// Check if message exists and is within the 15-minute editable window
if (!isMessageEditableOrDeletable(chatId, messageId)) {
messageWasDeletedSuccessfully(false);
return;
}

const messageDeleted = await removeMessage(chatId, messageId);

socket.broadcast
Expand Down
6 changes: 6 additions & 0 deletions server/sockets/editMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ module.exports = (socket) => {
return;
}

// Check if message exists and is within the 15-minute editable window
if (!isMessageEditableOrDeletable(chatId, messageId)) {
messageWasEditedSuccessfully(false);
return;
}

const messageEdited = await editMessage(chatId, {
id: messageId,
message: newMessage,
Expand Down
17 changes: 16 additions & 1 deletion server/utils/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const User = require('../models/UserModel');
const Message = require('../models/MessageModel');
const { generateObjectId } = require('./helper');

const { FIFTEEN_MINUTES } = require('../../constants.json');


/**
* @typedef {{
* id: string,
Expand Down Expand Up @@ -532,6 +535,17 @@ async function isUserBlocked(users) {
}
}

function isMessageEditableOrDeletable(chatId, messageId) {
const chat = chats[chatId];
if (!chat || !chat.messages || !chat.messages[messageId]) {
return false; // Message doesn't exist
}

const message = chat.messages[messageId];
const timeSinceCreated = Date.now() - new Date(message.time).getTime();
return timeSinceCreated <= FIFTEEN_MINUTES; // Check if within 15 minutes
}

module.exports = {
init,
createChat,
Expand All @@ -552,5 +566,6 @@ module.exports = {
delActiveUser,
seenMessage,
blockUser,
isUserBlocked
isUserBlocked,
isMessageEditableOrDeletable
};
Loading