2019-07-11 10:48:52 +02:00
|
|
|
/**
|
|
|
|
* @module converse-notification
|
2020-01-26 16:21:20 +01:00
|
|
|
* @copyright 2020, the Converse.js contributors
|
|
|
|
* @license Mozilla Public License (MPLv2)
|
2019-07-11 10:48:52 +02:00
|
|
|
*/
|
2020-11-05 14:44:22 +01:00
|
|
|
import Favico from 'favico.js-slevomat';
|
2019-11-06 11:01:34 +01:00
|
|
|
import log from "@converse/headless/log";
|
2020-09-10 07:33:00 +02:00
|
|
|
import { __ } from './i18n';
|
2020-05-18 10:54:37 +02:00
|
|
|
import { _converse, api, converse } from "@converse/headless/converse-core";
|
2016-02-29 21:58:01 +01:00
|
|
|
|
2020-09-10 10:09:30 +02:00
|
|
|
const { Strophe } = converse.env;
|
2019-10-10 14:43:03 +02:00
|
|
|
const u = converse.env.utils;
|
2016-02-29 21:58:01 +01:00
|
|
|
|
2020-06-03 09:57:14 +02:00
|
|
|
const supports_html5_notification = "Notification" in window;
|
|
|
|
|
2020-11-05 14:44:22 +01:00
|
|
|
converse.env.Favico = Favico;
|
|
|
|
let favicon;
|
|
|
|
|
|
|
|
function updateUnreadFavicon () {
|
|
|
|
if (api.settings.get('update_title')) {
|
|
|
|
favicon = favicon ?? new converse.env.Favico({type: 'circle', animation: 'pop'});
|
|
|
|
const chats = _converse.chatboxes.models;
|
|
|
|
const num_unread = chats.reduce((acc, chat) => (acc + (chat.get('num_unread') || 0)), 0);
|
|
|
|
favicon.badge(num_unread);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-29 13:55:51 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
converse.plugins.add('converse-notification', {
|
2016-12-20 11:42:20 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
dependencies: ["converse-chatboxes"],
|
2016-03-13 17:22:42 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
initialize () {
|
|
|
|
/* The initialize function gets called as soon as the plugin is
|
|
|
|
* loaded by converse.js's plugin machinery.
|
|
|
|
*/
|
2016-03-01 09:58:36 +01:00
|
|
|
|
2020-06-03 09:17:13 +02:00
|
|
|
api.settings.extend({
|
2020-11-05 14:44:22 +01:00
|
|
|
update_title: true,
|
2018-10-23 03:41:38 +02:00
|
|
|
notify_all_room_messages: false,
|
|
|
|
show_desktop_notifications: true,
|
2019-08-03 21:49:44 +02:00
|
|
|
show_chat_state_notifications: false,
|
2018-10-23 03:41:38 +02:00
|
|
|
chatstate_notification_blacklist: [],
|
|
|
|
// ^ a list of JIDs to ignore concerning chat state notifications
|
|
|
|
play_sounds: true,
|
2020-03-31 13:15:57 +02:00
|
|
|
sounds_path: api.settings.get("assets_path")+'/sounds/',
|
2018-11-04 08:49:00 +01:00
|
|
|
notification_icon: 'logo/conversejs-filled.svg',
|
2020-08-26 00:02:53 +02:00
|
|
|
notification_delay: 5000,
|
|
|
|
notify_nicknames_without_references: false
|
2018-10-23 03:41:38 +02:00
|
|
|
});
|
2016-03-08 11:42:52 +01:00
|
|
|
|
2020-09-10 07:33:00 +02:00
|
|
|
/**
|
|
|
|
* Is this a group message for which we should notify the user?
|
|
|
|
* @private
|
|
|
|
* @method _converse#shouldNotifyOfGroupMessage
|
2020-09-10 10:09:30 +02:00
|
|
|
* @param { MUCMessageAttributes } attrs
|
2020-09-10 07:33:00 +02:00
|
|
|
*/
|
2020-09-10 10:09:30 +02:00
|
|
|
_converse.shouldNotifyOfGroupMessage = function (attrs) {
|
|
|
|
if (!attrs?.body) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const jid = attrs.from;
|
|
|
|
const room_jid = attrs.from_muc;
|
2020-08-26 00:02:53 +02:00
|
|
|
const notify_all = api.settings.get('notify_all_room_messages');
|
2018-10-23 03:41:38 +02:00
|
|
|
const room = _converse.chatboxes.get(room_jid);
|
2020-08-26 00:02:53 +02:00
|
|
|
const resource = Strophe.getResourceFromJid(jid);
|
|
|
|
const sender = resource && Strophe.unescapeNode(resource) || '';
|
|
|
|
let is_mentioned = false;
|
|
|
|
const nick = room.get('nick');
|
|
|
|
|
|
|
|
if (api.settings.get('notify_nicknames_without_references')) {
|
2020-09-10 10:09:30 +02:00
|
|
|
is_mentioned = (new RegExp(`\\b${nick}\\b`)).test(attrs.body);
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
2020-08-26 00:02:53 +02:00
|
|
|
|
2020-09-10 10:09:30 +02:00
|
|
|
const is_referenced = attrs.references.map(r => r.value).includes(nick);
|
2020-08-26 00:02:53 +02:00
|
|
|
const is_not_mine = sender !== nick;
|
|
|
|
const should_notify_user = notify_all === true
|
|
|
|
|| (Array.isArray(notify_all) && notify_all.includes(room_jid))
|
|
|
|
|| is_referenced
|
|
|
|
|| is_mentioned;
|
|
|
|
return is_not_mine && !!should_notify_user;
|
2018-10-23 03:41:38 +02:00
|
|
|
};
|
|
|
|
|
2020-09-10 10:09:30 +02:00
|
|
|
/**
|
|
|
|
* Given parsed attributes for a message stanza, get the related
|
|
|
|
* chatbox and check whether it's hidden.
|
|
|
|
* @private
|
|
|
|
* @method _converse#isMessageToHiddenChat
|
|
|
|
* @param { MUCMessageAttributes } attrs
|
|
|
|
*/
|
|
|
|
_converse.isMessageToHiddenChat = function (attrs) {
|
|
|
|
return _converse.chatboxes.get(attrs.from)?.isHidden() ?? false;
|
|
|
|
};
|
2018-10-23 03:41:38 +02:00
|
|
|
|
2020-09-10 10:09:30 +02:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @method _converse#shouldNotifyOfMessage
|
|
|
|
* @param { MessageData|MUCMessageData } data
|
|
|
|
*/
|
|
|
|
_converse.shouldNotifyOfMessage = function (data) {
|
|
|
|
const { attrs, stanza } = data;
|
|
|
|
if (!attrs || stanza.querySelector('forwarded') !== null) {
|
2018-10-23 03:41:38 +02:00
|
|
|
return false;
|
2020-09-10 10:09:30 +02:00
|
|
|
}
|
|
|
|
if (attrs['type'] === 'groupchat') {
|
|
|
|
return _converse.shouldNotifyOfGroupMessage(attrs);
|
|
|
|
} else if (attrs.is_headline) {
|
2018-10-23 03:41:38 +02:00
|
|
|
// We want to show notifications for headline messages.
|
2020-09-10 10:09:30 +02:00
|
|
|
return _converse.isMessageToHiddenChat(attrs);
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
2020-09-10 10:09:30 +02:00
|
|
|
const is_me = Strophe.getBareJidFromJid(attrs.from) === _converse.bare_jid;
|
|
|
|
return !u.isOnlyChatStateNotification(stanza) &&
|
|
|
|
!u.isOnlyMessageDeliveryReceipt(stanza) &&
|
2018-10-23 03:41:38 +02:00
|
|
|
!is_me &&
|
2020-09-10 10:09:30 +02:00
|
|
|
(api.settings.get('show_desktop_notifications') === 'all' || _converse.isMessageToHiddenChat(attrs));
|
2018-10-23 03:41:38 +02:00
|
|
|
};
|
2016-03-01 09:58:36 +01:00
|
|
|
|
2018-10-22 18:32:57 +02:00
|
|
|
|
2019-10-10 14:43:03 +02:00
|
|
|
/**
|
|
|
|
* Plays a notification sound
|
|
|
|
* @private
|
|
|
|
* @method _converse#playSoundNotification
|
|
|
|
*/
|
2018-10-23 03:41:38 +02:00
|
|
|
_converse.playSoundNotification = function () {
|
2020-06-03 09:57:14 +02:00
|
|
|
if (api.settings.get('play_sounds') && window.Audio !== undefined) {
|
|
|
|
const audioOgg = new Audio(api.settings.get('sounds_path')+"msg_received.ogg");
|
2018-10-23 12:34:13 +02:00
|
|
|
const canPlayOgg = audioOgg.canPlayType('audio/ogg');
|
|
|
|
if (canPlayOgg === 'probably') {
|
2018-10-26 14:39:10 +02:00
|
|
|
return audioOgg.play();
|
2018-10-23 12:34:13 +02:00
|
|
|
}
|
2020-06-03 09:57:14 +02:00
|
|
|
const audioMp3 = new Audio(api.settings.get('sounds_path')+"msg_received.mp3");
|
2018-10-26 14:39:10 +02:00
|
|
|
const canPlayMp3 = audioMp3.canPlayType('audio/mp3');
|
2018-10-23 12:34:13 +02:00
|
|
|
if (canPlayMp3 === 'probably') {
|
|
|
|
audioMp3.play();
|
|
|
|
} else if (canPlayOgg === 'maybe') {
|
|
|
|
audioOgg.play();
|
|
|
|
} else if (canPlayMp3 === 'maybe') {
|
|
|
|
audioMp3.play();
|
2016-03-01 09:58:36 +01:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
};
|
2016-02-29 21:58:01 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
_converse.areDesktopNotificationsEnabled = function () {
|
2020-06-03 09:57:14 +02:00
|
|
|
return supports_html5_notification &&
|
|
|
|
api.settings.get('show_desktop_notifications') &&
|
2018-10-23 03:41:38 +02:00
|
|
|
Notification.permission === "granted";
|
|
|
|
};
|
2016-03-08 11:42:52 +01:00
|
|
|
|
2019-10-10 14:43:03 +02:00
|
|
|
/**
|
|
|
|
* Shows an HTML5 Notification with the passed in message
|
|
|
|
* @private
|
|
|
|
* @method _converse#showMessageNotification
|
2020-09-10 10:09:30 +02:00
|
|
|
* @param { MessageData|MUCMessageData } data
|
2019-10-10 14:43:03 +02:00
|
|
|
*/
|
2020-09-10 10:09:30 +02:00
|
|
|
_converse.showMessageNotification = function (data) {
|
|
|
|
const { attrs } = data;
|
|
|
|
if (attrs.is_error) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-06 13:51:19 +01:00
|
|
|
if (!_converse.areDesktopNotificationsEnabled()) {
|
|
|
|
return;
|
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
let title, roster_item;
|
2020-09-10 10:09:30 +02:00
|
|
|
const full_from_jid = attrs.from,
|
2018-10-23 03:41:38 +02:00
|
|
|
from_jid = Strophe.getBareJidFromJid(full_from_jid);
|
2020-09-10 10:09:30 +02:00
|
|
|
if (attrs.type === 'headline') {
|
2020-03-31 13:15:57 +02:00
|
|
|
if (!from_jid.includes('@') || api.settings.get("allow_non_roster_messaging")) {
|
2017-09-24 20:36:39 +02:00
|
|
|
title = __("Notification from %1$s", from_jid);
|
2016-03-22 09:45:54 +01:00
|
|
|
} else {
|
2018-09-10 16:21:30 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-10-10 14:43:03 +02:00
|
|
|
} else if (!from_jid.includes('@')) {
|
2018-10-23 03:41:38 +02:00
|
|
|
// workaround for Prosody which doesn't give type "headline"
|
|
|
|
title = __("Notification from %1$s", from_jid);
|
2020-09-10 10:09:30 +02:00
|
|
|
} else if (attrs.type === 'groupchat') {
|
2018-10-23 03:41:38 +02:00
|
|
|
title = __("%1$s says", Strophe.getResourceFromJid(full_from_jid));
|
|
|
|
} else {
|
2019-07-29 10:19:05 +02:00
|
|
|
if (_converse.roster === undefined) {
|
2019-11-06 11:01:34 +01:00
|
|
|
log.error("Could not send notification, because roster is undefined");
|
2016-03-18 10:10:14 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
roster_item = _converse.roster.get(from_jid);
|
2019-07-29 10:19:05 +02:00
|
|
|
if (roster_item !== undefined) {
|
2018-10-23 03:41:38 +02:00
|
|
|
title = __("%1$s says", roster_item.getDisplayName());
|
|
|
|
} else {
|
2020-03-31 13:15:57 +02:00
|
|
|
if (api.settings.get("allow_non_roster_messaging")) {
|
2018-10-23 03:41:38 +02:00
|
|
|
title = __("%1$s says", from_jid);
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2016-02-29 22:59:56 +01:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
2020-09-10 10:09:30 +02:00
|
|
|
|
|
|
|
const body = attrs.is_encrypted ? __('Encrypted message received') : attrs.body;
|
2018-10-23 03:41:38 +02:00
|
|
|
if (!body) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const n = new Notification(title, {
|
|
|
|
'body': body,
|
|
|
|
'lang': _converse.locale,
|
2020-06-03 09:57:14 +02:00
|
|
|
'icon': api.settings.get('notification_icon'),
|
2018-11-04 08:49:00 +01:00
|
|
|
'requireInteraction': !_converse.notification_delay
|
2018-10-23 03:41:38 +02:00
|
|
|
});
|
2020-06-03 09:57:14 +02:00
|
|
|
if (api.settings.get('notification_delay')) {
|
|
|
|
setTimeout(n.close.bind(n), api.settings.get('notification_delay'));
|
2018-11-04 08:49:00 +01:00
|
|
|
}
|
2019-12-29 13:55:47 +01:00
|
|
|
n.onclick = function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
window.focus();
|
|
|
|
const chat = _converse.chatboxes.get(from_jid);
|
|
|
|
chat.maybeShow(true);
|
|
|
|
}
|
|
|
|
n.onclick.bind(_converse);
|
2018-10-23 03:41:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
_converse.showChatStateNotification = function (contact) {
|
|
|
|
/* Creates an HTML5 Notification to inform of a change in a
|
|
|
|
* contact's chat state.
|
|
|
|
*/
|
2019-10-10 14:43:03 +02:00
|
|
|
if (_converse.chatstate_notification_blacklist.includes(contact.jid)) {
|
2018-10-23 03:41:38 +02:00
|
|
|
// Don't notify if the user is being ignored.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const chat_state = contact.chat_status;
|
|
|
|
let message = null;
|
|
|
|
if (chat_state === 'offline') {
|
|
|
|
message = __('has gone offline');
|
|
|
|
} else if (chat_state === 'away') {
|
|
|
|
message = __('has gone away');
|
|
|
|
} else if ((chat_state === 'dnd')) {
|
|
|
|
message = __('is busy');
|
|
|
|
} else if (chat_state === 'online') {
|
|
|
|
message = __('has come online');
|
|
|
|
}
|
|
|
|
if (message === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const n = new Notification(contact.getDisplayName(), {
|
|
|
|
body: message,
|
|
|
|
lang: _converse.locale,
|
|
|
|
icon: _converse.notification_icon
|
|
|
|
});
|
|
|
|
setTimeout(n.close.bind(n), 5000);
|
|
|
|
};
|
|
|
|
|
|
|
|
_converse.showContactRequestNotification = function (contact) {
|
|
|
|
const n = new Notification(contact.getDisplayName(), {
|
|
|
|
body: __('wants to be your contact'),
|
|
|
|
lang: _converse.locale,
|
|
|
|
icon: _converse.notification_icon
|
|
|
|
});
|
|
|
|
setTimeout(n.close.bind(n), 5000);
|
|
|
|
};
|
2016-02-29 22:59:56 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
_converse.showFeedbackNotification = function (data) {
|
|
|
|
if (data.klass === 'error' || data.klass === 'warn') {
|
|
|
|
const n = new Notification(data.subject, {
|
|
|
|
body: data.message,
|
2017-04-19 14:18:30 +02:00
|
|
|
lang: _converse.locale,
|
2017-02-15 20:12:45 +01:00
|
|
|
icon: _converse.notification_icon
|
2016-03-08 12:01:23 +01:00
|
|
|
});
|
|
|
|
setTimeout(n.close.bind(n), 5000);
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
};
|
2016-03-08 12:01:23 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
_converse.handleChatStateNotification = function (contact) {
|
|
|
|
/* Event handler for on('contactPresenceChanged').
|
|
|
|
* Will show an HTML5 notification to indicate that the chat
|
|
|
|
* status has changed.
|
|
|
|
*/
|
2020-06-03 09:57:14 +02:00
|
|
|
if (_converse.areDesktopNotificationsEnabled() && api.settings.get('show_chat_state_notifications')) {
|
2018-10-23 03:41:38 +02:00
|
|
|
_converse.showChatStateNotification(contact);
|
|
|
|
}
|
|
|
|
};
|
2016-03-31 14:23:05 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
_converse.handleMessageNotification = function (data) {
|
|
|
|
/* Event handler for the on('message') event. Will call methods
|
|
|
|
* to play sounds and show HTML5 notifications.
|
|
|
|
*/
|
2020-09-10 10:09:30 +02:00
|
|
|
if (!_converse.shouldNotifyOfMessage(data)) {
|
2018-10-23 03:41:38 +02:00
|
|
|
return false;
|
|
|
|
}
|
2019-03-29 15:47:23 +01:00
|
|
|
/**
|
|
|
|
* Triggered when a notification (sound or HTML5 notification) for a new
|
|
|
|
* message has will be made.
|
|
|
|
* @event _converse#messageNotification
|
2020-09-10 10:09:30 +02:00
|
|
|
* @type { MessageData|MUCMessageData}
|
2019-03-29 15:47:23 +01:00
|
|
|
* @example _converse.api.listen.on('messageNotification', stanza => { ... });
|
|
|
|
*/
|
2020-09-10 10:09:30 +02:00
|
|
|
api.trigger('messageNotification', data);
|
2018-10-23 03:41:38 +02:00
|
|
|
_converse.playSoundNotification();
|
2020-09-10 10:09:30 +02:00
|
|
|
_converse.showMessageNotification(data);
|
2018-10-23 03:41:38 +02:00
|
|
|
};
|
2016-03-01 09:58:36 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
_converse.handleContactRequestNotification = function (contact) {
|
|
|
|
if (_converse.areDesktopNotificationsEnabled(true)) {
|
|
|
|
_converse.showContactRequestNotification(contact);
|
|
|
|
}
|
|
|
|
};
|
2016-03-01 09:58:36 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
_converse.handleFeedback = function (data) {
|
|
|
|
if (_converse.areDesktopNotificationsEnabled(true)) {
|
|
|
|
_converse.showFeedbackNotification(data);
|
|
|
|
}
|
|
|
|
};
|
2016-03-08 12:01:23 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
_converse.requestPermission = function () {
|
2020-06-03 09:57:14 +02:00
|
|
|
if (supports_html5_notification && !['denied', 'granted'].includes(Notification.permission)) {
|
2018-10-23 03:41:38 +02:00
|
|
|
// Ask user to enable HTML5 notifications
|
|
|
|
Notification.requestPermission();
|
|
|
|
}
|
|
|
|
};
|
2016-03-31 14:23:05 +02:00
|
|
|
|
2020-11-05 14:44:22 +01:00
|
|
|
/************************ BEGIN Event Handlers ************************/
|
|
|
|
|
|
|
|
api.listen.on('clearSession', () => (favicon = null)); // Needed for tests
|
|
|
|
|
|
|
|
api.waitUntil('chatBoxesInitialized').then(
|
|
|
|
() => _converse.chatboxes.on('change:num_unread', updateUnreadFavicon));
|
|
|
|
|
2020-03-31 13:15:57 +02:00
|
|
|
api.listen.on('pluginsInitialized', function () {
|
2018-10-23 03:41:38 +02:00
|
|
|
// We only register event handlers after all plugins are
|
|
|
|
// registered, because other plugins might override some of our
|
|
|
|
// handlers.
|
2020-03-31 13:15:57 +02:00
|
|
|
api.listen.on('contactRequest', _converse.handleContactRequestNotification);
|
|
|
|
api.listen.on('contactPresenceChanged', _converse.handleChatStateNotification);
|
|
|
|
api.listen.on('message', _converse.handleMessageNotification);
|
|
|
|
api.listen.on('feedback', _converse.handleFeedback);
|
|
|
|
api.listen.on('connected', _converse.requestPermission);
|
2018-10-23 03:41:38 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|