Hide the friend button when the resquest is accepted

pull/1/head
yflory 8 years ago
parent 96fde4e5ed
commit 5b5a7ab286

@ -5,6 +5,8 @@ define([
var pending = {}; 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) { Msg.removeFromFriendList = function (common, edPublic, cb) {
var proxy = common.getProxy(); var proxy = common.getProxy();
if (!proxy.friends) { if (!proxy.friends) {
@ -28,6 +30,7 @@ define([
friends[pubKey] = data; friends[pubKey] = data;
common.whenRealtimeSyncs(common.getRealtime(), cb); common.whenRealtimeSyncs(common.getRealtime(), cb);
common.changeDisplayName(proxy[common.displayNameKey]);
}; };
var createData = function (common, hash) { 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) { Msg.addDirectMessageHandler = function (common) {
var network = common.getNetwork(); var network = common.getNetwork();
if (!network) { return void console.error('Network not ready'); } if (!network) { return void console.error('Network not ready'); }
var proxy = common.getProxy();
network.on('message', function (message, sender) { network.on('message', function (message, sender) {
var msg; var msg;
if (sender === network.historyKeeper) { return; } if (sender === network.historyKeeper) { return; }
@ -60,15 +69,23 @@ define([
msg = JSON.parse(decryptMsg); msg = JSON.parse(decryptMsg);
if (msg[1] !== parsed.hashData.channel) { return; } if (msg[1] !== parsed.hashData.channel) { return; }
var msgData = msg[2]; var msgData = msg[2];
var msg;
var msgStr;
if (msg[0] === "FRIEND_REQ") { 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 common.confirm("Accept friend?", function (yes) { // XXX
var msg = ["FRIEND_REQ_NOK", chan];
if (yes) { if (yes) {
pending[sender] = msgData; pending[sender] = msgData;
msg = ["FRIEND_REQ_OK", chan, createData(common, msgData.channelHash)]; msg = ["FRIEND_REQ_OK", chan, createData(common, msgData.channelHash)];
} }
var msgStr = Crypto.encrypt(JSON.stringify(msg), key); msgStr = Crypto.encrypt(JSON.stringify(msg), key);
// Send encrypted message
network.sendto(sender, msgStr); network.sendto(sender, msgStr);
}); });
return; return;

@ -52,7 +52,8 @@ define(function () {
name: exp.myUserName, name: exp.myUserName,
uid: Cryptpad.getUid(), uid: Cryptpad.getUid(),
avatar: Cryptpad.getAvatarUrl(), avatar: Cryptpad.getAvatarUrl(),
profile: Cryptpad.getProfileUrl() profile: Cryptpad.getProfileUrl(),
edPublic: Cryptpad.getProxy().edPublic
}; };
addToUserData(myData); addToUserData(myData);
Cryptpad.setAttribute('username', exp.myUserName, function (err) { Cryptpad.setAttribute('username', exp.myUserName, function (err) {
@ -81,7 +82,8 @@ define(function () {
name: "", name: "",
uid: Cryptpad.getUid(), uid: Cryptpad.getUid(),
avatar: Cryptpad.getAvatarUrl(), avatar: Cryptpad.getAvatarUrl(),
profile: Cryptpad.getProfileUrl() profile: Cryptpad.getProfileUrl(),
edPublic: Cryptpad.getProxy().edPublic
}; };
addToUserData(myData); addToUserData(myData);
onLocal(); onLocal();

@ -153,6 +153,14 @@ define([
} }
return; return;
}; };
common.getUserlist = function () {
if (store) {
if (store.getProxy() && store.getProxy().info) {
return store.getProxy().info.userList;
}
}
return;
};
common.getProfileUrl = function () { common.getProfileUrl = function () {
if (store && store.getProfile()) { if (store && store.getProfile()) {
return store.getProfile().view; return store.getProfile().view;

@ -219,6 +219,10 @@ define([
// Trigger userlist update when the avatar has changed // Trigger userlist update when the avatar has changed
Cryptpad.changeDisplayName(proxy[Cryptpad.displayNameKey]); 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 () { proxy.on('change', [tokenKey], function () {
var localToken = tryParsing(localStorage.getItem(tokenKey)); var localToken = tryParsing(localStorage.getItem(tokenKey));
if (localToken !== proxy[tokenKey]) { if (localToken !== proxy[tokenKey]) {

@ -210,13 +210,16 @@ define([
var $span = $('<span>', {'title': name}); var $span = $('<span>', {'title': name});
var $rightCol = $('<span>', {'class': 'right-col'}); var $rightCol = $('<span>', {'class': 'right-col'});
$('<span>', {'class': 'name'}).text(name).appendTo($rightCol); $('<span>', {'class': 'name'}).text(name).appendTo($rightCol);
// TODO: if account var proxy = Cryptpad.getProxy();
if (Cryptpad.isLoggedIn() && data.edPublic && data.edPublic !== proxy.edPublic) {
if (!proxy.friends || !proxy.friends[data.edPublic]) {
var $button = $('<button>', {'class': 'friend'}).appendTo($rightCol); var $button = $('<button>', {'class': 'friend'}).appendTo($rightCol);
$button.text('Add friend').click(function (e) { $button.text('Add friend').click(function (e) {
e.stopPropagation(); e.stopPropagation();
Cryptpad.inviteFromUserlist(Cryptpad, data.netfluxId); Cryptpad.inviteFromUserlist(Cryptpad, data.netfluxId);
}); });
// TODO: end if }
}
if (data.profile) { if (data.profile) {
$span.addClass('clickable'); $span.addClass('clickable');
$span.click(function () { $span.click(function () {

Loading…
Cancel
Save