2017-05-18 20:08:54 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const $ = require("jquery");
|
|
|
|
const socket = require("../socket");
|
2017-09-06 19:03:56 +00:00
|
|
|
const utils = require("../utils");
|
2017-09-05 16:07:29 +00:00
|
|
|
const options = require("../options");
|
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");
|
2018-07-06 18:15:15 +00:00
|
|
|
const {vueApp, findChannel} = require("../vue");
|
2017-09-05 16:07:29 +00:00
|
|
|
|
|
|
|
let pop;
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2017-09-05 16:07:29 +00:00
|
|
|
try {
|
|
|
|
pop = new Audio();
|
2018-07-17 06:14:58 +00:00
|
|
|
pop.src = "audio/pop.wav";
|
2017-09-05 16:07:29 +00:00
|
|
|
} 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) {
|
2018-07-09 13:42:03 +00:00
|
|
|
if (utils.lastMessageId < data.msg.id) {
|
|
|
|
utils.lastMessageId = data.msg.id;
|
|
|
|
}
|
|
|
|
|
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;
|
2018-07-09 19:06:06 +00:00
|
|
|
let channelContainer = $("#chat").find(target);
|
2018-07-06 18:15:15 +00:00
|
|
|
let channel = findChannel(data.chan);
|
|
|
|
|
2018-07-08 09:09:33 +00:00
|
|
|
channel.channel.highlight = data.highlight;
|
|
|
|
channel.channel.unread = data.unread;
|
2018-07-06 18:15:15 +00:00
|
|
|
|
2018-07-08 09:09:33 +00:00
|
|
|
if (data.msg.self || data.msg.highlight) {
|
2018-07-06 18:15:15 +00:00
|
|
|
utils.updateTitle();
|
|
|
|
}
|
2017-07-28 08:53:36 +00:00
|
|
|
|
|
|
|
// Display received notices and errors in currently active channel.
|
|
|
|
// Reloading the page will put them back into the lobby window.
|
2018-07-06 18:15:15 +00:00
|
|
|
// We only want to put errors/notices in active channel if they arrive on the same network
|
|
|
|
if (data.msg.showInActive && vueApp.activeChannel && vueApp.activeChannel.network === channel.network) {
|
|
|
|
channel = vueApp.activeChannel;
|
2017-07-28 08:53:36 +00:00
|
|
|
|
2018-07-06 18:15:15 +00:00
|
|
|
targetId = data.chan = vueApp.activeChannel.channel.id;
|
2017-08-13 09:23:51 +00:00
|
|
|
|
2018-07-06 18:15:15 +00:00
|
|
|
target = "#chan-" + targetId;
|
2018-07-09 19:06:06 +00:00
|
|
|
channelContainer = $("#chat").find(target);
|
2017-07-28 08:53:36 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 18:15:15 +00:00
|
|
|
const scrollContainer = channelContainer.find(".chat");
|
|
|
|
const container = channelContainer.find(".messages");
|
2017-05-18 20:08:54 +00:00
|
|
|
|
2018-03-11 18:17:57 +00:00
|
|
|
if (data.msg.type === "channel_list" || data.msg.type === "ban_list" || data.msg.type === "ignore_list") {
|
2017-05-18 20:08:54 +00:00
|
|
|
$(container).empty();
|
|
|
|
}
|
|
|
|
|
2018-07-08 12:18:17 +00:00
|
|
|
channel.channel.messages.push(data.msg);
|
2017-06-22 20:08:36 +00:00
|
|
|
|
2018-07-06 18:15:15 +00:00
|
|
|
notifyMessage(targetId, channelContainer, data);
|
2017-05-18 20:08:54 +00:00
|
|
|
|
2018-03-15 15:52:04 +00:00
|
|
|
let shouldMoveMarker = data.msg.self;
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2018-03-15 15:52:04 +00:00
|
|
|
if (!shouldMoveMarker) {
|
|
|
|
const lastChild = container.children().last();
|
|
|
|
|
|
|
|
// If last element is hidden (e.g. hidden status messages) check the element before it.
|
|
|
|
// If it's unread marker or date marker, then move unread marker to the bottom
|
|
|
|
// so that it isn't displayed as the last element in chat.
|
|
|
|
// display properly is checked instead of using `:hidden` selector because it doesn't work in non-active channels.
|
|
|
|
if (lastChild.css("display") === "none") {
|
|
|
|
const prevChild = lastChild.prev();
|
|
|
|
|
|
|
|
shouldMoveMarker =
|
|
|
|
prevChild.hasClass("unread-marker") ||
|
|
|
|
(prevChild.hasClass("date-marker") && prevChild.prev().hasClass("unread-marker"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldMoveMarker) {
|
2017-05-18 20:08:54 +00:00
|
|
|
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
|
|
|
|
2018-07-09 19:06:06 +00:00
|
|
|
let messageLimit = 0;
|
|
|
|
|
|
|
|
if (!vueApp.activeChannel || vueApp.activeChannel.channel !== channel.channel) {
|
|
|
|
// If message arrives in non active channel, keep only 100 messages
|
|
|
|
messageLimit = 100;
|
|
|
|
} else {
|
|
|
|
const el = scrollContainer.get(0);
|
|
|
|
|
|
|
|
// If message arrives in active channel, keep 500 messages if scroll is currently at the bottom
|
|
|
|
if (el.scrollHeight - el.scrollTop - el.offsetHeight <= 30) {
|
|
|
|
messageLimit = 500;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (messageLimit > 0) {
|
|
|
|
channel.channel.messages.splice(0, channel.channel.messages.length - messageLimit);
|
|
|
|
}
|
|
|
|
|
2018-07-08 14:57:02 +00:00
|
|
|
if ((data.msg.type === "message" || data.msg.type === "action") && channel.channel.type === "channel") {
|
|
|
|
const user = channel.channel.users.find((u) => u.nick === data.msg.from.nick);
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2018-07-08 14:57:02 +00:00
|
|
|
if (user) {
|
|
|
|
user.lastMessage = (new Date(data.msg.time)).getTime() || Date.now();
|
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) {
|
2018-06-11 11:29:57 +00:00
|
|
|
const serverUnread = msg.unread;
|
|
|
|
const serverHighlight = msg.highlight;
|
|
|
|
|
2017-09-05 16:07:29 +00:00
|
|
|
msg = msg.msg;
|
|
|
|
|
|
|
|
if (msg.self) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-09 19:06:06 +00:00
|
|
|
const button = $("#sidebar .chan[data-id='" + targetId + "']");
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2017-12-11 19:01:15 +00:00
|
|
|
if (msg.highlight || (options.settings.notifyAllMessages && msg.type === "message")) {
|
2017-09-05 16:07:29 +00:00
|
|
|
if (!document.hasFocus() || !channel.hasClass("active")) {
|
2017-12-11 19:01:15 +00:00
|
|
|
if (options.settings.notification) {
|
2017-09-05 16:07:29 +00:00
|
|
|
try {
|
|
|
|
pop.play();
|
|
|
|
} catch (exception) {
|
|
|
|
// On mobile, sounds can not be played without user interaction.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
utils.toggleNotificationMarkers(true);
|
|
|
|
|
2017-12-11 19:01:15 +00:00
|
|
|
if (options.settings.desktopNotifications && ("Notification" in window) && Notification.permission === "granted") {
|
2017-09-05 16:07:29 +00:00
|
|
|
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;
|
2018-02-20 07:28:04 +00:00
|
|
|
|
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
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2017-09-05 16:07:29 +00:00
|
|
|
if (msg.type === "message") {
|
|
|
|
title += " says:";
|
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
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}`,
|
2018-01-28 07:20:24 +00:00
|
|
|
badge: "img/icon-alerted-black-transparent-bg-72x72px.png",
|
|
|
|
icon: "img/icon-alerted-grey-bg-192x192px.png",
|
2017-09-28 08:53:32 +00:00
|
|
|
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.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|