diff --git a/client/js/socket-events/more.js b/client/js/socket-events/more.js index 8640a883..b36be2dc 100644 --- a/client/js/socket-events/more.js +++ b/client/js/socket-events/more.js @@ -7,7 +7,6 @@ const chat = $("#chat"); const templates = require("../../views"); socket.on("more", function(data) { - const documentFragment = render.buildChannelMessages(data); const chan = chat .find("#chan-" + data.chan) .find(".messages"); @@ -16,6 +15,12 @@ socket.on("more", function(data) { const scrollable = chan.closest(".chat"); const heightOld = chan.height(); + // If there are no more messages to show, just hide the button and do nothing else + if (!data.messages.length) { + scrollable.find(".show-more").removeClass("show"); + return; + } + // Remove the date-change marker we put at the top, because it may // not actually be a date change now const children = $(chan).children(); @@ -29,6 +34,7 @@ socket.on("more", function(data) { } // Add the older messages + const documentFragment = render.buildChannelMessages(data); chan.prepend(documentFragment).end(); // restore scroll position diff --git a/src/client.js b/src/client.js index f259030f..9acce727 100644 --- a/src/client.js +++ b/src/client.js @@ -403,14 +403,19 @@ Client.prototype.inputLine = function(data) { }; Client.prototype.more = function(data) { - var client = this; - var target = client.find(data.target); + const client = this; + const target = client.find(data.target); + if (!target) { return; } - var chan = target.chan; - var index = chan.messages.findIndex((val) => val.id === data.lastId); - var messages = chan.messages.slice(Math.max(0, index - 100), index); + + const chan = target.chan; + const index = chan.messages.findIndex((val) => val.id === data.lastId); + + // If we don't find the requested message, send an empty array + const messages = index > 0 ? chan.messages.slice(Math.max(0, index - 100), index) : []; + client.emit("more", { chan: chan.id, messages: messages