pull/1/head
ansuz 7 years ago
parent 1c526feffb
commit 284124a32d

@ -2,8 +2,42 @@ define([
'jquery', 'jquery',
'/bower_components/chainpad-crypto/crypto.js', '/bower_components/chainpad-crypto/crypto.js',
'/common/curve.js', '/common/curve.js',
'/common/common-hash.js',
'/bower_components/marked/marked.min.js', '/bower_components/marked/marked.min.js',
], function ($, Crypto, Curve, Marked) { '/common/common-realtime.js',
// displayAvatar
// whenRealtimeSyncs
// getRealtime -> removeFromFriendList
/* UI
Messages
confirm
fixHTML
displayAvatar
clearOwnedChannel
alert
pushMsg
removeFromFriendList
onDirectMessage
getNetwork
getProxy
pushMsg
Init
getNetwork
getProxy
onDirectMessage
removeFromFriendList
notify
onMessage
*/
], function ($, Crypto, Curve, Hash, Marked, Realtime) {
var Msg = { var Msg = {
inputs: [], inputs: [],
}; };
@ -28,11 +62,10 @@ define([
return Marked(content); return Marked(content);
}; };
var createData = Msg.createData = function (common, hash) { var createData = Msg.createData = function (proxy, hash) {
var proxy = common.getProxy();
return { return {
channel: hash || common.createChannelId(), channel: hash || Hash.createChannelId(),
displayName: proxy[common.displayNameKey], displayName: proxy['cryptpad.username'],
profile: proxy.profile && proxy.profile.view, profile: proxy.profile && proxy.profile.view,
edPublic: proxy.edPublic, edPublic: proxy.edPublic,
curvePublic: proxy.curvePublic, curvePublic: proxy.curvePublic,
@ -40,39 +73,39 @@ define([
}; };
}; };
var getFriend = function (common, pubkey) { var getFriend = function (proxy, pubkey) {
var proxy = common.getProxy();
if (pubkey === proxy.curvePublic) { if (pubkey === proxy.curvePublic) {
var data = createData(common); var data = createData(proxy);
delete data.channel; delete data.channel;
return data; return data;
} }
return proxy.friends ? proxy.friends[pubkey] : undefined; return proxy.friends ? proxy.friends[pubkey] : undefined;
}; };
var removeFromFriendList = Msg.removeFromFriendList = function (common, curvePublic, cb) { var removeFromFriendList = function (proxy, realtime, curvePublic, cb) {
var proxy = common.getProxy(); if (!proxy.friends) { return; }
if (!proxy.friends) {
return;
}
var friends = proxy.friends; var friends = proxy.friends;
delete friends[curvePublic]; delete friends[curvePublic];
common.whenRealtimeSyncs(common.getRealtime(), cb); Realtime.whenRealtimeSyncs(realtime, cb);
}; };
// TODO set this up as an observable data structure var getFriendList = Msg.getFriendList = function (proxy) {
var getFriendList = Msg.getFriendList = function (common) {
var proxy = common.getProxy();
if (!proxy.friends) { proxy.friends = {}; } if (!proxy.friends) { proxy.friends = {}; }
return proxy.friends; return proxy.friends;
}; };
Msg.getFriendChannelsList = function (common) { var eachFriend = function (friends, cb) {
var friends = getFriendList(common); Object.keys(friends).forEach(function (id) {
if (id === 'me') { return; }
cb(friends[id], id, friends);
});
};
Msg.getFriendChannelsList = function (proxy) {
var list = []; var list = [];
Object.keys(friends).forEach(function (key) { eachFriend(proxy, function (friend) {
if (key === "me") { return; } list.push(friend.channel);
list.push(friends[key].channel);
}); });
return list; return list;
}; };
@ -85,131 +118,6 @@ define([
var UI = Msg.UI = {}; var UI = Msg.UI = {};
// TODO extract into UI method
var createChatBox = function (common, $container, curvePublic, ui) {
var data = getFriend(common, curvePublic);
// Input
var channel = channels[data.channel];
var $header = $('<div>', {
'class': 'header',
}).appendTo($container);
var $avatar = $('<div>', {'class': 'avatar'}).appendTo($header);
// more history...
$('<span>', {
'class': 'more-history',
})
.text('get more history')
.click(function () {
console.log("GETTING HISTORY");
channel.getPreviousMessages();
})
.appendTo($header);
var $removeHistory = $('<span>', {
'class': 'remove-history fa fa-eraser',
title: common.Messages.contacts_removeHistoryTitle
})
.click(function () {
common.confirm(common.Messages.contacts_confirmRemoveHistory, function (yes) {
if (!yes) { return; }
common.clearOwnedChannel(data.channel, function (e) {
if (e) {
console.error(e);
common.alert(common.Messages.contacts_removeHistoryServerError);
return;
}
});
});
});
$removeHistory.appendTo($header);
$('<div>', {'class': 'messages'}).appendTo($container);
var $inputBlock = $('<div>', {'class': 'input'}).appendTo($container);
var $input = $('<textarea>').appendTo($inputBlock);
$input.attr('placeholder', common.Messages.contacts_typeHere);
ui.input = $input[0];
var send = function () {
// TODO implement sending queue
// TODO separate message logic from UI
var channel = channels[data.channel];
if (channel.sending) {
console.error("still sending");
return;
}
if (!$input.val()) {
console.error("nothing to send");
return;
}
if ($input.attr('disabled')) {
console.error("input is disabled");
return;
}
var payload = $input.val();
// Send the message
channel.sending = true;
channel.send(payload, function (e) {
if (e) {
channel.sending = false;
console.error(e);
return;
}
$input.val('');
channel.refresh();
channel.sending = false;
});
};
$('<button>', {
'class': 'btn btn-primary fa fa-paper-plane',
title: common.Messages.contacts_send,
}).appendTo($inputBlock).click(send);
var onKeyDown = function (e) {
if (e.keyCode === 13) {
if (e.ctrlKey || e.shiftKey) {
var val = this.value;
if (typeof this.selectionStart === "number" && typeof this.selectionEnd === "number") {
var start = this.selectionStart;
this.value = val.slice(0, start) + "\n" + val.slice(this.selectionEnd);
this.selectionStart = this.selectionEnd = start + 1;
} else if (document.selection && document.selection.createRange) {
this.focus();
var range = document.selection.createRange();
range.text = "\r\n";
range.collapse(false);
range.select();
}
return false;
}
send();
return false;
}
};
$input.on('keydown', onKeyDown);
// Header
var $rightCol = $('<span>', {'class': 'right-col'});
$('<span>', {'class': 'name'}).text(data.displayName).appendTo($rightCol);
if (data.avatar && avatars[data.avatar]) {
$avatar.append(avatars[data.avatar]);
$avatar.append($rightCol);
} else {
common.displayAvatar($avatar, data.avatar, data.displayName, function ($img) {
if (data.avatar && $img) {
avatars[data.avatar] = $img[0].outerHTML;
}
$avatar.append($rightCol);
});
}
};
UI.init = function (common, $listContainer, $msgContainer) { UI.init = function (common, $listContainer, $msgContainer) {
var ui = { var ui = {
containers: { containers: {
@ -260,9 +168,8 @@ define([
ui.createFriendList = function (friends, display, remove) { ui.createFriendList = function (friends, display, remove) {
var $block = ui.containers.friendBlock = $('<div>'); var $block = ui.containers.friendBlock = $('<div>');
Object.keys(friends).forEach(function (f) { eachFriend(friends, function (friend) {
if (f === 'me') { return; } ui.addToFriendList(friend, display, remove);
ui.addToFriendList(friends[f], display, remove);
}); });
$block.appendTo($listContainer); $block.appendTo($listContainer);
}; };
@ -282,7 +189,8 @@ define([
}; };
ui.update = function (curvePublic, types) { ui.update = function (curvePublic, types) {
var data = getFriend(common, curvePublic); var proxy = common.getProxy();
var data = getFriend(proxy, 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);
@ -311,7 +219,7 @@ define([
ui.updateStatus = function (curvePublic, online) { ui.updateStatus = function (curvePublic, online) {
ui.getFriend(curvePublic).find('.status') ui.getFriend(curvePublic).find('.status')
.attr('class', 'status ' + online? 'online' : 'offline'); .attr('class', 'status ' + (online? 'online' : 'offline'));
}; };
ui.getChannel = function (curvePublic) { ui.getChannel = function (curvePublic) {
@ -384,6 +292,129 @@ define([
} }
}; };
ui.createChatBox = function (proxy, $container, curvePublic) {
var data = getFriend(proxy, curvePublic);
// Input
var channel = channels[data.channel];
var $header = $('<div>', {
'class': 'header',
}).appendTo($container);
var $avatar = $('<div>', {'class': 'avatar'}).appendTo($header);
// more history...
$('<span>', {
'class': 'more-history',
})
.text('get more history')
.click(function () {
console.log("GETTING HISTORY");
channel.getPreviousMessages();
})
.appendTo($header);
var $removeHistory = $('<span>', {
'class': 'remove-history fa fa-eraser',
title: common.Messages.contacts_removeHistoryTitle
})
.click(function () {
common.confirm(common.Messages.contacts_confirmRemoveHistory, function (yes) {
if (!yes) { return; }
common.clearOwnedChannel(data.channel, function (e) {
if (e) {
console.error(e);
common.alert(common.Messages.contacts_removeHistoryServerError);
return;
}
});
});
});
$removeHistory.appendTo($header);
$('<div>', {'class': 'messages'}).appendTo($container);
var $inputBlock = $('<div>', {'class': 'input'}).appendTo($container);
var $input = $('<textarea>').appendTo($inputBlock);
$input.attr('placeholder', common.Messages.contacts_typeHere);
ui.input = $input[0];
var send = function () {
// TODO implement sending queue
// TODO separate message logic from UI
var channel = channels[data.channel];
if (channel.sending) {
console.error("still sending");
return;
}
if (!$input.val()) {
console.error("nothing to send");
return;
}
if ($input.attr('disabled')) {
console.error("input is disabled");
return;
}
var payload = $input.val();
// Send the message
channel.sending = true;
channel.send(payload, function (e) {
if (e) {
channel.sending = false;
console.error(e);
return;
}
$input.val('');
channel.refresh();
channel.sending = false;
});
};
$('<button>', {
'class': 'btn btn-primary fa fa-paper-plane',
title: common.Messages.contacts_send,
}).appendTo($inputBlock).click(send);
var onKeyDown = function (e) {
if (e.keyCode === 13) {
if (e.ctrlKey || e.shiftKey) {
var val = this.value;
if (typeof this.selectionStart === "number" && typeof this.selectionEnd === "number") {
var start = this.selectionStart;
this.value = val.slice(0, start) + "\n" + val.slice(this.selectionEnd);
this.selectionStart = this.selectionEnd = start + 1;
} else if (document.selection && document.selection.createRange) {
this.focus();
var range = document.selection.createRange();
range.text = "\r\n";
range.collapse(false);
range.select();
}
return false;
}
send();
return false;
}
};
$input.on('keydown', onKeyDown);
// Header
var $rightCol = $('<span>', {'class': 'right-col'});
$('<span>', {'class': 'name'}).text(data.displayName).appendTo($rightCol);
if (data.avatar && avatars[data.avatar]) {
$avatar.append(avatars[data.avatar]);
$avatar.append($rightCol);
} else {
common.displayAvatar($avatar, data.avatar, data.displayName, function ($img) {
if (data.avatar && $img) {
avatars[data.avatar] = $img[0].outerHTML;
}
$avatar.append($rightCol);
});
}
};
return ui; return ui;
}; };
@ -393,7 +424,8 @@ define([
}); });
}; };
var pushMsg = function (common, channel, cryptMsg) { // TODO remove dependency on common
var pushMsg = function (realtime, proxy, common, channel, cryptMsg) {
var msg = channel.encryptor.decrypt(cryptMsg); var msg = channel.encryptor.decrypt(cryptMsg);
var sig = cryptMsg.slice(0, 64); var sig = cryptMsg.slice(0, 64);
@ -413,12 +445,10 @@ define([
channel.messages.push(res); channel.messages.push(res);
return true; return true;
} }
var proxy;
if (parsedMsg[0] === Types.update) { if (parsedMsg[0] === Types.update) {
proxy = common.getProxy(); if (parsedMsg[1] === proxy.curvePublic) { 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(proxy, parsedMsg[1]);
var types = []; var types = [];
Object.keys(newdata).forEach(function (k) { Object.keys(newdata).forEach(function (k) {
if (data[k] !== newdata[k]) { if (data[k] !== newdata[k]) {
@ -430,11 +460,7 @@ define([
return; return;
} }
if (parsedMsg[0] === Types.unfriend) { if (parsedMsg[0] === Types.unfriend) {
proxy = common.getProxy(); removeFromFriendList(proxy, realtime, channel.friendEd, function () {
// FIXME pushMsg shouldn't need access to common
// implement this as a callback or use some other API
removeFromFriendList(common, channel.friendEd, function () {
channel.wc.leave(Types.unfriend); channel.wc.leave(Types.unfriend);
channel.removeUI(); channel.removeUI();
}); });
@ -444,10 +470,10 @@ define([
/* Broadcast a display name, profile, or avatar change to all contacts /* Broadcast a display name, profile, or avatar change to all contacts
*/ */
var updateMyData = function (common) { var updateMyData = function (proxy) {
var friends = getFriendList(common); var friends = getFriendList(proxy);
var mySyncData = friends.me; var mySyncData = friends.me;
var myData = createData(common); var myData = createData(proxy);
if (!mySyncData || mySyncData.displayName !== myData.displayName if (!mySyncData || mySyncData.displayName !== myData.displayName
|| mySyncData.profile !== myData.profile || mySyncData.profile !== myData.profile
|| mySyncData.avatar !== myData.avatar) { || mySyncData.avatar !== myData.avatar) {
@ -467,20 +493,20 @@ define([
} }
}; };
var onChannelReady = function (common, chanId) { var onChannelReady = function (proxy, chanId) {
if (ready.indexOf(chanId) !== -1) { return; } if (ready.indexOf(chanId) !== -1) { return; }
ready.push(chanId); ready.push(chanId);
channels[chanId].updateStatus(); // c'est quoi? channels[chanId].updateStatus(); // c'est quoi?
var friends = getFriendList(common); var friends = getFriendList(proxy);
if (ready.length === Object.keys(friends).length) { if (ready.length === Object.keys(friends).length) {
// All channels are ready // All channels are ready
updateMyData(common); updateMyData(proxy);
} }
return ready.length; return ready.length;
}; };
// Id message allows us to map a netfluxId with a public curve key // Id message allows us to map a netfluxId with a public curve key
var onIdMessage = function (common, msg, sender) { var onIdMessage = function (proxy, network, msg, sender) {
var channel; var channel;
var isId = Object.keys(channels).some(function (chanId) { var isId = Object.keys(channels).some(function (chanId) {
if (channels[chanId].userList.indexOf(sender) !== -1) { if (channels[chanId].userList.indexOf(sender) !== -1) {
@ -516,6 +542,9 @@ define([
return; return;
} }
if (parsed[0] !== Types.mapId && parsed[0] !== Types.mapIdAck) { return; } if (parsed[0] !== Types.mapId && parsed[0] !== Types.mapIdAck) { return; }
// check that the responding peer's encrypted netflux id matches
// the sender field. This is to prevent replay attacks.
if (parsed[2] !== sender || !parsed[1]) { return; } if (parsed[2] !== sender || !parsed[1]) { return; }
channel.mapId[sender] = parsed[1]; channel.mapId[sender] = parsed[1];
@ -523,15 +552,19 @@ define([
if (parsed[0] !== Types.mapId) { return; } // Don't send your key if it's already an ACK if (parsed[0] !== Types.mapId) { return; } // Don't send your key if it's already an ACK
// Answer with your own key // Answer with your own key
var proxy = common.getProxy();
var network = common.getNetwork();
var rMsg = [Types.mapIdAck, proxy.curvePublic, channel.wc.myID]; var rMsg = [Types.mapIdAck, proxy.curvePublic, channel.wc.myID];
var rMsgStr = JSON.stringify(rMsg); var rMsgStr = JSON.stringify(rMsg);
var cryptMsg = channel.encryptor.encrypt(rMsgStr); var cryptMsg = channel.encryptor.encrypt(rMsgStr);
network.sendto(sender, cryptMsg); network.sendto(sender, cryptMsg);
}; };
// HERE
var onDirectMessage = function (common, msg, sender) { var onDirectMessage = function (common, msg, sender) {
if (sender !== Msg.hk) { return void onIdMessage(common, msg, sender); } var proxy = common.getProxy();
var network = common.getNetwork();
var realtime = common.getRealtime();
if (sender !== Msg.hk) { return void onIdMessage(proxy, network, msg, sender); }
var parsed = JSON.parse(msg); var parsed = JSON.parse(msg);
if ((parsed.validateKey || parsed.owners) && parsed.channel) { if ((parsed.validateKey || parsed.owners) && parsed.channel) {
return; return;
@ -542,7 +575,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); onChannelReady(proxy, 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);
@ -552,12 +585,16 @@ define([
} }
var chan = parsed[3]; var chan = parsed[3];
if (!chan || !channels[chan]) { return; } if (!chan || !channels[chan]) { return; }
pushMsg(common, channels[chan], parsed[4]); pushMsg(realtime, proxy, common, channels[chan], parsed[4]);
channels[chan].refresh(); channels[chan].refresh();
}; };
var onMessage = function (common, msg, sender, chan) { var onMessage = function (common, msg, sender, chan) {
if (!channels[chan.id]) { return; } if (!channels[chan.id]) { return; }
var isMessage = pushMsg(common, channels[chan.id], msg);
var realtime = common.getRealtime();
var proxy = common.getProxy();
var isMessage = pushMsg(realtime, proxy, common, channels[chan.id], msg);
if (isMessage) { if (isMessage) {
// Don't notify for your own messages // Don't notify for your own messages
if (channels[chan.id].wc.myID !== sender) { if (channels[chan.id].wc.myID !== sender) {
@ -602,12 +639,15 @@ define([
}); });
}; };
/* TODO remove dependency on common
*/
Msg.init = function (common, ui) { Msg.init = function (common, ui) {
// declare common variables // declare common variables
var network = common.getNetwork(); var network = common.getNetwork();
var proxy = common.getProxy(); var proxy = common.getProxy();
var realtime = common.getRealtime();
Msg.hk = network.historyKeeper; Msg.hk = network.historyKeeper;
var friends = getFriendList(common); var friends = getFriendList(proxy);
// listen for messages... // listen for messages...
network.on('message', function(msg, sender) { network.on('message', function(msg, sender) {
@ -645,7 +685,7 @@ define([
for (var i = last + 1; i<messages.length; i++) { for (var i = last + 1; i<messages.length; i++) {
msg = messages[i]; msg = messages[i];
name = (msg.channel !== channel.lastSender)? name = (msg.channel !== channel.lastSender)?
getFriend(common, msg.channel).displayName: undefined; getFriend(proxy, msg.channel).displayName: undefined;
ui.createMessage(msg, name).appendTo($messages); ui.createMessage(msg, name).appendTo($messages);
channel.lastSender = msg.channel; channel.lastSender = msg.channel;
@ -667,7 +707,7 @@ define([
var $chat = ui.getChannel(curvePublic); var $chat = ui.getChannel(curvePublic);
if (!$chat) { if (!$chat) {
$chat = ui.createChat(curvePublic); $chat = ui.createChat(curvePublic);
createChatBox(common, $chat, curvePublic, ui); ui.createChatBox(proxy, $chat, curvePublic);
} }
// Show the correct div // Show the correct div
ui.hideChat(); ui.hideChat();
@ -682,9 +722,8 @@ define([
// TODO take a callback // TODO take a callback
var remove = function (curvePublic) { var remove = function (curvePublic) {
var data = getFriend(common, curvePublic); var data = getFriend(proxy, curvePublic);
var channel = channels[data.channel]; var channel = channels[data.channel];
//var newdata = createData(common, data.channel);
var msg = [Types.unfriend, proxy.curvePublic, +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);
@ -704,9 +743,7 @@ define([
// Open the channels // Open the channels
// TODO extract this into an external function // TODO extract this into an external function
var openFriendChannel = function (f) { var openFriendChannel = function (data, f) {
if (f === "me") { return; }
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);
network.join(data.channel).then(function (chan) { network.join(data.channel).then(function (chan) {
@ -719,13 +756,14 @@ define([
refresh: function () { refresh(data.curvePublic); }, refresh: function () { refresh(data.curvePublic); },
notify: function () { notify: function () {
ui.notify(data.curvePublic); ui.notify(data.curvePublic);
common.notify(); common.notify(); // HERE
}, },
unnotify: function () { ui.unnotify(data.curvePublic); }, unnotify: function () { ui.unnotify(data.curvePublic); },
removeUI: function () { ui.remove(data.curvePublic); }, removeUI: function () { ui.remove(data.curvePublic); },
updateUI: function (types) { ui.update(data.curvePublic, types); }, updateUI: function (types) { ui.update(data.curvePublic, types); },
updateStatus: function () { updateStatus: function () {
ui.updateStatus(data.curvePublic, channel.getStatus(data.curvePublic)); ui.updateStatus(data.curvePublic,
channel.getStatus(data.curvePublic));
}, },
setLastMessageRead: function (hash) { setLastMessageRead: function (hash) {
data.lastKnownHash = hash; data.lastKnownHash = hash;
@ -774,7 +812,7 @@ define([
var cryptMsg = channel.encryptor.encrypt(msgStr); var cryptMsg = channel.encryptor.encrypt(msgStr);
channel.wc.bcast(cryptMsg).then(function () { channel.wc.bcast(cryptMsg).then(function () {
pushMsg(common, channel, cryptMsg); pushMsg(realtime, proxy, common, channel, cryptMsg);
cb(); cb();
}, function (err) { }, function (err) {
cb(err); cb(err);
@ -823,7 +861,7 @@ define([
}; };
var openFriendChannels = messenger.openFriendChannels = function () { var openFriendChannels = messenger.openFriendChannels = function () {
Object.keys(friends).forEach(openFriendChannel); eachFriend(friends, openFriendChannel);
}; };
messenger.setEditable = ui.setEditable; messenger.setEditable = ui.setEditable;
@ -832,13 +870,10 @@ define([
// TODO split loop innards into ui methods // TODO split loop innards into ui methods
var checkNewFriends = function () { var checkNewFriends = function () {
Object.keys(friends).forEach(function (f) { eachFriend(friends, function (friend, id) {
var $friend = ui.getFriend(f); var $friend = ui.getFriend(id);
if (!$friend.length) { if (!$friend.length) {
openFriendChannel(f); openFriendChannel(friend, id);
if (f === 'me') { return; }
var friend = friends[f];
ui.addToFriendList(friend, display, remove); ui.addToFriendList(friend, display, remove);
} }
}); });
@ -846,7 +881,7 @@ define([
common.onDisplayNameChanged(function () { common.onDisplayNameChanged(function () {
checkNewFriends(); checkNewFriends();
updateMyData(common); updateMyData(proxy);
}); });
return messenger; return messenger;
@ -856,17 +891,14 @@ define([
// FIXME there are too many functions with this name // FIXME there are too many functions with this name
var addToFriendList = Msg.addToFriendList = function (common, data, cb) { var addToFriendList = Msg.addToFriendList = function (common, data, cb) {
var proxy = common.getProxy(); var proxy = common.getProxy();
if (!proxy.friends) { var friends = getFriendList(proxy);
proxy.friends = {};
}
var friends = proxy.friends;
var pubKey = data.curvePublic; var pubKey = data.curvePublic;
if (pubKey === proxy.curvePublic) { return void cb("E_MYKEY"); } if (pubKey === proxy.curvePublic) { return void cb("E_MYKEY"); }
friends[pubKey] = data; friends[pubKey] = data;
common.whenRealtimeSyncs(common.getRealtime(), function () { Realtime.whenRealtimeSyncs(common.getRealtime(), function () {
cb(); cb();
common.pinPads([data.channel]); common.pinPads([data.channel]);
}); });
@ -876,6 +908,7 @@ define([
/* Used to accept friend requests within apps other than /contacts/ */ /* Used to accept friend requests within apps other than /contacts/ */
Msg.addDirectMessageHandler = function (common) { Msg.addDirectMessageHandler = function (common) {
var network = common.getNetwork(); var network = common.getNetwork();
var proxy = common.getProxy();
if (!network) { return void console.error('Network not ready'); } if (!network) { return void console.error('Network not ready'); }
network.on('message', function (message, sender) { network.on('message', function (message, sender) {
var msg; var msg;
@ -910,7 +943,7 @@ define([
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); var existing = getFriend(proxy, msgData.curvePublic);
if (existing) { if (existing) {
todo(true); todo(true);
return; return;
@ -941,7 +974,6 @@ define([
var i = pendingRequests.indexOf(sender); var i = pendingRequests.indexOf(sender);
if (i !== -1) { pendingRequests.splice(i, 1); } if (i !== -1) { pendingRequests.splice(i, 1); }
common.log(common.Messages.contacts_rejected); common.log(common.Messages.contacts_rejected);
var proxy = common.getProxy();
common.changeDisplayName(proxy[common.displayNameKey]); common.changeDisplayName(proxy[common.displayNameKey]);
return; return;
} }

@ -0,0 +1,42 @@
define([
'/customize/application_config.js',
'/customize/messages.js',
], function (AppConfig, Messages) {
var common = {};
common.infiniteSpinnerDetected = false;
var BAD_STATE_TIMEOUT = typeof(AppConfig.badStateTimeout) === 'number'?
AppConfig.badStateTimeout: 30000;
/*
TODO make this not blow up when disconnected or lagging...
*/
common.whenRealtimeSyncs = function (realtime, cb) {
realtime.sync();
window.setTimeout(function () {
if (realtime.getAuthDoc() === realtime.getUserDoc()) {
return void cb();
}
var to = setTimeout(function () {
realtime.abort();
// don't launch more than one popup
if (common.infiniteSpinnerDetected) { return; }
// inform the user their session is in a bad state
common.confirm(Messages.realtime_unrecoverableError, function (yes) {
if (!yes) { return; }
window.location.reload();
});
common.infiniteSpinnerDetected = true;
}, BAD_STATE_TIMEOUT);
realtime.onSettle(function () {
clearTimeout(to);
cb();
});
}, 0);
};
return common;
});

@ -14,13 +14,15 @@ define([
'/common/common-codemirror.js', '/common/common-codemirror.js',
'/common/common-file.js', '/common/common-file.js',
'/file/file-crypto.js', '/file/file-crypto.js',
'/common/common-realtime.js',
'/common/clipboard.js', '/common/clipboard.js',
'/common/pinpad.js', '/common/pinpad.js',
'/customize/application_config.js', '/customize/application_config.js',
'/common/media-tag.js', '/common/media-tag.js',
], function ($, Config, Messages, Store, Util, Hash, UI, History, UserList, Title, Metadata, ], function ($, Config, Messages, Store, Util, Hash, UI, History, UserList, Title, Metadata,
Messaging, CodeMirror, Files, FileCrypto, Clipboard, Pinpad, AppConfig, MediaTag) { Messaging, CodeMirror, Files, FileCrypto, Realtime, Clipboard,
Pinpad, AppConfig, MediaTag) {
// Configure MediaTags to use our local viewer // Configure MediaTags to use our local viewer
if (MediaTag && MediaTag.PdfPlugin) { if (MediaTag && MediaTag.PdfPlugin) {
@ -129,6 +131,9 @@ define([
common.getLatestMessages = Messaging.getLatestMessages; common.getLatestMessages = Messaging.getLatestMessages;
common.initMessagingUI = Messaging.UI.init; common.initMessagingUI = Messaging.UI.init;
// Realtime
var whenRealtimeSyncs = common.whenRealtimeSyncs = Realtime.whenRealtimeSyncs;
// Userlist // Userlist
common.createUserList = UserList.create; common.createUserList = UserList.create;
@ -244,36 +249,6 @@ define([
return; return;
}; };
common.infiniteSpinnerDetected = false;
var BAD_STATE_TIMEOUT = typeof(AppConfig.badStateTimeout) === 'number'?
AppConfig.badStateTimeout: 30000;
var whenRealtimeSyncs = common.whenRealtimeSyncs = function (realtime, cb) {
realtime.sync();
window.setTimeout(function () {
if (realtime.getAuthDoc() === realtime.getUserDoc()) {
return void cb();
}
var to = setTimeout(function () {
realtime.abort();
// don't launch more than one popup
if (common.infiniteSpinnerDetected) { return; }
// inform the user their session is in a bad state
common.confirm(Messages.realtime_unrecoverableError, function (yes) {
if (!yes) { return; }
window.location.reload();
});
common.infiniteSpinnerDetected = true;
}, BAD_STATE_TIMEOUT);
realtime.onSettle(function () {
clearTimeout(to);
cb();
});
}, 0);
};
common.getWebsocketURL = function () { common.getWebsocketURL = function () {
if (!Config.websocketPath) { return Config.websocketURL; } if (!Config.websocketPath) { return Config.websocketURL; }
var path = Config.websocketPath; var path = Config.websocketPath;

Loading…
Cancel
Save