// 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