On-demand anon creation drive

pull/1/head
yflory 4 years ago
parent d81e1f3ecd
commit 508201ab28

@ -499,7 +499,6 @@ define([
var isNew = function () { var isNew = function () {
error = undefined; error = undefined;
postMessage('IS_NEW_CHANNEL', {channel: channel}, function (obj) { postMessage('IS_NEW_CHANNEL', {channel: channel}, function (obj) {
console.error(obj);
if (obj && obj.error) { error = obj.error; } if (obj && obj.error) { error = obj.error; }
if (!obj) { error = "INVALID_RESPONSE"; } if (!obj) { error = "INVALID_RESPONSE"; }

@ -26,13 +26,14 @@ define([
'/bower_components/chainpad/chainpad.dist.js', '/bower_components/chainpad/chainpad.dist.js',
'/bower_components/chainpad-netflux/chainpad-netflux.js', '/bower_components/chainpad-netflux/chainpad-netflux.js',
'/bower_components/chainpad-listmap/chainpad-listmap.js', '/bower_components/chainpad-listmap/chainpad-listmap.js',
'/bower_components/netflux-websocket/netflux-client.js',
'/bower_components/nthen/index.js', '/bower_components/nthen/index.js',
'/bower_components/saferphore/index.js', '/bower_components/saferphore/index.js',
], function (Sortify, UserObject, ProxyManager, Migrate, Hash, Util, Constants, Feedback, ], function (Sortify, UserObject, ProxyManager, Migrate, Hash, Util, Constants, Feedback,
Realtime, Messaging, Pinpad, Cache, Realtime, Messaging, Pinpad, Cache,
SF, Cursor, OnlyOffice, Mailbox, Profile, Team, Messenger, History, SF, Cursor, OnlyOffice, Mailbox, Profile, Team, Messenger, History,
NetConfig, AppConfig, NetConfig, AppConfig,
Crypto, ChainPad, CpNetflux, Listmap, nThen, Saferphore) { Crypto, ChainPad, CpNetflux, Listmap, Netflux, nThen, Saferphore) {
var onReadyEvt = Util.mkEvent(true); var onReadyEvt = Util.mkEvent(true);
var onCacheReadyEvt = Util.mkEvent(true); var onCacheReadyEvt = Util.mkEvent(true);
@ -588,8 +589,8 @@ define([
var metadata = { var metadata = {
// "user" is shared with everybody via the userlist // "user" is shared with everybody via the userlist
user: { user: {
name: proxy[Constants.displayNameKey] || "", name: proxy[Constants.displayNameKey] || store.noDriveName || "",
uid: proxy.uid, uid: proxy.uid || Hash.createChannelId(), // Random uid in nodrive mode
avatar: Util.find(proxy, ['profile', 'avatar']), avatar: Util.find(proxy, ['profile', 'avatar']),
profile: Util.find(proxy, ['profile', 'view']), profile: Util.find(proxy, ['profile', 'view']),
color: getUserColor(), color: getUserColor(),
@ -855,6 +856,10 @@ define([
// Set the display name (username) in the proxy // Set the display name (username) in the proxy
Store.setDisplayName = function (clientId, value, cb) { Store.setDisplayName = function (clientId, value, cb) {
if (!store.proxy) {
store.noDriveName = value;
return void cb();
}
if (store.modules['profile']) { if (store.modules['profile']) {
store.modules['profile'].setName(value); store.modules['profile'].setName(value);
} }
@ -1446,6 +1451,8 @@ define([
}; };
// Teams support offline/cache mode // Teams support offline/cache mode
if (obj.type === "team") { return void todo(); } if (obj.type === "team") { return void todo(); }
// If we're in "noDrive" mode
if (!store.proxy) { return void todo(); }
// Other modules should wait for the ready event // Other modules should wait for the ready event
onReadyEvt.reg(todo); onReadyEvt.reg(todo);
} }
@ -1634,7 +1641,6 @@ define([
}; };
Store.joinPad = function (clientId, data) { Store.joinPad = function (clientId, data) {
console.error('JOINPAD');
if (data.versionHash) { if (data.versionHash) {
return void getVersionHash(clientId, data); return void getVersionHash(clientId, data);
} }
@ -1711,12 +1717,15 @@ define([
postMessage(clientId, "PAD_CACHE_READY"); postMessage(clientId, "PAD_CACHE_READY");
}, },
onReady: function (pad) { onReady: function (pad) {
console.warn(pad);
var padData = pad.metadata || {}; var padData = pad.metadata || {};
channel.data = padData; channel.data = padData;
if (padData && padData.validateKey && store.messenger) { if (padData && padData.validateKey && store.messenger) {
store.messenger.storeValidateKey(data.channel, padData.validateKey); store.messenger.storeValidateKey(data.channel, padData.validateKey);
} }
if (!store.proxy) {
postMessage(clientId, "PAD_READY", pad.noCache);
return;
}
onReadyEvt.reg(function () { onReadyEvt.reg(function () {
postMessage(clientId, "PAD_READY", pad.noCache); postMessage(clientId, "PAD_READY", pad.noCache);
}); });
@ -1775,7 +1784,6 @@ console.warn(pad);
websocketURL: NetConfig.getWebsocketURL(), websocketURL: NetConfig.getWebsocketURL(),
//readOnly: data.readOnly, //readOnly: data.readOnly,
onConnect: function (wc, sendMessage) { onConnect: function (wc, sendMessage) {
console.warn('CONNECT');
channel.sendMessage = function (msg, cId, cb) { channel.sendMessage = function (msg, cId, cb) {
// Send to server // Send to server
sendMessage(msg, function (err) { sendMessage(msg, function (err) {
@ -2754,11 +2762,6 @@ console.warn('CONNECT');
return void cb({error: '[Store.init] Unable to find or create a drive hash. Aborting...'}); return void cb({error: '[Store.init] Unable to find or create a drive hash. Aborting...'});
} }
var lock = false;
if (!data.userHash && !data.anonHash) {
lock = true;
}
var updateProgress = function (data) { var updateProgress = function (data) {
data.type = 'drive'; data.type = 'drive';
postMessage(clientId, 'LOADING_DRIVE', data); postMessage(clientId, 'LOADING_DRIVE', data);
@ -2770,6 +2773,7 @@ console.warn('CONNECT');
var listmapConfig = { var listmapConfig = {
data: {}, data: {},
websocketURL: NetConfig.getWebsocketURL(), websocketURL: NetConfig.getWebsocketURL(),
network: store.network,
channel: secret.channel, channel: secret.channel,
readOnly: false, readOnly: false,
validateKey: secret.keys.validateKey || undefined, validateKey: secret.keys.validateKey || undefined,
@ -2778,7 +2782,6 @@ console.warn('CONNECT');
userName: 'fs', userName: 'fs',
logLevel: 1, logLevel: 1,
ChainPad: ChainPad, ChainPad: ChainPad,
lock: lock,
updateProgress: updateProgress, updateProgress: updateProgress,
classic: true, classic: true,
}; };
@ -2926,7 +2929,50 @@ console.warn('CONNECT');
*/ */
var initialized = false; var initialized = false;
var noDrive = []; // If we load CryptPad for the first time from an existing pad, don't create a
// drive automatically.
var onNoDrive = function (clientId, cb) {
var andThen = function () {
// To be able to use all the features inside the pad, we need to
// initialize the chat (messenger) and the cursor modules.
loadUniversal(Cursor, 'cursor', function () {});
loadUniversal(Messenger, 'messenger', function () {});
store.messenger = store.modules['messenger'];
// And now we're ready
initAnonRpc(null, null, function () {
cb({});
});
};
// We need an anonymous RPC to be able to check if the pad exists and to get
// its metadata, so we have to create a network first.
if (!store.network) {
var wsUrl = NetConfig.getWebsocketURL();
return void Netflux.connect(wsUrl).then(function (network) {
store.network = network;
// We need to know the HistoryKeeper ID to initialize the anon RPC
// Join a basic ephemeral channel, get the ID and leave it instantly
network.join('0000000000000000000000000000000000').then(function (wc) {
var hk;
wc.members.forEach(function (p) { if (p.length === 16) { hk = p; } });
network.historyKeeper = hk;
wc.leave();
andThen();
}, function (err) {
console.error(err);
cb({error: 'GET_HK'});
});
}, function (err) {
console.error(err);
cb({error: 'OFFLINE'});
});
}
andThen();
};
Store.init = function (clientId, data, _callback) { Store.init = function (clientId, data, _callback) {
var callback = Util.once(_callback); var callback = Util.once(_callback);
@ -2961,20 +3007,28 @@ console.warn('CONNECT');
Cache.disable(); Cache.disable();
} }
postMessage = function (clientId, cmd, d, cb) { if (data.query && data.broadcast) {
data.query(clientId, cmd, d, cb); postMessage = function (clientId, cmd, d, cb) {
}; data.query(clientId, cmd, d, cb);
broadcast = function (excludes, cmd, d, cb) { };
data.broadcast(excludes, cmd, d, cb); broadcast = function (excludes, cmd, d, cb) {
}; data.broadcast(excludes, cmd, d, cb);
};
}
// First tab, no user hash, no anon hash and this app doesn't need a drive // First tab, no user hash, no anon hash and this app doesn't need a drive
// ==> don't create a drive // ==> don't create a drive
if (data.noDrive && !data.userHash && !data.anonHash) { if (data.noDrive && !data.userHash && !data.anonHash) {
noDrive.push(clientId); return void onNoDrive(clientId, function (obj) {
// XXX We need a network before initAnonRpc... if (obj && obj.error) {
return void initAnonRpc(null, null, function () { // if we can't properly initialize the noDrive mode, use normal mode
callback({}); if (obj.error === 'GET_HK') {
data.noDrive = false;
Store.init(clientId, data, _callback);
return;
}
}
callback(obj);
}); });
} }

@ -180,9 +180,10 @@ define([
var updateCursor = function (ctx, data, client, cb) { var updateCursor = function (ctx, data, client, cb) {
var c = ctx.clients[client]; var c = ctx.clients[client];
if (!c) { return void cb({error: 'NO_CLIENT'}); } if (!c) { return void cb({error: 'NO_CLIENT'}); }
data.color = Util.find(ctx.store.proxy, ['settings', 'general', 'cursor', 'color']); var proxy = ctx.store.proxy || {};
data.name = ctx.store.proxy[Constants.displayNameKey] || Messages.anonymous; data.color = Util.find(proxy, ['settings', 'general', 'cursor', 'color']);
data.avatar = Util.find(ctx.store.proxy, ['profile', 'avatar']); data.name = proxy[Constants.displayNameKey] || ctx.store.noDriveName || Messages.anonymous;
data.avatar = Util.find(proxy, ['profile', 'avatar']);
c.cursor = data; c.cursor = data;
sendMyCursor(ctx, client); sendMyCursor(ctx, client);
cb(); cb();
@ -241,6 +242,12 @@ define([
Cursor.init = function (cfg, waitFor, emit) { Cursor.init = function (cfg, waitFor, emit) {
var cursor = {}; var cursor = {};
// Already initialized by a "noDrive" tab?
if (cfg.store && cfg.store.modules && cfg.store.modules['cursor']) {
return cfg.store.modules['cursor'];
}
var ctx = { var ctx = {
store: cfg.store, store: cfg.store,
emit: emit, emit: emit,

@ -213,7 +213,7 @@ define([
if (parsed[0] !== Types.mapId) { return; } // Don't send your key if it's already an ACK if (parsed[0] !== Types.mapId) { return; } // Don't send your key if it's already an ACK
// Answer with your own key // Answer with your own key
var proxy = ctx.store.proxy; var proxy = ctx.store.proxy || {};
var myData = createData(proxy); var myData = createData(proxy);
delete myData.channel; delete myData.channel;
var rMsg = [Types.mapIdAck, myData, channel.wc.myID]; var rMsg = [Types.mapIdAck, myData, channel.wc.myID];
@ -537,6 +537,8 @@ define([
if (peer === hk) { return; } if (peer === hk) { return; }
if (channel.readOnly) { return; } if (channel.readOnly) { return; }
if (channel.isPadChat) { return; }
// Join event will be sent once we are able to ID this peer // Join event will be sent once we are able to ID this peer
var myData = createData(proxy); var myData = createData(proxy);
delete myData.channel; delete myData.channel;
@ -653,7 +655,7 @@ define([
if (channel.onUserlistUpdate) { if (channel.onUserlistUpdate) {
channel.onUserlistUpdate(); channel.onUserlistUpdate();
} }
var proxy = ctx.store.proxy; var proxy = ctx.store.proxy || {};
var online = channel.wc.members.some(function (nId) { var online = channel.wc.members.some(function (nId) {
if (nId === ctx.store.network.historyKeeper) { return; } if (nId === ctx.store.network.historyKeeper) { return; }
var data = channel.mapId[nId] || undefined; var data = channel.mapId[nId] || undefined;
@ -664,7 +666,7 @@ define([
}; };
var getMyInfo = function (ctx, cb) { var getMyInfo = function (ctx, cb) {
var proxy = ctx.store.proxy; var proxy = ctx.store.proxy || {};
cb({ cb({
curvePublic: proxy.curvePublic, curvePublic: proxy.curvePublic,
displayName: proxy[Constants.displayNameKey] displayName: proxy[Constants.displayNameKey]
@ -939,6 +941,14 @@ define([
Msg.init = function (cfg, waitFor, emit) { Msg.init = function (cfg, waitFor, emit) {
var messenger = {}; var messenger = {};
var store = cfg.store; var store = cfg.store;
// Already initialized by a "noDrive" tab
// If this one is a normal tab, add the missing listener if we can
if (store.messenger) {
store.messenger.addListener();
return store.messenger;
}
if (AppConfig.availablePadTypes.indexOf('contacts') === -1) { return; } if (AppConfig.availablePadTypes.indexOf('contacts') === -1) { return; }
var ctx = { var ctx = {
store: store, store: store,
@ -952,9 +962,14 @@ define([
range_requests: {} range_requests: {}
}; };
store.proxy.on('change', ['mutedUsers'], function () { messenger.addListener = function () {
ctx.emit('UPDATE_MUTED', null, getAllClients(ctx)); if (store.proxy) {
}); store.proxy.on('change', ['mutedUsers'], function () {
ctx.emit('UPDATE_MUTED', null, getAllClients(ctx));
});
}
};
messenger.addListener();
ctx.store.network.on('message', function(msg, sender) { ctx.store.network.on('message', function(msg, sender) {
onDirectMessage(ctx, msg, sender); onDirectMessage(ctx, msg, sender);

@ -152,7 +152,6 @@ define([
cb('OK'); cb('OK');
}); });
sframeChan.on('EV_RT_READY', function () { sframeChan.on('EV_RT_READY', function () {
console.error('INNER READY');
if (isReady) { return; } if (isReady) { return; }
if (updateLoadingProgress) { if (updateLoadingProgress) {
updateLoadingProgress({ updateLoadingProgress({

@ -487,7 +487,6 @@ define([
} }
// Not a file, so we can use `isNewChannel` // Not a file, so we can use `isNewChannel`
Cryptpad.isNewChannel(currentPad.href, password, w(function(e, isNew) { Cryptpad.isNewChannel(currentPad.href, password, w(function(e, isNew) {
alert('ko');
if (isNew && expire && expire < (+new Date())) { if (isNew && expire && expire < (+new Date())) {
sframeChan.event("EV_EXPIRED_ERROR"); sframeChan.event("EV_EXPIRED_ERROR");
waitFor.abort(); waitFor.abort();

Loading…
Cancel
Save