Hide duplicates when receiving access to a pad via notifications

pull/1/head
yflory 6 years ago
parent 9aebeb216e
commit 7a0f30488c

@ -1,6 +1,7 @@
define([ define([
'/common/common-messaging.js', '/common/common-messaging.js',
], function (Messaging) { '/common/common-hash.js',
], function (Messaging, Hash) {
var getRandomTimeout = function (ctx) { var getRandomTimeout = function (ctx) {
var lag = ctx.store.realtime.getLag().lag || 0; var lag = ctx.store.realtime.getLag().lag || 0;
@ -156,6 +157,50 @@ define([
cb(true); cb(true);
}; };
// Hide duplicates when receiving a SHARE_PAD notification:
// Keep only one notification per channel: the stronger and more recent one
var channels = {};
handlers['SHARE_PAD'] = function (ctx, box, data, cb) {
var msg = data.msg;
var hash = data.hash;
var content = msg.content;
// content.name, content.title, content.href, content.password
var channel = Hash.hrefToHexChannelId(content.href, content.password);
var parsed = Hash.parsePadUrl(content.href);
var mode = parsed.hashData && parsed.hashData.mode || 'n/a';
var old = channels[channel];
var toRemove;
if (old) {
// New hash is weaker, ignore
if (old.mode === 'edit' && mode === 'view') {
return void cb(true);
}
// New hash is not weaker, clear the old one
toRemove = old.data;
}
// Update the data
channels[channel] = {
mode: mode,
data: {
type: box.type,
hash: hash
}
}
cb(false, toRemove);
};
removeHandlers['SHARE_PAD'] = function (ctx, box, data, hash) {
var content = data.content;
var channel = Hash.hrefToHexChannelId(content.href, content.password);
var old = channels[channel];
if (old && old.data && old.data.hash === hash) {
delete channels[channel];
}
};
return { return {
add: function (ctx, box, data, cb) { add: function (ctx, box, data, cb) {
/** /**

@ -157,6 +157,7 @@ proxy.mailboxes = {
var openChannel = function (ctx, type, m, onReady) { var openChannel = function (ctx, type, m, onReady) {
var box = ctx.boxes[type] = { var box = ctx.boxes[type] = {
type: type,
queue: [], // Store the messages to send when the channel is ready queue: [], // Store the messages to send when the channel is ready
history: [], // All the hashes loaded from the server in corretc order history: [], // All the hashes loaded from the server in corretc order
content: {}, // Content of the messages that should be displayed content: {}, // Content of the messages that should be displayed
@ -228,8 +229,8 @@ proxy.mailboxes = {
msg: msg, msg: msg,
hash: hash hash: hash
}; };
Handlers.add(ctx, box, message, function (toDismiss) { Handlers.add(ctx, box, message, function (dismissed, toDismiss) {
if (toDismiss) { if (dismissed) { // This message should be removed
dismiss(ctx, { dismiss(ctx, {
type: type, type: type,
hash: hash hash: hash
@ -238,6 +239,11 @@ proxy.mailboxes = {
}); });
return; return;
} }
if (toDismiss) { // List of other messages to remove
dismiss(ctx, toDismiss, '', function () {
console.log('Notification handled automatically');
});
}
box.content[hash] = msg; box.content[hash] = msg;
showMessage(ctx, type, message); showMessage(ctx, type, message);
}); });

Loading…
Cancel
Save