2017-05-18 16:08:54 -04:00
|
|
|
"use strict";
|
|
|
|
|
2019-11-16 12:24:03 -05:00
|
|
|
import socket from "../socket";
|
|
|
|
import cleanIrcMessage from "../helpers/ircmessageparser/cleanIrcMessage";
|
|
|
|
import store from "../store";
|
|
|
|
import {switchToChannel} from "../router";
|
2017-09-05 12:07:29 -04:00
|
|
|
|
|
|
|
let pop;
|
2018-02-20 02:28:04 -05:00
|
|
|
|
2017-09-05 12:07:29 -04:00
|
|
|
try {
|
|
|
|
pop = new Audio();
|
2018-07-17 02:14:58 -04:00
|
|
|
pop.src = "audio/pop.wav";
|
2017-09-05 12:07:29 -04:00
|
|
|
} catch (e) {
|
|
|
|
pop = {
|
2019-11-04 05:46:18 -05:00
|
|
|
play() {},
|
2017-09-05 12:07:29 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-05-18 16:08:54 -04:00
|
|
|
socket.on("msg", function(data) {
|
2019-11-03 09:59:43 -05:00
|
|
|
const receivingChannel = store.getters.findChannel(data.chan);
|
2018-07-06 14:15:15 -04:00
|
|
|
|
2018-09-21 11:04:41 -04:00
|
|
|
if (!receivingChannel) {
|
|
|
|
return;
|
2018-07-11 05:27:44 -04:00
|
|
|
}
|
|
|
|
|
2018-09-21 11:04:41 -04:00
|
|
|
let channel = receivingChannel.channel;
|
2019-11-02 15:40:59 -04:00
|
|
|
const isActiveChannel =
|
|
|
|
store.state.activeChannel && store.state.activeChannel.channel === channel;
|
2018-09-21 11:04:41 -04:00
|
|
|
|
2017-07-28 04:53:36 -04:00
|
|
|
// Display received notices and errors in currently active channel.
|
|
|
|
// Reloading the page will put them back into the lobby window.
|
2020-01-05 11:30:56 -05:00
|
|
|
if (data.msg.showInActive) {
|
|
|
|
// We only want to put errors/notices in active channel if they arrive on the same network
|
|
|
|
if (
|
|
|
|
store.state.activeChannel &&
|
|
|
|
store.state.activeChannel.network === receivingChannel.network
|
|
|
|
) {
|
|
|
|
channel = store.state.activeChannel.channel;
|
|
|
|
|
|
|
|
if (data.chan === channel.id) {
|
|
|
|
// If active channel is the intended channel for this message,
|
|
|
|
// remove the showInActive flag
|
|
|
|
delete data.msg.showInActive;
|
|
|
|
} else {
|
|
|
|
data.chan = channel.id;
|
|
|
|
}
|
2019-10-22 13:22:29 -04:00
|
|
|
} else {
|
2020-01-05 11:30:56 -05:00
|
|
|
delete data.msg.showInActive;
|
2019-10-22 13:22:29 -04:00
|
|
|
}
|
2020-01-05 11:30:56 -05:00
|
|
|
}
|
2018-10-11 07:46:48 -04:00
|
|
|
|
2020-01-05 11:30:56 -05:00
|
|
|
// Do not set unread counter for channel if it is currently active on this client
|
|
|
|
// It may increase on the server before it processes channel open event from this client
|
|
|
|
if (!isActiveChannel) {
|
2018-10-11 07:46:48 -04:00
|
|
|
if (typeof data.highlight !== "undefined") {
|
|
|
|
channel.highlight = data.highlight;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof data.unread !== "undefined") {
|
|
|
|
channel.unread = data.unread;
|
|
|
|
}
|
2017-07-28 04:53:36 -04:00
|
|
|
}
|
|
|
|
|
2018-07-12 15:34:05 -04:00
|
|
|
channel.messages.push(data.msg);
|
2017-05-18 16:08:54 -04:00
|
|
|
|
2018-07-10 04:06:08 -04:00
|
|
|
if (data.msg.self) {
|
2018-07-17 04:41:12 -04:00
|
|
|
channel.firstUnread = data.msg.id;
|
2018-07-12 15:34:05 -04:00
|
|
|
} else {
|
2019-11-02 15:40:59 -04:00
|
|
|
notifyMessage(data.chan, channel, store.state.activeChannel, data.msg);
|
2017-05-18 16:08:54 -04:00
|
|
|
}
|
2017-08-13 05:23:51 -04:00
|
|
|
|
2018-07-09 15:06:06 -04:00
|
|
|
let messageLimit = 0;
|
|
|
|
|
2018-09-21 11:04:41 -04:00
|
|
|
if (!isActiveChannel) {
|
2018-07-09 15:06:06 -04:00
|
|
|
// If message arrives in non active channel, keep only 100 messages
|
|
|
|
messageLimit = 100;
|
2018-07-13 16:40:20 -04:00
|
|
|
} else if (channel.scrolledToBottom) {
|
2018-07-09 15:06:06 -04:00
|
|
|
// If message arrives in active channel, keep 500 messages if scroll is currently at the bottom
|
2018-07-13 16:40:20 -04:00
|
|
|
messageLimit = 500;
|
2018-07-09 15:06:06 -04:00
|
|
|
}
|
|
|
|
|
2018-07-13 03:55:39 -04:00
|
|
|
if (messageLimit > 0 && channel.messages.length > messageLimit) {
|
2018-07-12 15:34:05 -04:00
|
|
|
channel.messages.splice(0, channel.messages.length - messageLimit);
|
2018-07-12 16:39:23 -04:00
|
|
|
channel.moreHistoryAvailable = true;
|
2018-07-09 15:06:06 -04:00
|
|
|
}
|
|
|
|
|
2018-07-12 15:34:05 -04:00
|
|
|
if ((data.msg.type === "message" || data.msg.type === "action") && channel.type === "channel") {
|
|
|
|
const user = channel.users.find((u) => u.nick === data.msg.from.nick);
|
2018-02-20 02:28:04 -05:00
|
|
|
|
2018-07-08 10:57:02 -04:00
|
|
|
if (user) {
|
2019-07-17 05:33:59 -04:00
|
|
|
user.lastMessage = new Date(data.msg.time).getTime() || Date.now();
|
2017-09-04 12:52:02 -04:00
|
|
|
}
|
|
|
|
}
|
2018-07-11 05:27:44 -04:00
|
|
|
});
|
2017-09-05 12:07:29 -04:00
|
|
|
|
2018-07-12 15:34:05 -04:00
|
|
|
function notifyMessage(targetId, channel, activeChannel, msg) {
|
2019-11-03 13:05:19 -05:00
|
|
|
if (msg.highlight || (store.state.settings.notifyAllMessages && msg.type === "message")) {
|
2018-07-12 15:34:05 -04:00
|
|
|
if (!document.hasFocus() || !activeChannel || activeChannel.channel !== channel) {
|
2019-11-03 13:05:19 -05:00
|
|
|
if (store.state.settings.notification) {
|
2017-09-05 12:07:29 -04:00
|
|
|
try {
|
|
|
|
pop.play();
|
|
|
|
} catch (exception) {
|
|
|
|
// On mobile, sounds can not be played without user interaction.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-17 05:33:59 -04:00
|
|
|
if (
|
2019-11-03 13:05:19 -05:00
|
|
|
store.state.settings.desktopNotifications &&
|
2019-07-17 05:33:59 -04:00
|
|
|
"Notification" in window &&
|
|
|
|
Notification.permission === "granted"
|
|
|
|
) {
|
2017-09-05 12:07:29 -04:00
|
|
|
let title;
|
|
|
|
let body;
|
|
|
|
|
|
|
|
if (msg.type === "invite") {
|
|
|
|
title = "New channel invite:";
|
2017-11-21 06:54:12 -05:00
|
|
|
body = msg.from.nick + " invited you to " + msg.channel;
|
2017-09-05 12:07:29 -04:00
|
|
|
} else {
|
2017-11-21 06:54:12 -05:00
|
|
|
title = msg.from.nick;
|
2018-02-20 02:28:04 -05:00
|
|
|
|
2018-07-12 15:34:05 -04:00
|
|
|
if (channel.type !== "query") {
|
|
|
|
title += ` (${channel.name})`;
|
2017-09-05 12:07:29 -04:00
|
|
|
}
|
2018-02-20 02:28:04 -05:00
|
|
|
|
2017-09-05 12:07:29 -04:00
|
|
|
if (msg.type === "message") {
|
|
|
|
title += " says:";
|
|
|
|
}
|
2018-02-20 02:28:04 -05:00
|
|
|
|
2017-09-28 04:58:43 -04:00
|
|
|
body = cleanIrcMessage(msg.text);
|
2017-09-05 12:07:29 -04:00
|
|
|
}
|
|
|
|
|
2018-01-09 11:47:13 -05:00
|
|
|
const timestamp = Date.parse(msg.time);
|
|
|
|
|
2017-09-05 12:07:29 -04:00
|
|
|
try {
|
2019-11-16 12:24:03 -05:00
|
|
|
if (store.state.hasServiceWorker) {
|
2017-09-28 04:53:32 -04:00
|
|
|
navigator.serviceWorker.ready.then((registration) => {
|
|
|
|
registration.active.postMessage({
|
|
|
|
type: "notification",
|
|
|
|
chanId: targetId,
|
2018-01-09 11:47:13 -05:00
|
|
|
timestamp: timestamp,
|
2017-09-28 04:53:32 -04:00
|
|
|
title: title,
|
|
|
|
body: body,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
const notify = new Notification(title, {
|
|
|
|
tag: `chan-${targetId}`,
|
2018-01-28 02:20:24 -05:00
|
|
|
badge: "img/icon-alerted-black-transparent-bg-72x72px.png",
|
|
|
|
icon: "img/icon-alerted-grey-bg-192x192px.png",
|
2017-09-28 04:53:32 -04:00
|
|
|
body: body,
|
2018-01-09 11:47:13 -05:00
|
|
|
timestamp: timestamp,
|
2017-09-28 04:53:32 -04:00
|
|
|
});
|
|
|
|
notify.addEventListener("click", function() {
|
|
|
|
this.close();
|
2019-11-04 05:46:18 -05:00
|
|
|
window.focus();
|
|
|
|
|
2019-11-07 04:29:04 -05:00
|
|
|
const channelTarget = store.getters.findChannel(targetId);
|
2019-11-04 05:46:18 -05:00
|
|
|
|
|
|
|
if (channelTarget) {
|
2019-11-11 14:18:55 -05:00
|
|
|
switchToChannel(channelTarget);
|
2019-11-04 05:46:18 -05:00
|
|
|
}
|
2017-09-28 04:53:32 -04:00
|
|
|
});
|
|
|
|
}
|
2017-09-05 12:07:29 -04:00
|
|
|
} catch (exception) {
|
|
|
|
// `new Notification(...)` is not supported and should be silenced.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|