From 869909b101d36ec0a7d001619a948a56cf5560af Mon Sep 17 00:00:00 2001 From: ansuz Date: Fri, 22 Dec 2017 16:24:17 +0100 Subject: [PATCH] implement more reliable check for whether a channel on the server is new --- rpc.js | 31 +++++++++++++++++++++++++++++++ www/common/cryptpad-common.js | 8 ++++++++ www/common/outer/async-store.js | 15 +++++++++++++++ www/common/outer/store-rpc.js | 7 ++++++- www/common/sframe-common-outer.js | 10 ++++------ 5 files changed, 64 insertions(+), 7 deletions(-) diff --git a/rpc.js b/rpc.js index dc21968d1..af03b8c06 100644 --- a/rpc.js +++ b/rpc.js @@ -952,11 +952,38 @@ var upload_status = function (Env, publicKey, filesize, cb) { }); }; +var isNewChannel = function (Env, channel, cb) { + if (!isValidId(channel)) { return void cb('INVALID_CHAN'); } + if (channel.length !== 32) { return void cb('INVALID_CHAN'); } + + var count = 0; + var done = false; + Env.msgStore.getMessages(channel, function (msg) { + if (done) { return; } + var parsed; + try { + parsed = JSON.parse(msg); + if (parsed && typeof(parsed) === 'object') { count++; } + if (count >= 2) { + done = true; + cb(void 0, false); // it is not a new file + } + } catch (e) { + WARN('invalid message read from store', e); + } + }, function () { + if (done) { return; } + // no more messages... + cb(void 0, true); + }); +}; + var isUnauthenticatedCall = function (call) { return [ 'GET_FILE_SIZE', 'GET_MULTIPLE_FILE_SIZE', 'IS_CHANNEL_PINNED', + 'IS_NEW_CHANNEL', ].indexOf(call) !== -1; }; @@ -1051,6 +1078,10 @@ RPC.create = function (config /*:typeof(ConfigType)*/, cb /*:(?Error, ?Function) return void isChannelPinned(Env, msg[1], function (isPinned) { respond(null, [null, isPinned, null]); }); + case 'IS_NEW_CHANNEL': + return void isNewChannel(Env, msg[1], function (e, isNew) { + respond(null, [null, isNew, null]); + }); default: console.error("unsupported!"); return respond('UNSUPPORTED_RPC_CALL', msg); diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index a06d77077..ac81b7bc3 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -252,6 +252,14 @@ define([ }); }; + common.isNewChannel = function (href, cb) { + postMessage('IS_NEW_CHANNEL', {href: href}, function (obj) { + if (obj.error) { return void cb(obj.error); } + if (!obj) { return void cb('INVALID_RESPONSE'); } + cb(undefined, obj.isNew); + }); + }; + // Store diff --git a/www/common/outer/async-store.js b/www/common/outer/async-store.js index 9ae0bd330..fca25b3b1 100644 --- a/www/common/outer/async-store.js +++ b/www/common/outer/async-store.js @@ -277,6 +277,21 @@ define([ }); }; + Store.isNewChannel = function (data, cb) { + if (!store.anon_rpc) { return void cb({error: 'ANON_RPC_NOT_READY'}); } + var channelId = Hash.hrefToHexChannelId(data.href); + store.anon_rpc.send("IS_NEW_CHANNEL", channelId, function (e, response) { + if (e) { return void cb({error: e}); } + if (response && response.length && typeof(response[0]) === 'boolean') { + return void cb({ + isNew: response[0] + }); + } else { + cb({error: 'INVALID_RESPONSE'}); + } + }); + }; + Store.getMultipleFileSize = function (data, cb) { if (!store.anon_rpc) { return void cb({error: 'ANON_RPC_NOT_READY'}); } if (!Array.isArray(data.files)) { diff --git a/www/common/outer/store-rpc.js b/www/common/outer/store-rpc.js index cabaeaea2..39895b68c 100644 --- a/www/common/outer/store-rpc.js +++ b/www/common/outer/store-rpc.js @@ -160,8 +160,13 @@ define([ case 'DRIVE_USEROBJECT': { Store.userObjectCommand(data, cb); break; } + + case 'IS_NEW_CHANNEL': { + Store.isNewChannel(data, cb); break; + } default: { - + console.error("UNHANDLED_STORE_RPC"); + break; } } diff --git a/www/common/sframe-common-outer.js b/www/common/sframe-common-outer.js index 0df169f6b..e31143e80 100644 --- a/www/common/sframe-common-outer.js +++ b/www/common/sframe-common-outer.js @@ -140,12 +140,10 @@ define([ }).nThen(function (waitFor) { // Check if the pad exists on server if (!window.location.hash) { isNewFile = true; return; } - Cryptpad.getFileSize(window.location.href, waitFor(function (err, size) { - if (typeof(size) === 'number' && size >= 108) { - isNewFile = false; - return; - } - isNewFile = true; + + Cryptpad.isNewChannel(window.location.href, waitFor(function (e, isNew) { + if (e) { return console.error(e); } + isNewFile = Boolean(isNew); })); }).nThen(function () { var readOnly = secret.keys && !secret.keys.editKeyStr;