diff --git a/spec/notification.js b/spec/notification.js index 83953dec7..a15604b4f 100644 --- a/spec/notification.js +++ b/spec/notification.js @@ -1,7 +1,8 @@ (function (root, factory) { - define(["mock", "converse-core", "test_utils"], factory); -} (this, function (mock, converse, test_utils) { + define(["mock", "converse-core", "test_utils", "utils"], factory); +} (this, function (mock, converse, test_utils, utils) { "use strict"; + var _ = converse.env._; var $msg = converse.env.$msg; describe("Notifications", function () { @@ -64,6 +65,59 @@ } })); + it("is shown for headline messages", mock.initConverse(function (_converse) { + spyOn(_converse, 'showMessageNotification').andCallThrough(); + spyOn(_converse, 'areDesktopNotificationsEnabled').andReturn(true); + runs(function () { + var stanza = $msg({ + 'type': 'headline', + 'from': 'notify.example.com', + 'to': 'dummy@localhost', + 'xml:lang': 'en' + }) + .c('subject').t('SIEVE').up() + .c('body').t('<juliet@example.com> You got mail.').up() + .c('x', {'xmlns': 'jabber:x:oob'}) + .c('url').t('imap://romeo@example.com/INBOX;UIDVALIDITY=385759043/;UID=18'); + _converse.connection._dataRecv(test_utils.createRequest(stanza)); + }); + waits(250); + runs(function () { + expect( + _.includes(_converse.chatboxviews.keys(), + 'notify.example.com') + ).toBeTruthy(); + expect(_converse.showMessageNotification).toHaveBeenCalled(); + }); + })); + + it("is not shown for full JID headline messages if allow_non_roster_messaging is false", mock.initConverse(function (_converse) { + _converse.allow_non_roster_messaging = false; + spyOn(_converse, 'showMessageNotification').andCallThrough(); + spyOn(_converse, 'areDesktopNotificationsEnabled').andReturn(true); + runs(function () { + var stanza = $msg({ + 'type': 'headline', + 'from': 'someone@notify.example.com', + 'to': 'dummy@localhost', + 'xml:lang': 'en' + }) + .c('subject').t('SIEVE').up() + .c('body').t('<juliet@example.com> You got mail.').up() + .c('x', {'xmlns': 'jabber:x:oob'}) + .c('url').t('imap://romeo@example.com/INBOX;UIDVALIDITY=385759043/;UID=18'); + _converse.connection._dataRecv(test_utils.createRequest(stanza)); + }); + waits(250); + runs(function () { + expect( + _.includes(_converse.chatboxviews.keys(), + 'someone@notify.example.com') + ).toBeFalsy(); + expect(_converse.showMessageNotification).not.toHaveBeenCalled(); + }); + })); + it("is shown when a user changes their chat state (if show_chatstate_notifications is true)", mock.initConverse(function (_converse) { // TODO: not yet testing show_desktop_notifications setting _converse.show_chatstate_notifications = true; diff --git a/src/converse-notification.js b/src/converse-notification.js index f2b792f7c..ea42ef1c5 100644 --- a/src/converse-notification.js +++ b/src/converse-notification.js @@ -130,26 +130,38 @@ /* Shows an HTML5 Notification to indicate that a new chat * message was received. */ - var n, title, contact_jid, roster_item, - from_jid = message.getAttribute('from'); - if (message.getAttribute('type') === 'headline' || !_.includes(from_jid, '@')) { - // XXX: 2nd check is workaround for Prosody which doesn't - // give type "headline" - title = __(___("Notification from %1$s"), from_jid); - } else { - if (message.getAttribute('type') === 'groupchat') { - title = __(___("%1$s says"), Strophe.getResourceFromJid(from_jid)); + var title, roster_item, + from_jid = Strophe.getBareJidFromJid(message.getAttribute('from')); + if (message.getAttribute('type') === 'headline') { + if (!_.includes(from_jid, '@') || _converse.allow_non_roster_messaging) { + title = __(___("Notification from %1$s"), from_jid); } else { - if (_.isUndefined(_converse.roster)) { - _converse.log("Could not send notification, because roster is undefined", "error"); + return; + } + } else if (!_.includes(from_jid, '@')) { + // XXX: workaround for Prosody which doesn't give type "headline" + title = __(___("Notification from %1$s"), from_jid); + } else if (message.getAttribute('type') === 'groupchat') { + title = __(___("%1$s says"), Strophe.getResourceFromJid(from_jid)); + } else { + if (_.isUndefined(_converse.roster)) { + _converse.log( + "Could not send notification, because roster is undefined", + "error"); + return; + } + roster_item = _converse.roster.get(from_jid); + if (!_.isUndefined(roster_item)) { + title = __(___("%1$s says"), roster_item.get('fullname')); + } else { + if (_converse.allow_non_roster_messaging) { + title = __(___("%1$s says"), from_jid); + } else { return; } - contact_jid = Strophe.getBareJidFromJid(message.getAttribute('from')); - roster_item = _converse.roster.get(contact_jid); - title = __(___("%1$s says"), roster_item.get('fullname')); } } - n = new Notification(title, { + var n = new Notification(title, { body: message.querySelector('body').textContent, lang: _converse.i18n.locale_data.converse[""].lang, icon: _converse.notification_icon