Merge pull request #1803 from thelounge/xpaw/merge-condensed

Merge condensed messages when loading more history
This commit is contained in:
Jérémie Astori 2017-12-06 09:07:33 -05:00 committed by GitHub
commit 6ec1a2ee14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 5 deletions

View File

@ -1,21 +1,29 @@
"use strict"; "use strict";
const _ = require("lodash");
const constants = require("./constants"); const constants = require("./constants");
const templates = require("../views"); const templates = require("../views");
module.exports = { module.exports = {
updateText, updateText,
getStoredTypes,
}; };
function updateText(condensed, addedTypes) { function getStoredTypes(condensed) {
const obj = {}; const obj = {};
constants.condensedTypes.forEach((type) => { constants.condensedTypes.forEach((type) => {
obj[type] = condensed.data(type) || 0; obj[type] = condensed.data(type) || 0;
}); });
addedTypes.forEach((type) => { return obj;
obj[type]++; }
function updateText(condensed, addedTypes) {
const obj = getStoredTypes(condensed);
_.forOwn(addedTypes, (count, type) => {
obj[type] += count;
condensed.data(type, obj[type]); condensed.data(type, obj[type]);
}); });

View File

@ -61,18 +61,21 @@ function appendMessage(container, chanId, chanType, msg) {
return; return;
} }
const obj = {};
obj[msg.type] = 1;
// If the previous message is already condensed, // If the previous message is already condensed,
// we just append to it and update text // we just append to it and update text
if (lastChild.hasClass("condensed")) { if (lastChild.hasClass("condensed")) {
lastChild.append(renderedMessage); lastChild.append(renderedMessage);
condensed.updateText(lastChild, [msg.type]); condensed.updateText(lastChild, obj);
return; return;
} }
// Always create a condensed container // Always create a condensed container
const newCondensed = $(templates.msg_condensed({time: msg.time})); const newCondensed = $(templates.msg_condensed({time: msg.time}));
condensed.updateText(newCondensed, [msg.type]); condensed.updateText(newCondensed, obj);
newCondensed.append(renderedMessage); newCondensed.append(renderedMessage);
container.append(newCondensed); container.append(newCondensed);
} }

View File

@ -3,6 +3,7 @@
const $ = require("jquery"); const $ = require("jquery");
const socket = require("../socket"); const socket = require("../socket");
const render = require("../render"); const render = require("../render");
const condensed = require("../condensed");
const chat = $("#chat"); const chat = $("#chat");
socket.on("more", function(data) { socket.on("more", function(data) {
@ -58,6 +59,21 @@ socket.on("more", function(data) {
} }
} }
// Join duplicate condensed messages together
const condensedDuplicate = chan.find(".msg.condensed + .msg.condensed");
if (condensedDuplicate) {
const condensedCorrect = condensedDuplicate.prev();
condensed.updateText(condensedCorrect, condensed.getStoredTypes(condensedDuplicate));
condensedCorrect
.append(condensedDuplicate.find(".msg"))
.toggleClass("closed", condensedDuplicate.hasClass("closed"));
condensedDuplicate.remove();
}
// restore scroll position // restore scroll position
const position = chan.height() - heightOld; const position = chan.height() - heightOld;
scrollable.finish().scrollTop(position); scrollable.finish().scrollTop(position);