Skip to content

Using state for edit and delete messages as well as doing reply to message function #45

Merged
Prakhar896 merged 18 commits intomainfrom
nicholas
Jul 11, 2024
Merged

Using state for edit and delete messages as well as doing reply to message function #45
Prakhar896 merged 18 commits intomainfrom
nicholas

Conversation

@nicholascheww
Copy link
Copy Markdown
Contributor

What was done since the last PR:

  • Changed edit message function to use state instead of reloading after each edit
  • Added a (edited) after a message if it has been edited
  • Messages that have been edited will have a boolean value inside the database to tell whether if the message is edited or not
  • Changed delete message function to use state instead of reloading after each delete
  • Added a replyToID, repliedMessage and edited to models chatMessage
  • Done reply message where the id and the message the user is replying to will be saved into the database
  • Removed console.log() for logging user's messages
  • Removed chat.js file as realised it was redundant

@nicholascheww nicholascheww added the enhancement New feature or request label Jul 8, 2024
@nicholascheww nicholascheww requested a review from Prakhar896 July 8, 2024 09:50
Copy link
Copy Markdown
Contributor

@Prakhar896 Prakhar896 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few things to make consistent and more efficient.

Comment thread models/ChatMessage.js Outdated
Comment thread routes/chat/WebSocketServer.js Outdated

// Static user IDs for demonstration
const user1ID = "Jamie";
const user2ID = "James";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use real user IDs of real host and guest accounts. Currently, in onDBSynchronise, Jamie Oliver record is being created as a Host and Susie Jones record is being created as Guest. Their primary key identifiers are stored in Universal.data["DUMMY_HOST_ID"] and Universal.data["DUMMY_GUEST_ID"] respectively. You can use those.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

im going to be working on doing auth soon so i shall leave this behind first

Comment thread routes/chat/WebSocketServer.js Outdated
handleEditMessage(parsedMessage, user1ID, user2ID, ws);
} else if (parsedMessage.action === "delete") {
handleDeleteMessage(parsedMessage);
} else {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the message is indeed a normal message event. I recommend having an if condition in this if a normal message is being sent and then making the else reject the client's message event. Example:

...
} else if (parsedMessage.action === "newMessage") {
...
} else {
    broadcastMessage(JSON.stringify({"error": "Invalid event update"}))
}

Comment thread routes/chat/WebSocketServer.js Outdated
Comment thread routes/chat/WebSocketServer.js Outdated
Comment thread routes/chat/WebSocketServer.js Outdated
const jsonMessage = {
action: "error",
message: "Error occured on the server",
message: "Error occurred on the server",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this a server error? The client just provided an invalid messageId.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its because the message its not in the database anymore as if someone went into our database and delete that message from the database hence i put the error on the server is that wrong?

Comment thread routes/chat/WebSocketServer.js Outdated
Comment thread routes/chat/WebSocketServer.js Outdated
Comment thread routes/chat/WebSocketServer.js Outdated
};
broadcastMessage(JSON.stringify(jsonMessage));
} catch (error) {
console.error("Error deleting message:", error);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logger.log this as well.

Comment thread routes/chat/WebSocketServer.js Outdated
function broadcastMessage(message, sender) {


function broadcastMessage(message) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just realised, this broadcastMessage method broadcasts messages to all clients. So, won't client-specific error messages be unnecessarily broadcasted to all clients?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my point for this is because if the message has been already deleted in our database it should reload both the clients pages idk if i am right or wrong about this part

@nicholascheww nicholascheww requested a review from Prakhar896 July 9, 2024 13:55
Copy link
Copy Markdown
Contributor

@Prakhar896 Prakhar896 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor changes needed.

Comment thread routes/chat/WebSocketServer.js Outdated
sender: parsedMessage.sender,
datetime: parsedMessage.datetime,
chatID: chatID,
replyToID: parsedMessage.replyToID || null,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't you be checking that the replyToID is a valid and existing ChatMessage first?

Comment thread routes/chat/WebSocketServer.js Outdated
ws.on("message", async (message) => {
const parsedMessage = JSON.parse(message);
if (parsedMessage.action === "edit") {
handleEditMessage(parsedMessage, user1ID, user2ID, ws);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handleEditMessage only takes in editedMessage, the rest of the arguments are unnecessary.

Comment thread routes/chat/WebSocketServer.js Outdated

let replyToMessage = null;
if (parsedMessage.replyToID) {
replyToMessage = await ChatMessage.findByPk(parsedMessage.replyToID);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the earlier comment on checking the existence of the reply to message, you can move this line of code up above the ChatMessage creation and carry out the check in this if chain itself. Basically:

let replyToMessage = null;
if (parsedMessage.replyToID) {
    replyToMessage = await ChatMessage.findByPk(parsedMessage.replyToID);
    if (!replyToMessage) {
        // carry out error handling and reporting here
    }
}

const createdMessage = await ChatMessage.create(....

}

function broadcastMessage(message) {
clients.forEach((client) => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You still need to understand that this whole flow won't work at all the second you add a third client. Since you're broadcasting everything to all clients, event updates for one conversation will interfere other conversations on the frontend as well. This is for future PRs, but I did inform you of this issue much earlier.

@nicholascheww nicholascheww requested a review from Prakhar896 July 11, 2024 07:42
Copy link
Copy Markdown
Contributor

@Prakhar896 Prakhar896 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check your code before requesting a review.

Comment thread routes/chat/WebSocketServer.js Outdated
} else if (parsedMessage.action === "send") {
try {
let replyToMessages = await ChatMessage.findByPk(parsedMessage.replyToID);
console.log(replyToMessages);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove console log

Comment thread routes/chat/WebSocketServer.js Outdated
handleDeleteMessage(parsedMessage);
} else if (parsedMessage.action === "send") {
try {
let replyToMessages = await ChatMessage.findByPk(parsedMessage.replyToID);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable name is plural when you are actually storing a singular ChatMessage. Change to replyToMessage instead.

@nicholascheww nicholascheww requested a review from Prakhar896 July 11, 2024 08:54
Copy link
Copy Markdown
Contributor

@Prakhar896 Prakhar896 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Prakhar896 Prakhar896 merged commit 397f3c9 into main Jul 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants