diff --git a/www/common/common-ui-elements.js b/www/common/common-ui-elements.js index c0dfbe49b..fccef7ce2 100644 --- a/www/common/common-ui-elements.js +++ b/www/common/common-ui-elements.js @@ -77,6 +77,8 @@ define([ waitFor.abort(); return void cb(err || 'EEMPTY'); } + delete val.owners; + delete val.expire; Util.extend(data, val); if (data.href) { data.href = base + data.href; } if (data.roHref) { data.roHref = base + data.roHref; } diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index 7941163ab..aafab7755 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -759,7 +759,6 @@ define([ pad.onMetadataEvent = Util.mkEvent(); pad.getPadMetadata = function (data, cb) { - postMessage('GET_PAD_METADATA', data, cb); }; pad.requestAccess = function (data, cb) { @@ -773,10 +772,7 @@ define([ postMessage('SET_PAD_METADATA', data, cb); }; common.getPadMetadata = function (data, cb) { - common.anonRpcMsg('GET_METADATA', data.channel, function (err, obj) { - if (err) { return void cb({error: err}); } - cb(obj && obj[0]); - }); + postMessage('GET_PAD_METADATA', data, cb); }; common.changePadPassword = function (Crypt, Crypto, href, newPassword, edPublic, cb) { diff --git a/www/common/outer/async-store.js b/www/common/outer/async-store.js index 1148df343..0ed9971e3 100644 --- a/www/common/outer/async-store.js +++ b/www/common/outer/async-store.js @@ -1282,32 +1282,15 @@ define([ // data.send === true ==> send the request Store.requestPadAccess = function (clientId, data, cb) { var owner = data.owner; - var channel = channels[data.channel]; - if (!channel) { return void cb({error: 'ENOTFOUND'}); } - if (!data.send && channel && (!channel.data || !channel.data.channel)) { - var i = 0; - var it = setInterval(function () { - if (channel.data && channel.data.channel) { - clearInterval(it); - Store.requestPadAccess(clientId, data, cb); - return; - } - if (i >= 300) { // One minute timeout - clearInterval(it); - return void cb({error: 'ETIMEOUT'}); - } - i++; - }, 200); - return; - } + var owners = data.owners; // If the owner was not is the pad metadata, check if it is a friend. // We'll contact the first owner for whom we know the mailbox - var fData = channel.data || {}; - if (!owner && fData.owners) { + if (!owner && Array.isArray(owners)) { var friends = store.proxy.friends || {}; - if (Object.keys(friends).length > 1) { - fData.owners.some(function (edPublic) { + // If we have friends, check if an owner is one of them (with a mailbox) + if (Object.keys(friends).filter(function (curve) { return curve !== 'me' }).length) { + owners.some(function (edPublic) { return Object.keys(friends).some(function (curve) { if (curve === "me") { return; } if (edPublic === friends[curve].edPublic && @@ -1377,25 +1360,11 @@ define([ Store.getPadMetadata = function (clientId, data, cb) { if (!data.channel) { return void cb({ error: 'ENOTFOUND'}); } - var channel = channels[data.channel]; - if (!channel) { return void cb({ error: 'ENOTFOUND' }); } - if (!channel.data || !channel.data.channel) { - var i = 0; - var it = setInterval(function () { - if (channel.data && channel.data.channel) { - clearInterval(it); - Store.getPadMetadata(clientId, data, cb); - return; - } - if (i >= 300) { // One minute timeout - clearInterval(it); - return void cb({error: 'ETIMEOUT'}); - } - i++; - }, 200); - return; - } - cb(channel.data || {}); + store.anon_rpc.send('GET_METADATA', data.channel, function (err, obj) { + if (err) { return void cb({error: err}); } + // XXX update local owner and expire here + cb((obj && obj[0]) || {}); + }); }; Store.setPadMetadata = function (clientId, data, cb) { if (!data.channel) { return void cb({ error: 'ENOTFOUND'}); } diff --git a/www/common/sframe-common-outer.js b/www/common/sframe-common-outer.js index a8bc62bd0..e39e6880f 100644 --- a/www/common/sframe-common-outer.js +++ b/www/common/sframe-common-outer.js @@ -1010,21 +1010,26 @@ define([ sframeChan.on('EV_GIVE_ACCESS', function (data, cb) { Cryptpad.padRpc.giveAccess(data, cb); }); - sframeChan.on('Q_REQUEST_ACCESS', function (data, cb) { + // REQUEST_ACCESS is used both to check IF we can contact an owner (send === false) + // AND also to send the request if we want (send === true) + sframeChan.on('Q_REQUEST_ACCESS', function (send, cb) { if (readOnly && hashes.editHash) { return void cb({error: 'ALREADYKNOWN'}); } - var owner; + var owner, owners; var crypto = Crypto.createEncryptor(secret.keys); nThen(function (waitFor) { // Try to get the owner's mailbox from the pad metadata first. // If it's is an older owned pad, check if the owner is a friend // or an acquaintance (from async-store directly in requestAccess) - Cryptpad.padRpc.getPadMetadata({ + Cryptpad.getPadMetadata({ channel: secret.channel }, waitFor(function (obj) { obj = obj || {}; if (obj.error) { return; } + + owners = obj.owners; + var mailbox; // Get the first available mailbox (the field can be an string or an object) // TODO maybe we should send the request to all the owners? @@ -1043,10 +1048,16 @@ define([ } })); }).nThen(function () { + // If we are just checking (send === false) and there is a mailbox field, cb state true + // If there is no mailbox, we'll have to check if an owner is a friend in the worker + if (owner && !send) { + return void cb({state: true}); + } Cryptpad.padRpc.requestAccess({ - send: data, + send: send, channel: secret.channel, - owner: owner + owner: owner, + owners: owners }, cb); }); });