From b84c4be69c8269cce169bcba0cc374ac902a2b15 Mon Sep 17 00:00:00 2001 From: yflory Date: Mon, 9 Sep 2019 17:37:11 +0200 Subject: [PATCH] Display OS notifications when receiving a notification on CryptPad --- www/common/outer/async-store.js | 4 ++-- www/common/outer/mailbox.js | 14 ++++++++++---- www/common/sframe-common-mailbox.js | 12 +++++++++--- www/common/sframe-common-outer.js | 8 ++++++-- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/www/common/outer/async-store.js b/www/common/outer/async-store.js index 1dfe35716..2b301b20b 100644 --- a/www/common/outer/async-store.js +++ b/www/common/outer/async-store.js @@ -1752,12 +1752,12 @@ define([ broadcast([], "UPDATE_METADATA"); }, pinPads: function (data, cb) { Store.pinPads(null, data, cb); }, - }, waitFor, function (ev, data, clients) { + }, waitFor, function (ev, data, clients, cb) { clients.forEach(function (cId) { postMessage(cId, 'MAILBOX_EVENT', { ev: ev, data: data - }); + }, cb); }); }); }; diff --git a/www/common/outer/mailbox.js b/www/common/outer/mailbox.js index ac88b616e..34b3b13f0 100644 --- a/www/common/outer/mailbox.js +++ b/www/common/outer/mailbox.js @@ -2,10 +2,11 @@ define([ '/common/common-util.js', '/common/common-hash.js', '/common/common-realtime.js', + '/common/notify.js', '/common/outer/mailbox-handlers.js', '/bower_components/chainpad-netflux/chainpad-netflux.js', '/bower_components/chainpad-crypto/crypto.js', -], function (Util, Hash, Realtime, Handlers, CpNetflux, Crypto) { +], function (Util, Hash, Realtime, Notify, Handlers, CpNetflux, Crypto) { var Mailbox = {}; var TYPES = [ @@ -54,11 +55,11 @@ proxy.mailboxes = { return (m.viewed || []).indexOf(hash) === -1 && hash !== m.lastKnownHash; }; - var showMessage = function (ctx, type, msg, cId) { + var showMessage = function (ctx, type, msg, cId, cb) { ctx.emit('MESSAGE', { type: type, content: msg - }, cId ? [cId] : ctx.clients); + }, cId ? [cId] : ctx.clients, cb); }; var hideMessage = function (ctx, type, hash, clients) { ctx.emit('VIEWED', { @@ -272,7 +273,11 @@ proxy.mailboxes = { }); } box.content[hash] = msg; - showMessage(ctx, type, message); + showMessage(ctx, type, message, null, function (obj) { + if (!box.ready) { return; } + if (!obj || !obj.msg) { return; } + Notify.system(undefined, obj.msg); + }); }); } else { // Message has already been viewed by the user @@ -320,6 +325,7 @@ proxy.mailboxes = { view(n); } }); + box.ready = true; // Continue onReady(); }; diff --git a/www/common/sframe-common-mailbox.js b/www/common/sframe-common-mailbox.js index e468b29f1..eb50a8398 100644 --- a/www/common/sframe-common-mailbox.js +++ b/www/common/sframe-common-mailbox.js @@ -124,10 +124,16 @@ define([ removeFromHistory(data.type, data.hash); }; - var onMessage = function (data) { + var onMessage = function (data, cb) { // data = { type: 'type', content: {msg: 'msg', hash: 'hash'} } console.log(data.type, data.content); pushMessage(data); + if (data.content && typeof (data.content.getFormatText) == "function") { + var text = $('
').html(data.content.getFormatText()).text(); + cb({ + msg: text + }); + } if (!history[data.type]) { history[data.type] = []; } history[data.type].push(data.content); }; @@ -224,7 +230,7 @@ define([ // CHANNEL WITH WORKER - sframeChan.on('EV_MAILBOX_EVENT', function (obj) { + sframeChan.on('EV_MAILBOX_EVENT', function (obj, cb) { // obj = { ev: 'type', data: obj } var ev = obj.ev; var data = obj.data; @@ -232,7 +238,7 @@ define([ return void onHistory(data); } if (ev === 'MESSAGE') { - return void onMessage(data); + return void onMessage(data, cb); } if (ev === 'VIEWED') { return void onViewed(data); diff --git a/www/common/sframe-common-outer.js b/www/common/sframe-common-outer.js index e39e6880f..ade5dcb72 100644 --- a/www/common/sframe-common-outer.js +++ b/www/common/sframe-common-outer.js @@ -414,8 +414,12 @@ define([ }); }); - Cryptpad.mailbox.onEvent.reg(function (data) { - sframeChan.event('EV_MAILBOX_EVENT', data); + Cryptpad.mailbox.onEvent.reg(function (data, cb) { + sframeChan.query('EV_MAILBOX_EVENT', data, function (err, obj) { + if (!cb) { return; } + if (err) { return void cb({error: err}); } + cb(obj); + }); }); sframeChan.on('Q_MAILBOX_COMMAND', function (data, cb) { Cryptpad.mailbox.execCommand(data, cb);