From 664625a6ef488a40547887d5d0133cbda37ec3fa Mon Sep 17 00:00:00 2001 From: yflory Date: Thu, 30 Nov 2017 18:22:26 +0100 Subject: [PATCH] Fix more async store issues --- www/common/cryptpad-common.js | 27 +++++++++++++++---------- www/common/outer/async-store.js | 33 +++++++++++++++++++++++++------ www/common/outer/store-rpc.js | 3 +++ www/common/outer/upload.js | 2 +- www/common/sframe-common-outer.js | 9 +++++++-- www/debug/inner.js | 11 +---------- www/profile/main.js | 2 +- 7 files changed, 57 insertions(+), 30 deletions(-) diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index 14728be3a..45c401175 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -67,7 +67,7 @@ define([ key: [Constants.tokenKey], value: token }, function (obj) { - if (obj.error) { return void cb(obj.error); } + if (obj && obj.error) { return void cb(obj.error); } cb(); }); }; @@ -402,29 +402,36 @@ define([ common.clearOwnedChannel = function (channel, cb) { postMessage("CLEAR_OWNED_CHANNEL", {channel: channel}, function (obj) { - if (obj.error) { return void cb(obj.error); } - cb(); + if (obj && obj.error) { return void cb(obj.error); } + cb(null, obj); + }); + }; + + common.uploadChunk = function (data, cb) { + postMessage("UPLOAD_CHUNK", {chunk: data}, function (obj) { + if (obj && obj.error) { return void cb(obj.error); } + cb(null, obj); }); }; common.uploadComplete = function (cb) { postMessage("UPLOAD_COMPLETE", null, function (obj) { - if (obj.error) { return void cb(obj.error); } - cb(); + if (obj && obj.error) { return void cb(obj.error); } + cb(null, obj); }); }; common.uploadStatus = function (size, cb) { postMessage("UPLOAD_STATUS", {size: size}, function (obj) { - if (obj.error) { return void cb(obj.error); } - cb(); + if (obj && obj.error) { return void cb(obj.error); } + cb(null, obj); }); }; common.uploadCancel = function (cb) { postMessage("UPLOAD_CANCEL", null, function (obj) { - if (obj.error) { return void cb(obj.error); } - cb(); + if (obj && obj.error) { return void cb(obj.error); } + cb(null, obj); }); }; @@ -434,7 +441,7 @@ define([ netfluxId: netfluxId, href: window.location.href }, function (obj) { - if (obj.error) { return void cb(obj.error); } + if (obj && obj.error) { return void cb(obj.error); } cb(); }); }; diff --git a/www/common/outer/async-store.js b/www/common/outer/async-store.js index 1a5086200..bf7dae60e 100644 --- a/www/common/outer/async-store.js +++ b/www/common/outer/async-store.js @@ -29,7 +29,7 @@ define([ Store.get = function (key, cb) { - cb({result: Util.find(store.proxy, key)}); + cb(Util.find(store.proxy, key)); }; Store.set = function (data, cb) { var path = data.key.slice(); @@ -164,22 +164,34 @@ define([ Store.clearOwnedChannel = function (data, cb) { if (!store.rpc) { return void cb({error: 'RPC_NOT_READY'}); } - store.rpc.clearOwnedChannel(data.channel, cb); + store.rpc.clearOwnedChannel(data.channel, function (err, res) { + if (err) { return void cb({error:err}); } + cb(res); + }); }; Store.uploadComplete = function (data, cb) { if (!store.rpc) { return void cb({error: 'RPC_NOT_READY'}); } - store.rpc.uploadComplete(cb); + store.rpc.uploadComplete(function (err, res) { + if (err) { return void cb({error:err}); } + cb(res); + }); }; Store.uploadStatus = function (data, cb) { if (!store.rpc) { return void cb({error: 'RPC_NOT_READY'}); } - store.rpc.uploadStatus(data.size, cb); + store.rpc.uploadStatus(data.size, function (err, res) { + if (err) { return void cb({error:err}); } + cb(res); + }); }; Store.uploadCancel = function (data, cb) { if (!store.rpc) { return void cb({error: 'RPC_NOT_READY'}); } - store.rpc.uploadCancel(cb); + store.rpc.uploadCancel(function (err, res) { + if (err) { return void cb({error:err}); } + cb(res); + }); }; var arePinsSynced = function (cb) { @@ -203,6 +215,15 @@ define([ }); }; + Store.uploadChunk = function (data, cb) { + store.rpc.send.unauthenticated('UPLOAD', data.chunk, function (e, msg) { + cb({ + error: e, + msg: msg + }); + }); + }; + Store.initRpc = function (data, cb) { require(['/common/pinpad.js'], function (Pinpad) { Pinpad.create(store.network, store.proxy, function (e, call) { @@ -692,7 +713,7 @@ define([ postMessage("UPDATE_METADATA"); }); proxy.on('change', [Constants.tokenKey], function () { - postMessage("UPDATE_TOKEN", { data: proxy[Constants.tokenKey] }); + postMessage("UPDATE_TOKEN", { token: proxy[Constants.tokenKey] }); }); }; userObject.migrate(todo); diff --git a/www/common/outer/store-rpc.js b/www/common/outer/store-rpc.js index 7479b3025..0c0c71221 100644 --- a/www/common/outer/store-rpc.js +++ b/www/common/outer/store-rpc.js @@ -31,6 +31,9 @@ define([ case 'CLEAR_OWNED_CHANNEL': { Store.clearOwnedChannel(data, cb); break; } + case 'UPLOAD_CHUNK': { + Store.uploadChunk(data, cb); break; + } case 'UPLOAD_COMPLETE': { Store.uploadComplete(data, cb); break; } diff --git a/www/common/outer/upload.js b/www/common/outer/upload.js index 4e42d94da..381c9bc4d 100644 --- a/www/common/outer/upload.js +++ b/www/common/outer/upload.js @@ -20,7 +20,7 @@ define([ var sendChunk = function (box, cb) { var enc = Nacl.util.encodeBase64(box); - common.rpc.send.unauthenticated('UPLOAD', enc, function (e, msg) { + common.uploadChunk(enc, function (e, msg) { cb(e, msg); }); }; diff --git a/www/common/sframe-common-outer.js b/www/common/sframe-common-outer.js index ccdf1814d..9d245959a 100644 --- a/www/common/sframe-common-outer.js +++ b/www/common/sframe-common-outer.js @@ -114,8 +114,13 @@ define([ }); if (cfg.getSecrets) { + var w = waitFor(); cfg.getSecrets(Cryptpad, Utils, waitFor(function (err, s) { secret = s; + Cryptpad.getShareHashes(secret, function (err, h) { + hashes = h; + w(); + }); })); } else { secret = Utils.Hash.getSecrets(); @@ -123,8 +128,8 @@ define([ // New pad: create a new random channel id secret.channel = Utils.Hash.createChannelId(); } + Cryptpad.getShareHashes(secret, waitFor(function (err, h) { hashes = h; })); } - Cryptpad.getShareHashes(secret, waitFor(function (err, h) { hashes = h; })); }).nThen(function () { var readOnly = secret.keys && !secret.keys.editKeyStr; @@ -247,7 +252,7 @@ define([ cb('ERROR'); return; } - Cryptpad.changeDisplayName(newName, true); + Cryptpad.changeMetadata(); cb(); }); }); diff --git a/www/debug/inner.js b/www/debug/inner.js index f45670b41..bb4e1ecf2 100644 --- a/www/debug/inner.js +++ b/www/debug/inner.js @@ -1,10 +1,8 @@ define([ 'jquery', '/bower_components/chainpad-crypto/crypto.js', - '/bower_components/textpatcher/TextPatcher.js', '/common/toolbar3.js', 'json.sortify', - '/bower_components/chainpad-json-validator/json-ot.js', '/common/common-util.js', '/bower_components/nthen/index.js', '/common/sframe-common.js', @@ -22,10 +20,8 @@ define([ ], function ( $, Crypto, - TextPatcher, Toolbar, JSONSortify, - JsonOT, Util, nThen, SFCommon, @@ -61,7 +57,6 @@ define([ var config = APP.config = { readOnly: readOnly, - transformFunction: JsonOT.validate, // cryptpad debug logging (default is 1) // logLevel: 0, validateContent: function (content) { @@ -123,11 +118,7 @@ define([ config.onReady = function (info) { if (APP.realtime !== info.realtime) { - var realtime = APP.realtime = info.realtime; - APP.patchText = TextPatcher.create({ - realtime: realtime, - //logging: true - }); + APP.realtime = info.realtime; } var userDoc = APP.realtime.getUserDoc(); diff --git a/www/profile/main.js b/www/profile/main.js index a77829242..e325cdf6d 100644 --- a/www/profile/main.js +++ b/www/profile/main.js @@ -49,7 +49,7 @@ define([ editHash = hash; })); }).nThen(function () { - if (!editHash) { + if (editHash) { return void cb(null, Hash.getSecrets('profile', editHash)); } // 3rd case: profile creation (create a new random hash, store it later if needed)