From c3e9b51f7646edcee736abcfd81dd11ebce4fc8b Mon Sep 17 00:00:00 2001 From: yflory Date: Mon, 27 May 2019 17:51:55 +0200 Subject: [PATCH] Realtime update of the friendship status in the profile --- www/common/notifications.js | 6 +++--- www/common/outer/async-store.js | 1 + www/common/outer/mailbox-handlers.js | 21 +++++++++++++-------- www/common/outer/profile.js | 1 + www/profile/inner.js | 23 +++++++++++++++++++++++ 5 files changed, 41 insertions(+), 11 deletions(-) diff --git a/www/common/notifications.js b/www/common/notifications.js index fe32c1896..dc95fe3d4 100644 --- a/www/common/notifications.js +++ b/www/common/notifications.js @@ -19,7 +19,7 @@ define([ // Display the notification $(el).find('.cp-notification-content').addClass("cp-clickable"); $(el).find('.cp-notification-content p') - .html(Messages._getKey('friendRequest_notification', [msg.content.displayName])) + .html(Messages._getKey('friendRequest_notification', [msg.content.displayName || Messages.anonymous])) .click(function () { UIElements.displayFriendRequestModal(common, data); }); @@ -29,7 +29,7 @@ define([ var content = data.content; var msg = content.msg; $(el).find('.cp-notification-content p') - .html(Messages._getKey('friendRequest_accepted', [msg.content.name])); + .html(Messages._getKey('friendRequest_accepted', [msg.content.name || Messages.anonymous])); $(el).find('.cp-notification-dismiss').css('display', 'flex'); }; @@ -37,7 +37,7 @@ define([ var content = data.content; var msg = content.msg; $(el).find('.cp-notification-content p') - .html(Messages._getKey('friendRequest_declined', [msg.content.name])); + .html(Messages._getKey('friendRequest_declined', [msg.content.name || Messages.anonymous])); $(el).find('.cp-notification-dismiss').css('display', 'flex'); }; diff --git a/www/common/outer/async-store.js b/www/common/outer/async-store.js index ed9409c31..869890b7c 100644 --- a/www/common/outer/async-store.js +++ b/www/common/outer/async-store.js @@ -959,6 +959,7 @@ define([ channel: msg.content.notifications, curvePublic: msg.content.curvePublic }, function (obj) { + broadcast([], "UPDATE_METADATA"); cb(obj); }); dismiss(); diff --git a/www/common/outer/mailbox-handlers.js b/www/common/outer/mailbox-handlers.js index 6f2c6c04d..f8d2c8ae8 100644 --- a/www/common/outer/mailbox-handlers.js +++ b/www/common/outer/mailbox-handlers.js @@ -12,9 +12,12 @@ define([ // Store the friend request displayed to avoid duplicates var friendRequest = {}; - handlers['FRIEND_REQUEST'] = function (ctx, data, cb) { + handlers['FRIEND_REQUEST'] = function (ctx, box, data, cb) { + // Check if the request is valid (send by the correct user) - if (data.msg.author !== data.msg.content.curvePublic) { return void cb(true); } + if (data.msg.author !== data.msg.content.curvePublic) { + return void cb(true); + } // Don't show duplicate friend request: if we already have a friend request // in memory from the same user, dismiss the new one @@ -23,7 +26,8 @@ define([ friendRequest[data.msg.author] = true; // If the user is already in our friend list, automatically accept the request - if (Messaging.getFriend(ctx.store.proxy, data.msg.author)) { + if (Messaging.getFriend(ctx.store.proxy, data.msg.author) || + ctx.store.proxy.friends_pending[data.msg.author]) { Messaging.acceptFriendRequest(ctx.store, data.msg.content, function (obj) { if (obj && obj.error) { return void cb(); @@ -36,8 +40,8 @@ define([ cb(); }; removeHandlers['FRIEND_REQUEST'] = function (ctx, box, data) { - if (friendRequest[data.curvePublic]) { - delete friendRequest[data.curvePublic]; + if (friendRequest[data.content.curvePublic]) { + delete friendRequest[data.content.curvePublic]; } }; @@ -51,8 +55,6 @@ define([ delete ctx.store.proxy.friends_pending[data.msg.author]; ctx.updateMetadata(); if (friendRequestDeclined[data.msg.author]) { return; } - // If you have a profile page open, update it - if (ctx.store.modules['profile']) { ctx.store.modules['profile'].update(); } box.sendMessage({ type: 'FRIEND_REQUEST_DECLINED', content: { @@ -69,6 +71,7 @@ define([ cb(true); }; handlers['FRIEND_REQUEST_DECLINED'] = function (ctx, box, data, cb) { + ctx.updateMetadata(); if (friendRequestDeclined[data.msg.content.user]) { return void cb(true); } friendRequestDeclined[data.msg.content.user] = true; cb(); @@ -97,9 +100,9 @@ define([ ctx.store.messenger.onFriendAdded(data.msg.content); } ctx.updateMetadata(); - if (friendRequestAccepted[data.msg.author]) { return; } // If you have a profile page open, update it if (ctx.store.modules['profile']) { ctx.store.modules['profile'].update(); } + if (friendRequestAccepted[data.msg.author]) { return; } // Display the "accepted" state in the UI box.sendMessage({ type: 'FRIEND_REQUEST_ACCEPTED', @@ -118,6 +121,7 @@ define([ cb(true); }; handlers['FRIEND_REQUEST_ACCEPTED'] = function (ctx, box, data, cb) { + ctx.updateMetadata(); if (friendRequestAccepted[data.msg.content.user]) { return void cb(true); } friendRequestAccepted[data.msg.content.user] = true; cb(); @@ -131,6 +135,7 @@ define([ return { add: function (ctx, box, data, cb) { + if (!data.msg) { return void cb(true); } var type = data.msg.type; if (handlers[type]) { diff --git a/www/common/outer/profile.js b/www/common/outer/profile.js index 5ecdcc235..0e1e1f35e 100644 --- a/www/common/outer/profile.js +++ b/www/common/outer/profile.js @@ -135,6 +135,7 @@ define([ removeClient(ctx, clientId); }; profile.update = function () { + if (!ctx.listmap) { return; } ctx.emit('UPDATE', ctx.listmap.proxy, ctx.clients); }; profile.execCommand = function (clientId, obj, cb) { diff --git a/www/profile/inner.js b/www/profile/inner.js index d27961016..98523070c 100644 --- a/www/profile/inner.js +++ b/www/profile/inner.js @@ -7,6 +7,7 @@ define([ '/common/sframe-common.js', '/common/common-util.js', '/common/common-interface.js', + '/common/common-ui-elements.js', '/common/common-realtime.js', '/common/hyperscript.js', '/customize/messages.js', @@ -33,6 +34,7 @@ define([ SFCommon, Util, UI, + UIElements, Realtime, h, Messages, @@ -199,6 +201,16 @@ define([ 'class': 'btn btn-success cp-app-profile-friend-request', }).appendTo(APP.$friend); + // If this curve has sent us a friend request, we should not be able to sent it to them + var friendRequests = common.getFriendRequests(); + if (friendRequests[data.curvePublic]) { + $button.html(Messages._getKey('friendRequest_received', [data.name || Messages.anonymous])) + .click(function () { + UIElements.displayFriendRequestModal(common, friendRequests[data.curvePublic]); + }); + return; + } + var pendingFriends = APP.common.getPendingFriends(); // Friend requests sent if (pendingFriends[data.curvePublic]) { $button.attr('disabled', 'disabled').text(Messages.profile_friendRequestSent); @@ -548,8 +560,19 @@ define([ lm.proxy.on('ready', function () { updateValues(lm.proxy); UI.removeLoadingScreen(); + common.mailbox.subscribe({ + onMessage: function () { + refreshFriendRequest(lm.proxy); + }, + onViewed: function () { + refreshFriendRequest(lm.proxy); + }, + }); }).on('change', [], function () { updateValues(lm.proxy); }); + metadataMgr.onChange(function () { + updateValues(lm.proxy); + }); }); });