implement more reliable check for whether a channel on the server is new

pull/1/head
ansuz 7 years ago
parent b6430d640e
commit 869909b101

@ -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) { var isUnauthenticatedCall = function (call) {
return [ return [
'GET_FILE_SIZE', 'GET_FILE_SIZE',
'GET_MULTIPLE_FILE_SIZE', 'GET_MULTIPLE_FILE_SIZE',
'IS_CHANNEL_PINNED', 'IS_CHANNEL_PINNED',
'IS_NEW_CHANNEL',
].indexOf(call) !== -1; ].indexOf(call) !== -1;
}; };
@ -1051,6 +1078,10 @@ RPC.create = function (config /*:typeof(ConfigType)*/, cb /*:(?Error, ?Function)
return void isChannelPinned(Env, msg[1], function (isPinned) { return void isChannelPinned(Env, msg[1], function (isPinned) {
respond(null, [null, isPinned, null]); respond(null, [null, isPinned, null]);
}); });
case 'IS_NEW_CHANNEL':
return void isNewChannel(Env, msg[1], function (e, isNew) {
respond(null, [null, isNew, null]);
});
default: default:
console.error("unsupported!"); console.error("unsupported!");
return respond('UNSUPPORTED_RPC_CALL', msg); return respond('UNSUPPORTED_RPC_CALL', msg);

@ -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 // Store

@ -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) { Store.getMultipleFileSize = function (data, cb) {
if (!store.anon_rpc) { return void cb({error: 'ANON_RPC_NOT_READY'}); } if (!store.anon_rpc) { return void cb({error: 'ANON_RPC_NOT_READY'}); }
if (!Array.isArray(data.files)) { if (!Array.isArray(data.files)) {

@ -160,8 +160,13 @@ define([
case 'DRIVE_USEROBJECT': { case 'DRIVE_USEROBJECT': {
Store.userObjectCommand(data, cb); break; Store.userObjectCommand(data, cb); break;
} }
case 'IS_NEW_CHANNEL': {
Store.isNewChannel(data, cb); break;
}
default: { default: {
console.error("UNHANDLED_STORE_RPC");
break; break;
} }
} }

@ -140,12 +140,10 @@ define([
}).nThen(function (waitFor) { }).nThen(function (waitFor) {
// Check if the pad exists on server // Check if the pad exists on server
if (!window.location.hash) { isNewFile = true; return; } if (!window.location.hash) { isNewFile = true; return; }
Cryptpad.getFileSize(window.location.href, waitFor(function (err, size) {
if (typeof(size) === 'number' && size >= 108) { Cryptpad.isNewChannel(window.location.href, waitFor(function (e, isNew) {
isNewFile = false; if (e) { return console.error(e); }
return; isNewFile = Boolean(isNew);
}
isNewFile = true;
})); }));
}).nThen(function () { }).nThen(function () {
var readOnly = secret.keys && !secret.keys.editKeyStr; var readOnly = secret.keys && !secret.keys.editKeyStr;

Loading…
Cancel
Save