diff --git a/www/common/common-messaging.js b/www/common/common-messaging.js index 73bcc6bd8..d26d664dc 100644 --- a/www/common/common-messaging.js +++ b/www/common/common-messaging.js @@ -5,6 +5,8 @@ define([ var pending = {}; + // Remove should be called from the friend app at the moment + // The other user will know it from the private channel ("REMOVE_FRIEND" message?) Msg.removeFromFriendList = function (common, edPublic, cb) { var proxy = common.getProxy(); if (!proxy.friends) { @@ -28,6 +30,7 @@ define([ friends[pubKey] = data; common.whenRealtimeSyncs(common.getRealtime(), cb); + common.changeDisplayName(proxy[common.displayNameKey]); }; var createData = function (common, hash) { @@ -41,9 +44,15 @@ define([ }; }; + var getFriend = function (common, pubkey) { + var proxy = common.getProxy(); + return proxy.friends ? proxy.friends[pubkey] : undefined; + }; + Msg.addDirectMessageHandler = function (common) { var network = common.getNetwork(); if (!network) { return void console.error('Network not ready'); } + var proxy = common.getProxy(); network.on('message', function (message, sender) { var msg; if (sender === network.historyKeeper) { return; } @@ -60,15 +69,23 @@ define([ msg = JSON.parse(decryptMsg); if (msg[1] !== parsed.hashData.channel) { return; } var msgData = msg[2]; + var msg; + var msgStr; if (msg[0] === "FRIEND_REQ") { + msg = ["FRIEND_REQ_NOK", chan]; + var existing = getFriend(common, msgData.edPublic); + if (existing) { + msg = ["FRIEND_REQ_OK", chan, createData(common, existing.channelHash)]; + msgStr = Crypto.encrypt(JSON.stringify(msg), key); + network.sendto(sender, msgStr); + return; + } common.confirm("Accept friend?", function (yes) { // XXX - var msg = ["FRIEND_REQ_NOK", chan]; if (yes) { pending[sender] = msgData; msg = ["FRIEND_REQ_OK", chan, createData(common, msgData.channelHash)]; } - var msgStr = Crypto.encrypt(JSON.stringify(msg), key); - // Send encrypted message + msgStr = Crypto.encrypt(JSON.stringify(msg), key); network.sendto(sender, msgStr); }); return; diff --git a/www/common/common-userlist.js b/www/common/common-userlist.js index 2e1fe4adf..fc0f40931 100644 --- a/www/common/common-userlist.js +++ b/www/common/common-userlist.js @@ -52,7 +52,8 @@ define(function () { name: exp.myUserName, uid: Cryptpad.getUid(), avatar: Cryptpad.getAvatarUrl(), - profile: Cryptpad.getProfileUrl() + profile: Cryptpad.getProfileUrl(), + edPublic: Cryptpad.getProxy().edPublic }; addToUserData(myData); Cryptpad.setAttribute('username', exp.myUserName, function (err) { @@ -81,7 +82,8 @@ define(function () { name: "", uid: Cryptpad.getUid(), avatar: Cryptpad.getAvatarUrl(), - profile: Cryptpad.getProfileUrl() + profile: Cryptpad.getProfileUrl(), + edPublic: Cryptpad.getProxy().edPublic }; addToUserData(myData); onLocal(); diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index e58ba3875..3df7b6766 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -153,6 +153,14 @@ define([ } return; }; + common.getUserlist = function () { + if (store) { + if (store.getProxy() && store.getProxy().info) { + return store.getProxy().info.userList; + } + } + return; + }; common.getProfileUrl = function () { if (store && store.getProfile()) { return store.getProfile().view; diff --git a/www/common/fsStore.js b/www/common/fsStore.js index 880429ed9..48267e756 100644 --- a/www/common/fsStore.js +++ b/www/common/fsStore.js @@ -219,6 +219,10 @@ define([ // Trigger userlist update when the avatar has changed Cryptpad.changeDisplayName(proxy[Cryptpad.displayNameKey]); }); + proxy.on('change', ['friends'], function () { + // Trigger userlist update when the avatar has changed + Cryptpad.changeDisplayName(proxy[Cryptpad.displayNameKey]); + }); proxy.on('change', [tokenKey], function () { var localToken = tryParsing(localStorage.getItem(tokenKey)); if (localToken !== proxy[tokenKey]) { diff --git a/www/common/toolbar2.js b/www/common/toolbar2.js index ba564d9cb..340a74af1 100644 --- a/www/common/toolbar2.js +++ b/www/common/toolbar2.js @@ -210,13 +210,16 @@ define([ var $span = $('', {'title': name}); var $rightCol = $('', {'class': 'right-col'}); $('', {'class': 'name'}).text(name).appendTo($rightCol); - // TODO: if account - var $button = $('