Show a desktop notification when a contact request is received

updates #443
This commit is contained in:
JC Brand 2016-03-08 11:01:23 +00:00
parent 0aa0c02124
commit d92645c8d3
3 changed files with 39 additions and 8 deletions

View File

@ -659,8 +659,13 @@ show_desktop_notifications
* Default: ``true``
Should HTML5 desktop notifications be shown when the browser is not visible or
not focused?
Should HTML5 desktop notifications be shown?
Notification will be shown in the following cases:
* the browser is not visible nor focused and a private message is received.
* the browser is not visible nor focused and a groupchat message is received which mentions you.
* `auto_subscribe` is set to `false` and a new contact request is received.
Requires the `src/converse-notification.js` plugin.

View File

@ -41,6 +41,7 @@
}).c('body').t(message).up()
.c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
converse.chatboxes.onMessage(msg); // This will emit 'message'
expect(converse.areDesktopNotificationsEnabled).toHaveBeenCalled();
expect(converse.showMessageNotification).toHaveBeenCalled();
});
@ -50,6 +51,7 @@
spyOn(converse, 'showChatStateNotification');
var jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
converse.roster.get(jid).set('chat_status', 'busy'); // This will emit 'contactStatusChanged'
expect(converse.areDesktopNotificationsEnabled).toHaveBeenCalled();
expect(converse.showChatStateNotification).toHaveBeenCalled();
});
});
@ -57,7 +59,11 @@
describe("When a new contact request is received", function () {
it("an HTML5 Notification is received", function () {
// TODO
spyOn(converse, 'areDesktopNotificationsEnabled').andReturn(true);
spyOn(converse, 'showContactRequestNotification');
converse.emit('contactRequest', {'fullname': 'Peter Parker', 'jid': 'peter@parker.com'});
expect(converse.areDesktopNotificationsEnabled).toHaveBeenCalled();
expect(converse.showContactRequestNotification).toHaveBeenCalled();
});
});
});

View File

@ -105,11 +105,15 @@
}
};
converse.areDesktopNotificationsEnabled = function () {
return (supports_html5_notification &&
converse.show_desktop_notifications &&
converse.windowState === 'blur' &&
Notification.permission === "granted");
converse.areDesktopNotificationsEnabled = function (ignore_blur) {
var enabled = supports_html5_notification &&
converse.show_desktop_notifications &&
Notification.permission === "granted";
if (ignore_blur) {
return enabled;
} else {
return enabled && converse.windowState === 'blur';
}
};
converse.showMessageNotification = function ($message) {
@ -152,6 +156,15 @@
setTimeout(n.close.bind(n), 5000);
};
converse.showContactRequestNotification = function (contact) {
var n = new Notification(contact.fullname, {
body: __('is requesting to be your contact'),
lang: converse.i18n.locale_data.converse[""].lang,
icon: 'logo/conversejs.png'
});
setTimeout(n.close.bind(n), 5000);
};
converse.handleChatStateNotification = function (evt, contact) {
/* Event handler for on('contactStatusChanged').
* Will show an HTML5 notification to indicate that the chat
@ -176,6 +189,13 @@
}
};
converse.handleContactRequestNotification = function (evt, contact) {
if (converse.areDesktopNotificationsEnabled(true)) {
converse.showContactRequestNotification(contact);
}
};
converse.on('contactRequest', converse.handleContactRequestNotification);
converse.on('contactStatusChanged', converse.handleChatStateNotification);
converse.on('message', converse.handleMessageNotification);
}