diff --git a/www/common/cryptget.js b/www/common/cryptget.js index 144c9dff9..31e48e8ce 100644 --- a/www/common/cryptget.js +++ b/www/common/cryptget.js @@ -1,59 +1,87 @@ define([ - '/customize/messages.js?app=read', + '/customize/messages.js?app=cryptget', '/bower_components/chainpad-crypto/crypto.js', '/bower_components/chainpad-netflux/chainpad-netflux.js', '/common/cryptpad-common.js', -], function (Messages, Crypto, Realtime, Cryptpad) { - var Crypt = {}; + '/bower_components/textpatcher/TextPatcher.js', + '/bower_components/jquery/dist/jquery.min.js', +], function (Messages, Crypto, Realtime, Cryptpad, TextPatcher) { + var noop = function () {}; + var finish = function (S, err, doc) { + if (S.done) { return; } + S.cb(err, doc); + S.done = true; - var finish = function (Session, err, doc) { - if (Session.done) { return; } - Session.cb(err, doc); - Session.done = true; - - var abort = Cryptpad.find(Session, - ['realtime', 'realtime', 'abort']); - if (typeof(abort) === 'function') { abort(); } - }; - - var get = Crypt.get = function (hash, cb, opt) { - if (typeof(cb) !== 'function') { - throw new Error('Crypt.get expects a callback'); + var abort = Cryptpad.find(S, ['realtime', 'realtime', 'abort']); + if (typeof(abort) === 'function') { + S.realtime.realtime.sync(); + abort(); } + }; - var Session = { - cb: cb, - }; - opt = opt || {}; - var secret = Session.secret = Cryptpad.getSecrets(hash); - + var makeConfig = function (hash) { + var secret = Cryptpad.getSecrets(hash); + if (!secret.keys) { secret.keys = secret.key; } // support old hashses var config = { websocketURL: Cryptpad.getWebsocketURL(), channel: secret.channel, + validateKey: secret.keys.validateKey || undefined, crypto: Crypto.createEncryptor(secret.keys), logLevel: 0, }; + return config; + }; - var onError = config.onError = function () { - finish(Session, Messages.websocketError); - }; - var onAbort = config.onAbort = function () { - finish(Session, Messages.disconnectAlert); - }; + var isObject = function (o) { + return typeof(o) === 'object'; + }; + + var overwrite = function (a, b) { + if (!(isObject(a) && isObject(b))) { return; } + Object.keys(b).forEach(function (k) { a[k] = b[k]; }); + }; + + var get = function (hash, cb, opt) { + if (typeof(cb) !== 'function') { + throw new Error('Cryptget expects a callback'); + } + var Session = { cb: cb, }; + var config = makeConfig(hash); var onReady = config.onReady = function (info) { - var realtime = Session.realtime = info.realtime; - finish(Session, void 0, realtime.getUserDoc()); + var rt = Session.session = info.realtime; + finish(Session, void 0, rt.getUserDoc()); }; + overwrite(config, opt); + + var realtime = Session.realtime = Realtime.start(config); + }; - var onConnectionChange = config.onConnectionChange = function (info) { - if (info.state) { return; } - finish(Session, Messages.disconnectAlert); + var put = function (hash, doc, cb, opt) { + if (typeof(cb) !== 'function') { + throw new Error('Cryptput expects a callback'); + } + + var config = makeConfig(hash); + var Session = { cb: cb, }; + config.onReady = function (info) { + var realtime = Session.session = info.realtime; + + TextPatcher.create({ + realtime: realtime, + })(doc); + realtime.sync(); + realtime.abort(); + + finish(Session, void 0); }; - Object.keys(opt).forEach(function (k) { config[k] = opt[k]; }); + overwrite(config, opt); - return (Session.realtime = Realtime.start(config)); + var realtime = Session.session = Realtime.start(config); }; - return Crypt; + return { + get: get, + put: put, + }; }); diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index 09f44d3d9..f02e1747f 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -6,12 +6,10 @@ define([ '/bower_components/alertifyjs/dist/js/alertify.js', '/bower_components/spin.js/spin.min.js', '/common/clipboard.js', - '/customize/fsStore.js', - '/customize/user.js', '/bower_components/jquery/dist/jquery.min.js', -], function (Config, Messages, Store, Crypto, Alertify, Spinner, Clipboard, FS, User) { +], function (Config, Messages, Store, Crypto, Alertify, Spinner, Clipboard, FS) { /* This file exposes functionality which is specific to Cryptpad, but not to any particular pad type. This includes functions for committing metadata about pads to your local storage for future use and improved usability. @@ -26,14 +24,11 @@ define([ var storeToUse = USE_FS_STORE ? FS : Store; - var common = { - User: User, + var common = window.Cryptpad = { Messages: Messages, }; var store; var fsStore; - var userProxy; - var userStore; var find = common.find = function (map, path) { return (map && path.reduce(function (p, n) { @@ -42,7 +37,6 @@ define([ }; var getStore = common.getStore = function (legacy) { - if (!legacy && userStore) { return userStore; } if ((!USE_FS_STORE || legacy) && store) { return store; } if (USE_FS_STORE && !legacy && fsStore) { return fsStore; } throw new Error("Store is not ready!"); @@ -60,42 +54,6 @@ define([ return url; }; - /* - * cb(err, proxy); - */ - var authorize = common.authorize = function (cb) { - console.log("Authorizing"); - - User.session(void 0, function (err, secret) { - if (!secret) { - // user is not authenticated - cb('user is not authenticated', void 0); - } - - // for now we assume that things always work - User.connect(secret, function (err, proxy) { - cb(void 0, proxy); - }); - }); - }; - - // HERE - var deauthorize = common.deauthorize = function (cb) { - console.log("Deauthorizing"); - - // erase session data from storage - User.session(null, function (err) { - if (err) { - console.error(err); - } - /* - TODO better abort for this stuff... - */ - userStore = undefined; - userProxy = undefined; - }); - }; - var userHashKey = common.userHashKey = 'User_hash'; var fileHashKey = common.fileHashKey = 'FS_hash'; @@ -677,48 +635,6 @@ define([ } cb(); }); - return; -/* - authorize(function (err, proxy) { - /* - TODO - listen for log(in|out) events - update information accordingly - * / - - store.change(function (data) { - if (data.key === User.localKey) { - // HERE - if (!data.newValue) { - deauthorize(function (err) { - console.log("Deauthorized!!"); - }); - } else { - authorize(function (err, proxy) { - if (err) { - // not logged in - } - if (!proxy) { - userProxy = proxy; - userStore = User.prepareStore(proxy); - } - }); - } - } - }); - - if (err) { - // not logged in - } - if (!proxy) { - cb(); - return; - } - userProxy = env.proxy = proxy; - userStore = env.userStore = User.prepareStore(proxy); - cb(); - - }); */ }, common); }; diff --git a/www/login/main.js b/www/login/main.js index 70f86b5ae..1530dd0b2 100644 --- a/www/login/main.js +++ b/www/login/main.js @@ -220,7 +220,6 @@ define([ $logoutBox.slideDown(); } else { revealLogin(); - //$logoutBox.hide(); } $login.click(function () { diff --git a/www/read/index.html b/www/read/index.html index 32f6f5a06..326e46158 100644 --- a/www/read/index.html +++ b/www/read/index.html @@ -30,6 +30,9 @@ pre { - -

+
+ + + + diff --git a/www/read/main.js b/www/read/main.js index 4a8d36eeb..9bce1ebe7 100644 --- a/www/read/main.js +++ b/www/read/main.js @@ -9,8 +9,8 @@ define([ var useDoc = function (err, doc) { if (err) { return console.error(err); } - console.log(doc); - $dest.text(doc); + //console.log(doc); + $('#putter').val(doc); }; $('#get').click(function () { @@ -19,5 +19,17 @@ define([ Crypt.get(val, useDoc); }); + $('#put').click(function () { + var hash = $target.val().trim(); + Crypt.put(hash, $('#putter').val(), function (e) { + if (e) { console.error(e); } + $('#get').click(); + }); + }); + + $('#open').click(function () { + window.open('/code/#' + $target.val()); + }); + if (window.location.hash) { Crypt.get(void 0, useDoc); } });