Display OS notifications when receiving a notification on CryptPad

pull/1/head
yflory 5 years ago
parent d993827c7e
commit b84c4be69c

@ -1752,12 +1752,12 @@ define([
broadcast([], "UPDATE_METADATA"); broadcast([], "UPDATE_METADATA");
}, },
pinPads: function (data, cb) { Store.pinPads(null, data, cb); }, pinPads: function (data, cb) { Store.pinPads(null, data, cb); },
}, waitFor, function (ev, data, clients) { }, waitFor, function (ev, data, clients, cb) {
clients.forEach(function (cId) { clients.forEach(function (cId) {
postMessage(cId, 'MAILBOX_EVENT', { postMessage(cId, 'MAILBOX_EVENT', {
ev: ev, ev: ev,
data: data data: data
}); }, cb);
}); });
}); });
}; };

@ -2,10 +2,11 @@ define([
'/common/common-util.js', '/common/common-util.js',
'/common/common-hash.js', '/common/common-hash.js',
'/common/common-realtime.js', '/common/common-realtime.js',
'/common/notify.js',
'/common/outer/mailbox-handlers.js', '/common/outer/mailbox-handlers.js',
'/bower_components/chainpad-netflux/chainpad-netflux.js', '/bower_components/chainpad-netflux/chainpad-netflux.js',
'/bower_components/chainpad-crypto/crypto.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 Mailbox = {};
var TYPES = [ var TYPES = [
@ -54,11 +55,11 @@ proxy.mailboxes = {
return (m.viewed || []).indexOf(hash) === -1 && hash !== m.lastKnownHash; 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', { ctx.emit('MESSAGE', {
type: type, type: type,
content: msg content: msg
}, cId ? [cId] : ctx.clients); }, cId ? [cId] : ctx.clients, cb);
}; };
var hideMessage = function (ctx, type, hash, clients) { var hideMessage = function (ctx, type, hash, clients) {
ctx.emit('VIEWED', { ctx.emit('VIEWED', {
@ -272,7 +273,11 @@ proxy.mailboxes = {
}); });
} }
box.content[hash] = msg; 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 { } else {
// Message has already been viewed by the user // Message has already been viewed by the user
@ -320,6 +325,7 @@ proxy.mailboxes = {
view(n); view(n);
} }
}); });
box.ready = true;
// Continue // Continue
onReady(); onReady();
}; };

@ -124,10 +124,16 @@ define([
removeFromHistory(data.type, data.hash); removeFromHistory(data.type, data.hash);
}; };
var onMessage = function (data) { var onMessage = function (data, cb) {
// data = { type: 'type', content: {msg: 'msg', hash: 'hash'} } // data = { type: 'type', content: {msg: 'msg', hash: 'hash'} }
console.log(data.type, data.content); console.log(data.type, data.content);
pushMessage(data); pushMessage(data);
if (data.content && typeof (data.content.getFormatText) == "function") {
var text = $('<div>').html(data.content.getFormatText()).text();
cb({
msg: text
});
}
if (!history[data.type]) { history[data.type] = []; } if (!history[data.type]) { history[data.type] = []; }
history[data.type].push(data.content); history[data.type].push(data.content);
}; };
@ -224,7 +230,7 @@ define([
// CHANNEL WITH WORKER // CHANNEL WITH WORKER
sframeChan.on('EV_MAILBOX_EVENT', function (obj) { sframeChan.on('EV_MAILBOX_EVENT', function (obj, cb) {
// obj = { ev: 'type', data: obj } // obj = { ev: 'type', data: obj }
var ev = obj.ev; var ev = obj.ev;
var data = obj.data; var data = obj.data;
@ -232,7 +238,7 @@ define([
return void onHistory(data); return void onHistory(data);
} }
if (ev === 'MESSAGE') { if (ev === 'MESSAGE') {
return void onMessage(data); return void onMessage(data, cb);
} }
if (ev === 'VIEWED') { if (ev === 'VIEWED') {
return void onViewed(data); return void onViewed(data);

@ -414,8 +414,12 @@ define([
}); });
}); });
Cryptpad.mailbox.onEvent.reg(function (data) { Cryptpad.mailbox.onEvent.reg(function (data, cb) {
sframeChan.event('EV_MAILBOX_EVENT', data); 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) { sframeChan.on('Q_MAILBOX_COMMAND', function (data, cb) {
Cryptpad.mailbox.execCommand(data, cb); Cryptpad.mailbox.execCommand(data, cb);

Loading…
Cancel
Save