From 7a0f30488cea5c03c7217ee26ac9c796c5ceadc0 Mon Sep 17 00:00:00 2001 From: yflory Date: Mon, 1 Jul 2019 18:07:40 +0200 Subject: [PATCH] Hide duplicates when receiving access to a pad via notifications --- www/common/outer/mailbox-handlers.js | 47 +++++++++++++++++++++++++++- www/common/outer/mailbox.js | 10 ++++-- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/www/common/outer/mailbox-handlers.js b/www/common/outer/mailbox-handlers.js index 0ab804dcf..befd0d11f 100644 --- a/www/common/outer/mailbox-handlers.js +++ b/www/common/outer/mailbox-handlers.js @@ -1,6 +1,7 @@ define([ '/common/common-messaging.js', -], function (Messaging) { + '/common/common-hash.js', +], function (Messaging, Hash) { var getRandomTimeout = function (ctx) { var lag = ctx.store.realtime.getLag().lagĀ || 0; @@ -156,6 +157,50 @@ define([ 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 { add: function (ctx, box, data, cb) { /** diff --git a/www/common/outer/mailbox.js b/www/common/outer/mailbox.js index 550573411..90d536bc3 100644 --- a/www/common/outer/mailbox.js +++ b/www/common/outer/mailbox.js @@ -157,6 +157,7 @@ proxy.mailboxes = { var openChannel = function (ctx, type, m, onReady) { var box = ctx.boxes[type] = { + type: type, queue: [], // Store the messages to send when the channel is ready history: [], // All the hashes loaded from the server in corretc order content: {}, // Content of the messages that should be displayed @@ -228,8 +229,8 @@ proxy.mailboxes = { msg: msg, hash: hash }; - Handlers.add(ctx, box, message, function (toDismiss) { - if (toDismiss) { + Handlers.add(ctx, box, message, function (dismissed, toDismiss) { + if (dismissed) { // This message should be removed dismiss(ctx, { type: type, hash: hash @@ -238,6 +239,11 @@ proxy.mailboxes = { }); return; } + if (toDismiss) { // List of other messages to remove + dismiss(ctx, toDismiss, '', function () { + console.log('Notification handled automatically'); + }); + } box.content[hash] = msg; showMessage(ctx, type, message); });