thelounge/client/js/socket-events/msg.js

171 lines
4.5 KiB
JavaScript
Raw Normal View History

2017-05-18 20:08:54 +00:00
"use strict";
const $ = require("jquery");
const socket = require("../socket");
const options = require("../options");
const cleanIrcMessage = require("../libs/handlebars/ircmessageparser/cleanIrcMessage");
const webpush = require("../webpush");
const {vueApp} = require("../vue");
const store = require("../store").default;
let pop;
try {
pop = new Audio();
2018-07-17 06:14:58 +00:00
pop.src = "audio/pop.wav";
} catch (e) {
pop = {
play: $.noop,
};
}
2017-05-18 20:08:54 +00:00
socket.on("msg", function(data) {
const receivingChannel = store.getters.findChannel(data.chan);
if (!receivingChannel) {
return;
2018-07-11 09:27:44 +00:00
}
let channel = receivingChannel.channel;
const isActiveChannel =
store.state.activeChannel && store.state.activeChannel.channel === channel;
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.
// We only want to put errors/notices in active channel if they arrive on the same network
2019-07-17 09:33:59 +00:00
if (
data.msg.showInActive &&
store.state.activeChannel &&
store.state.activeChannel.network === receivingChannel.network
2019-07-17 09:33:59 +00:00
) {
channel = store.state.activeChannel.channel;
2017-07-28 08:53:36 +00:00
if (data.chan === channel.id) {
// If active channel is the intended channel for this message,
// remove the showInActive flag
data.msg.showInActive = false;
} else {
data.chan = channel.id;
}
} else if (!isActiveChannel) {
// 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 (typeof data.highlight !== "undefined") {
channel.highlight = data.highlight;
}
if (typeof data.unread !== "undefined") {
channel.unread = data.unread;
}
2017-07-28 08:53:36 +00:00
}
2018-07-12 19:34:05 +00:00
channel.messages.push(data.msg);
2017-05-18 20:08:54 +00:00
2018-07-10 08:06:08 +00:00
if (data.msg.self) {
channel.firstUnread = data.msg.id;
2018-07-12 19:34:05 +00:00
} else {
notifyMessage(data.chan, channel, store.state.activeChannel, data.msg);
2017-05-18 20:08:54 +00:00
}
2018-07-09 19:06:06 +00:00
let messageLimit = 0;
if (!isActiveChannel) {
2018-07-09 19:06:06 +00:00
// If message arrives in non active channel, keep only 100 messages
messageLimit = 100;
} else if (channel.scrolledToBottom) {
2018-07-09 19:06:06 +00:00
// If message arrives in active channel, keep 500 messages if scroll is currently at the bottom
messageLimit = 500;
2018-07-09 19:06:06 +00:00
}
2018-07-13 07:55:39 +00:00
if (messageLimit > 0 && channel.messages.length > messageLimit) {
2018-07-12 19:34:05 +00:00
channel.messages.splice(0, channel.messages.length - messageLimit);
2018-07-12 20:39:23 +00:00
channel.moreHistoryAvailable = true;
2018-07-09 19:06:06 +00:00
}
2018-07-12 19:34:05 +00: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-07-08 14:57:02 +00:00
if (user) {
2019-07-17 09:33:59 +00:00
user.lastMessage = new Date(data.msg.time).getTime() || Date.now();
}
}
if (data.msg.self || data.msg.highlight) {
2019-10-17 16:56:44 +00:00
vueApp.synchronizeNotifiedState();
}
2018-07-11 09:27:44 +00:00
});
2018-07-12 19:34:05 +00:00
function notifyMessage(targetId, channel, activeChannel, msg) {
2018-07-09 19:06:06 +00:00
const button = $("#sidebar .chan[data-id='" + targetId + "']");
Offer optional syncing of client settings Write synced settings to localstorage. move settings and webpush init to init.js stub for server sending clientsettings get very basic setting sync working Also update client.config.clientSettings on settings:set Full setting sync with mandatory and excluded sync options Actually check client preferences. Further settings restructuring. Refactor options.js make storage act in a sane manner. Add new parameter to applySetting Do not sync if the setting is stored as a result of syncing General clean up, commenting and restructing. sync from server on checking "sync" offer initial sync Better deal with DOM being ready and instances of inital sync showing Don't try to disable autocompletion when not enabled. Restructure option.js to seperate functions from settings. More consistency in naming options vs settings Switch processSetting and applySetting names reflecting their functionality better. move options init back to configuration. simplify how settings are synced around. move options init after template building. Remove unneeded hasOwnProperty Use global for #theme and only apply theme in applySetting Return when no server side clientsettings excist. Autocompletion options to options.settings Make nocss param in url work again. Actually filter out empty highlight values. Clarify alwaysSync comment. Remove manual step for initial sync change attr to prop in options.js replace unbind with off in autocompletion.js Do not sync settings when the lounge is set to public. fix eslint error Fix merge error Do not show sync warning after page refresh when sync is enabled Move setting sync label in actual label. Improve server setting sync handling performance and failure potential. Don't give impression that the desktop notificiation is off when the browser permission is denied. Refine showing and hiding of notification warnings. rename all setting socket events to singular setting. add experimental note and icon to settingsync. fix css linting error
2017-12-11 19:01:15 +00:00
if (msg.highlight || (options.settings.notifyAllMessages && msg.type === "message")) {
2018-07-12 19:34:05 +00:00
if (!document.hasFocus() || !activeChannel || activeChannel.channel !== channel) {
Offer optional syncing of client settings Write synced settings to localstorage. move settings and webpush init to init.js stub for server sending clientsettings get very basic setting sync working Also update client.config.clientSettings on settings:set Full setting sync with mandatory and excluded sync options Actually check client preferences. Further settings restructuring. Refactor options.js make storage act in a sane manner. Add new parameter to applySetting Do not sync if the setting is stored as a result of syncing General clean up, commenting and restructing. sync from server on checking "sync" offer initial sync Better deal with DOM being ready and instances of inital sync showing Don't try to disable autocompletion when not enabled. Restructure option.js to seperate functions from settings. More consistency in naming options vs settings Switch processSetting and applySetting names reflecting their functionality better. move options init back to configuration. simplify how settings are synced around. move options init after template building. Remove unneeded hasOwnProperty Use global for #theme and only apply theme in applySetting Return when no server side clientsettings excist. Autocompletion options to options.settings Make nocss param in url work again. Actually filter out empty highlight values. Clarify alwaysSync comment. Remove manual step for initial sync change attr to prop in options.js replace unbind with off in autocompletion.js Do not sync settings when the lounge is set to public. fix eslint error Fix merge error Do not show sync warning after page refresh when sync is enabled Move setting sync label in actual label. Improve server setting sync handling performance and failure potential. Don't give impression that the desktop notificiation is off when the browser permission is denied. Refine showing and hiding of notification warnings. rename all setting socket events to singular setting. add experimental note and icon to settingsync. fix css linting error
2017-12-11 19:01:15 +00:00
if (options.settings.notification) {
try {
pop.play();
} catch (exception) {
// On mobile, sounds can not be played without user interaction.
}
}
2019-07-17 09:33:59 +00:00
if (
options.settings.desktopNotifications &&
"Notification" in window &&
Notification.permission === "granted"
) {
let title;
let body;
if (msg.type === "invite") {
title = "New channel invite:";
body = msg.from.nick + " invited you to " + msg.channel;
} else {
title = msg.from.nick;
2018-07-12 19:34:05 +00:00
if (channel.type !== "query") {
title += ` (${channel.name})`;
}
if (msg.type === "message") {
title += " says:";
}
body = cleanIrcMessage(msg.text);
}
const timestamp = Date.parse(msg.time);
try {
if (webpush.hasServiceWorker) {
navigator.serviceWorker.ready.then((registration) => {
registration.active.postMessage({
type: "notification",
chanId: targetId,
timestamp: timestamp,
title: title,
body: body,
});
});
} else {
const notify = new Notification(title, {
tag: `chan-${targetId}`,
badge: "img/icon-alerted-black-transparent-bg-72x72px.png",
icon: "img/icon-alerted-grey-bg-192x192px.png",
body: body,
timestamp: timestamp,
});
notify.addEventListener("click", function() {
window.focus();
button.trigger("click");
this.close();
});
}
} catch (exception) {
// `new Notification(...)` is not supported and should be silenced.
}
}
}
}
}