From 5c3237e0934ca6d77acc9653407cee949b261d39 Mon Sep 17 00:00:00 2001 From: yflory Date: Thu, 30 Nov 2017 17:21:58 +0100 Subject: [PATCH] Fix issues with the async store --- www/common/common-ui-elements.js | 11 +++++----- www/common/cryptpad-common.js | 36 ++++++++++++++----------------- www/common/metadata-manager.js | 22 +++++++++++++++++-- www/common/outer/async-store.js | 10 ++++----- www/common/sframe-common-outer.js | 3 ++- www/common/sframe-common.js | 5 +++-- www/common/userObject.js | 5 ++--- www/file/inner.js | 2 -- 8 files changed, 53 insertions(+), 41 deletions(-) diff --git a/www/common/common-ui-elements.js b/www/common/common-ui-elements.js index 57392a5cd..19d4e03bf 100644 --- a/www/common/common-ui-elements.js +++ b/www/common/common-ui-elements.js @@ -571,12 +571,7 @@ define([ // getPinnedUsage updates common.account.usage, and other values // so we can just use those and only check for errors var $container = $('', {'class':'cp-limit-container'}); - var todo; - var updateUsage = Util.notAgainForAnother(function () { - common.getPinUsage(todo); - }, LIMIT_REFRESH_RATE); - - todo = function (err, data) { + var todo = function (err, data) { if (err) { return void console.error(err); } var usage = data.usage; @@ -645,6 +640,10 @@ define([ $limit.append($usage).append($text); }; + var updateUsage = Util.notAgainForAnother(function () { + common.getPinUsage(todo); + }, LIMIT_REFRESH_RATE); + setInterval(function () { updateUsage(); }, LIMIT_REFRESH_RATE * 3); diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index f1b0eff31..14728be3a 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -1,5 +1,4 @@ define([ - 'jquery', '/api/config', '/customize/messages.js', '/common/common-util.js', @@ -15,7 +14,7 @@ define([ '/common/pinpad.js', '/customize/application_config.js', '/bower_components/nthen/index.js', -], function ($, Config, Messages, Util, Hash, +], function (Config, Messages, Util, Hash, Messaging, Realtime, Language, Constants, Feedback, LocalStore, AStore, Pinpad, AppConfig, Nthen) { @@ -238,13 +237,13 @@ define([ postMessage("GET_TEMPLATES", null, function (obj) { if (obj && obj.error) { return void cb(obj.error); } if (!Array.isArray(obj)) { return void cb ('NOT_AN_ARRAY'); } - if (!type) { return void cb(obj); } + if (!type) { return void cb(null, obj); } var templates = obj.filter(function (f) { var parsed = Hash.parsePadUrl(f.href); return parsed.type === type; }); - cb(templates); + cb(null, templates); }); }; @@ -257,7 +256,8 @@ define([ if (e) { throw new Error(e); } postMessage("ADD_PAD", { href: href, - title: data.title + title: data.title, + path: ['template'] }, function (obj) { if (obj && obj.error) { return void cb(obj.error); } cb(); @@ -267,7 +267,7 @@ define([ common.isTemplate = function (href, cb) { var rhref = Hash.getRelativeHref(href); - common.listTemplates(null, function (templates) { + common.listTemplates(null, function (err, templates) { cb(void 0, templates.some(function (t) { return t.href === rhref; })); @@ -321,22 +321,22 @@ define([ common.pinPads = function (pads, cb) { postMessage("PIN_PADS", pads, function (obj) { - if (obj.error) { return void cb(obj.error); } - cb(); + if (obj && obj.error) { return void cb(obj.error); } + cb(null, obj.hash); }); }; common.unpinPads = function (pads, cb) { postMessage("UNPIN_PADS", pads, function (obj) { - if (obj.error) { return void cb(obj.error); } - cb(); + if (obj && obj.error) { return void cb(obj.error); } + cb(null, obj.hash); }); }; common.getPinnedUsage = function (cb) { postMessage("GET_PINNED_USAGE", null, function (obj) { if (obj.error) { return void cb(obj.error); } - cb(); + cb(null, obj.bytes); }); }; @@ -544,12 +544,6 @@ define([ return void setTimeout(function () { f(void 0, env); }); } - // TODO - if (sessionStorage[Constants.newPadPathKey]) { - common.initialPath = sessionStorage[Constants.newPadPathKey]; - delete sessionStorage[Constants.newPadPathKey]; - } - var provideFeedback = function () { if (typeof(window.Proxy) === 'undefined') { Feedback.send("NO_PROXIES"); @@ -581,8 +575,12 @@ define([ query: onMessage, // TODO temporary, will be replaced by a webworker channel userHash: LocalStore.getUserHash(), anonHash: LocalStore.getFSHash(), - localToken: localStorage.getItem(Constants.tokenKey) + localToken: tryParsing(localStorage.getItem(Constants.tokenKey)) }; + if (sessionStorage[Constants.newPadPathKey]) { + cfg.initialPath = sessionStorage[Constants.newPadPathKey]; + delete sessionStorage[Constants.newPadPathKey]; + } AStore.query("CONNECT", cfg, waitFor(function (data) { if (data.error) { throw new Error(data.error); } @@ -606,8 +604,6 @@ define([ //Messaging.addDirectMessageHandler(common); initFeedback(data.feedback); })); - }).nThen(function (waitFor) { - $(waitFor()); }).nThen(function (waitFor) { // Load the new pad when the hash has changed var oldHref = document.location.href; diff --git a/www/common/metadata-manager.js b/www/common/metadata-manager.js index 0e375b736..bf99e4b84 100644 --- a/www/common/metadata-manager.js +++ b/www/common/metadata-manager.js @@ -59,7 +59,6 @@ define(['json.sortify'], function (Sortify) { } if (metadataObj.title !== rememberedTitle) { - console.log("Title update\n" + metadataObj.title + '\n'); rememberedTitle = metadataObj.title; titleChangeHandlers.forEach(function (f) { f(metadataObj.title); }); } @@ -73,30 +72,45 @@ define(['json.sortify'], function (Sortify) { }); }; + var netfluxId; + var isReady = false; + var readyHandlers = []; sframeChan.on('EV_METADATA_UPDATE', function (ev) { meta = ev; if (ev.priv) { priv = ev.priv; } + if (netfluxId) { + meta.user.netfluxId = netfluxId; + } + if (!isReady) { + isReady = true; + readyHandlers.forEach(function (f) { f(); }); + } change(true); }); sframeChan.on('EV_RT_CONNECT', function (ev) { - meta.user.netfluxId = ev.myID; + netfluxId = ev.myID; members = ev.members; + if (!meta.user) { return; } + meta.user.netfluxId = netfluxId; change(true); }); sframeChan.on('EV_RT_JOIN', function (ev) { members.push(ev); + if (!meta.user) { return; } change(false); }); sframeChan.on('EV_RT_LEAVE', function (ev) { var idx = members.indexOf(ev); if (idx === -1) { console.log('Error: ' + ev + ' not in members'); return; } members.splice(idx, 1); + if (!meta.user) { return; } change(false); }); sframeChan.on('EV_RT_DISCONNECT', function () { members = []; + if (!meta.user) { return; } change(true); }); @@ -140,6 +154,10 @@ define(['json.sortify'], function (Sortify) { }, getNetfluxId : function () { return meta.user.netfluxId; + }, + onReady: function (f) { + if (isReady) { return void f(); } + readyHandlers.push(f); } }); }; diff --git a/www/common/outer/async-store.js b/www/common/outer/async-store.js index 116e0cdd7..1a5086200 100644 --- a/www/common/outer/async-store.js +++ b/www/common/outer/async-store.js @@ -300,7 +300,6 @@ define([ avatar: Util.find(store.proxy, ['profile', 'avatar']), profile: Util.find(store.proxy, ['profile', 'view']), curvePublic: store.proxy.curvePublic, - netfluxId: store.network.webChannels[0].myID }, // "priv" is not shared with other users but is needed by the apps priv: { @@ -458,7 +457,8 @@ define([ var pad = makePad(data.href, data.title); store.userObject.pushData(pad, function (e, id) { if (e) { return void cb({error: "Error while adding a template:"+ e}); } - store.userObject.add(id, ['template']); + var path = data.path || ['root']; + store.userObject.add(id, path); onSync(cb); }); }; @@ -546,7 +546,8 @@ define([ if (!contains) { Store.addPad({ href: href, - title: title + title: title, + path: store.data && store.data.initialPath }, cb); return; } @@ -599,7 +600,6 @@ define([ // Messaging var getMessagingCfg = function () { - console.log(store, store.network); return { proxy: store.proxy, realtime: store.realtime, @@ -624,7 +624,7 @@ define([ var onReady = function (returned, cb) { var proxy = store.proxy; var userObject = store.userObject = UserObject.init(proxy.drive, { - pinPads: function (pads, cb) { Store.pinPads({pads: pads}, cb); }, + pinPads: Store.pinPads, loggedIn: store.loggedIn }); var todo = function () { diff --git a/www/common/sframe-common-outer.js b/www/common/sframe-common-outer.js index a32cecf3f..ccdf1814d 100644 --- a/www/common/sframe-common-outer.js +++ b/www/common/sframe-common-outer.js @@ -170,6 +170,7 @@ define([ if (cfg.addData) { cfg.addData(metaObj.priv, Cryptpad); } + sframeChan.event('EV_METADATA_UPDATE', metaObj); }); }; @@ -228,7 +229,7 @@ define([ sframeChan.on('Q_SET_PAD_TITLE_IN_DRIVE', function (newTitle, cb) { currentTitle = newTitle; setDocumentTitle(); - Cryptpad.renamePad(newTitle, undefined, function (err) { + Cryptpad.setPadTitle(newTitle, undefined, function (err) { cb(err); }); }); diff --git a/www/common/sframe-common.js b/www/common/sframe-common.js index ee2589727..01ae4c38c 100644 --- a/www/common/sframe-common.js +++ b/www/common/sframe-common.js @@ -122,7 +122,6 @@ define([ funcs.getFileSize = function (href, cb) { var channelId = Hash.hrefToHexChannelId(href); funcs.sendAnonRpcMsg("GET_FILE_SIZE", channelId, function (data) { - console.log(data); if (!data) { return void cb("No response"); } if (data.error) { return void cb(data.error); } if (data.response && data.response.length && typeof(data.response[0]) === 'number') { @@ -329,7 +328,7 @@ define([ nThen(function (waitFor) { SFrameChannel.create(window.parent, waitFor(function (sfc) { ctx.sframeChan = sfc; }), true); // CpNfInner.start() should be here.... - }).nThen(function () { + }).nThen(function (waitFor) { localForage.clear(); ctx.metadataMgr = MetadataMgr.create(ctx.sframeChan); @@ -390,6 +389,8 @@ define([ } catch (e) { Feedback.init(false); } }); + ctx.metadataMgr.onReady(waitFor()); + }).nThen(function () { ctx.sframeChan.ready(); cb(funcs); }); diff --git a/www/common/userObject.js b/www/common/userObject.js index 88c354b5b..35d5690b9 100644 --- a/www/common/userObject.js +++ b/www/common/userObject.js @@ -487,7 +487,6 @@ define([ // FILES DATA exp.pushData = function (data, cb) { - // TODO: can only be called from outside atm if (!pinPads) { return; } if (typeof cb !== "function") { cb = function () {}; } var todo = function () { @@ -498,8 +497,8 @@ define([ if (!loggedIn || !AppConfig.enablePinning || config.testMode) { return void todo(); } - pinPads([Hash.hrefToHexChannelId(data.href)], function (e) { - if (e) { return void cb(e); } + pinPads([Hash.hrefToHexChannelId(data.href)], function (obj) { + if (obj && obj.error) { return void cb(obj.error); } todo(); }); }; diff --git a/www/file/inner.js b/www/file/inner.js index 09a302b0a..0bc7a2000 100644 --- a/www/file/inner.js +++ b/www/file/inner.js @@ -120,7 +120,6 @@ define([ decrypted.callback(); } - console.log(decrypted); $dlview.show(); $dlform.hide(); var $dlButton = $dlview.find('media-tag button'); @@ -174,7 +173,6 @@ define([ var progress = e.originalEvent; var p = progress.percent +'%'; $progress.width(p); - console.log(progress.percent); }); /**