From 45ec0d495bf05aa62fba0be4a0e07ed29500707b Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Fri, 9 Dec 2022 09:34:46 -0800 Subject: [PATCH] Bring newly-commented images up to the top of the feed This won't disrupt any current clients, (which would be super annoying), but for any refreshes or new visits to the page, they will see the most-recently-commented images at the top of the feed. --- index.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 4108ebc..49a8566 100644 --- a/index.js +++ b/index.js @@ -89,11 +89,24 @@ io.on('connection', (socket) => { // When any client comments, send that to all clients (including sender) socket.on('comment', (comment) => { + const images = state.images; + + // Send comment to clients after adding commenter's name comment.name = socket.request.session.name; io.emit('comment', comment); - image = state.images.find(image => image.id == comment.image_id); + + const index = images.findIndex(image => image.id == comment.image_id); + + // Before adding the comment to server's state, drop the image_id delete comment.image_id; + + // Now add the comment to the image, remove the image from the + // images array and then add it back at the end, (so it appears + // as the most-recently-modified image for any new clients) + const image = images[index]; image.comments.push(comment); + images.splice(index, 1); + images.push(image); }); // Generate an image when requested -- 2.43.0