summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
614372a)
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.
// When any client comments, send that to all clients (including sender)
socket.on('comment', (comment) => {
// 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);
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
+
+ // 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);
image.comments.push(comment);
+ images.splice(index, 1);
+ images.push(image);
});
// Generate an image when requested
});
// Generate an image when requested