From c5c6dc822375ac85b4a5591c409b64a84d36854e Mon Sep 17 00:00:00 2001 From: yflory Date: Tue, 2 Oct 2018 12:08:25 +0200 Subject: [PATCH] Enable browser notifications for messaging tools --- www/common/common-notifier.js | 14 +++++++++++++- www/common/notify.js | 21 ++++++++++++++------- www/common/sframe-common-outer.js | 5 +++-- www/common/sframe-common.js | 4 ++-- www/contacts/messenger-ui.js | 7 ++++++- 5 files changed, 38 insertions(+), 13 deletions(-) diff --git a/www/common/common-notifier.js b/www/common/common-notifier.js index 7a658e193..99415b553 100644 --- a/www/common/common-notifier.js +++ b/www/common/common-notifier.js @@ -12,13 +12,25 @@ define([ } }; - Notifier.notify = function () { + Notifier.notify = function (data) { if (Visible.isSupported() && !Visible.currently()) { + if (data) { + var title = data.title; + if (document.title) { title += ' (' + document.title + ')'; } + Notify.system(data.msg, title); + return; + } Notifier.unnotify(); notify.tabNotification = Notify.tab(1000, 10); } }; + Notifier.getPermission = function () { + if (Notify.isSupported()) { + Notify.getPermission(); + } + }; + if (Visible.isSupported()) { Visible.onChange(function (yes) { if (yes) { Notifier.unnotify(); } diff --git a/www/common/notify.js b/www/common/notify.js index 8f52d7c51..5109e8748 100644 --- a/www/common/notify.js +++ b/www/common/notify.js @@ -1,6 +1,10 @@ define(['/api/config'], function (ApiConfig) { var Module = {}; + var DEFAULT_MAIN = '/customize/main-favicon.png?' + ApiConfig.requireConf.urlArgs; + var DEFAULT_ALT = '/customize/alt-favicon.png?' + ApiConfig.requireConf.urlArgs; + + var isSupported = Module.isSupported = function () { return typeof(window.Notification) === 'function'; }; @@ -10,22 +14,28 @@ define(['/api/config'], function (ApiConfig) { }; var getPermission = Module.getPermission = function (f) { + f = f || function () {}; Notification.requestPermission(function (permission) { if (permission === "granted") { f(true); } else { f(false); } }); }; - var create = Module.create = function (msg, title) { + var create = Module.create = function (msg, title, icon) { + if (!icon) { + var favicon = document.getElementById('favicon'); + icon = favicon.getAttribute('data-main-favicon') || DEFAULT_MAIN; + } + return new Notification(title,{ - // icon: icon, + icon: icon, body: msg, }); }; Module.system = function (msg, title, icon) { // Let's check if the browser supports notifications - if (!isSupported()) { console.log("Notifications are not supported"); } + if (!isSupported()) { return; /*console.log("Notifications are not supported");*/ } // Let's check whether notification permissions have already been granted else if (hasPermission()) { @@ -41,9 +51,6 @@ define(['/api/config'], function (ApiConfig) { } }; - var DEFAULT_MAIN = '/customize/main-favicon.png?' + ApiConfig.requireConf.urlArgs; - var DEFAULT_ALT = '/customize/alt-favicon.png?' + ApiConfig.requireConf.urlArgs; - var createFavicon = function () { console.log("creating favicon"); var fav = document.createElement('link'); @@ -112,4 +119,4 @@ define(['/api/config'], function (ApiConfig) { }; return Module; -}); \ No newline at end of file +}); diff --git a/www/common/sframe-common-outer.js b/www/common/sframe-common-outer.js index dcffcc606..e07bc7558 100644 --- a/www/common/sframe-common-outer.js +++ b/www/common/sframe-common-outer.js @@ -421,8 +421,8 @@ define([ Utils.LocalStore.logout(cb); }); - sframeChan.on('EV_NOTIFY', function () { - Notifier.notify(); + sframeChan.on('EV_NOTIFY', function (data) { + Notifier.notify(data); }); sframeChan.on('Q_SET_LOGIN_REDIRECT', function (data, cb) { @@ -745,6 +745,7 @@ define([ } if (cfg.messaging) { + Notifier.getPermission(); sframeChan.on('Q_CONTACTS_GET_FRIEND_LIST', function (data, cb) { Cryptpad.messenger.getFriendList(cb); }); diff --git a/www/common/sframe-common.js b/www/common/sframe-common.js index 2b094c055..3a37b8999 100644 --- a/www/common/sframe-common.js +++ b/www/common/sframe-common.js @@ -189,8 +189,8 @@ define([ ctx.sframeChan.query('Q_LOGOUT', null, cb); }; - funcs.notify = function () { - ctx.sframeChan.event('EV_NOTIFY'); + funcs.notify = function (data) { + ctx.sframeChan.event('EV_NOTIFY', data); }; funcs.setTabTitle = function (newTitle) { ctx.sframeChan.event('EV_SET_TAB_TITLE', newTitle); diff --git a/www/contacts/messenger-ui.js b/www/contacts/messenger-ui.js index 5872f4780..6770166e4 100644 --- a/www/contacts/messenger-ui.js +++ b/www/contacts/messenger-ui.js @@ -549,6 +549,11 @@ define([ var el_message = markup.message(message); common.notify(); + if (message.type === 'MSG') { + var name = typeof message.name !== "undefined" ? (message.name || Messages.anonymous) + : contactsData[message.author].displayName; + common.notify({title: name, msg: message.text}); + } notifyToolbar(); channel.messages.push(message); @@ -578,7 +583,7 @@ define([ } var lastMsg = channel.messages.slice(-1)[0]; if (lastMsg.sig !== channel.HEAD) { - return void notify(chanId); + return void notify(chanId, message); } unnotify(chanId); });