diff --git a/www/common/realtime-input.js b/www/common/realtime-input.js index 6a958e406..5a8726e35 100644 --- a/www/common/realtime-input.js +++ b/www/common/realtime-input.js @@ -15,16 +15,15 @@ * along with this program. If not, see . */ define([ - '/common/messages.js', '/common/netflux-client.js', - '/common/crypto.js', '/common/es6-promise.min.js', '/common/chainpad.js', '/bower_components/jquery/dist/jquery.min.js', -], function (Messages, Netflux, Crypto) { +], function (Netflux) { var $ = window.jQuery; var ChainPad = window.ChainPad; var PARANOIA = true; + var USE_HISTORY = true; var module = { exports: {} }; /** @@ -44,14 +43,14 @@ define([ var websocketUrl = config.websocketURL; var userName = config.userName; var channel = config.channel; - var chanKey = config.cryptKey; + var chanKey = config.cryptKey || ''; + var Crypto = config.crypto; var cryptKey = Crypto.parseKey(chanKey).cryptKey; var passwd = 'y'; // make sure configuration is defined config = config || {}; - var allMessages = []; var initializing = true; var recoverableErrorCount = 0; // unused var toReturn = {}; @@ -99,6 +98,11 @@ define([ }; var onReady = function(wc, network) { + if(config.setMyID) { + config.setMyID({ + myID: wc.myID + }); + } // Trigger onJoining with our own Cryptpad username to tell the toolbar that we are synced onJoining(wc.myID); @@ -127,11 +131,10 @@ define([ var message = chainpadAdapter.msgIn(peer, msg); verbose(message); - allMessages.push(message); if (!initializing) { - if (toReturn.onLocal) { - toReturn.onLocal(); + if (config.onLocal) { + config.onLocal(); } } // pass the message into Chainpad @@ -205,11 +208,6 @@ define([ wc.on('join', onJoining); wc.on('leave', onLeaving); - if(config.setMyID) { - config.setMyID({ - myID: wc.myID - }); - } // Open a Chainpad session realtime = createRealtime(); @@ -249,14 +247,22 @@ define([ }); // Get the channel history - var hc; - wc.members.forEach(function (p) { - if (p.length === 16) { hc = p; } - }); - wc.history_keeper = hc; - if (hc) { network.sendto(hc, JSON.stringify(['GET_HISTORY', wc.id])); } + if(USE_HISTORY) { + var hc; + + wc.members.forEach(function (p) { + if (p.length === 16) { hc = p; } + }); + wc.history_keeper = hc; + + if (hc) { network.sendto(hc, JSON.stringify(['GET_HISTORY', wc.id])); } + } realtime.start(); + + if(!USE_HISTORY) { + onReady(wc, network); + } }; var findChannelById = function(webChannels, channelId) { diff --git a/www/pad/main.js b/www/pad/main.js index 09055bc33..11b45843e 100644 --- a/www/pad/main.js +++ b/www/pad/main.js @@ -87,7 +87,6 @@ define([ } var fixThings = false; - // var key = Crypto.parseKey(window.location.hash.substring(1)); var editor = window.editor = Ckeditor.replace('editor1', { // https://dev.ckeditor.com/ticket/10907 needsBrFiller: fixThings, @@ -184,7 +183,6 @@ define([ } }; - var now = function () { return new Date().getTime(); }; var initializing = true; var userList = {}; // List of pretty name of all users (mapped with their server ID) @@ -250,12 +248,29 @@ define([ // our encryption key cryptKey: key, + // method which allows us to get the id of the user setMyID: setMyID, + // Crypto object to avoid loading it twice in Cryptpad + crypto: Crypto, + // really basic operational transform transformFunction : JsonOT.validate }; + var updateUserList = function(shjson) { + // Extract the user list (metadata) from the hyperjson + var hjson = JSON.parse(shjson); + var peerUserList = hjson[hjson.length-1]; + if(peerUserList.metadata) { + var userData = peerUserList.metadata; + // Update the local user data + addToUserList(userData); + hjson.pop(); + } + return hjson; + } + var onRemote = realtimeOptions.onRemote = function (info) { if (initializing) { return; } @@ -265,18 +280,7 @@ define([ cursor.update(); // Extract the user list (metadata) from the hyperjson - var hjson = JSON.parse(shjson); - var peerUserList = hjson[hjson.length-1]; - if(peerUserList.metadata) { - var userData = peerUserList.metadata; - // Update the local user data - userList = userData; - // Send the new data to the toolbar - if(toolbarList && typeof toolbarList.onChange === "function") { - toolbarList.onChange(userList); - } - hjson.pop(); - } + var hjson = updateUserList(shjson); // build a dom from HJSON, diff, and patch the editor applyHjson(shjson);