Do not unintentionally send incorrect messages from history

This commit is contained in:
Pavel Djundik 2017-08-23 12:32:59 +03:00
parent 91ecd99c84
commit 1bf2be1b51
2 changed files with 17 additions and 6 deletions

View File

@ -7,7 +7,6 @@ const chat = $("#chat");
const templates = require("../../views"); const templates = require("../../views");
socket.on("more", function(data) { socket.on("more", function(data) {
const documentFragment = render.buildChannelMessages(data);
const chan = chat const chan = chat
.find("#chan-" + data.chan) .find("#chan-" + data.chan)
.find(".messages"); .find(".messages");
@ -16,6 +15,12 @@ socket.on("more", function(data) {
const scrollable = chan.closest(".chat"); const scrollable = chan.closest(".chat");
const heightOld = chan.height(); 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 // Remove the date-change marker we put at the top, because it may
// not actually be a date change now // not actually be a date change now
const children = $(chan).children(); const children = $(chan).children();
@ -29,6 +34,7 @@ socket.on("more", function(data) {
} }
// Add the older messages // Add the older messages
const documentFragment = render.buildChannelMessages(data);
chan.prepend(documentFragment).end(); chan.prepend(documentFragment).end();
// restore scroll position // restore scroll position

View File

@ -403,14 +403,19 @@ Client.prototype.inputLine = function(data) {
}; };
Client.prototype.more = function(data) { Client.prototype.more = function(data) {
var client = this; const client = this;
var target = client.find(data.target); const target = client.find(data.target);
if (!target) { if (!target) {
return; return;
} }
var chan = target.chan;
var index = chan.messages.findIndex((val) => val.id === data.lastId); const chan = target.chan;
var messages = chan.messages.slice(Math.max(0, index - 100), index); 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", { client.emit("more", {
chan: chan.id, chan: chan.id,
messages: messages messages: messages