2017-05-18 20:08:54 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const $ = require("jquery");
|
|
|
|
const socket = require("../socket");
|
|
|
|
const render = require("../render");
|
2017-09-06 19:03:56 +00:00
|
|
|
const utils = require("../utils");
|
2017-09-05 16:07:29 +00:00
|
|
|
const options = require("../options");
|
|
|
|
const helpers_roundBadgeNumber = require("../libs/handlebars/roundBadgeNumber");
|
2017-09-28 08:58:43 +00:00
|
|
|
const cleanIrcMessage = require("../libs/handlebars/ircmessageparser/cleanIrcMessage");
|
2017-09-28 08:53:32 +00:00
|
|
|
const webpush = require("../webpush");
|
2017-05-18 20:08:54 +00:00
|
|
|
const chat = $("#chat");
|
2017-09-05 16:07:29 +00:00
|
|
|
const sidebar = $("#sidebar");
|
|
|
|
|
|
|
|
let pop;
|
|
|
|
try {
|
|
|
|
pop = new Audio();
|
|
|
|
pop.src = "audio/pop.ogg";
|
|
|
|
} catch (e) {
|
|
|
|
pop = {
|
2017-11-15 06:35:15 +00:00
|
|
|
play: $.noop,
|
2017-09-05 16:07:29 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-05-18 20:08:54 +00:00
|
|
|
socket.on("msg", function(data) {
|
2017-09-06 19:03:56 +00:00
|
|
|
// We set a maximum timeout of 2 seconds so that messages don't take too long to appear.
|
|
|
|
utils.requestIdleCallback(() => processReceivedMessage(data), 2000);
|
2017-08-25 12:30:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function processReceivedMessage(data) {
|
2017-07-28 08:53:36 +00:00
|
|
|
let targetId = data.chan;
|
|
|
|
let target = "#chan-" + targetId;
|
|
|
|
let channel = chat.find(target);
|
|
|
|
let sidebarTarget = sidebar.find("[data-target='" + target + "']");
|
|
|
|
|
|
|
|
// Display received notices and errors in currently active channel.
|
|
|
|
// Reloading the page will put them back into the lobby window.
|
|
|
|
if (data.msg.showInActive) {
|
|
|
|
const activeOnNetwork = sidebarTarget.parent().find(".active");
|
|
|
|
|
|
|
|
// We only want to put errors/notices in active channel if they arrive on the same network
|
|
|
|
if (activeOnNetwork.length > 0) {
|
|
|
|
targetId = data.chan = activeOnNetwork.data("id");
|
2017-08-13 09:23:51 +00:00
|
|
|
|
2017-07-28 08:53:36 +00:00
|
|
|
target = "#chan-" + targetId;
|
|
|
|
channel = chat.find(target);
|
|
|
|
sidebarTarget = sidebar.find("[data-target='" + target + "']");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const container = channel.find(".messages");
|
2017-08-13 09:23:51 +00:00
|
|
|
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,
|
2018-02-03 08:43:39 +00:00
|
|
|
channel.data("type"),
|
2017-08-24 13:50:47 +00:00
|
|
|
data.msg
|
2017-06-22 20:08:36 +00:00
|
|
|
);
|
|
|
|
|
2017-11-23 17:33:19 +00:00
|
|
|
if (activeChannelId === targetId) {
|
|
|
|
container.trigger("keepToBottom");
|
|
|
|
}
|
2017-09-05 16:07:29 +00:00
|
|
|
|
|
|
|
notifyMessage(targetId, channel, data);
|
2017-05-18 20:08:54 +00:00
|
|
|
|
2018-01-11 11:33:36 +00:00
|
|
|
const lastVisible = container.find("div:visible").last();
|
2017-05-18 20:08:54 +00:00
|
|
|
if (data.msg.self
|
|
|
|
|| lastVisible.hasClass("unread-marker")
|
|
|
|
|| (lastVisible.hasClass("date-marker")
|
|
|
|
&& lastVisible.prev().hasClass("unread-marker"))) {
|
|
|
|
container
|
|
|
|
.find(".unread-marker")
|
2017-09-12 12:40:26 +00:00
|
|
|
.data("unread-id", 0)
|
2017-05-18 20:08:54 +00:00
|
|
|
.appendTo(container);
|
|
|
|
}
|
2017-08-13 09:23:51 +00:00
|
|
|
|
2017-10-17 12:42:43 +00:00
|
|
|
// Clear unread/highlight counter if self-message
|
|
|
|
if (data.msg.self) {
|
2017-07-28 08:53:36 +00:00
|
|
|
sidebarTarget.find(".badge").removeClass("highlight").empty();
|
2017-10-17 12:42:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-23 14:23:32 +00:00
|
|
|
let messageLimit = 0;
|
2017-08-13 09:23:51 +00:00
|
|
|
|
2017-11-23 14:23:32 +00:00
|
|
|
if (activeChannelId !== targetId) {
|
|
|
|
// If message arrives in non active channel, keep only 100 messages
|
|
|
|
messageLimit = 100;
|
|
|
|
} else if (container.isScrollBottom()) {
|
|
|
|
// If message arrives in active channel, keep 500 messages if scroll is currently at the bottom
|
|
|
|
messageLimit = 500;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (messageLimit > 0) {
|
|
|
|
render.trimMessageInChannel(channel, messageLimit);
|
2017-08-13 09:23:51 +00:00
|
|
|
}
|
2017-09-04 16:52:02 +00:00
|
|
|
|
2017-07-28 08:53:36 +00:00
|
|
|
if ((data.msg.type === "message" || data.msg.type === "action") && channel.hasClass("channel")) {
|
2017-09-04 16:52:02 +00:00
|
|
|
const nicks = channel.find(".users").data("nicks");
|
|
|
|
if (nicks) {
|
2017-11-21 11:54:12 +00:00
|
|
|
const find = nicks.indexOf(data.msg.from.nick);
|
2017-09-04 16:52:02 +00:00
|
|
|
if (find !== -1) {
|
|
|
|
nicks.splice(find, 1);
|
2017-11-21 11:54:12 +00:00
|
|
|
nicks.unshift(data.msg.from.nick);
|
2017-09-04 16:52:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-25 12:30:47 +00:00
|
|
|
}
|
2017-09-05 16:07:29 +00:00
|
|
|
|
|
|
|
function notifyMessage(targetId, channel, msg) {
|
|
|
|
const unread = msg.unread;
|
|
|
|
msg = msg.msg;
|
|
|
|
|
|
|
|
if (msg.self) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const button = sidebar.find(".chan[data-id='" + targetId + "']");
|
|
|
|
if (msg.highlight || (options.notifyAllMessages && msg.type === "message")) {
|
|
|
|
if (!document.hasFocus() || !channel.hasClass("active")) {
|
|
|
|
if (options.notification) {
|
|
|
|
try {
|
|
|
|
pop.play();
|
|
|
|
} catch (exception) {
|
|
|
|
// On mobile, sounds can not be played without user interaction.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
utils.toggleNotificationMarkers(true);
|
|
|
|
|
|
|
|
if (options.desktopNotifications && Notification.permission === "granted") {
|
|
|
|
let title;
|
|
|
|
let body;
|
|
|
|
|
|
|
|
if (msg.type === "invite") {
|
|
|
|
title = "New channel invite:";
|
2017-11-21 11:54:12 +00:00
|
|
|
body = msg.from.nick + " invited you to " + msg.channel;
|
2017-09-05 16:07:29 +00:00
|
|
|
} else {
|
2017-11-21 11:54:12 +00:00
|
|
|
title = msg.from.nick;
|
2017-09-05 16:07:29 +00:00
|
|
|
if (!button.hasClass("query")) {
|
2017-12-28 12:15:28 +00:00
|
|
|
title += " (" + button.attr("aria-label").trim() + ")";
|
2017-09-05 16:07:29 +00:00
|
|
|
}
|
|
|
|
if (msg.type === "message") {
|
|
|
|
title += " says:";
|
|
|
|
}
|
2017-09-28 08:58:43 +00:00
|
|
|
body = cleanIrcMessage(msg.text);
|
2017-09-05 16:07:29 +00:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:47:13 +00:00
|
|
|
const timestamp = Date.parse(msg.time);
|
|
|
|
|
2017-09-05 16:07:29 +00:00
|
|
|
try {
|
2017-09-28 08:53:32 +00:00
|
|
|
if (webpush.hasServiceWorker) {
|
|
|
|
navigator.serviceWorker.ready.then((registration) => {
|
|
|
|
registration.active.postMessage({
|
|
|
|
type: "notification",
|
|
|
|
chanId: targetId,
|
2018-01-09 16:47:13 +00:00
|
|
|
timestamp: timestamp,
|
2017-09-28 08:53:32 +00:00
|
|
|
title: title,
|
|
|
|
body: body,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
const notify = new Notification(title, {
|
|
|
|
tag: `chan-${targetId}`,
|
|
|
|
badge: "img/logo-64.png",
|
|
|
|
icon: "img/touch-icon-192x192.png",
|
|
|
|
body: body,
|
2018-01-09 16:47:13 +00:00
|
|
|
timestamp: timestamp,
|
2017-09-28 08:53:32 +00:00
|
|
|
});
|
|
|
|
notify.addEventListener("click", function() {
|
|
|
|
window.focus();
|
2018-01-30 09:38:33 +00:00
|
|
|
button.trigger("click");
|
2017-09-28 08:53:32 +00:00
|
|
|
this.close();
|
|
|
|
});
|
|
|
|
}
|
2017-09-05 16:07:29 +00:00
|
|
|
} catch (exception) {
|
|
|
|
// `new Notification(...)` is not supported and should be silenced.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!unread || button.hasClass("active")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const badge = button.find(".badge").html(helpers_roundBadgeNumber(unread));
|
|
|
|
|
|
|
|
if (msg.highlight) {
|
|
|
|
badge.addClass("highlight");
|
|
|
|
}
|
|
|
|
}
|