diff options
Diffstat (limited to 'addons/mail_bot/static/src/js')
| -rw-r--r-- | addons/mail_bot/static/src/js/mailbot_service.js | 36 | ||||
| -rw-r--r-- | addons/mail_bot/static/src/js/systray_messaging_menu.js | 121 |
2 files changed, 156 insertions, 1 deletions
diff --git a/addons/mail_bot/static/src/js/mailbot_service.js b/addons/mail_bot/static/src/js/mailbot_service.js index 300e95d06db..a75780ab090 100644 --- a/addons/mail_bot/static/src/js/mailbot_service.js +++ b/addons/mail_bot/static/src/js/mailbot_service.js @@ -5,8 +5,9 @@ var AbstractService = require('web.AbstractService'); var core = require('web.core'); var session = require('web.session'); +var _t = core._t; + var MailBotService = AbstractService.extend({ - name: 'mailbot_service', /** * @override */ @@ -22,7 +23,40 @@ var MailBotService = AbstractService.extend({ }, 2*60*1000); } }, + /** + * Get the previews related to the OdooBot (conversation not included). + * For instance, when there is no conversation with OdooBot and OdooBot has + * a request, it should display a preview in the systray messaging menu. + * + * @param {string|undefined} [filter] + * @returns {Object[]} list of objects that are compatible with the + * 'mail.Preview' template. + */ + getPreviews: function (filter) { + var previews = []; + if (this.hasRequest() && (filter === 'mailbox_inbox' || !filter)) { + previews.push({ + title: _t("OdooBot has a request"), + imageSRC: "/mail/static/src/img/odoobot.png", + status: 'bot', + body: _t("Enable desktop notifications to chat"), + id: 'request_notification', + unreadCounter: 1, + }); + } + return previews; + }, + /** + * Tell whether OdooBot has a request or not. + * + * @returns {boolean} + */ + hasRequest: function () { + return window.Notification && window.Notification.permission === "default"; + }, }); + core.serviceRegistry.add('mailbot_service', MailBotService); return MailBotService; + }); diff --git a/addons/mail_bot/static/src/js/systray_messaging_menu.js b/addons/mail_bot/static/src/js/systray_messaging_menu.js new file mode 100644 index 00000000000..05c0ee54f24 --- /dev/null +++ b/addons/mail_bot/static/src/js/systray_messaging_menu.js @@ -0,0 +1,121 @@ +odoo.define('mail_bot.systray.MessagingMenu', function (require) { +"use strict"; + +var MessagingMenu = require('mail.systray.MessagingMenu'); +var core = require('web.core'); + +var _t = core._t; + +return MessagingMenu.include({ + + //-------------------------------------------------------------------------- + // Private + //-------------------------------------------------------------------------- + + /** + * Override so that 'mailbot has a request' is included in the computation + * of of the counter. + * + * @override + * @private + * @returns {integer} + */ + _computeCounter: function () { + var counter = this._super.apply(this, arguments); + if (this.call('mailbot_service', 'hasRequest')) { + counter++; + } + return counter; + }, + /** + * Override so that the mailbot previews are included in the systray + * messaging menu (e.g. 'OdooBot has a request') + * + * @override + * @private + * @returns {$.Promise<Object[]>} resolved with list of previews that are + * compatible with the 'mail.Preview' template. + */ + _getPreviews: function () { + var mailbotPreviews = this.call('mailbot_service', 'getPreviews', this._filter); + return this._super.apply(this, arguments).then(function (previews) { + return _.union(mailbotPreviews, previews); + }); + }, + /** + * Handle the response of the user when prompted whether push notifications + * are granted or denied. + * + * Also refreshes the counter after a response from a push notification + * request. This is useful because the counter contains a part for the + * OdooBot, and the OdooBot influences the counter by 1 when it requests + * for notifications. This should no longer be the case when push + * notifications are either granted or denied. + * + * @private + * @param {string} value + */ + _handleResponseNotificationPermission: function (value) { + if (value !== 'granted') { + this.call('bus_service', 'sendNotification', _t('Permission denied'), + _t('Odoo will not have the permission to send native notifications on this device.')); + } else { + this.call('bus_service', 'sendNotification', _t('Yay, push notifications are enabled!'), + _t('Odoo has now the permission to send you native notifications on this device.')); + } + this._updateCounter(); + }, + /** + * Display the browser notification request dialog when the user clicks on + * systray's corresponding notification + * + * @private + */ + _requestNotificationPermission: function () { + var def = window.Notification && window.Notification.requestPermission(); + if (def) { + def.then(this._handleResponseNotificationPermission.bind(this)); + } + this.$('.o_mail_navbar_request_permission').slideUp(); + }, + + //-------------------------------------------------------------------------- + // Handlers + //-------------------------------------------------------------------------- + + /** + * Override so that it handles preview related to OdooBot + * + * @override + * @private + * @param {MouseEvent} ev + */ + _onClickPreview: function (ev) { + var previewID = $(ev.currentTarget).data('preview-id'); + if (previewID === 'request_notification') { + this._requestNotificationPermission(); + } else { + this._super.apply(this, arguments); + } + }, + /** + * Override so that it handles clicking on 'mark as read' similarly to + * requesting push notification permission. + * + * @override + * @private + * @param {MouseEvent} ev + */ + _onClickPreviewMarkAsRead: function (ev) { + ev.stopPropagation(); + var $preview = $(ev.currentTarget).closest('.o_mail_preview'); + var previewID = $preview.data('preview-id'); + if (previewID === 'request_notification') { + this._requestNotificationPermission(); + } else { + this._super.apply(this, arguments); + } + } +}); + +}); |
