2017-05-18 20:08:54 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const $ = require("jquery");
|
|
|
|
const socket = require("../socket");
|
|
|
|
const render = require("../render");
|
|
|
|
const chat = $("#chat");
|
|
|
|
|
|
|
|
socket.on("msg", function(data) {
|
2017-08-25 12:30:47 +00:00
|
|
|
if (window.requestIdleCallback) {
|
|
|
|
// During an idle period the user agent will run idle callbacks in FIFO order
|
|
|
|
// until either the idle period ends or there are no more idle callbacks eligible to be run.
|
|
|
|
// We set a maximum timeout of 2 seconds so that messages don't take too long to appear.
|
|
|
|
window.requestIdleCallback(() => processReceivedMessage(data), {timeout: 2000});
|
|
|
|
} else {
|
|
|
|
processReceivedMessage(data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function processReceivedMessage(data) {
|
2017-06-22 20:08:36 +00:00
|
|
|
const targetId = data.chan;
|
|
|
|
const target = "#chan-" + targetId;
|
|
|
|
const channel = chat.find(target);
|
2017-08-13 09:23:51 +00:00
|
|
|
const container = channel.find(".messages");
|
|
|
|
|
|
|
|
const activeChannelId = chat.find(".chan.active").data("id");
|
2017-05-18 20:08:54 +00:00
|
|
|
|
|
|
|
if (data.msg.type === "channel_list" || data.msg.type === "ban_list") {
|
|
|
|
$(container).empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add message to the container
|
2017-06-22 20:08:36 +00:00
|
|
|
render.appendMessage(
|
|
|
|
container,
|
2017-08-24 13:50:47 +00:00
|
|
|
targetId,
|
2017-06-22 20:08:36 +00:00
|
|
|
$(target).attr("data-type"),
|
2017-08-24 13:50:47 +00:00
|
|
|
data.msg
|
2017-06-22 20:08:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
container.trigger("msg", [
|
|
|
|
target,
|
|
|
|
data
|
|
|
|
]).trigger("keepToBottom");
|
2017-05-18 20:08:54 +00:00
|
|
|
|
|
|
|
var lastVisible = container.find("div:visible").last();
|
|
|
|
if (data.msg.self
|
|
|
|
|| lastVisible.hasClass("unread-marker")
|
|
|
|
|| (lastVisible.hasClass("date-marker")
|
|
|
|
&& lastVisible.prev().hasClass("unread-marker"))) {
|
|
|
|
container
|
|
|
|
.find(".unread-marker")
|
|
|
|
.appendTo(container);
|
|
|
|
}
|
2017-08-13 09:23:51 +00:00
|
|
|
|
|
|
|
// Message arrived in a non active channel, trim it to 100 messages
|
2017-06-22 20:08:36 +00:00
|
|
|
if (activeChannelId !== targetId && container.find(".msg").slice(0, -100).remove().length) {
|
2017-08-13 09:23:51 +00:00
|
|
|
channel.find(".show-more").addClass("show");
|
|
|
|
|
2017-08-25 15:58:16 +00:00
|
|
|
// Remove date-separators that would otherwise
|
2017-08-13 09:23:51 +00:00
|
|
|
// be "stuck" at the top of the channel
|
|
|
|
channel.find(".date-marker-container").each(function() {
|
|
|
|
if ($(this).next().hasClass("date-marker-container")) {
|
|
|
|
$(this).remove();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-08-25 12:30:47 +00:00
|
|
|
}
|