Browse Source

Don't show notifications for full JID headline messages if...

allow_non_roster_messaging is false.
JC Brand 8 years ago
parent
commit
b15c107e4e
2 changed files with 81 additions and 15 deletions
  1. 56 2
      spec/notification.js
  2. 25 13
      src/converse-notification.js

+ 56 - 2
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;

+ 25 - 13
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"
+                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 {
+                        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 (message.getAttribute('type') === 'groupchat') {
-                        title = __(___("%1$s says"), Strophe.getResourceFromJid(from_jid));
+                    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 (_.isUndefined(_converse.roster)) {
-                            _converse.log("Could not send notification, because roster is undefined", "error");
+                        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