2017-05-18 20:08:54 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const $ = require("jquery");
|
|
|
|
const templates = require("../views");
|
|
|
|
const options = require("./options");
|
2017-07-06 06:16:01 +00:00
|
|
|
const renderPreview = require("./renderPreview");
|
2017-05-18 20:08:54 +00:00
|
|
|
const utils = require("./utils");
|
|
|
|
const sorting = require("./sorting");
|
2017-06-22 20:08:36 +00:00
|
|
|
const constants = require("./constants");
|
2017-08-19 18:47:23 +00:00
|
|
|
const condensed = require("./condensed");
|
2017-12-21 20:40:50 +00:00
|
|
|
const JoinChannel = require("./join-channel");
|
2017-08-28 20:06:28 +00:00
|
|
|
const helpers_parse = require("./libs/handlebars/parse");
|
2017-12-16 18:58:56 +00:00
|
|
|
const Userlist = require("./userlist");
|
2018-03-15 09:18:06 +00:00
|
|
|
const storage = require("./localStorage");
|
2017-05-18 20:08:54 +00:00
|
|
|
|
|
|
|
const chat = $("#chat");
|
|
|
|
const sidebar = $("#sidebar");
|
|
|
|
|
2017-08-22 21:04:55 +00:00
|
|
|
require("intersection-observer");
|
|
|
|
|
2017-06-29 09:19:37 +00:00
|
|
|
const historyObserver = window.IntersectionObserver ?
|
|
|
|
new window.IntersectionObserver(loadMoreHistory, {
|
2017-11-15 06:35:15 +00:00
|
|
|
root: chat.get(0),
|
2017-06-29 09:19:37 +00:00
|
|
|
}) : null;
|
|
|
|
|
2017-05-18 20:08:54 +00:00
|
|
|
module.exports = {
|
2017-06-22 20:08:36 +00:00
|
|
|
appendMessage,
|
2017-05-18 20:08:54 +00:00
|
|
|
buildChannelMessages,
|
|
|
|
renderChannel,
|
|
|
|
renderChannelUsers,
|
2017-07-06 06:16:01 +00:00
|
|
|
renderNetworks,
|
2017-11-23 14:23:32 +00:00
|
|
|
trimMessageInChannel,
|
2017-05-18 20:08:54 +00:00
|
|
|
};
|
|
|
|
|
2017-08-28 20:06:28 +00:00
|
|
|
function buildChannelMessages(container, chanId, chanType, messages) {
|
2017-08-24 13:50:47 +00:00
|
|
|
return messages.reduce((docFragment, message) => {
|
|
|
|
appendMessage(docFragment, chanId, chanType, message);
|
2017-05-18 20:08:54 +00:00
|
|
|
return docFragment;
|
2017-08-28 20:06:28 +00:00
|
|
|
}, container);
|
2017-05-18 20:08:54 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 13:50:47 +00:00
|
|
|
function appendMessage(container, chanId, chanType, msg) {
|
2017-08-28 15:03:27 +00:00
|
|
|
if (utils.lastMessageId < msg.id) {
|
|
|
|
utils.lastMessageId = msg.id;
|
|
|
|
}
|
|
|
|
|
2017-09-01 11:43:24 +00:00
|
|
|
let lastChild = container.children(".msg, .date-marker-container").last();
|
2017-09-04 16:52:02 +00:00
|
|
|
const renderedMessage = buildChatMessage(msg);
|
2017-08-24 13:50:47 +00:00
|
|
|
|
|
|
|
// Check if date changed
|
|
|
|
const msgTime = new Date(msg.time);
|
2017-09-01 11:43:24 +00:00
|
|
|
const prevMsgTime = new Date(lastChild.data("time"));
|
2017-08-24 13:50:47 +00:00
|
|
|
|
|
|
|
// Insert date marker if date changed compared to previous message
|
|
|
|
if (prevMsgTime.toDateString() !== msgTime.toDateString()) {
|
2017-09-04 16:52:02 +00:00
|
|
|
lastChild = $(templates.date_marker({time: msg.time}));
|
2017-09-01 11:43:24 +00:00
|
|
|
container.append(lastChild);
|
2017-08-24 13:50:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If current window is not a channel or this message is not condensable,
|
|
|
|
// then just append the message to container and be done with it
|
2018-03-21 12:48:07 +00:00
|
|
|
if (msg.self || msg.highlight || constants.condensedTypes.indexOf(msg.type) === -1 || chanType !== "channel") {
|
2017-08-24 13:50:47 +00:00
|
|
|
container.append(renderedMessage);
|
2017-08-19 18:47:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-06 12:07:43 +00:00
|
|
|
const obj = {};
|
|
|
|
obj[msg.type] = 1;
|
|
|
|
|
2017-08-24 13:50:47 +00:00
|
|
|
// If the previous message is already condensed,
|
|
|
|
// we just append to it and update text
|
|
|
|
if (lastChild.hasClass("condensed")) {
|
|
|
|
lastChild.append(renderedMessage);
|
2017-12-06 12:07:43 +00:00
|
|
|
condensed.updateText(lastChild, obj);
|
2017-08-30 12:43:31 +00:00
|
|
|
return;
|
2017-06-22 20:08:36 +00:00
|
|
|
}
|
2017-08-30 12:43:31 +00:00
|
|
|
|
2017-09-01 11:43:24 +00:00
|
|
|
// Always create a condensed container
|
2017-09-04 16:52:02 +00:00
|
|
|
const newCondensed = $(templates.msg_condensed({time: msg.time}));
|
2017-08-30 12:43:31 +00:00
|
|
|
|
2017-12-06 12:07:43 +00:00
|
|
|
condensed.updateText(newCondensed, obj);
|
2017-08-30 12:43:31 +00:00
|
|
|
newCondensed.append(renderedMessage);
|
|
|
|
container.append(newCondensed);
|
2017-06-22 20:08:36 +00:00
|
|
|
}
|
|
|
|
|
2017-09-04 16:52:02 +00:00
|
|
|
function buildChatMessage(msg) {
|
2017-08-24 13:50:47 +00:00
|
|
|
const type = msg.type;
|
2017-05-18 20:08:54 +00:00
|
|
|
let template = "msg";
|
|
|
|
|
2017-07-21 08:35:05 +00:00
|
|
|
// See if any of the custom highlight regexes match
|
2017-08-24 13:50:47 +00:00
|
|
|
if (!msg.highlight && !msg.self
|
2017-07-21 08:35:05 +00:00
|
|
|
&& options.highlightsRE
|
|
|
|
&& (type === "message" || type === "notice")
|
2017-08-24 13:50:47 +00:00
|
|
|
&& options.highlightsRE.exec(msg.text)) {
|
|
|
|
msg.highlight = true;
|
2017-05-18 20:08:54 +00:00
|
|
|
}
|
|
|
|
|
2018-02-13 11:05:33 +00:00
|
|
|
if (typeof templates.actions[type] !== "undefined") {
|
2017-05-18 20:08:54 +00:00
|
|
|
template = "msg_action";
|
|
|
|
} else if (type === "unhandled") {
|
|
|
|
template = "msg_unhandled";
|
|
|
|
}
|
|
|
|
|
2017-08-24 13:50:47 +00:00
|
|
|
const renderedMessage = $(templates[template](msg));
|
|
|
|
const content = renderedMessage.find(".content");
|
2017-05-18 20:08:54 +00:00
|
|
|
|
|
|
|
if (template === "msg_action") {
|
2017-08-24 13:50:47 +00:00
|
|
|
content.html(templates.actions[type](msg));
|
2017-05-18 20:08:54 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 13:50:47 +00:00
|
|
|
msg.previews.forEach((preview) => {
|
|
|
|
renderPreview(preview, renderedMessage);
|
2017-07-18 05:27:18 +00:00
|
|
|
});
|
|
|
|
|
2017-08-24 13:50:47 +00:00
|
|
|
return renderedMessage;
|
2017-05-18 20:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function renderChannel(data) {
|
|
|
|
renderChannelMessages(data);
|
|
|
|
|
|
|
|
if (data.type === "channel") {
|
2017-12-16 18:58:56 +00:00
|
|
|
const users = renderChannelUsers(data);
|
|
|
|
|
|
|
|
Userlist.handleKeybinds(users.find(".search"));
|
2017-05-18 20:08:54 +00:00
|
|
|
}
|
2017-06-29 09:19:37 +00:00
|
|
|
|
|
|
|
if (historyObserver) {
|
|
|
|
historyObserver.observe(chat.find("#chan-" + data.id + " .show-more").get(0));
|
|
|
|
}
|
2017-05-18 20:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function renderChannelMessages(data) {
|
2017-08-28 20:06:28 +00:00
|
|
|
const documentFragment = buildChannelMessages($(document.createDocumentFragment()), data.id, data.type, data.messages);
|
2017-05-18 20:08:54 +00:00
|
|
|
const channel = chat.find("#chan-" + data.id + " .messages").append(documentFragment);
|
|
|
|
|
2017-10-01 09:00:59 +00:00
|
|
|
renderUnreadMarker($(templates.unread_marker()), data.firstUnread, channel);
|
|
|
|
}
|
2017-09-12 12:40:26 +00:00
|
|
|
|
2017-10-01 09:00:59 +00:00
|
|
|
function renderUnreadMarker(template, firstUnread, channel) {
|
|
|
|
if (firstUnread > 0) {
|
|
|
|
let first = channel.find("#msg-" + firstUnread);
|
2017-05-18 20:08:54 +00:00
|
|
|
|
|
|
|
if (!first.length) {
|
2017-10-01 09:00:59 +00:00
|
|
|
template.data("unread-id", firstUnread);
|
2017-09-12 12:40:26 +00:00
|
|
|
channel.prepend(template);
|
2017-05-18 20:08:54 +00:00
|
|
|
} else {
|
2017-09-12 12:40:26 +00:00
|
|
|
const parent = first.parent();
|
|
|
|
|
|
|
|
if (parent.hasClass("condensed")) {
|
|
|
|
first = parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
first.before(template);
|
2017-05-18 20:08:54 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-09-12 12:40:26 +00:00
|
|
|
channel.append(template);
|
2017-05-18 20:08:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderChannelUsers(data) {
|
2018-03-04 20:03:11 +00:00
|
|
|
const users = chat.find("#chan-" + data.id).find(".userlist");
|
2017-07-10 10:56:37 +00:00
|
|
|
const nicks = data.users
|
2017-07-10 10:56:58 +00:00
|
|
|
.concat() // Make a copy of the user list, sort is applied in-place
|
2017-07-10 10:56:37 +00:00
|
|
|
.sort((a, b) => b.lastMessage - a.lastMessage)
|
|
|
|
.map((a) => a.nick);
|
2017-05-18 20:08:54 +00:00
|
|
|
|
2017-12-18 04:06:58 +00:00
|
|
|
// Before re-rendering the list of names, there might have been an entry
|
|
|
|
// marked as active (i.e. that was highlighted by keyboard navigation).
|
|
|
|
// It is `undefined` if there was none.
|
2018-03-04 20:03:11 +00:00
|
|
|
const previouslyActive = users.find(".active");
|
2017-12-18 04:06:58 +00:00
|
|
|
|
2017-05-18 20:08:54 +00:00
|
|
|
const search = users
|
|
|
|
.find(".search")
|
2018-01-30 09:38:33 +00:00
|
|
|
.prop("placeholder", nicks.length + " " + (nicks.length === 1 ? "user" : "users"));
|
2017-05-18 20:08:54 +00:00
|
|
|
|
|
|
|
users
|
|
|
|
.data("nicks", nicks)
|
|
|
|
.find(".names-original")
|
|
|
|
.html(templates.user(data));
|
|
|
|
|
|
|
|
// Refresh user search
|
|
|
|
if (search.val().length) {
|
|
|
|
search.trigger("input");
|
|
|
|
}
|
2017-12-16 18:58:56 +00:00
|
|
|
|
2017-12-18 04:06:58 +00:00
|
|
|
// If a nick was highlighted before re-rendering the lists, re-highlight it in
|
|
|
|
// the newly-rendered list.
|
2018-03-04 20:03:11 +00:00
|
|
|
if (previouslyActive.length > 0) {
|
2017-12-18 04:06:58 +00:00
|
|
|
// We need to un-highlight everything first because triggering `input` with
|
|
|
|
// a value highlights the first entry.
|
|
|
|
users.find(".user").removeClass("active");
|
2018-04-28 08:19:49 +00:00
|
|
|
users.find(`.user[data-name="${previouslyActive.attr("data-name")}"]`).addClass("active");
|
2017-12-18 04:06:58 +00:00
|
|
|
}
|
|
|
|
|
2017-12-16 18:58:56 +00:00
|
|
|
return users;
|
2017-05-18 20:08:54 +00:00
|
|
|
}
|
|
|
|
|
2017-08-28 20:06:28 +00:00
|
|
|
function renderNetworks(data, singleNetwork) {
|
2018-03-15 09:18:06 +00:00
|
|
|
const collapsed = new Set(JSON.parse(storage.get("thelounge.networks.collapsed")));
|
|
|
|
|
2017-05-18 20:08:54 +00:00
|
|
|
sidebar.find(".empty").hide();
|
|
|
|
sidebar.find(".networks").append(
|
|
|
|
templates.network({
|
2017-11-15 06:35:15 +00:00
|
|
|
networks: data.networks,
|
2018-05-29 10:07:18 +00:00
|
|
|
}).trim()
|
2017-05-18 20:08:54 +00:00
|
|
|
);
|
|
|
|
|
2017-12-21 20:40:50 +00:00
|
|
|
// Add keyboard handlers to the "Join a channel…" form inputs/button
|
2018-05-02 13:57:19 +00:00
|
|
|
JoinChannel.handleKeybinds(data.networks);
|
2017-12-21 20:40:50 +00:00
|
|
|
|
2017-08-28 20:06:28 +00:00
|
|
|
let newChannels;
|
2017-05-18 20:08:54 +00:00
|
|
|
const channels = $.map(data.networks, function(n) {
|
2018-06-08 09:11:00 +00:00
|
|
|
if (collapsed.has(n.uuid)) {
|
|
|
|
collapseNetwork($(`.network[data-uuid="${n.uuid}"] button.collapse-network`));
|
|
|
|
}
|
|
|
|
|
2017-05-18 20:08:54 +00:00
|
|
|
return n.channels;
|
|
|
|
});
|
2017-08-28 20:06:28 +00:00
|
|
|
|
|
|
|
if (!singleNetwork && utils.lastMessageId > -1) {
|
|
|
|
newChannels = [];
|
|
|
|
|
|
|
|
channels.forEach((channel) => {
|
|
|
|
const chan = $("#chan-" + channel.id);
|
|
|
|
|
|
|
|
if (chan.length > 0) {
|
|
|
|
if (chan.data("type") === "channel") {
|
|
|
|
chan
|
|
|
|
.data("needsNamesRefresh", true)
|
|
|
|
.find(".header .topic")
|
|
|
|
.html(helpers_parse(channel.topic))
|
2018-01-30 09:38:33 +00:00
|
|
|
.prop("title", channel.topic);
|
2017-08-28 20:06:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (channel.messages.length > 0) {
|
|
|
|
const container = chan.find(".messages");
|
|
|
|
buildChannelMessages(container, channel.id, channel.type, channel.messages);
|
|
|
|
|
2017-10-01 09:00:59 +00:00
|
|
|
const unreadMarker = container.find(".unread-marker").data("unread-id", 0);
|
|
|
|
renderUnreadMarker(unreadMarker, channel.firstUnread, container);
|
|
|
|
|
2017-08-28 20:06:28 +00:00
|
|
|
if (container.find(".msg").length >= 100) {
|
|
|
|
container.find(".show-more").addClass("show");
|
|
|
|
}
|
|
|
|
|
2018-02-23 16:21:42 +00:00
|
|
|
container.parent().trigger("keepToBottom");
|
2017-08-28 20:06:28 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
newChannels.push(channel);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
newChannels = channels;
|
|
|
|
}
|
|
|
|
|
2018-02-06 08:57:16 +00:00
|
|
|
if (newChannels.length > 0) {
|
|
|
|
chat.append(
|
|
|
|
templates.chat({
|
|
|
|
channels: newChannels,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
newChannels.forEach((channel) => {
|
|
|
|
renderChannel(channel);
|
|
|
|
|
|
|
|
if (channel.type === "channel") {
|
|
|
|
chat.find("#chan-" + channel.id).data("needsNamesRefresh", true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-05-18 20:08:54 +00:00
|
|
|
|
|
|
|
utils.confirmExit();
|
|
|
|
sorting();
|
|
|
|
|
|
|
|
if (sidebar.find(".highlight").length) {
|
|
|
|
utils.toggleNotificationMarkers(true);
|
|
|
|
}
|
|
|
|
}
|
2017-06-29 09:19:37 +00:00
|
|
|
|
2017-11-23 14:23:32 +00:00
|
|
|
function trimMessageInChannel(channel, messageLimit) {
|
|
|
|
const messages = channel.find(".messages .msg").slice(0, -messageLimit);
|
|
|
|
|
|
|
|
if (messages.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
messages.remove();
|
|
|
|
|
|
|
|
channel.find(".show-more").addClass("show");
|
|
|
|
|
|
|
|
// Remove date-separators that would otherwise 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-06-29 09:19:37 +00:00
|
|
|
function loadMoreHistory(entries) {
|
|
|
|
entries.forEach((entry) => {
|
|
|
|
if (!entry.isIntersecting) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-15 03:02:35 +00:00
|
|
|
const target = $(entry.target).find("button");
|
2017-06-29 09:19:37 +00:00
|
|
|
|
2018-01-30 09:38:33 +00:00
|
|
|
if (target.prop("disabled")) {
|
2017-06-29 09:19:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-30 09:38:33 +00:00
|
|
|
target.trigger("click");
|
2017-06-29 09:19:37 +00:00
|
|
|
});
|
|
|
|
}
|
2018-02-20 08:42:50 +00:00
|
|
|
|
2018-03-15 09:18:06 +00:00
|
|
|
sidebar.on("click", "button.collapse-network", (e) => collapseNetwork($(e.target)));
|
|
|
|
|
|
|
|
function collapseNetwork(target) {
|
|
|
|
const collapseButton = target.closest(".collapse-network");
|
|
|
|
const networks = new Set(JSON.parse(storage.get("thelounge.networks.collapsed")));
|
|
|
|
const networkuuid = collapseButton.closest(".network").data("uuid");
|
2018-02-20 08:42:50 +00:00
|
|
|
|
2018-03-14 12:07:05 +00:00
|
|
|
if (collapseButton.closest(".network").find(".active").length > 0) {
|
2018-03-26 08:46:01 +00:00
|
|
|
collapseButton.closest(".lobby").trigger("click", {
|
|
|
|
keepSidebarOpen: true,
|
|
|
|
});
|
2018-03-14 12:07:05 +00:00
|
|
|
}
|
|
|
|
|
2018-03-04 19:02:27 +00:00
|
|
|
collapseButton.closest(".network").toggleClass("collapsed");
|
2018-02-20 08:42:50 +00:00
|
|
|
|
|
|
|
if (collapseButton.attr("aria-expanded") === "true") {
|
|
|
|
collapseButton.attr("aria-expanded", false);
|
|
|
|
collapseButton.attr("aria-label", "Expand");
|
2018-03-15 09:18:06 +00:00
|
|
|
networks.add(networkuuid);
|
2018-02-20 08:42:50 +00:00
|
|
|
} else {
|
|
|
|
collapseButton.attr("aria-expanded", true);
|
|
|
|
collapseButton.attr("aria-label", "Collapse");
|
2018-03-15 09:18:06 +00:00
|
|
|
networks.delete(networkuuid);
|
2018-02-20 08:42:50 +00:00
|
|
|
}
|
2018-03-04 19:02:27 +00:00
|
|
|
|
2018-03-15 09:18:06 +00:00
|
|
|
storage.set("thelounge.networks.collapsed", JSON.stringify([...networks]));
|
2018-03-04 19:02:27 +00:00
|
|
|
return false;
|
2018-03-15 09:18:06 +00:00
|
|
|
}
|