Fix friend removal and update user data

pull/1/head
yflory 7 years ago
parent 7114bfaa52
commit 009d01abde

@ -23,6 +23,7 @@ define([
// - mute a channel (hide notifications or don't open it?) // - mute a channel (hide notifications or don't open it?)
// //
var ready = [];
var pending = {}; var pending = {};
var createData = Msg.createData = function (common, hash) { var createData = Msg.createData = function (common, hash) {
@ -39,7 +40,7 @@ define([
var getFriend = function (common, pubkey) { var getFriend = function (common, pubkey) {
var proxy = common.getProxy(); var proxy = common.getProxy();
if (pubkey === proxy.edPublic) { if (pubkey === proxy.curvePublic) {
var data = createData(common); var data = createData(common);
delete data.channel; delete data.channel;
return data; return data;
@ -47,70 +48,78 @@ define([
return proxy.friends ? proxy.friends[pubkey] : undefined; return proxy.friends ? proxy.friends[pubkey] : undefined;
}; };
var removeFromFriendList = Msg.removeFromFriendList = function (common, edPublic, cb) { var removeFromFriendList = Msg.removeFromFriendList = function (common, curvePublic, cb) {
var proxy = common.getProxy(); var proxy = common.getProxy();
if (!proxy.friends) { if (!proxy.friends) {
return; return;
} }
var friends = proxy.friends; var friends = proxy.friends;
delete friends[edPublic]; delete friends[curvePublic];
common.whenRealtimeSyncs(common.getRealtime(), cb); common.whenRealtimeSyncs(common.getRealtime(), cb);
}; };
var getFriendList = Msg.getFriendList = function (common) { var getFriendList = Msg.getFriendList = function (common) {
var proxy = common.getProxy(); var proxy = common.getProxy();
return proxy.friends || {}; if (!proxy.friends) { proxy.friends = {}; }
return proxy.friends;
}; };
Msg.getFriendChannelsList = function (common) { Msg.getFriendChannelsList = function (common) {
var friends = getFriendList(common); var friends = getFriendList(common);
var list = []; var list = [];
Object.keys(friends).forEach(function (key) { Object.keys(friends).forEach(function (key) {
if (key === "me") { return; }
list.push(friends[key].channel); list.push(friends[key].channel);
}); });
return list; return list;
}; };
// Messaging tools // Messaging tools
var avatars = {}; var avatars = {};
var addToFriendListUI = function (common, $block, open, remove, f) {
var proxy = common.getProxy();
var friends = proxy.friends || {};
if (f === "me") { return; }
var data = friends[f];
var $friend = $('<div>', {'class': 'friend avatar'}).appendTo($block);
$friend.data('key', f);
var $rightCol = $('<span>', {'class': 'right-col'});
$('<span>', {'class': 'name'}).text(data.displayName).appendTo($rightCol);
var $remove = $('<span>', {'class': 'remove fa fa-user-times'}).appendTo($rightCol);
$friend.dblclick(function () {
if (data.profile) {
window.open('/profile/#' + data.profile);
}
});
$friend.click(function () {
open(data.curvePublic);
});
$remove.click(function (e) {
e.stopPropagation();
common.confirm("TODO: Are you sure?", function (yes) {//XXX
if (!yes) { return; }
remove(data.curvePublic);
});
});
if (data.avatar && avatars[data.avatar]) {
$friend.append(avatars[data.avatar]);
$friend.append($rightCol);
} else {
common.displayAvatar($friend, data.avatar, data.displayName, function ($img) {
if (data.avatar && $img) {
avatars[data.avatar] = $img[0].outerHTML;
}
$friend.append($rightCol);
});
}
};
Msg.getFriendListUI = function (common, open, remove) { Msg.getFriendListUI = function (common, open, remove) {
var proxy = common.getProxy(); var proxy = common.getProxy();
var $block = $('<div>'); var $block = $('<div>');
var friends = proxy.friends || {}; var friends = proxy.friends || {};
Object.keys(friends).forEach(function (f) { Object.keys(friends).forEach(function (f) {
var data = friends[f]; addToFriendListUI(common, $block, open, remove, f);
var $friend = $('<div>', {'class': 'friend avatar'}).appendTo($block);
$friend.data('key', f);
var $rightCol = $('<span>', {'class': 'right-col'});
$('<span>', {'class': 'name'}).text(data.displayName).appendTo($rightCol);
var $remove = $('<span>', {'class': 'remove fa fa-user-times'}).appendTo($rightCol);
$friend.dblclick(function () {
if (data.profile) {
window.open('/profile/#' + data.profile);
}
});
$friend.click(function () {
open(data.edPublic);
});
$remove.click(function (e) {
e.stopPropagation();
common.confirm("TODO: Are you sure?", function (yes) {
if (!yes) { return; }
remove(data.edPublic);
});
});
if (data.avatar && avatars[data.avatar]) {
$friend.append(avatars[data.avatar]);
$friend.append($rightCol);
} else {
common.displayAvatar($friend, data.avatar, data.displayName, function ($img) {
if (data.avatar && $img) {
avatars[data.avatar] = $img[0].outerHTML;
}
$friend.append($rightCol);
});
}
}); });
return $block; return $block;
}; };
@ -136,16 +145,15 @@ define([
var pushMsg = function (common, channel, cryptMsg) { var pushMsg = function (common, channel, cryptMsg) {
var msg = channel.encryptor.decrypt(cryptMsg); var msg = channel.encryptor.decrypt(cryptMsg);
var parsedMsg = JSON.parse(msg); var parsedMsg = JSON.parse(msg);
console.log(parsedMsg);
if (parsedMsg[0] === Types.message) { if (parsedMsg[0] === Types.message) {
parsedMsg.shift(); parsedMsg.shift();
channel.messages.push([cryptMsg.slice(0,64), parsedMsg]); channel.messages.push([cryptMsg.slice(0,64), parsedMsg]);
return; return true;
} }
var proxy; var proxy;
if (parsedMsg[0] === Types.update) { if (parsedMsg[0] === Types.update) {
proxy = common.getProxy(); proxy = common.getProxy();
if (parsedMsg[1] === common.getProxy().edPublic) { return; } if (parsedMsg[1] === common.getProxy().curvePublic) { return; }
var newdata = parsedMsg[3]; var newdata = parsedMsg[3];
var data = getFriend(common, parsedMsg[1]); var data = getFriend(common, parsedMsg[1]);
var types = []; var types = [];
@ -160,13 +168,48 @@ define([
} }
if (parsedMsg[0] === Types.unfriend) { if (parsedMsg[0] === Types.unfriend) {
proxy = common.getProxy(); proxy = common.getProxy();
if (parsedMsg[1] === common.getProxy().edPublic) { return; } removeFromFriendList(common, channel.friendEd, function () {
channel.wc.leave(Types.unfriend); channel.wc.leave(Types.unfriend);
channel.removeUI(); channel.removeUI();
});
return; return;
} }
}; };
var updateMyData = function (common) {
var friends = getFriendList(common);
var mySyncData = friends.me;
var myData = createData(common);
if (!mySyncData || mySyncData.displayName !== myData.displayName
|| mySyncData.profile !== myData.profile
|| mySyncData.avatar !== myData.avatar) {
delete myData.channel;
Object.keys(channels).forEach(function (chan) {
var channel = channels[chan];
var msg = [Types.update, myData.curvePublic, +new Date(), myData];
var msgStr = JSON.stringify(msg);
var cryptMsg = channel.encryptor.encrypt(msgStr);
channel.wc.bcast(cryptMsg).then(function () {
channel.refresh();
}, function (err) {
console.error(err);
});
});
friends.me = myData;
}
};
var onChannelReady = function (common, chanId) {
if (ready.indexOf(chanId) !== -1) { return; }
ready.push(chanId);
var friends = getFriendList(common);
if (ready.length === Object.keys(friends).length) {
// All channels are ready
updateMyData(common);
}
return ready.length;
};
var onDirectMessage = function (common, msg, sender) { var onDirectMessage = function (common, msg, sender) {
if (sender !== Msg.hk) { return; } if (sender !== Msg.hk) { return; }
var parsed = JSON.parse(msg); var parsed = JSON.parse(msg);
@ -179,6 +222,7 @@ define([
// TODO: call a function that shows that the channel is ready? (remove a spinner, ...) // TODO: call a function that shows that the channel is ready? (remove a spinner, ...)
// channel[parsed.channel].ready(); // channel[parsed.channel].ready();
channels[parsed.channel].ready = true; channels[parsed.channel].ready = true;
onChannelReady(common, parsed.channel);
var updateTypes = channels[parsed.channel].updateOnReady; var updateTypes = channels[parsed.channel].updateOnReady;
if (updateTypes) { if (updateTypes) {
channels[parsed.channel].updateUI(updateTypes); channels[parsed.channel].updateUI(updateTypes);
@ -192,13 +236,15 @@ define([
}; };
var onMessage = function (common, msg, sender, chan) { var onMessage = function (common, msg, sender, chan) {
if (!channels[chan.id]) { return; } if (!channels[chan.id]) { return; }
pushMsg(common, channels[chan.id], msg); var isMessage = pushMsg(common, channels[chan.id], msg);
channels[chan.id].notify(); if (isMessage) {
channels[chan.id].refresh(); channels[chan.id].notify();
channels[chan.id].refresh();
}
}; };
var createChatBox = function (common, $container, edPublic) { var createChatBox = function (common, $container, curvePublic) {
var data = getFriend(common, edPublic); var data = getFriend(common, curvePublic);
var proxy = common.getProxy(); var proxy = common.getProxy();
var $header = $('<div>', {'class': 'header avatar'}).appendTo($container); var $header = $('<div>', {'class': 'header avatar'}).appendTo($container);
@ -208,17 +254,22 @@ define([
// Input // Input
var channel = channels[data.channel]; var channel = channels[data.channel];
var $input = $('<input>', {type: 'text'}).appendTo($inputBlock); var $input = $('<input>', {type: 'text'}).appendTo($inputBlock);
var sending = false;
var send = function () { var send = function () {
if (sending) { return; }
if (!$input.val()) { return; } if (!$input.val()) { return; }
// Send the message // Send the message
var msg = [Types.message, proxy.edPublic, +new Date(), $input.val()]; var msg = [Types.message, proxy.curvePublic, +new Date(), $input.val()];
var msgStr = JSON.stringify(msg); var msgStr = JSON.stringify(msg);
var cryptMsg = channel.encryptor.encrypt(msgStr); var cryptMsg = channel.encryptor.encrypt(msgStr);
sending = true;
channel.wc.bcast(cryptMsg).then(function () { channel.wc.bcast(cryptMsg).then(function () {
$input.val(''); $input.val('');
pushMsg(common, channel, cryptMsg); pushMsg(common, channel, cryptMsg);
channel.refresh(); channel.refresh();
sending = false;
}, function (err) { }, function (err) {
sending = false;
console.error(err); console.error(err);
}); });
}; };
@ -254,14 +305,15 @@ define([
}); });
// Refresh the active channel // Refresh the active channel
var refresh = function (edPublic) { var refresh = function (curvePublic) {
if (Msg.active !== edPublic) { return; } if (Msg.active !== curvePublic) { return; }
var data = friends[edPublic]; var data = friends[curvePublic];
if (!data) { return; }
var channel = channels[data.channel]; var channel = channels[data.channel];
if (!channel) { return; } if (!channel) { return; }
var $chat = $msgContainer.find('.chat').filter(function (idx, el) { var $chat = $msgContainer.find('.chat').filter(function (idx, el) {
return $(el).data('key') === edPublic; return $(el).data('key') === curvePublic;
}); });
if (!$chat.length) { return; } if (!$chat.length) { return; }
@ -299,34 +351,35 @@ define([
} }
}; };
// Display a new channel // Display a new channel
var display = function (edPublic) { var display = function (curvePublic) {
var isNew = false; var isNew = false;
var $chat = $msgContainer.find('.chat').filter(function (idx, el) { var $chat = $msgContainer.find('.chat').filter(function (idx, el) {
return $(el).data('key') === edPublic; return $(el).data('key') === curvePublic;
}); });
if (!$chat.length) { if (!$chat.length) {
$chat = $('<div>', {'class':'chat'}).data('key', edPublic).appendTo($msgContainer); $chat = $('<div>', {'class':'chat'})
createChatBox(common, $chat, edPublic); .data('key', curvePublic).appendTo($msgContainer);
createChatBox(common, $chat, curvePublic);
isNew = true; isNew = true;
} }
// Show the correct div // Show the correct div
$msgContainer.find('.chat').hide(); $msgContainer.find('.chat').hide();
$chat.show(); $chat.show();
Msg.active = edPublic; Msg.active = curvePublic;
refresh(edPublic); refresh(curvePublic);
}; };
var remove = function (edPublic) { var remove = function (curvePublic) {
var data = getFriend(common, edPublic); var data = getFriend(common, curvePublic);
var channel = channels[data.channel]; var channel = channels[data.channel];
//var newdata = createData(common, data.channel); //var newdata = createData(common, data.channel);
var msg = [Types.unfriend, proxy.edPublic, +new Date()]; var msg = [Types.unfriend, proxy.curvePublic, +new Date()];
var msgStr = JSON.stringify(msg); var msgStr = JSON.stringify(msg);
var cryptMsg = channel.encryptor.encrypt(msgStr); var cryptMsg = channel.encryptor.encrypt(msgStr);
channel.wc.bcast(cryptMsg).then(function () { channel.wc.bcast(cryptMsg).then(function () {
removeFromFriendList(common, edPublic, function () { removeFromFriendList(common, curvePublic, function () {
channel.wc.leave(Types.unfriend); channel.wc.leave(Types.unfriend);
channel.removeUI(); channel.removeUI();
}); });
@ -339,34 +392,34 @@ define([
common.getFriendListUI(common, display, remove).appendTo($listContainer); common.getFriendListUI(common, display, remove).appendTo($listContainer);
// Notify on new messages // Notify on new messages
var notify = function (edPublic) { var notify = function (curvePublic) {
if (Msg.active === edPublic) { return; } if (Msg.active === curvePublic) { return; }
var $friend = $listContainer.find('.friend').filter(function (idx, el) { var $friend = $listContainer.find('.friend').filter(function (idx, el) {
return $(el).data('key') === edPublic; return $(el).data('key') === curvePublic;
}); });
$friend.addClass('notify'); $friend.addClass('notify');
}; };
var unnotify = function (edPublic) { var unnotify = function (curvePublic) {
var $friend = $listContainer.find('.friend').filter(function (idx, el) { var $friend = $listContainer.find('.friend').filter(function (idx, el) {
return $(el).data('key') === edPublic; return $(el).data('key') === curvePublic;
}); });
$friend.removeClass('notify'); $friend.removeClass('notify');
}; };
var removeUI = function (edPublic) { var removeUI = function (curvePublic) {
var $friend = $listContainer.find('.friend').filter(function (idx, el) { var $friend = $listContainer.find('.friend').filter(function (idx, el) {
return $(el).data('key') === edPublic; return $(el).data('key') === curvePublic;
}); });
$friend.remove(); $friend.remove();
}; };
var updateUI = function (edPublic, types) { var updateUI = function (curvePublic, types) {
var data = getFriend(common, edPublic); var data = getFriend(common, curvePublic);
var chan = channels[data.channel]; var chan = channels[data.channel];
if (!chan.ready) { if (!chan.ready) {
chan.updateOnReady = (chan.updateOnReady || []).concat(types); chan.updateOnReady = (chan.updateOnReady || []).concat(types);
return; return;
} }
var $friend = $listContainer.find('.friend').filter(function (idx, el) { var $friend = $listContainer.find('.friend').filter(function (idx, el) {
return $(el).data('key') === edPublic; return $(el).data('key') === curvePublic;
}); });
if (types.indexOf('displayName') >= 0) { if (types.indexOf('displayName') >= 0) {
$friend.find('.name').text(data.displayName); $friend.find('.name').text(data.displayName);
@ -380,7 +433,6 @@ define([
common.displayAvatar($friend, data.avatar, data.displayName, function ($img) { common.displayAvatar($friend, data.avatar, data.displayName, function ($img) {
if (data.avatar && $img) { if (data.avatar && $img) {
avatars[data.avatar] = $img[0].outerHTML; avatars[data.avatar] = $img[0].outerHTML;
} }
}); });
} }
@ -388,22 +440,24 @@ define([
}; };
// Open the channels // Open the channels
Object.keys(friends).forEach(function (f) { var openFriendChannel = function (f) {
if (f === "me") { return; }
var data = friends[f]; var data = friends[f];
var keys = Curve.deriveKeys(data.curvePublic, proxy.curvePrivate); var keys = Curve.deriveKeys(data.curvePublic, proxy.curvePrivate);
var encryptor = Curve.createEncryptor(keys); var encryptor = Curve.createEncryptor(keys);
channels[data.channel] = {
keys: keys,
encryptor: encryptor,
messages: [],
refresh: function () { refresh(data.edPublic); },
notify: function () { notify(data.edPublic); },
unnotify: function () { unnotify(data.edPublic); },
removeUI: function () { removeUI(data.edPublic); },
updateUI: function (types) { updateUI(data.edPublic, types); }
};
network.join(data.channel).then(function (chan) { network.join(data.channel).then(function (chan) {
channels[data.channel].wc = chan; channels[data.channel] = {
friendEd: f,
keys: keys,
encryptor: encryptor,
messages: [],
refresh: function () { refresh(data.curvePublic); },
notify: function () { notify(data.curvePublic); },
unnotify: function () { unnotify(data.curvePublic); },
removeUI: function () { removeUI(data.curvePublic); },
updateUI: function (types) { updateUI(data.curvePublic, types); },
wc: chan
};
chan.on('message', function (msg, sender) { chan.on('message', function (msg, sender) {
onMessage(common, msg, sender, chan); onMessage(common, msg, sender, chan);
}); });
@ -420,22 +474,24 @@ define([
}, function (err) { }, function (err) {
console.error(err); console.error(err);
}); });
}); };
Object.keys(friends).forEach(openFriendChannel);
common.onDisplayNameChanged(function() { var checkNewFriends = function () {
Object.keys(channels).forEach(function (chan) { Object.keys(friends).forEach(function (f) {
var channel = channels[chan]; var $friend = $listContainer.find('.friend').filter(function (idx, el) {
var newdata = createData(common, chan); return $(el).data('key') === f;
delete newdata.channel;
var msg = [Types.update, proxy.edPublic, +new Date(), newdata];
var msgStr = JSON.stringify(msg);
var cryptMsg = channel.encryptor.encrypt(msgStr);
channel.wc.bcast(cryptMsg).then(function () {
channel.refresh();
}, function (err) {
console.error(err);
}); });
if (!$friend.length) {
openFriendChannel(f);
addToFriendListUI(common, $listContainer.find('> div'), display, remove, f);
}
}); });
};
common.onDisplayNameChanged(function () {
checkNewFriends();
updateMyData(common);
}); });
}; };
@ -447,12 +503,12 @@ define([
proxy.friends = {}; proxy.friends = {};
} }
var friends = proxy.friends; var friends = proxy.friends;
var pubKey = data.edPublic; var pubKey = data.curvePublic;
if (pubKey === proxy.edPublic) { return void cb("E_MYKEY"); } if (pubKey === proxy.curvePublic) { return void cb("E_MYKEY"); }
if (friends[pubKey]) { return void cb("E_EXISTS"); }
friends[pubKey] = data; friends[pubKey] = data;
common.whenRealtimeSyncs(common.getRealtime(), function () { common.whenRealtimeSyncs(common.getRealtime(), function () {
common.pinPads([data.channel], cb); common.pinPads([data.channel], cb);
}); });
@ -481,21 +537,20 @@ define([
var msgStr; var msgStr;
if (msg[0] === "FRIEND_REQ") { if (msg[0] === "FRIEND_REQ") {
msg = ["FRIEND_REQ_NOK", chan]; msg = ["FRIEND_REQ_NOK", chan];
var existing = getFriend(common, msgData.edPublic); var todo = function (yes) {
if (existing) {
msg = ["FRIEND_REQ_OK", chan, createData(common, existing.channel)];
msgStr = Crypto.encrypt(JSON.stringify(msg), key);
network.sendto(sender, msgStr);
return;
}
common.confirm("Accept friend?", function (yes) { // XXX
if (yes) { if (yes) {
pending[sender] = msgData; pending[sender] = msgData;
msg = ["FRIEND_REQ_OK", chan, createData(common, msgData.channel)]; msg = ["FRIEND_REQ_OK", chan, createData(common, msgData.channel)];
} }
msgStr = Crypto.encrypt(JSON.stringify(msg), key); msgStr = Crypto.encrypt(JSON.stringify(msg), key);
network.sendto(sender, msgStr); network.sendto(sender, msgStr);
}); };
var existing = getFriend(common, msgData.curvePublic);
if (existing) {
todo(true);
return;
}
common.confirm("Accept friend?", todo);// TODO
return; return;
} }
if (msg[0] === "FRIEND_REQ_OK") { if (msg[0] === "FRIEND_REQ_OK") {

@ -53,7 +53,7 @@ define(function () {
uid: Cryptpad.getUid(), uid: Cryptpad.getUid(),
avatar: Cryptpad.getAvatarUrl(), avatar: Cryptpad.getAvatarUrl(),
profile: Cryptpad.getProfileUrl(), profile: Cryptpad.getProfileUrl(),
edPublic: Cryptpad.getProxy().edPublic curvePublic: Cryptpad.getProxy().curvePublic
}; };
addToUserData(myData); addToUserData(myData);
/*Cryptpad.setAttribute('username', exp.myUserName, function (err) { /*Cryptpad.setAttribute('username', exp.myUserName, function (err) {
@ -84,7 +84,7 @@ define(function () {
uid: Cryptpad.getUid(), uid: Cryptpad.getUid(),
avatar: Cryptpad.getAvatarUrl(), avatar: Cryptpad.getAvatarUrl(),
profile: Cryptpad.getProfileUrl(), profile: Cryptpad.getProfileUrl(),
edPublic: Cryptpad.getProxy().edPublic curvePublic: Cryptpad.getProxy().curvePublic
}; };
addToUserData(myData); addToUserData(myData);
onLocal(); onLocal();

@ -200,7 +200,6 @@ define([
// Update the userlist // Update the userlist
var $editUsers = $userlistContent.find('.' + USERLIST_CLS).html(''); var $editUsers = $userlistContent.find('.' + USERLIST_CLS).html('');
var $editUsersList = $('<div>', {'class': 'userlist-others'}); var $editUsersList = $('<div>', {'class': 'userlist-others'});
// Editors // Editors
@ -210,13 +209,14 @@ define([
var $rightCol = $('<span>', {'class': 'right-col'}); var $rightCol = $('<span>', {'class': 'right-col'});
var $nameSpan = $('<span>', {'class': 'name'}).text(name).appendTo($rightCol); var $nameSpan = $('<span>', {'class': 'name'}).text(name).appendTo($rightCol);
var proxy = Cryptpad.getProxy(); var proxy = Cryptpad.getProxy();
var isMe = data.edPublic === proxy.edPublic; var isMe = data.curvePublic === proxy.curvePublic;
if (Cryptpad.isLoggedIn() && data.edPublic) {
if (Cryptpad.isLoggedIn() && data.curvePublic) {
if (isMe) { if (isMe) {
$nameSpan.attr('title', Messages._getKey('userlist_thisIsYou', [ $nameSpan.attr('title', Messages._getKey('userlist_thisIsYou', [
name name
])).text(name); ])).text(name);
} else if (!proxy.friends || !proxy.friends[data.edPublic]) { } else if (!proxy.friends || !proxy.friends[data.curvePublic]) {
$('<span>', { $('<span>', {
'class': 'fa fa-user-plus friend', 'class': 'fa fa-user-plus friend',
'title': Messages._getKey('userlist_addAsFriendTitle', [ 'title': Messages._getKey('userlist_addAsFriendTitle', [
@ -777,8 +777,14 @@ define([
}); });
}); });
Cryptpad.onDisplayNameChanged(function () { Cryptpad.onDisplayNameChanged(function () {
Cryptpad.findCancelButton().click(); window.setTimeout(function () {
updateDisplayName(toolbar, config); Cryptpad.findCancelButton().click();
if (config.userList) {
updateUserList(toolbar, config);
return;
}
updateDisplayName(toolbar, config);
}, 0);
}); });
updateDisplayName(toolbar, config); updateDisplayName(toolbar, config);

Loading…
Cancel
Save