From 9e31b5de2a6c4544a1fd5664fa23b9727e665a67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Fri, 5 Feb 2016 13:28:35 +0100 Subject: [PATCH] [FIX] mail: discuss notifications small fixes - discuss notifications are always sent natively if possible - clicking on native notifications focus the tab - clicking on native notifications closes them - granting notification permissions will display a confirmation notification --- addons/mail/static/src/js/chat_manager.js | 43 ++++++++++++++------- addons/mail/static/src/js/client_action.js | 7 +++- addons/mail/static/src/js/window_manager.js | 2 +- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/addons/mail/static/src/js/chat_manager.js b/addons/mail/static/src/js/chat_manager.js index a1272cea1ec..18df4e9b821 100644 --- a/addons/mail/static/src/js/chat_manager.js +++ b/addons/mail/static/src/js/chat_manager.js @@ -51,31 +51,44 @@ bus.on("window_focus", null, function() { web_client.set_title_part("_chat"); }); +// to do: move this to mail.utils +function send_native_notification(title, content) { + var notification = new Notification(title, {body: content, icon: "/mail/static/src/img/odoo_o.png"}); + notification.onclick = function (e) { + window.focus(); + if (this.cancel) { + this.cancel(); + } else if (this.close) { + this.close(); + } + }; +} + function notify_incoming_message (msg, options) { + if (bus.is_odoo_focused() && options.is_displayed) { + // no need to notify + return; + } var title = _t('New message'); if (msg.author_id[1]) { title = _.escape(msg.author_id[1]); } var content = parse_and_transform(msg.body, strip_html).substr(0, preview_msg_max_size); - if (bus.is_odoo_focused()) { - if (!options.is_displayed) { - web_client.do_notify(title, content); - } - } else { + if (!bus.is_odoo_focused()) { global_unread_counter++; var tab_title = _.str.sprintf(_t("%d Messages"), global_unread_counter); web_client.set_title_part("_chat", tab_title); + } - if (Notification && Notification.permission === "granted") { - if (bus.is_master) { - new Notification(title, {body: content, icon: "/mail/static/src/img/odoo_o.png", silent: false}); - } - } else { - web_client.do_notify(title, content); - if (bus.is_master) { - beep(); - } + if (Notification && Notification.permission === "granted") { + if (bus.is_master) { + send_native_notification(title, content); + } + } else { + web_client.do_notify(title, content); + if (bus.is_master) { + beep(); } } } @@ -987,6 +1000,8 @@ var chat_manager = { return values; }); }, + + send_native_notification: send_native_notification, }; // Initialization diff --git a/addons/mail/static/src/js/client_action.js b/addons/mail/static/src/js/client_action.js index 7b4024f66d0..b2ea7eb732e 100644 --- a/addons/mail/static/src/js/client_action.js +++ b/addons/mail/static/src/js/client_action.js @@ -122,7 +122,12 @@ var ChatAction = Widget.extend(ControlPanelMixin, { "click .o_mail_request_permission": function (event) { event.preventDefault(); this.$(".o_mail_annoying_notification_bar").slideUp(); - window.Notification.requestPermission(); + var def = window.Notification.requestPermission(); + if (def) { + def.then(function () { + chat_manager.send_native_notification('Permission granted', 'Odoo has now the permission to send you native notifications on this device.'); + }); + } }, "keydown": function (event) { if (event.which === $.ui.keyCode.ESCAPE && this.selected_message) { diff --git a/addons/mail/static/src/js/window_manager.js b/addons/mail/static/src/js/window_manager.js index 3ee3007a7ea..a98a9de5cc0 100644 --- a/addons/mail/static/src/js/window_manager.js +++ b/addons/mail/static/src/js/window_manager.js @@ -302,7 +302,7 @@ core.bus.on('web_client_ready', null, function () { chat_manager.bus.on('anyone_listening', null, function (channel, query) { _.each(chat_sessions, function (session) { - if (channel.id === session.id && session.window.thread.is_at_bottom()) { + if (channel.id === session.id && session.window.thread.is_at_bottom() && !session.window.is_hidden) { query.is_displayed = true; } });