diff --git a/www/common/notifications.js b/www/common/notifications.js index a77f1d155..fe32c1896 100644 --- a/www/common/notifications.js +++ b/www/common/notifications.js @@ -25,19 +25,19 @@ define([ }); }; - handlers['ACCEPT_FRIEND_REQUEST'] = function (common, data, el) { + handlers['FRIEND_REQUEST_ACCEPTED'] = function (common, data, el) { var content = data.content; var msg = content.msg; $(el).find('.cp-notification-content p') - .html(Messages._getKey('friendRequest_accepted', [msg.content.displayName])); + .html(Messages._getKey('friendRequest_accepted', [msg.content.name])); $(el).find('.cp-notification-dismiss').css('display', 'flex'); }; - handlers['DECLINE_FRIEND_REQUEST'] = function (common, data, el) { + handlers['FRIEND_REQUEST_DECLINED'] = function (common, data, el) { var content = data.content; var msg = content.msg; $(el).find('.cp-notification-content p') - .html(Messages._getKey('friendRequest_declined', [msg.content.displayName])); + .html(Messages._getKey('friendRequest_declined', [msg.content.name])); $(el).find('.cp-notification-dismiss').css('display', 'flex'); }; diff --git a/www/common/outer/mailbox-handlers.js b/www/common/outer/mailbox-handlers.js index e174c15bf..15eca8321 100644 --- a/www/common/outer/mailbox-handlers.js +++ b/www/common/outer/mailbox-handlers.js @@ -2,11 +2,19 @@ define([ '/common/common-messaging.js', ], function (Messaging) { + var getRandomTimeout = function (ctx) { + var lag = ctx.store.realtime.getLag().lagĀ || 0; + return (Math.max(0, lag) + 300) * 20 * (0.5 + Math.random()); + }; + var handlers = {}; handlers['FRIEND_REQUEST'] = function (ctx, data, cb) { - if (data.msg.author === data.msg.content.curvePublic && - Messaging.getFriend(ctx.store.proxy, data.msg.author)) { + // Check if the request is valid (send by the correct user) + if (data.msg.author !== data.msg.content.curvePublic) { return void cb(true); } + + // If the user is already in our friend list, automatically accept the request + if (Messaging.getFriend(ctx.store.proxy, data.msg.author)) { Messaging.acceptFriendRequest(ctx.store, data.msg.content, function (obj) { if (obj && obj.error) { return void cb(); } cb(true); @@ -16,30 +24,51 @@ define([ cb(); }; handlers['DECLINE_FRIEND_REQUEST'] = function (ctx, box, data, cb) { - // Our friend request was declined. - if (!ctx.store.proxy.friends_pending[data.msg.author]) { return void cb(); } - delete ctx.store.proxy.friends_pending[data.msg.author]; - ctx.updateMetadata(); - cb(); + setTimeout(function () { + // Our friend request was declined. + if (!ctx.store.proxy.friends_pending[data.msg.author]) { return; } + + // Remove the pending message and display the "declined" state in the UI + delete ctx.store.proxy.friends_pending[data.msg.author]; + ctx.updateMetadata(); + box.sendMessage({ + type: 'FRIEND_REQUEST_DECLINED', + content: { + user: data.msg.author, + name: data.msg.content.displayName + } + }); + }, getRandomTimeout(ctx)); + cb(true); }; handlers['ACCEPT_FRIEND_REQUEST'] = function (ctx, box, data, cb) { // Our friend request was accepted. - // Make sure we really sent it - if (!ctx.store.proxy.friends_pending[data.msg.author]) { return void cb(); } - // And add the friend - Messaging.addToFriendList({ - proxy: ctx.store.proxy, - realtime: ctx.store.realtime, - pinPads: ctx.pinPads - }, data.msg.content, function (err) { - if (err) { console.error(err); } - delete ctx.store.proxy.friends_pending[data.msg.author]; - if (ctx.store.messenger) { - ctx.store.messenger.onFriendAdded(data.msg.content); - } - ctx.updateMetadata(); - }); - cb(); + setTimeout(function () { + // Make sure we really sent it + if (!ctx.store.proxy.friends_pending[data.msg.author]) { return; } + // And add the friend + Messaging.addToFriendList({ + proxy: ctx.store.proxy, + realtime: ctx.store.realtime, + pinPads: ctx.pinPads + }, data.msg.content, function (err) { + if (err) { console.error(err); } + delete ctx.store.proxy.friends_pending[data.msg.author]; + if (ctx.store.messenger) { + ctx.store.messenger.onFriendAdded(data.msg.content); + } + ctx.updateMetadata(); + // Display the "accepted" state in the UI + box.sendMessage({ + type: 'FRIEND_REQUEST_ACCEPTED', + content: { + curvePublic: data.msg.author, + displayName: data.msg.content.displayName + } + }); + }); + }, getRandomTimeout(ctx)); + cb(true); }; return function (ctx, box, data, cb) { @@ -49,6 +78,7 @@ define([ try { handlers[type](ctx, box, data, cb); } catch (e) { + console.error(e); cb(); } } else { diff --git a/www/common/outer/mailbox.js b/www/common/outer/mailbox.js index 3d45bee0f..c16f57a24 100644 --- a/www/common/outer/mailbox.js +++ b/www/common/outer/mailbox.js @@ -201,16 +201,19 @@ proxy.mailboxes = { cfg.onConnect = function (wc, sendMessage) { // Send a message to our box? // NOTE: we use our own curvePublic so that we can decrypt our own message :) - box.sendMessage = function (msg) { + box.sendMessage = function (_msg) { try { - msg = JSON.stringify(msg); + msg = JSON.stringify(_msg); } catch (e) { console.error(e); } sendMessage(msg, function (err, hash) { - if (m.viewed.indexOf(hash) === -1) { - m.viewed.push(hash); - } + box.content[hash] = _msg; + var message = { + msg: _msg, + hash: hash + }; + showMessage(ctx, type, message); }, keys.curvePublic); }; box.queue.forEach(function (msg) { @@ -357,7 +360,6 @@ proxy.mailboxes = { } }); - // XXX test function used to populate a mailbox, should be removed in prod mailbox.post = function (box, type, content) { var b = ctx.boxes[box]; if (!b) { return; }