Improve contact requests process

pull/1/head
yflory 4 years ago
parent e405dcff7e
commit 892fca7d99

@ -1330,13 +1330,16 @@ define([
store.proxy.friends_pending = store.proxy.friends_pending || {}; store.proxy.friends_pending = store.proxy.friends_pending || {};
var twoDaysAgo = +new Date() - (2 * 24 * 3600 * 1000); var p = store.proxy.friends_pending[data.curvePublic];
if (store.proxy.friends_pending[data.curvePublic] && if (p) {
store.proxy.friends_pending[data.curvePublic] > twoDaysAgo) { return void cb({error: 'ALREADY_SENT'});
return void cb({error: 'TIMEOUT'});
} }
store.proxy.friends_pending[data.curvePublic] = +new Date(); store.proxy.friends_pending[data.curvePublic] = {
time: +new Date(),
channel: data.notifications,
curvePublic: data.curvePublic
};
broadcast([], "UPDATE_METADATA"); broadcast([], "UPDATE_METADATA");
store.mailbox.sendTo('FRIEND_REQUEST', { store.mailbox.sendTo('FRIEND_REQUEST', {
@ -1348,6 +1351,37 @@ define([
cb(obj); cb(obj);
}); });
}; };
Store.cancelFriendRequest = function (data, cb) {
if (!data.curvePublic || !data.notifications) {
return void cb({error: 'EINVAL'});
}
var proxy = store.proxy;
var f = Messaging.getFriend(proxy, data.curvePublic);
if (f) {
// Already friend
console.error("You can't cancel an accepted friend request");
return void cb({error: 'ALREADY_FRIEND'});
}
var pending = Util.find(store, ['proxy', 'friends_pending']) || {};
if (!pending) { return void cb(); }
store.mailbox.sendTo('CANCEL_FRIEND_REQUEST', {
user: Messaging.createData(store.proxy)
}, {
channel: data.notifications,
curvePublic: data.curvePublic
}, function (obj) {
if (obj && obj.error) { return void cb(obj); }
delete store.proxy.friends_pending[data.curvePublic];
broadcast([], "UPDATE_METADATA");
onSync(null, function () {
cb(obj);
});
});
};
Store.anonGetPreviewContent = function (clientId, data, cb) { Store.anonGetPreviewContent = function (clientId, data, cb) {
Team.anonGetPreviewContent({ Team.anonGetPreviewContent({
@ -2448,18 +2482,6 @@ define([
}); });
}; };
var cleanFriendRequests = function () {
try {
if (!store.proxy.friends_pending) { return; }
var twoDaysAgo = +new Date() - (2 * 24 * 3600 * 1000);
Object.keys(store.proxy.friends_pending).forEach(function (curve) {
if (store.proxy.friends_pending[curve] < twoDaysAgo) {
delete store.proxy.friends_pending[curve];
}
});
} catch (e) {}
};
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
/////////////////////// Init ///////////////////////////////////// /////////////////////// Init /////////////////////////////////////
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
@ -2543,7 +2565,6 @@ define([
loadUniversal(Profile, 'profile', waitFor); loadUniversal(Profile, 'profile', waitFor);
loadUniversal(Team, 'team', waitFor, clientId); loadUniversal(Team, 'team', waitFor, clientId);
loadUniversal(History, 'history', waitFor); loadUniversal(History, 'history', waitFor);
cleanFriendRequests();
}).nThen(function () { }).nThen(function () {
var requestLogin = function () { var requestLogin = function () {
broadcast([], "REQUEST_LOGIN"); broadcast([], "REQUEST_LOGIN");

@ -33,11 +33,15 @@ define([
// in memory from the same user, dismiss the new one // in memory from the same user, dismiss the new one
if (friendRequest[data.msg.author]) { return void cb(true); } if (friendRequest[data.msg.author]) { return void cb(true); }
friendRequest[data.msg.author] = true; friendRequest[data.msg.author] = {
type: box.type,
hash: data.hash
};
// 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]) { ctx.store.proxy.friends_pending[data.msg.author]) {
delete ctx.store.proxy.friends_pending[data.msg.author]; delete ctx.store.proxy.friends_pending[data.msg.author];
Messaging.acceptFriendRequest(ctx.store, userData, function (obj) { Messaging.acceptFriendRequest(ctx.store, userData, function (obj) {
@ -49,14 +53,17 @@ define([
realtime: ctx.store.realtime, realtime: ctx.store.realtime,
pinPads: ctx.pinPads pinPads: ctx.pinPads
}, userData, function (err) { }, userData, function (err) {
if (err) { return void console.error(err); } if (err) {
console.error(err);
return void cb(true);
}
if (ctx.store.messenger) { if (ctx.store.messenger) {
ctx.store.messenger.onFriendAdded(userData); ctx.store.messenger.onFriendAdded(userData);
} }
});
ctx.updateMetadata(); ctx.updateMetadata();
cb(true); cb(true);
}); });
});
return; return;
} }
@ -97,20 +104,29 @@ define([
ctx.updateMetadata(); ctx.updateMetadata();
if (friendRequestDeclined[data.msg.author]) { return; } if (friendRequestDeclined[data.msg.author]) { return; }
friendRequestDeclined[data.msg.author] = true;
box.sendMessage({ box.sendMessage({
type: 'FRIEND_REQUEST_DECLINED', type: 'FRIEND_REQUEST_DECLINED',
content: { user: userData } content: { user: userData }
}, function () {}); }, function (hash) {
friendRequestDeclined[data.msg.author] = {
type: box.type,
hash: hash
};
});
}, getRandomTimeout(ctx)); }, getRandomTimeout(ctx));
}; };
// UI for declined friend request // UI for declined friend request
handlers['FRIEND_REQUEST_DECLINED'] = function (ctx, box, data, cb) { handlers['FRIEND_REQUEST_DECLINED'] = function (ctx, box, data, cb) {
ctx.updateMetadata(); ctx.updateMetadata();
var curve = data.msg.content.user.curvePublic || data.msg.content.user; var curve = data.msg.content.user.curvePublic || data.msg.content.user;
if (friendRequestDeclined[curve]) { return void cb(true); } var toRemove = friendRequestAccepted[curve];
friendRequestDeclined[curve] = true; delete friendRequestAccepted[curve];
cb(); if (friendRequestDeclined[curve]) { return void cb(true, toRemove); }
friendRequestDeclined[curve] = {
type: box.type,
hash: data.hash
};
cb(false, toRemove);
}; };
removeHandlers['FRIEND_REQUEST_DECLINED'] = function (ctx, box, data) { removeHandlers['FRIEND_REQUEST_DECLINED'] = function (ctx, box, data) {
var curve = data.content.user.curvePublic || data.content.user; var curve = data.content.user.curvePublic || data.content.user;
@ -148,11 +164,15 @@ define([
if (ctx.store.modules['profile']) { ctx.store.modules['profile'].update(); } if (ctx.store.modules['profile']) { ctx.store.modules['profile'].update(); }
// Display the "accepted" state in the UI // Display the "accepted" state in the UI
if (friendRequestAccepted[data.msg.author]) { return; } if (friendRequestAccepted[data.msg.author]) { return; }
friendRequestAccepted[data.msg.author] = true;
box.sendMessage({ box.sendMessage({
type: 'FRIEND_REQUEST_ACCEPTED', type: 'FRIEND_REQUEST_ACCEPTED',
content: { user: userData } content: { user: userData }
}, function () {}); }, function (hash) {
friendRequestAccepted[data.msg.author] = {
type: box.type,
hash: hash
};
});
}); });
}, getRandomTimeout(ctx)); }, getRandomTimeout(ctx));
}; };
@ -160,20 +180,32 @@ define([
handlers['FRIEND_REQUEST_ACCEPTED'] = function (ctx, box, data, cb) { handlers['FRIEND_REQUEST_ACCEPTED'] = function (ctx, box, data, cb) {
ctx.updateMetadata(); ctx.updateMetadata();
var curve = data.msg.content.user.curvePublic || data.msg.content.user; var curve = data.msg.content.user.curvePublic || data.msg.content.user;
if (friendRequestAccepted[curve]) { return void cb(true); } var toRemove = friendRequestDeclined[curve];
friendRequestAccepted[curve] = true; delete friendRequestDeclined[curve];
cb(); if (friendRequestAccepted[curve]) { return void cb(true, toRemove); }
friendRequestAccepted[curve] = {
type: box.type,
hash: data.hash
};
cb(false, toRemove);
}; };
removeHandlers['FRIEND_REQUEST_ACCEPTED'] = function (ctx, box, data) { removeHandlers['FRIEND_REQUEST_ACCEPTED'] = function (ctx, box, data) {
var curve = data.content.user.curvePublic || data.content.user; var curve = data.content.user.curvePublic || data.content.user;
if (friendRequestAccepted[curve]) { delete friendRequestAccepted[curve]; } if (friendRequestAccepted[curve]) { delete friendRequestAccepted[curve]; }
}; };
handlers['CANCEL_FRIEND_REQUEST'] = function (ctx, box, data, cb) {
var f = friendRequest[data.msg.author];
if (!f) { return void cb(true); }
cb(true, f);
};
handlers['UNFRIEND'] = function (ctx, box, data, cb) { handlers['UNFRIEND'] = function (ctx, box, data, cb) {
var curve = data.msg.author; var curve = data.msg.author;
var friend = Messaging.getFriend(ctx.store.proxy, curve); var friend = Messaging.getFriend(ctx.store.proxy, curve);
if (!friend) { return void cb(true); } if (!friend) { return void cb(true); }
delete ctx.store.proxy.friends[curve]; delete ctx.store.proxy.friends[curve];
delete ctx.store.proxy.friends_pending[curve];
if (ctx.store.messenger) { if (ctx.store.messenger) {
ctx.store.messenger.onFriendRemoved(curve, friend.channel); ctx.store.messenger.onFriendRemoved(curve, friend.channel);
} }

@ -271,7 +271,7 @@ proxy.mailboxes = {
hash: hash hash: hash
}; };
showMessage(ctx, type, message); showMessage(ctx, type, message);
cb(); cb(hash);
}, keys.curvePublic); }, keys.curvePublic);
}; };
box.queue.forEach(function (msg) { box.queue.forEach(function (msg) {

@ -459,6 +459,13 @@ define([
} }
}; };
// Cancel pending friend requests
var cancelFriend = function (ctx, data, _cb) {
var cb = Util.once(_cb);
if (typeof(cb) !== 'function') { return void console.error('NO_CALLBACK'); }
ctx.Store.cancelFriendRequest(data, cb);
};
var getAllClients = function (ctx) { var getAllClients = function (ctx) {
var all = []; var all = [];
Array.prototype.push.apply(all, ctx.friendsClients); Array.prototype.push.apply(all, ctx.friendsClients);
@ -935,6 +942,7 @@ define([
if (AppConfig.availablePadTypes.indexOf('contacts') === -1) { return; } if (AppConfig.availablePadTypes.indexOf('contacts') === -1) { return; }
var ctx = { var ctx = {
store: store, store: store,
Store: cfg.Store,
updateMetadata: cfg.updateMetadata, updateMetadata: cfg.updateMetadata,
pinPads: cfg.pinPads, pinPads: cfg.pinPads,
emit: emit, emit: emit,
@ -1047,6 +1055,9 @@ define([
if (cmd === 'REMOVE_FRIEND') { if (cmd === 'REMOVE_FRIEND') {
return void removeFriend(ctx, data, cb); return void removeFriend(ctx, data, cb);
} }
if (cmd === 'CANCEL_FRIEND') {
return void cancelFriend(ctx, data, cb);
}
if (cmd === 'MUTE_USER') { if (cmd === 'MUTE_USER') {
return void muteUser(ctx, data, cb); return void muteUser(ctx, data, cb);
} }

@ -229,7 +229,6 @@ MessengerUI, Messages) {
// Editors // Editors
var pendingFriends = Common.getPendingFriends(); // Friend requests sent var pendingFriends = Common.getPendingFriends(); // Friend requests sent
var friendRequests = Common.getFriendRequests(); // Friend requests received var friendRequests = Common.getFriendRequests(); // Friend requests received
var friendTo = +new Date() - (2 * 24 * 3600 * 1000);
editUsersNames.forEach(function (data) { editUsersNames.forEach(function (data) {
var name = data.name || Messages.anonymous; var name = data.name || Messages.anonymous;
var $span = $('<span>', {'class': 'cp-avatar'}); var $span = $('<span>', {'class': 'cp-avatar'});
@ -297,7 +296,7 @@ MessengerUI, Messages) {
} }
} else if (Common.isLoggedIn() && data.curvePublic && !friends[data.curvePublic] } else if (Common.isLoggedIn() && data.curvePublic && !friends[data.curvePublic]
&& !priv.readOnly) { && !priv.readOnly) {
if (pendingFriends[data.curvePublic] && pendingFriends[data.curvePublic] > friendTo) { if (pendingFriends[data.curvePublic]) {
$('<button>', { $('<button>', {
'class': 'fa fa-hourglass-half cp-toolbar-userlist-button', 'class': 'fa fa-hourglass-half cp-toolbar-userlist-button',
'title': Messages.profile_friendRequestSent 'title': Messages.profile_friendRequestSent
@ -322,7 +321,10 @@ MessengerUI, Messages) {
}).appendTo($nameSpan).click(function (e) { }).appendTo($nameSpan).click(function (e) {
e.stopPropagation(); e.stopPropagation();
Common.sendFriendRequest(data, function (err, obj) { Common.sendFriendRequest(data, function (err, obj) {
if (err || (obj && obj.error)) { return void console.error(err || obj.error); } if (err || (obj && obj.error)) {
UI.warn(Messages.error);
return void console.error(err || obj.error);
}
}); });
}); });
} }

@ -243,10 +243,35 @@ define([
return; return;
} }
Messages.contacts_confirmCancel = "Are you sure you want to cancel your contact request with <b>{0}</b>?"; // XXX
var addCancel = function () {
var cancelButton = h('button.btn.btn-danger.cp-app-profile-friend-request', [
h('i.fa.fa-user-times'),
Messages.cancel
]);
$(cancelButton).click(function () {
// Unfriend confirm
var content = h('div', [
UI.setHTML(h('p'), Messages._getKey('contacts_confirmCancel', [name]))
]);
UI.confirm(content, function (yes) {
if (!yes) { return; }
module.execCommand('CANCEL_FRIEND', {
curvePublic: data.curvePublic,
notifications: data.notifications
}, function (e) {
refreshFriendRequest(data);
if (e) { UI.warn(Messages.error); return void console.error(e); }
});
});
}).appendTo(APP.$friend);
};
// Pending friend (we've sent a friend request) // Pending friend (we've sent a friend request)
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').append(Messages.profile_friendRequestSent); $button.attr('disabled', 'disabled').append(Messages.profile_friendRequestSent);
addCancel();
return; return;
} }
// This is not a friend yet: we can send a friend request // This is not a friend yet: we can send a friend request
@ -255,8 +280,10 @@ define([
APP.common.sendFriendRequest({ APP.common.sendFriendRequest({
curvePublic: data.curvePublic, curvePublic: data.curvePublic,
notifications: data.notifications notifications: data.notifications
}, function () { }, function (err, obj) {
if (obj && obj.error) { return void UI.warn(Messages.error); }
$button.attr('disabled', 'disabled').append(Messages.profile_friendRequestSent); $button.attr('disabled', 'disabled').append(Messages.profile_friendRequestSent);
addCancel();
}); });
}); });
}; };

Loading…
Cancel
Save