]> git.cworth.org Git - zombocom-ai/commitdiff
Bring newly-commented images up to the top of the feed
authorCarl Worth <cworth@cworth.org>
Fri, 9 Dec 2022 17:34:46 +0000 (09:34 -0800)
committerCarl Worth <cworth@cworth.org>
Fri, 9 Dec 2022 18:44:11 +0000 (10:44 -0800)
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

index 4108ebc2c1a3e35fac22d93079b07fc1b4608f60..49a85669e45c3c436ccf2141042d4ca30f9c1339 100644 (file)
--- 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