Realtime update of the friendship status in the profile

pull/1/head
yflory 6 years ago
parent 6370c0eeae
commit c3e9b51f76

@ -19,7 +19,7 @@ define([
// Display the notification // Display the notification
$(el).find('.cp-notification-content').addClass("cp-clickable"); $(el).find('.cp-notification-content').addClass("cp-clickable");
$(el).find('.cp-notification-content p') $(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 () { .click(function () {
UIElements.displayFriendRequestModal(common, data); UIElements.displayFriendRequestModal(common, data);
}); });
@ -29,7 +29,7 @@ define([
var content = data.content; var content = data.content;
var msg = content.msg; var msg = content.msg;
$(el).find('.cp-notification-content p') $(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'); $(el).find('.cp-notification-dismiss').css('display', 'flex');
}; };
@ -37,7 +37,7 @@ define([
var content = data.content; var content = data.content;
var msg = content.msg; var msg = content.msg;
$(el).find('.cp-notification-content p') $(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'); $(el).find('.cp-notification-dismiss').css('display', 'flex');
}; };

@ -959,6 +959,7 @@ define([
channel: msg.content.notifications, channel: msg.content.notifications,
curvePublic: msg.content.curvePublic curvePublic: msg.content.curvePublic
}, function (obj) { }, function (obj) {
broadcast([], "UPDATE_METADATA");
cb(obj); cb(obj);
}); });
dismiss(); dismiss();

@ -12,9 +12,12 @@ define([
// Store the friend request displayed to avoid duplicates // Store the friend request displayed to avoid duplicates
var friendRequest = {}; 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) // 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 // Don't show duplicate friend request: if we already have a friend request
// in memory from the same user, dismiss the new one // in memory from the same user, dismiss the new one
@ -23,7 +26,8 @@ define([
friendRequest[data.msg.author] = true; friendRequest[data.msg.author] = true;
// If the user is already in our friend list, automatically accept the request // 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) { Messaging.acceptFriendRequest(ctx.store, data.msg.content, function (obj) {
if (obj && obj.error) { if (obj && obj.error) {
return void cb(); return void cb();
@ -36,8 +40,8 @@ define([
cb(); cb();
}; };
removeHandlers['FRIEND_REQUEST'] = function (ctx, box, data) { removeHandlers['FRIEND_REQUEST'] = function (ctx, box, data) {
if (friendRequest[data.curvePublic]) { if (friendRequest[data.content.curvePublic]) {
delete friendRequest[data.curvePublic]; delete friendRequest[data.content.curvePublic];
} }
}; };
@ -51,8 +55,6 @@ define([
delete ctx.store.proxy.friends_pending[data.msg.author]; delete ctx.store.proxy.friends_pending[data.msg.author];
ctx.updateMetadata(); ctx.updateMetadata();
if (friendRequestDeclined[data.msg.author]) { return; } 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({ box.sendMessage({
type: 'FRIEND_REQUEST_DECLINED', type: 'FRIEND_REQUEST_DECLINED',
content: { content: {
@ -69,6 +71,7 @@ define([
cb(true); cb(true);
}; };
handlers['FRIEND_REQUEST_DECLINED'] = function (ctx, box, data, cb) { handlers['FRIEND_REQUEST_DECLINED'] = function (ctx, box, data, cb) {
ctx.updateMetadata();
if (friendRequestDeclined[data.msg.content.user]) { return void cb(true); } if (friendRequestDeclined[data.msg.content.user]) { return void cb(true); }
friendRequestDeclined[data.msg.content.user] = true; friendRequestDeclined[data.msg.content.user] = true;
cb(); cb();
@ -97,9 +100,9 @@ define([
ctx.store.messenger.onFriendAdded(data.msg.content); ctx.store.messenger.onFriendAdded(data.msg.content);
} }
ctx.updateMetadata(); ctx.updateMetadata();
if (friendRequestAccepted[data.msg.author]) { return; }
// If you have a profile page open, update it // If you have a profile page open, update it
if (ctx.store.modules['profile']) { ctx.store.modules['profile'].update(); } if (ctx.store.modules['profile']) { ctx.store.modules['profile'].update(); }
if (friendRequestAccepted[data.msg.author]) { return; }
// Display the "accepted" state in the UI // Display the "accepted" state in the UI
box.sendMessage({ box.sendMessage({
type: 'FRIEND_REQUEST_ACCEPTED', type: 'FRIEND_REQUEST_ACCEPTED',
@ -118,6 +121,7 @@ define([
cb(true); cb(true);
}; };
handlers['FRIEND_REQUEST_ACCEPTED'] = function (ctx, box, data, cb) { handlers['FRIEND_REQUEST_ACCEPTED'] = function (ctx, box, data, cb) {
ctx.updateMetadata();
if (friendRequestAccepted[data.msg.content.user]) { return void cb(true); } if (friendRequestAccepted[data.msg.content.user]) { return void cb(true); }
friendRequestAccepted[data.msg.content.user] = true; friendRequestAccepted[data.msg.content.user] = true;
cb(); cb();
@ -131,6 +135,7 @@ define([
return { return {
add: function (ctx, box, data, cb) { add: function (ctx, box, data, cb) {
if (!data.msg) { return void cb(true); }
var type = data.msg.type; var type = data.msg.type;
if (handlers[type]) { if (handlers[type]) {

@ -135,6 +135,7 @@ define([
removeClient(ctx, clientId); removeClient(ctx, clientId);
}; };
profile.update = function () { profile.update = function () {
if (!ctx.listmap) { return; }
ctx.emit('UPDATE', ctx.listmap.proxy, ctx.clients); ctx.emit('UPDATE', ctx.listmap.proxy, ctx.clients);
}; };
profile.execCommand = function (clientId, obj, cb) { profile.execCommand = function (clientId, obj, cb) {

@ -7,6 +7,7 @@ define([
'/common/sframe-common.js', '/common/sframe-common.js',
'/common/common-util.js', '/common/common-util.js',
'/common/common-interface.js', '/common/common-interface.js',
'/common/common-ui-elements.js',
'/common/common-realtime.js', '/common/common-realtime.js',
'/common/hyperscript.js', '/common/hyperscript.js',
'/customize/messages.js', '/customize/messages.js',
@ -33,6 +34,7 @@ define([
SFCommon, SFCommon,
Util, Util,
UI, UI,
UIElements,
Realtime, Realtime,
h, h,
Messages, Messages,
@ -199,6 +201,16 @@ define([
'class': 'btn btn-success cp-app-profile-friend-request', 'class': 'btn btn-success cp-app-profile-friend-request',
}).appendTo(APP.$friend); }).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 var pendingFriends = APP.common.getPendingFriends(); // Friend requests sent
if (pendingFriends[data.curvePublic]) { if (pendingFriends[data.curvePublic]) {
$button.attr('disabled', 'disabled').text(Messages.profile_friendRequestSent); $button.attr('disabled', 'disabled').text(Messages.profile_friendRequestSent);
@ -548,8 +560,19 @@ define([
lm.proxy.on('ready', function () { lm.proxy.on('ready', function () {
updateValues(lm.proxy); updateValues(lm.proxy);
UI.removeLoadingScreen(); UI.removeLoadingScreen();
common.mailbox.subscribe({
onMessage: function () {
refreshFriendRequest(lm.proxy);
},
onViewed: function () {
refreshFriendRequest(lm.proxy);
},
});
}).on('change', [], function () { }).on('change', [], function () {
updateValues(lm.proxy); updateValues(lm.proxy);
}); });
metadataMgr.onChange(function () {
updateValues(lm.proxy);
});
}); });
}); });

Loading…
Cancel
Save