Open pads in offline mode

pull/1/head
yflory 4 years ago
parent 80a33a5cb5
commit 989020a436

@ -11,11 +11,13 @@ define([
'/common/outer/local-store.js', '/common/outer/local-store.js',
'/common/outer/worker-channel.js', '/common/outer/worker-channel.js',
'/common/outer/login-block.js', '/common/outer/login-block.js',
'/common/outer/cache-store.js',
'/customize/application_config.js', '/customize/application_config.js',
'/bower_components/nthen/index.js', '/bower_components/nthen/index.js',
], function (Config, Messages, Util, Hash, ], function (Config, Messages, Util, Hash,
Messaging, Constants, Feedback, Visible, UserObject, LocalStore, Channel, Block, Messaging, Constants, Feedback, Visible, UserObject, LocalStore, Channel, Block,
Cache,
AppConfig, Nthen) { AppConfig, Nthen) {
/* This file exposes functionality which is specific to Cryptpad, but not to /* This file exposes functionality which is specific to Cryptpad, but not to
@ -437,10 +439,35 @@ define([
}); });
}; };
common.getFileSize = function (href, password, cb) { common.getFileSize = function (href, password, _cb) {
postMessage("GET_FILE_SIZE", {href: href, password: password}, function (obj) { var cb = Util.once(Util.mkAsync(_cb));
if (obj && obj.error) { return void cb(obj.error); } var channel = Hash.hrefToHexChannelId(href, password);
var error;
Nthen(function (waitFor) {
// Blobs can't change, if it's in the cache, use it
Cache.getBlobCache(channel, waitFor(function(err, blob) {
if (err) { return; }
waitFor.abort();
cb(null, blob.length);
}));
}).nThen(function (waitFor) {
// If it's not in the cache or it's not a blob, try to get the value from the server
postMessage("GET_FILE_SIZE", {channel:channel}, waitFor(function (obj) {
if (obj && obj.error) {
// If disconnected, try to get the value from the channel cache (next nThen)
error = obj.error;
return;
}
waitFor.abort();
cb(undefined, obj.size); cb(undefined, obj.size);
}));
}).nThen(function () {
Cache.getChannelCache(channel, function(err, data) {
if (err) { return void cb(error); }
var size = data && Array.isArray(data.c) && data.c.join('').length;
cb(null, size || 0);
});
}); });
}; };
@ -451,11 +478,23 @@ define([
}); });
}; };
common.isNewChannel = function (href, password, cb) { common.isNewChannel = function (href, password, _cb) {
postMessage('IS_NEW_CHANNEL', {href: href, password: password}, function (obj) { var cb = Util.once(Util.mkAsync(_cb));
if (obj.error) { return void cb(obj.error); } var channel = Hash.hrefToHexChannelId(href, password);
if (!obj) { return void cb('INVALID_RESPONSE'); } var error;
Nthen(function (waitFor) {
// If it's not in the cache or it's not a blob, try to get the value from the server
postMessage('IS_NEW_CHANNEL', {channel: channel}, waitFor(function (obj) {
if (obj && obj.error) { error = obj.error; return; }
if (!obj) { error = "INVALID_RESPONSE"; return; }
waitFor.abort();
cb(undefined, obj.isNew); cb(undefined, obj.isNew);
}));
}).nThen(function () {
Cache.getChannelCache(channel, function(err, data) {
if (err || !data) { return void cb(error); }
cb(null, false);
});
}); });
}; };

@ -76,7 +76,7 @@ define([
MT.displayAvatar = function (common, $container, href, name, _cb) { MT.displayAvatar = function (common, $container, href, name, _cb) {
var cb = Util.once(Util.mkAsync(_cb || function () {})); var cb = Util.once(Util.mkAsync(_cb || function () {}));
var displayDefault = function () { var displayDefault = function () {
var text = (href && typeof(href) === "string") ? href : Util.getFirstCharacter(name); var text = Util.getFirstCharacter(name || Messages.anonymous);
var $avatar = $('<span>', {'class': 'cp-avatar-default'}).text(text); var $avatar = $('<span>', {'class': 'cp-avatar-default'}).text(text);
$container.append($avatar); $container.append($avatar);
if (cb) { cb(); } if (cb) { cb(); }

@ -1037,6 +1037,7 @@ define([
//}); //});
execCommand('GET_MY_INFO', null, function (e, info) { execCommand('GET_MY_INFO', null, function (e, info) {
if (e) { return; }
contactsData[info.curvePublic] = info; contactsData[info.curvePublic] = info;
}); });

@ -91,7 +91,7 @@ define([
Realtime.whenRealtimeSyncs(s.sharedFolders[k].realtime, waitFor()); Realtime.whenRealtimeSyncs(s.sharedFolders[k].realtime, waitFor());
} }
} }
}).nThen(function () { console.log('done');cb(); }); }).nThen(function () { cb(); });
}; };
Store.get = function (clientId, data, cb) { Store.get = function (clientId, data, cb) {
@ -1035,9 +1035,6 @@ define([
}); });
}; };
Store.setPadTitle = function (clientId, data, cb) { Store.setPadTitle = function (clientId, data, cb) {
if (store.offline) {
return void cb({ error: 'OFFLINE' });
}
var title = data.title; var title = data.title;
var href = data.href; var href = data.href;
var channel = data.channel; var channel = data.channel;
@ -1110,6 +1107,11 @@ define([
Array.prototype.push.apply(allData, res); Array.prototype.push.apply(allData, res);
}); });
var contains = allData.length !== 0; var contains = allData.length !== 0;
if (store.offline && !contains) {
return void cb({ error: 'OFFLINE' });
} else if (store.offline) {
return void cb();
}
allData.forEach(function (obj) { allData.forEach(function (obj) {
var pad = obj.data; var pad = obj.data;
pad.atime = +new Date(); pad.atime = +new Date();
@ -1689,7 +1691,7 @@ define([
noChainPad: true, noChainPad: true,
channel: data.channel, channel: data.channel,
metadata: data.metadata, metadata: data.metadata,
network: store.network, network: store.network || store.networkPromise,
//readOnly: data.readOnly, //readOnly: data.readOnly,
onConnect: function (wc, sendMessage) { onConnect: function (wc, sendMessage) {
channel.sendMessage = function (msg, cId, cb) { channel.sendMessage = function (msg, cId, cb) {

@ -45,6 +45,7 @@ define([
window.addEventListener('message', onMsg); window.addEventListener('message', onMsg);
}).nThen(function (/*waitFor*/) { }).nThen(function (/*waitFor*/) {
SFCommonO.start({ SFCommonO.start({
cache: true,
hash: hash, hash: hash,
href: href, href: href,
useCreationScreen: true, useCreationScreen: true,

@ -431,6 +431,10 @@ define([
} }
if (!stored && !parsed.hashData.password) { if (!stored && !parsed.hashData.password) {
// We've received a link without /p/ and it doesn't work without a password: abort // We've received a link without /p/ and it doesn't work without a password: abort
if (e === "ANON_RPC_NOT_READY") {
// We're currently offline and the pad is not in our cache
sframeChan.event('EV_OFFLINE');
}
return void todo(); return void todo();
} }
// Wrong password or deleted file? // Wrong password or deleted file?

@ -15,6 +15,7 @@ define([
'/common/metadata-manager.js', '/common/metadata-manager.js',
'/customize/application_config.js', '/customize/application_config.js',
'/common/outer/cache-store.js',
'/common/common-realtime.js', '/common/common-realtime.js',
'/common/common-util.js', '/common/common-util.js',
'/common/common-hash.js', '/common/common-hash.js',
@ -39,6 +40,7 @@ define([
MT, MT,
MetadataMgr, MetadataMgr,
AppConfig, AppConfig,
Cache,
CommonRealtime, CommonRealtime,
Util, Util,
Hash, Hash,
@ -167,6 +169,13 @@ define([
}; };
funcs.getFileSize = function (channelId, cb) { funcs.getFileSize = function (channelId, cb) {
nThen(function (waitFor) {
Cache.getBlobCache(channelId, waitFor(function(err, blob) {
if (err) { return; }
waitFor.abort();
cb(null, blob.length);
}));
}).nThen(function () {
funcs.sendAnonRpcMsg("GET_FILE_SIZE", channelId, function (data) { funcs.sendAnonRpcMsg("GET_FILE_SIZE", channelId, function (data) {
if (!data) { return void cb("No response"); } if (!data) { return void cb("No response"); }
if (data.error) { return void cb(data.error); } if (data.error) { return void cb(data.error); }
@ -176,6 +185,7 @@ define([
cb('INVALID_RESPONSE'); cb('INVALID_RESPONSE');
} }
}); });
});
}; };
// Universal direct channel // Universal direct channel
@ -678,6 +688,10 @@ define([
UI.errorLoadingScreen(Messages.restrictedError); UI.errorLoadingScreen(Messages.restrictedError);
}); });
ctx.sframeChan.on("EV_OFFLINE", function () {
UI.errorLoadingScreen("OFFLINE AND NO CACHE"); // XXX
});
ctx.sframeChan.on("EV_PAD_PASSWORD_ERROR", function () { ctx.sframeChan.on("EV_PAD_PASSWORD_ERROR", function () {
UI.errorLoadingScreen(Messages.password_error_seed); UI.errorLoadingScreen(Messages.password_error_seed);
}); });

@ -365,6 +365,9 @@ MessengerUI, Messages) {
if (!toolbar.connected) { return; } if (!toolbar.connected) { return; }
updateUserList(toolbar, config); updateUserList(toolbar, config);
}); });
setTimeout(function () {
updateUserList(toolbar, config, true);
});
} }
}; };
@ -1246,8 +1249,8 @@ MessengerUI, Messages) {
if (typeof el !== "string" || !el.trim()) { return; } if (typeof el !== "string" || !el.trim()) { return; }
if (typeof tb[el] === "function") { if (typeof tb[el] === "function") {
if (!init && config.displayed.indexOf(el) !== -1) { return; } // Already done if (!init && config.displayed.indexOf(el) !== -1) { return; } // Already done
toolbar[el] = tb[el](toolbar, config);
if (!init) { config.displayed.push(el); } if (!init) { config.displayed.push(el); }
toolbar[el] = tb[el](toolbar, config);
} }
}); });
checkSize(); checkSize();

Loading…
Cancel
Save