From 88cb4fd83ce4e9a3cb26fc5e5701e54d9390ffa6 Mon Sep 17 00:00:00 2001 From: yflory Date: Mon, 28 Aug 2017 16:21:05 +0200 Subject: [PATCH] Enable friend requests from secure pads --- www/common/common-messaging.js | 28 ++++++++++++++++++++++------ www/common/cryptpad-common.js | 7 +++++++ www/common/metadata-manager.js | 3 +++ www/common/sframe-common.js | 21 +++++++++++++++++++++ www/common/sframe-protocol.js | 4 ++++ www/common/toolbar3.js | 21 +++++++++++---------- www/pad/main.js | 15 ++++++++++++++- 7 files changed, 82 insertions(+), 17 deletions(-) diff --git a/www/common/common-messaging.js b/www/common/common-messaging.js index 7d75a7393..679694188 100644 --- a/www/common/common-messaging.js +++ b/www/common/common-messaging.js @@ -134,7 +134,8 @@ define([ var confirmMsg = common.Messages._getKey('contacts_request', [ common.fixHTML(msgData.displayName) ]); - common.confirm(confirmMsg, todo, null, true); + common.onFriendRequest(confirmMsg, todo); + //common.confirm(confirmMsg, todo, null, true); return; } if (msg[0] === "FRIEND_REQ_OK") { @@ -144,9 +145,15 @@ define([ // FIXME clarify this function's name addToFriendList(common, msgData, function (err) { if (err) { - return void common.log(common.Messages.contacts_addError); + return void Cryptpad.onFriendComplete({ + logText: common.Messages.contacts_addError, + netfluxId: sender + }); } - common.log(common.Messages.contacts_added); + Cryptpad.onFriendComplete({ + logText: common.Messages.contacts_added, + netfluxId: sender + }); var msg = ["FRIEND_REQ_ACK", chan]; var msgStr = Crypto.encrypt(JSON.stringify(msg), key); network.sendto(sender, msgStr); @@ -156,7 +163,10 @@ define([ if (msg[0] === "FRIEND_REQ_NOK") { var i = pendingRequests.indexOf(sender); if (i !== -1) { pendingRequests.splice(i, 1); } - common.log(common.Messages.contacts_rejected); + Cryptpad.onFriendComplete({ + logText: common.Messages.contacts_rejected, + netfluxId: sender + }); common.changeDisplayName(proxy[common.displayNameKey]); return; } @@ -165,9 +175,15 @@ define([ if (!data) { return; } addToFriendList(common, data, function (err) { if (err) { - return void common.log(common.Messages.contacts_addError); + return void Cryptpad.onFriendComplete({ + logText: common.Messages.contacts_addError, + netfluxId: sender + }); } - common.log(common.Messages.contacts_added); + Cryptpad.onFriendComplete({ + logText: common.Messages.contacts_added, + netfluxId: sender + }); }); return; } diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index f1501883a..ef55f6f6b 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -1922,6 +1922,13 @@ define([ delete sessionStorage[newPadPathKey]; } + common.onFriendRequest = function (confirmText, cb) { + common.confirm(confirmText, cb, null, true); + }; + common.onFriendComplete = function (data) { + common.log(data.logText); + }; + Store.ready(function (err, storeObj) { store = common.store = env.store = storeObj; common.addDirectMessageHandler(common); diff --git a/www/common/metadata-manager.js b/www/common/metadata-manager.js index 863a6a86e..cba796e53 100644 --- a/www/common/metadata-manager.js +++ b/www/common/metadata-manager.js @@ -132,6 +132,9 @@ define([], function () { getPrivateData : function () { return priv; }, + getUserData : function () { + return meta.user; + }, getNetfluxId : function () { return meta.user.netfluxId; } diff --git a/www/common/sframe-common.js b/www/common/sframe-common.js index 8c0b07a15..59debfc32 100644 --- a/www/common/sframe-common.js +++ b/www/common/sframe-common.js @@ -103,6 +103,17 @@ define([ ctx.sframeChan.query('Q_GET_FULL_HISTORY', null, cb); }; + // Friends + var pendingFriends = []; + funcs.getPendingFriends = function () { + return pendingFriends.slice(); + }; + funcs.sendFriendRequest = function (netfluxId) { + ctx.sframeChan.query('Q_SEND_FRIEND_REQUEST', netfluxId, $.noop); + pendingFriends.push(netfluxId); + }; + + // Feedback funcs.feedback = function (action, force) { if (force !== true) { if (!action) { return; } @@ -297,6 +308,16 @@ define([ ctx.sframeChan.on('EV_RT_CONNECT', function () { CommonRealtime.setConnectionState(true); }); ctx.sframeChan.on('EV_RT_DISCONNECT', function () { CommonRealtime.setConnectionState(false); }); + + ctx.sframeChan.on('Q_INCOMING_FRIEND_REQUEST', function (confirmMsg, cb) { + Cryptpad.confirm(confirmMsg, cb, null, true); + }); + ctx.sframeChan.on('EV_FRIEND_REQUEST', function (data) { + var i = pendingFriends.indexOf(data.sender); + if (i !== -1) { pendingFriends.splice(i, 1); } + Cryptpad.log(data.logText); + }); + cb(funcs); }); } }; diff --git a/www/common/sframe-protocol.js b/www/common/sframe-protocol.js index 7b0a41222..70151e58f 100644 --- a/www/common/sframe-protocol.js +++ b/www/common/sframe-protocol.js @@ -74,4 +74,8 @@ define({ // Save a pad as a template using the toolbar button 'Q_SAVE_AS_TEMPLATE': true, + // Friend requests from the userlist + 'Q_SEND_FRIEND_REQUEST': true, // Up query + 'Q_INCOMING_FRIEND_REQUEST': true, // Down query + 'EV_FRIEND_REQUEST': true, // Down event when the request is complete }); diff --git a/www/common/toolbar3.js b/www/common/toolbar3.js index a3a6df89b..c8faa738a 100644 --- a/www/common/toolbar3.js +++ b/www/common/toolbar3.js @@ -156,7 +156,10 @@ define([ var metadataMgr = config.metadataMgr; var userData = metadataMgr.getMetadata().users; var viewers = metadataMgr.getViewers(); - var origin = config.metadataMgr.getPrivateData().origin; + var priv = metadataMgr.getPrivateData(); + var origin = priv.origin; + var friends = priv.friends; + var user = metadataMgr.getUserData(); // If we are using old pads (readonly unavailable), only editing users are in userList. // With new pads, we also have readonly users in userList, so we have to intersect with @@ -186,22 +189,20 @@ define([ // Editors // TODO iframe enable friends - //var pendingFriends = Cryptpad.getPendingInvites(); + var pendingFriends = Common.getPendingFriends(); editUsersNames.forEach(function (data) { var name = data.name || Messages.anonymous; var $span = $('', {'class': 'avatar'}); var $rightCol = $('', {'class': 'right-col'}); - $('', {'class': 'name'}).text(name).appendTo($rightCol); - //var proxy = Cryptpad.getProxy(); - //var isMe = data.curvePublic === proxy.curvePublic; - - /*if (Cryptpad.isLoggedIn() && data.curvePublic) { + var $nameSpan = $('', {'class': 'name'}).text(name).appendTo($rightCol); + var isMe = data.curvePublic === user.curvePublic; + if (Common.isLoggedIn() && data.curvePublic) { if (isMe) { $span.attr('title', Messages._getKey('userlist_thisIsYou', [ name ])); $nameSpan.text(name); - } else if (!proxy.friends || !proxy.friends[data.curvePublic]) { + } else if (!friends[data.curvePublic]) { if (pendingFriends.indexOf(data.netfluxId) !== -1) { $('', {'class': 'friend'}).text(Messages.userlist_pending) .appendTo($rightCol); @@ -213,11 +214,11 @@ define([ ]) }).appendTo($rightCol).click(function (e) { e.stopPropagation(); - Cryptpad.inviteFromUserlist(Cryptpad, data.netfluxId); + Common.sendFriendRequest(data.netfluxId); }); } } - }*/ + } if (data.profile) { $span.addClass('clickable'); $span.click(function () { diff --git a/www/pad/main.js b/www/pad/main.js index a01dee3cd..2225f191d 100644 --- a/www/pad/main.js +++ b/www/pad/main.js @@ -104,7 +104,8 @@ define([ readOnly: readOnly, availableHashes: hashes, isTemplate: Cryptpad.isTemplate(window.location.href), - feedbackAllowed: Cryptpad.isFeedbackAllowed() + feedbackAllowed: Cryptpad.isFeedbackAllowed(), + friends: Cryptpad.getProxy().friends || {} } }); }); @@ -173,6 +174,18 @@ define([ Cryptpad.saveAsTemplate(Cryptget.put, data, cb); }); + sframeChan.on('Q_SEND_FRIEND_REQUEST', function (netfluxId, cb) { + Cryptpad.inviteFromUserlist(Cryptpad, netfluxId); + }); + Cryptpad.onFriendRequest = function (confirmText, cb) { + sframeChan.query('Q_INCOMING_FRIEND_REQUEST', confirmText, function (err, data) { + cb(data); + }); + }; + Cryptpad.onFriendComplete = function (data) { + sframeChan.event('EV_FRIEND_REQUEST', data); + }; + sframeChan.on('Q_GET_FULL_HISTORY', function (data, cb) { var network = Cryptpad.getNetwork(); var hkn = network.historyKeeper;