2016-02-29 21:58:01 +01:00
|
|
|
// Converse.js (A browser based XMPP chat client)
|
|
|
|
// http://conversejs.org
|
|
|
|
//
|
|
|
|
// Copyright (c) 2012-2016, Jan-Carel Brand <jc@opkode.com>
|
|
|
|
// Licensed under the Mozilla Public License (MPLv2)
|
|
|
|
//
|
|
|
|
/*global define */
|
|
|
|
|
|
|
|
(function (root, factory) {
|
|
|
|
define("converse-notification", ["converse-core", "converse-api"], factory);
|
|
|
|
}(this, function (converse, converse_api) {
|
|
|
|
"use strict";
|
2016-03-01 09:58:36 +01:00
|
|
|
var $ = converse_api.env.jQuery,
|
|
|
|
utils = converse_api.env.utils,
|
|
|
|
Strophe = converse_api.env.Strophe,
|
|
|
|
_ = converse_api.env._;
|
2016-02-29 21:58:01 +01:00
|
|
|
// For translations
|
|
|
|
var __ = utils.__.bind(converse);
|
|
|
|
var ___ = utils.___;
|
|
|
|
|
|
|
|
|
2016-06-09 11:01:54 +02:00
|
|
|
converse_api.plugins.add('converse-notification', {
|
2016-02-29 21:58:01 +01:00
|
|
|
|
|
|
|
initialize: function () {
|
|
|
|
/* The initialize function gets called as soon as the plugin is
|
|
|
|
* loaded by converse.js's plugin machinery.
|
|
|
|
*/
|
|
|
|
var converse = this.converse;
|
2016-06-16 10:56:32 +02:00
|
|
|
converse.supports_html5_notification = "Notification" in window;
|
2016-03-13 17:22:42 +01:00
|
|
|
|
|
|
|
this.updateSettings({
|
2016-06-16 10:56:32 +02:00
|
|
|
notify_all_room_messages: false,
|
2016-03-01 10:43:44 +01:00
|
|
|
show_desktop_notifications: true,
|
2016-03-18 10:10:14 +01:00
|
|
|
chatstate_notification_blacklist: [],
|
|
|
|
// ^ a list of JIDs to ignore concerning chat state notifications
|
2016-03-01 09:58:36 +01:00
|
|
|
play_sounds: false,
|
|
|
|
sounds_path: '/sounds/',
|
2016-07-02 13:40:51 +02:00
|
|
|
notification_icon: '/logo/conversejs128.png'
|
2016-03-13 17:22:42 +01:00
|
|
|
});
|
|
|
|
|
2016-03-01 09:58:36 +01:00
|
|
|
converse.isOnlyChatStateNotification = function ($msg) {
|
|
|
|
// See XEP-0085 Chat State Notification
|
|
|
|
return (
|
|
|
|
$msg.find('body').length === 0 && (
|
|
|
|
$msg.find(converse.ACTIVE).length !== 0 ||
|
|
|
|
$msg.find(converse.COMPOSING).length !== 0 ||
|
|
|
|
$msg.find(converse.INACTIVE).length !== 0 ||
|
|
|
|
$msg.find(converse.PAUSED).length !== 0 ||
|
|
|
|
$msg.find(converse.GONE).length !== 0
|
|
|
|
)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2016-03-08 11:42:52 +01:00
|
|
|
converse.shouldNotifyOfGroupMessage = function ($message) {
|
|
|
|
/* Is this a group message worthy of notification?
|
|
|
|
*/
|
2016-06-16 10:56:32 +02:00
|
|
|
var notify_all = converse.notify_all_room_messages,
|
|
|
|
jid = $message.attr('from'),
|
2016-03-08 11:42:52 +01:00
|
|
|
resource = Strophe.getResourceFromJid(jid),
|
2016-06-16 17:24:58 +02:00
|
|
|
room_jid = Strophe.getBareJidFromJid(jid),
|
2016-03-08 11:42:52 +01:00
|
|
|
sender = resource && Strophe.unescapeNode(resource) || '';
|
|
|
|
if (sender === '' || $message.find('delay').length > 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-16 17:24:58 +02:00
|
|
|
var room = converse.chatboxes.get(room_jid);
|
2016-06-17 11:33:09 +02:00
|
|
|
var $body = $message.children('body');
|
|
|
|
if (!$body.length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var mentioned = (new RegExp("\\b"+room.get('nick')+"\\b")).test($body.text());
|
2016-06-16 17:24:58 +02:00
|
|
|
notify_all = notify_all === true || (_.isArray(notify_all) && _.contains(notify_all, room_jid));
|
2016-06-16 10:56:32 +02:00
|
|
|
if (sender === room.get('nick') || (!notify_all && !mentioned)) {
|
2016-03-08 11:42:52 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2016-03-28 12:49:52 +02:00
|
|
|
converse.shouldNotifyOfMessage = function (message) {
|
2016-03-08 11:42:52 +01:00
|
|
|
/* Is this a message worthy of notification?
|
|
|
|
*/
|
2016-05-30 20:19:10 +02:00
|
|
|
if (utils.isOTRMessage(message)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-28 12:49:52 +02:00
|
|
|
var $message = $(message),
|
|
|
|
$forwarded = $message.find('forwarded');
|
2016-03-01 09:58:36 +01:00
|
|
|
if ($forwarded.length) {
|
|
|
|
return false;
|
2016-03-28 12:49:52 +02:00
|
|
|
} else if ($message.attr('type') === 'groupchat') {
|
2016-03-08 11:42:52 +01:00
|
|
|
return converse.shouldNotifyOfGroupMessage($message);
|
2016-03-28 12:49:52 +02:00
|
|
|
} else if (utils.isHeadlineMessage(message)) {
|
2016-03-22 09:45:54 +01:00
|
|
|
// We want to show notifications for headline messages.
|
|
|
|
return true;
|
|
|
|
}
|
2016-03-01 09:58:36 +01:00
|
|
|
var is_me = Strophe.getBareJidFromJid($message.attr('from')) === converse.bare_jid;
|
|
|
|
return !converse.isOnlyChatStateNotification($message) && !is_me;
|
|
|
|
};
|
|
|
|
|
|
|
|
converse.playSoundNotification = function ($message) {
|
|
|
|
/* Plays a sound to notify that a new message was recieved.
|
|
|
|
*/
|
|
|
|
// XXX Eventually this can be refactored to use Notification's sound
|
|
|
|
// feature, but no browser currently supports it.
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/notification/sound
|
|
|
|
var audio;
|
|
|
|
if (converse.play_sounds && typeof Audio !== "undefined") {
|
|
|
|
audio = new Audio(converse.sounds_path+"msg_received.ogg");
|
|
|
|
if (audio.canPlayType('/audio/ogg')) {
|
|
|
|
audio.play();
|
|
|
|
} else {
|
|
|
|
audio = new Audio(converse.sounds_path+"msg_received.mp3");
|
|
|
|
audio.play();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2016-02-29 21:58:01 +01:00
|
|
|
|
2016-06-17 11:30:47 +02:00
|
|
|
converse.areDesktopNotificationsEnabled = function (ignore_hidden) {
|
2016-06-16 10:56:32 +02:00
|
|
|
var enabled = converse.supports_html5_notification &&
|
2016-03-08 12:01:23 +01:00
|
|
|
converse.show_desktop_notifications &&
|
|
|
|
Notification.permission === "granted";
|
2016-06-17 11:30:47 +02:00
|
|
|
if (ignore_hidden) {
|
2016-03-08 12:01:23 +01:00
|
|
|
return enabled;
|
|
|
|
} else {
|
2016-06-16 18:14:22 +02:00
|
|
|
return enabled && converse.windowState === 'hidden';
|
2016-03-08 12:01:23 +01:00
|
|
|
}
|
2016-03-08 11:42:52 +01:00
|
|
|
};
|
|
|
|
|
2016-03-08 09:44:45 +01:00
|
|
|
converse.showMessageNotification = function ($message) {
|
|
|
|
/* Shows an HTML5 Notification to indicate that a new chat
|
|
|
|
* message was received.
|
|
|
|
*/
|
2016-03-22 09:45:54 +01:00
|
|
|
var n, title, contact_jid, roster_item,
|
|
|
|
from_jid = $message.attr('from');
|
|
|
|
if ($message.attr('type') === 'headline' || from_jid.indexOf('@') === -1) {
|
|
|
|
// XXX: 2nd check is workaround for Prosody which doesn't
|
|
|
|
// give type "headline"
|
|
|
|
title = __(___("Notification from %1$s"), from_jid);
|
|
|
|
} else {
|
2016-04-28 16:51:50 +02:00
|
|
|
if ($message.attr('type') === 'groupchat') {
|
|
|
|
title = __(___("%1$s says"), Strophe.getResourceFromJid(from_jid));
|
|
|
|
} else {
|
|
|
|
if (typeof converse.roster === 'undefined') {
|
|
|
|
converse.log("Could not send notification, because roster is undefined", "error");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
contact_jid = Strophe.getBareJidFromJid($message.attr('from'));
|
|
|
|
roster_item = converse.roster.get(contact_jid);
|
|
|
|
title = __(___("%1$s says"), roster_item.get('fullname'));
|
2016-03-22 09:45:54 +01:00
|
|
|
}
|
2016-03-21 11:40:47 +01:00
|
|
|
}
|
2016-03-22 09:45:54 +01:00
|
|
|
n = new Notification(title, {
|
2016-03-08 09:44:45 +01:00
|
|
|
body: $message.children('body').text(),
|
|
|
|
lang: converse.i18n.locale_data.converse[""].lang,
|
|
|
|
icon: converse.notification_icon
|
|
|
|
});
|
|
|
|
setTimeout(n.close.bind(n), 5000);
|
|
|
|
};
|
|
|
|
|
2016-03-08 11:42:52 +01:00
|
|
|
converse.showChatStateNotification = function (contact) {
|
|
|
|
/* Creates an HTML5 Notification to inform of a change in a
|
|
|
|
* contact's chat state.
|
2016-02-29 22:59:56 +01:00
|
|
|
*/
|
2016-03-22 08:15:48 +01:00
|
|
|
if (_.contains(converse.chatstate_notification_blacklist, contact.jid)) {
|
2016-03-18 10:10:14 +01:00
|
|
|
// Don't notify if the user is being ignored.
|
|
|
|
return;
|
|
|
|
}
|
2016-02-29 22:59:56 +01:00
|
|
|
var chat_state = contact.chat_status,
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
var n = new Notification(contact.fullname, {
|
|
|
|
body: message,
|
|
|
|
lang: converse.i18n.locale_data.converse[""].lang,
|
|
|
|
icon: 'logo/conversejs.png'
|
|
|
|
});
|
|
|
|
setTimeout(n.close.bind(n), 5000);
|
|
|
|
};
|
|
|
|
|
2016-03-08 12:01:23 +01:00
|
|
|
converse.showContactRequestNotification = function (contact) {
|
|
|
|
var n = new Notification(contact.fullname, {
|
2016-03-08 13:16:04 +01:00
|
|
|
body: __('wants to be your contact'),
|
2016-03-08 12:01:23 +01:00
|
|
|
lang: converse.i18n.locale_data.converse[""].lang,
|
|
|
|
icon: 'logo/conversejs.png'
|
|
|
|
});
|
|
|
|
setTimeout(n.close.bind(n), 5000);
|
|
|
|
};
|
|
|
|
|
2016-08-21 13:17:06 +02:00
|
|
|
converse.showFeedbackNotification = function (data) {
|
2016-08-23 13:35:16 +02:00
|
|
|
if (data.klass === 'error' || data.klass === 'warn') {
|
|
|
|
var n = new Notification(data.subject, {
|
|
|
|
body: data.message,
|
|
|
|
lang: converse.i18n.locale_data.converse[""].lang,
|
|
|
|
icon: 'logo/conversejs.png'
|
|
|
|
});
|
|
|
|
setTimeout(n.close.bind(n), 5000);
|
|
|
|
}
|
2016-03-31 14:23:05 +02:00
|
|
|
};
|
|
|
|
|
2016-03-08 11:42:52 +01:00
|
|
|
converse.handleChatStateNotification = function (evt, contact) {
|
|
|
|
/* Event handler for on('contactStatusChanged').
|
|
|
|
* Will show an HTML5 notification to indicate that the chat
|
|
|
|
* status has changed.
|
|
|
|
*/
|
|
|
|
if (converse.areDesktopNotificationsEnabled()) {
|
2016-03-08 13:11:35 +01:00
|
|
|
converse.showChatStateNotification(contact);
|
2016-03-08 11:42:52 +01:00
|
|
|
}
|
|
|
|
};
|
2016-03-01 09:58:36 +01:00
|
|
|
|
2016-03-08 11:42:52 +01:00
|
|
|
converse.handleMessageNotification = function (evt, message) {
|
2016-03-01 09:58:36 +01:00
|
|
|
/* Event handler for the on('message') event. Will call methods
|
|
|
|
* to play sounds and show HTML5 notifications.
|
|
|
|
*/
|
|
|
|
var $message = $(message);
|
2016-03-28 12:49:52 +02:00
|
|
|
if (!converse.shouldNotifyOfMessage(message)) {
|
2016-03-01 09:58:36 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
converse.playSoundNotification($message);
|
2016-03-08 11:42:52 +01:00
|
|
|
if (converse.areDesktopNotificationsEnabled()) {
|
|
|
|
converse.showMessageNotification($message);
|
|
|
|
}
|
2016-03-01 09:58:36 +01:00
|
|
|
};
|
|
|
|
|
2016-03-08 12:01:23 +01:00
|
|
|
converse.handleContactRequestNotification = function (evt, contact) {
|
|
|
|
if (converse.areDesktopNotificationsEnabled(true)) {
|
|
|
|
converse.showContactRequestNotification(contact);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-03-31 14:23:05 +02:00
|
|
|
converse.handleFeedback = function (evt, data) {
|
2016-08-21 13:17:06 +02:00
|
|
|
if (converse.areDesktopNotificationsEnabled(true)) {
|
|
|
|
converse.showFeedbackNotification(data);
|
2016-03-31 14:23:05 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-03-08 23:36:13 +01:00
|
|
|
converse.requestPermission = function (evt) {
|
2016-06-16 10:56:32 +02:00
|
|
|
if (converse.supports_html5_notification &&
|
2016-03-08 23:36:13 +01:00
|
|
|
! _.contains(['denied', 'granted'], Notification.permission)) {
|
|
|
|
// Ask user to enable HTML5 notifications
|
|
|
|
Notification.requestPermission();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-16 17:21:11 +02:00
|
|
|
converse.on('pluginsInitialized', function () {
|
|
|
|
// We only register event handlers after all plugins are
|
|
|
|
// registered, because other plugins might override some of our
|
|
|
|
// handlers.
|
|
|
|
converse.on('contactRequest', converse.handleContactRequestNotification);
|
|
|
|
converse.on('contactStatusChanged', converse.handleChatStateNotification);
|
|
|
|
converse.on('message', converse.handleMessageNotification);
|
|
|
|
converse.on('feedback', converse.handleFeedback);
|
|
|
|
converse.on('connected', converse.requestPermission);
|
|
|
|
});
|
2016-02-29 21:58:01 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}));
|