From 43c045721c5f78854fb77590c47ea87f742ee92d Mon Sep 17 00:00:00 2001 From: yflory Date: Fri, 14 Apr 2017 18:36:36 +0200 Subject: [PATCH] Get the channel history --- customize.dist/src/less/toolbar.less | 14 +++ customize.dist/toolbar.css | 16 +++ www/code/main.js | 15 +++ www/common/common-history.js | 156 +++++++++++++++++++++++++++ www/common/cryptpad-common.js | 8 +- www/common/toolbar.js | 4 +- 6 files changed, 211 insertions(+), 2 deletions(-) create mode 100644 www/common/common-history.js diff --git a/customize.dist/src/less/toolbar.less b/customize.dist/src/less/toolbar.less index 9b93a0da6..5ff104c46 100644 --- a/customize.dist/src/less/toolbar.less +++ b/customize.dist/src/less/toolbar.less @@ -409,6 +409,20 @@ .cryptpad-toolbar-rightside { text-align: right; } +.cryptpad-toolbar-history { + display: none; + text-align: center; + .next { + float: right; + } + .previous { + float: left; + } + .goto { + display: inline-block; + input { width: 50px; } + } +} .cryptpad-spinner { height: 16px; width: 16px; diff --git a/customize.dist/toolbar.css b/customize.dist/toolbar.css index bbcffbf2f..5c9fd2094 100644 --- a/customize.dist/toolbar.css +++ b/customize.dist/toolbar.css @@ -472,6 +472,22 @@ .cryptpad-toolbar-rightside { text-align: right; } +.cryptpad-toolbar-history { + display: none; + text-align: center; +} +.cryptpad-toolbar-history .next { + float: right; +} +.cryptpad-toolbar-history .previous { + float: left; +} +.cryptpad-toolbar-history .goto { + display: inline-block; +} +.cryptpad-toolbar-history .goto input { + width: 50px; +} .cryptpad-spinner { height: 16px; width: 16px; diff --git a/www/code/main.js b/www/code/main.js index 2b6553cf0..4a03efd09 100644 --- a/www/code/main.js +++ b/www/code/main.js @@ -402,6 +402,21 @@ define([ editHash = Cryptpad.getEditHashFromKeys(info.channel, secret.keys); } + /* add an export button */ + var $hist = Cryptpad.createButton(); + var historyRender = function () {}; + var historyClose = function () { + // TODO: enable onlocal, onremote... (or at least the display part) + }; + var historyTodo = function (hist) { + hist.display($bar, historyRender, historyClose); + }; + $hist.removeClass('fa-question').addClass('fa-history').click(function () { + // TODO: disable onlocal, onremote... + Cryptpad.getHistory(historyTodo); + }); + $rightside.append($hist); + /* save as template */ if (!Cryptpad.isTemplate(window.location.href)) { var templateObj = { diff --git a/www/common/common-history.js b/www/common/common-history.js new file mode 100644 index 000000000..d790ab553 --- /dev/null +++ b/www/common/common-history.js @@ -0,0 +1,156 @@ +define([ + '/bower_components/chainpad-json-validator/json-ot.js', + '/bower_components/chainpad-crypto/crypto.js', + '/bower_components/jquery/dist/jquery.min.js', + '/bower_components/chainpad/chainpad.dist.js', +], function (JsonOT, Crypto) { + var $ = window.jQuery; + var ChainPad = window.ChainPad; + var History = {}; + + + /* TODO + * Implement GET_FULL_HISTORY serverside + * All the history messages should be ['FULL_HISTORY', wc.id, msg] + * Send [FULL_HISTORY_END, wc.id] + * + * We also need a chainpad without pruning and with the ability to get old messages + */ + var loadHistory = function (common, cb) { + var network = common.getNetwork(); + var hkn = network.historyKeeper; + + var wcId = common.hrefToHexChannelId(window.location.href); + + var createRealtime = function(chan) { + console.log(ChainPad); + return ChainPad.create({ + userName: 'history', + initialState: '', + transformFunction: JsonOT.validate, + logLevel: 0 + }); + }; + var realtime = createRealtime(); + + var secret = Cryptpad.getSecrets(); + var crypto = Crypto.createEncryptor(secret.keys); + + var to = window.setTimeout(function () { + cb('[GET_FULL_HISTORY_TIMEOUT]'); + }, 3000); + + var parse = function (msg) { + try { + return JSON.parse(msg); + } catch (e) { + return null; + } + }; + var onMsg = function (msg) { + var parsed = parse(msg); + if (parsed[0] === 'FULL_HISTORY_END') { + window.clearTimeout(to); + cb(null, realtime); + return; + } + if (parsed[0] !== 'FULL_HISTORY') { return; } + var msg = parsed[1]; + var decryptedMsg = crypto.decrypt(msg, secret.keys.validateKey); + realtime.message(decryptedMsg); + }; + + network.on('message', function (msg, sender) { + onMsg(msg); + }); + + network.sendto(hkn, JSON.stringify(['GET_FULL_HISTORY', wcId])); + }; + + var create = History.create = function (common, cb) { + var exp = {}; + + var states = exp.states = ['a', 'b', 'c']; + var c = exp.current = states.length - 1; + console.log(c); + + var onUpdate; + + var update = exp.update = function () { + states = []; + if (typeof onUpdate === "function") { onUpdate(); } + return states; + }; + + var get = exp.get = function (i) { + i = parseInt(i); + console.log('getting', i); + if (typeof(i) !== "number" || i < 0 || i > states.length - 1) { return; } + var hash = states[i]; + c = i; + if (typeof onUpdate === "function") { onUpdate(); } + return ''; + }; + + var getNext = exp.getNext = function () { + if (c < states.length - 1) { return get(++c); } + }; + var getPrevious = exp.getPrevious = function () { + if (c > 0) { return get(--c); } + }; + + var display = exp.display = function ($toolbar, render, onClose) { + var $hist = $toolbar.find('.cryptpad-toolbar-history').html('').show(); + var $left = $toolbar.find('.cryptpad-toolbar-leftside').hide(); + var $right = $toolbar.find('.cryptpad-toolbar-rightside').hide(); + + var $prev =$('