Async migration

pull/1/head
yflory 7 years ago
parent 91860186d1
commit 6c4c5c135b

@ -1,12 +1,18 @@
define(['/common/common-feedback.js'], function (Feedback) {
define([
'/common/common-feedback.js',
'/common/common-hash.js',
'/common/common-util.js',
'/bower_components/nthen/index.js',
], function (Feedback, Hash, Util, nThen) {
// Start migration check
// Versions:
// 1: migrate pad attributes
// 2: migrate indent settings (codemirror)
return function (userObject) {
return function (userObject, cb) {
var version = userObject.version || 0;
nThen(function () {
// DEPRECATED
// Migration 1: pad attributes moved to filesData
var migratePadAttributesToData = function () {
@ -15,7 +21,7 @@ define(['/common/common-feedback.js'], function (Feedback) {
if (version < 1) {
migratePadAttributesToData();
}
}).nThen(function () {
// Migration 2: global attributes from root to 'settings' subobjects
var migrateAttributes = function () {
var drawer = 'cryptpad.userlist-drawer';
@ -49,9 +55,7 @@ define(['/common/common-feedback.js'], function (Feedback) {
Feedback.send('Migrate-2', true);
userObject.version = version = 2;
}
}).nThen(function () {
// Migration 3: language from localStorage to settings
var migrateLanguage = function () {
if (!localStorage.CRYPTPAD_LANG) { return; }
@ -63,9 +67,7 @@ define(['/common/common-feedback.js'], function (Feedback) {
Feedback.send('Migrate-3', true);
userObject.version = version = 3;
}
}).nThen(function () {
// Migration 4: allowUserFeedback to settings
var migrateFeedback = function () {
var settings = userObject.settings = userObject.settings || {};
@ -80,9 +82,7 @@ define(['/common/common-feedback.js'], function (Feedback) {
Feedback.send('Migrate-4', true);
userObject.version = version = 4;
}
}).nThen(function () {
// Migration 5: dates to Number
var migrateDates = function () {
var data = userObject.drive && userObject.drive.filesData;
@ -102,5 +102,41 @@ define(['/common/common-feedback.js'], function (Feedback) {
Feedback.send('Migrate-5', true);
userObject.version = version = 5;
}
}).nThen(function (waitFor) {
console.log('start');
var addChannelId = function () {
var data = userObject.drive.filesData;
var el, parsed;
var n = nThen(function () {});
Object.keys(data).forEach(function (k) {
n = n.nThen(function (w) {
setTimeout(w(function () {
el = data[k];
parsed = Hash.parsePadUrl(el.href);
if (!el.href) { return; }
if (!el.channel) {
if (parsed.hashData && parsed.hashData.type === "file") {
// PASSWORD_FILES
el.channel = Util.base64ToHex(parsed.hashData.channel);
} else {
var secret = Hash.getSecrets(parsed.type, parsed.hash, el.password);
el.channel = secret.channel;
}
console.log('Adding missing channel in filesData ', el.channel);
}
}));
});
});
n.nThen(waitFor());
};
if (version < 6) {
addChannelId();
Feedback.send('Migrate-6', true);
userObject.version = version = 6;
}
}).nThen(function () {
console.log('done');
cb();
});
};
});

@ -1052,11 +1052,13 @@ define([
postMessage("DRIVE_LOG", msg);
}
});
var todo = function () {
nThen(function (waitFor) {
userObject.migrate(waitFor());
}).nThen(function (waitFor) {
Migrate(proxy, waitFor());
}).nThen(function () {
userObject.fixFiles();
Migrate(proxy);
var requestLogin = function () {
postMessage("REQUEST_LOGIN");
};
@ -1119,8 +1121,7 @@ define([
proxy.on('change', [Constants.tokenKey], function () {
postMessage("UPDATE_TOKEN", { token: proxy[Constants.tokenKey] });
});
};
userObject.migrate(todo);
});
};
var connect = function (data, cb) {
@ -1139,7 +1140,7 @@ define([
validateKey: secret.keys.validateKey || undefined,
crypto: Crypto.createEncryptor(secret.keys),
userName: 'fs',
logLevel: 1,
logLevel: 2,
ChainPad: ChainPad,
classic: true,
};

Loading…
Cancel
Save