From 9d8433d10468c47aa66e929f11957ff18809b866 Mon Sep 17 00:00:00 2001 From: yflory Date: Mon, 27 May 2019 18:58:35 +0200 Subject: [PATCH] Unfriend via mailbox --- www/common/common-messenger.js | 32 +++++++++++++++++++--------- www/common/outer/mailbox-handlers.js | 10 +++++++++ 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/www/common/common-messenger.js b/www/common/common-messenger.js index f34c177ca..05ebfea54 100644 --- a/www/common/common-messenger.js +++ b/www/common/common-messenger.js @@ -564,7 +564,8 @@ define([ onDirectMessage(msg, sender); }); - var removeFriend = function (curvePublic, cb) { + var removeFriend = function (curvePublic, _cb) { + var cb = Util.once(_cb); if (typeof(cb) !== 'function') { throw new Error('NO_CALLBACK'); } var data = getFriend(proxy, curvePublic); @@ -590,18 +591,29 @@ define([ var cryptMsg = channel.encrypt(msgStr); try { - channel.wc.bcast(cryptMsg).then(function () { - removeFromFriendList(curvePublic, function () { - delete channels[channel.id]; - emit('UNFRIEND', { - curvePublic: curvePublic, - fromMe: true + if (store.mailbox && data.curvePublic && data.notifications) { + store.mailbox.sendTo('UNFRIEND', { + curvePublic: proxy.curvePublic + }, { + channel: data.notifications, + curvePublic: data.curvePublic + }, function (obj) { + console.log(obj); + if (obj && obj.error) { + return void cb(obj); + } + removeFromFriendList(curvePublic, function () { + delete channels[channel.id]; + emit('UNFRIEND', { + curvePublic: curvePublic, + fromMe: true + }); + cb(); }); - cb(); }); - }, function (err) { + } + channel.wc.bcast(cryptMsg).then(function () {}, function (err) { console.error(err); - cb({error: err}); }); } catch (e) { cb({error: e}); diff --git a/www/common/outer/mailbox-handlers.js b/www/common/outer/mailbox-handlers.js index f8d2c8ae8..360634187 100644 --- a/www/common/outer/mailbox-handlers.js +++ b/www/common/outer/mailbox-handlers.js @@ -132,6 +132,16 @@ define([ } }; + handlers['UNFRIEND'] = function (ctx, box, data, cb) { + var curve = data.msg.content.curvePublic; + var friend = Messaging.getFriend(ctx.store.proxy, curve); + if (!friend) { return void cb(true); } + delete ctx.store.proxy.friends[curve]; + if (ctx.store.messenger) { + ctx.store.messenger.onFriendRemoved(curve, friend.channel); + } + cb(true); + }; return { add: function (ctx, box, data, cb) {