Using state for edit and delete messages as well as doing reply to message function #45
Using state for edit and delete messages as well as doing reply to message function #45Prakhar896 merged 18 commits intomainfrom
Conversation
Sync-up PR (Nicholas)
… does not disappear anymore
…as edited which will ruin the sorting of messages
Prakhar896
left a comment
There was a problem hiding this comment.
A few things to make consistent and more efficient.
|
|
||
| // Static user IDs for demonstration | ||
| const user1ID = "Jamie"; | ||
| const user2ID = "James"; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
im going to be working on doing auth soon so i shall leave this behind first
| handleEditMessage(parsedMessage, user1ID, user2ID, ws); | ||
| } else if (parsedMessage.action === "delete") { | ||
| handleDeleteMessage(parsedMessage); | ||
| } else { |
There was a problem hiding this comment.
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"}))
}| const jsonMessage = { | ||
| action: "error", | ||
| message: "Error occured on the server", | ||
| message: "Error occurred on the server", |
There was a problem hiding this comment.
Why is this a server error? The client just provided an invalid messageId.
There was a problem hiding this comment.
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?
| }; | ||
| broadcastMessage(JSON.stringify(jsonMessage)); | ||
| } catch (error) { | ||
| console.error("Error deleting message:", error); |
There was a problem hiding this comment.
Logger.log this as well.
| function broadcastMessage(message, sender) { | ||
|
|
||
|
|
||
| function broadcastMessage(message) { |
There was a problem hiding this comment.
Just realised, this broadcastMessage method broadcasts messages to all clients. So, won't client-specific error messages be unnecessarily broadcasted to all clients?
There was a problem hiding this comment.
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
Prakhar896
left a comment
There was a problem hiding this comment.
Minor changes needed.
| sender: parsedMessage.sender, | ||
| datetime: parsedMessage.datetime, | ||
| chatID: chatID, | ||
| replyToID: parsedMessage.replyToID || null, |
There was a problem hiding this comment.
Shouldn't you be checking that the replyToID is a valid and existing ChatMessage first?
| ws.on("message", async (message) => { | ||
| const parsedMessage = JSON.parse(message); | ||
| if (parsedMessage.action === "edit") { | ||
| handleEditMessage(parsedMessage, user1ID, user2ID, ws); |
There was a problem hiding this comment.
handleEditMessage only takes in editedMessage, the rest of the arguments are unnecessary.
|
|
||
| let replyToMessage = null; | ||
| if (parsedMessage.replyToID) { | ||
| replyToMessage = await ChatMessage.findByPk(parsedMessage.replyToID); |
There was a problem hiding this comment.
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) => { |
There was a problem hiding this comment.
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.
Prakhar896
left a comment
There was a problem hiding this comment.
Please check your code before requesting a review.
| } else if (parsedMessage.action === "send") { | ||
| try { | ||
| let replyToMessages = await ChatMessage.findByPk(parsedMessage.replyToID); | ||
| console.log(replyToMessages); |
| handleDeleteMessage(parsedMessage); | ||
| } else if (parsedMessage.action === "send") { | ||
| try { | ||
| let replyToMessages = await ChatMessage.findByPk(parsedMessage.replyToID); |
There was a problem hiding this comment.
This variable name is plural when you are actually storing a singular ChatMessage. Change to replyToMessage instead.
What was done since the last PR: