Hide duplicates when receiving access to a pad via notifications
parent
9aebeb216e
commit
7a0f30488c
|
@ -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) {
|
||||
/**
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue