Remove the ACCEPT or DECLINE friend request messages instantly

This commit is contained in:
yflory 2019-05-27 11:18:53 +02:00
parent d514854724
commit 8dd557a0f6
3 changed files with 65 additions and 33 deletions

View File

@ -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 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.displayName])); .html(Messages._getKey('friendRequest_accepted', [msg.content.name]));
$(el).find('.cp-notification-dismiss').css('display', 'flex'); $(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 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.displayName])); .html(Messages._getKey('friendRequest_declined', [msg.content.name]));
$(el).find('.cp-notification-dismiss').css('display', 'flex'); $(el).find('.cp-notification-dismiss').css('display', 'flex');
}; };

View File

@ -2,11 +2,19 @@ define([
'/common/common-messaging.js', '/common/common-messaging.js',
], function (Messaging) { ], 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 = {}; var handlers = {};
handlers['FRIEND_REQUEST'] = function (ctx, data, cb) { handlers['FRIEND_REQUEST'] = function (ctx, data, cb) {
if (data.msg.author === data.msg.content.curvePublic && // Check if the request is valid (send by the correct user)
Messaging.getFriend(ctx.store.proxy, data.msg.author)) { 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) { Messaging.acceptFriendRequest(ctx.store, data.msg.content, function (obj) {
if (obj && obj.error) { return void cb(); } if (obj && obj.error) { return void cb(); }
cb(true); cb(true);
@ -16,30 +24,51 @@ define([
cb(); cb();
}; };
handlers['DECLINE_FRIEND_REQUEST'] = function (ctx, box, data, cb) { handlers['DECLINE_FRIEND_REQUEST'] = function (ctx, box, data, cb) {
// Our friend request was declined. setTimeout(function () {
if (!ctx.store.proxy.friends_pending[data.msg.author]) { return void cb(); } // Our friend request was declined.
delete ctx.store.proxy.friends_pending[data.msg.author]; if (!ctx.store.proxy.friends_pending[data.msg.author]) { return; }
ctx.updateMetadata();
cb(); // 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) { handlers['ACCEPT_FRIEND_REQUEST'] = function (ctx, box, data, cb) {
// Our friend request was accepted. // Our friend request was accepted.
// Make sure we really sent it setTimeout(function () {
if (!ctx.store.proxy.friends_pending[data.msg.author]) { return void cb(); } // Make sure we really sent it
// And add the friend if (!ctx.store.proxy.friends_pending[data.msg.author]) { return; }
Messaging.addToFriendList({ // And add the friend
proxy: ctx.store.proxy, Messaging.addToFriendList({
realtime: ctx.store.realtime, proxy: ctx.store.proxy,
pinPads: ctx.pinPads realtime: ctx.store.realtime,
}, data.msg.content, function (err) { pinPads: ctx.pinPads
if (err) { console.error(err); } }, data.msg.content, function (err) {
delete ctx.store.proxy.friends_pending[data.msg.author]; if (err) { console.error(err); }
if (ctx.store.messenger) { delete ctx.store.proxy.friends_pending[data.msg.author];
ctx.store.messenger.onFriendAdded(data.msg.content); if (ctx.store.messenger) {
} ctx.store.messenger.onFriendAdded(data.msg.content);
ctx.updateMetadata(); }
}); ctx.updateMetadata();
cb(); // 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) { return function (ctx, box, data, cb) {
@ -49,6 +78,7 @@ define([
try { try {
handlers[type](ctx, box, data, cb); handlers[type](ctx, box, data, cb);
} catch (e) { } catch (e) {
console.error(e);
cb(); cb();
} }
} else { } else {

View File

@ -201,16 +201,19 @@ proxy.mailboxes = {
cfg.onConnect = function (wc, sendMessage) { cfg.onConnect = function (wc, sendMessage) {
// Send a message to our box? // Send a message to our box?
// NOTE: we use our own curvePublic so that we can decrypt our own message :) // NOTE: we use our own curvePublic so that we can decrypt our own message :)
box.sendMessage = function (msg) { box.sendMessage = function (_msg) {
try { try {
msg = JSON.stringify(msg); msg = JSON.stringify(_msg);
} catch (e) { } catch (e) {
console.error(e); console.error(e);
} }
sendMessage(msg, function (err, hash) { sendMessage(msg, function (err, hash) {
if (m.viewed.indexOf(hash) === -1) { box.content[hash] = _msg;
m.viewed.push(hash); var message = {
} msg: _msg,
hash: hash
};
showMessage(ctx, type, message);
}, keys.curvePublic); }, keys.curvePublic);
}; };
box.queue.forEach(function (msg) { 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) { mailbox.post = function (box, type, content) {
var b = ctx.boxes[box]; var b = ctx.boxes[box];
if (!b) { return; } if (!b) { return; }