From 09457d8461a9f97df0314144f99f0ff3e45f5f76 Mon Sep 17 00:00:00 2001 From: JC Brand Date: Thu, 28 Apr 2016 14:51:50 +0000 Subject: [PATCH] Properly handle notifications for groupchat messages --- spec/notification.js | 34 +++++++++++++++++++++++++++++++++- src/converse-notification.js | 16 ++++++++++------ 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/spec/notification.js b/spec/notification.js index 2af09f033..a99fe888c 100644 --- a/spec/notification.js +++ b/spec/notification.js @@ -26,7 +26,7 @@ describe("And the desktop is not focused", function () { describe("an HTML5 Notification", function () { - it("is shown when a new message is received", function () { + it("is shown when a new private message is received", function () { // TODO: not yet testing show_desktop_notifications setting spyOn(converse, 'showMessageNotification'); spyOn(converse, 'areDesktopNotificationsEnabled').andReturn(true); @@ -45,6 +45,38 @@ expect(converse.showMessageNotification).toHaveBeenCalled(); }); + it("is shown when you are mentioned in a chat room", function () { + test_utils.openChatRoom('lounge', 'localhost', 'dummy'); + var view = converse.chatboxviews.get('lounge@localhost'); + if (!view.$el.find('.chat-area').length) { view.renderChatArea(); } + var no_notification = false; + if (typeof window.Notification === 'undefined') { + no_notification = true; + window.Notification = function () { + return { + 'close': function () {} + }; + }; + } + spyOn(converse, 'showMessageNotification').andCallThrough(); + spyOn(converse, 'areDesktopNotificationsEnabled').andReturn(true); + + var message = 'dummy: This message will show a desktop notification'; + var nick = mock.chatroom_names[0], + msg = $msg({ + from: 'lounge@localhost/'+nick, + id: (new Date()).getTime(), + to: 'dummy@localhost', + type: 'groupchat' + }).c('body').t(message).tree(); + converse.chatboxes.onMessage(msg); // This will emit 'message' + expect(converse.areDesktopNotificationsEnabled).toHaveBeenCalled(); + expect(converse.showMessageNotification).toHaveBeenCalled(); + if (no_notification) { + delete window.Notification; + } + }); + it("is shown when a user changes their chat state", function () { // TODO: not yet testing show_desktop_notifications setting spyOn(converse, 'areDesktopNotificationsEnabled').andReturn(true); diff --git a/src/converse-notification.js b/src/converse-notification.js index 43d739617..e73576487 100644 --- a/src/converse-notification.js +++ b/src/converse-notification.js @@ -125,13 +125,17 @@ // give type "headline" title = __(___("Notification from %1$s"), from_jid); } else { - if (typeof converse.roster === 'undefined') { - converse.log("Could not send notification, because roster is undefined", "error"); - return; + 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')); } - contact_jid = Strophe.getBareJidFromJid($message.attr('from')); - roster_item = converse.roster.get(contact_jid); - title = __(___("%1$s says"), roster_item.get('fullname')); } n = new Notification(title, { body: $message.children('body').text(),