Remove support for legacy store and add a way to import legacy pads

pull/1/head
yflory 8 years ago
parent 37c94935f8
commit f8366e0cfc

@ -12,8 +12,5 @@ define(function() {
*/ */
config.notificationTimeout = 5000; config.notificationTimeout = 5000;
config.USE_FS_STORE = true;
config.USE_HOMEPAGE_TABLE = false;
return config; return config;
}); });

@ -188,18 +188,17 @@ define([
var drive = rt.proxy.drive; var drive = rt.proxy.drive;
// Creating a new anon drive: import anon pads from localStorage // Creating a new anon drive: import anon pads from localStorage
if (!drive[Cryptpad.storageKey] || !Cryptpad.isArray(drive[Cryptpad.storageKey])) { if (!drive[Cryptpad.storageKey] || !Cryptpad.isArray(drive[Cryptpad.storageKey])) {
var oldStore = Cryptpad.getStore(true); Cryptpad.getLegacyPads(function (err, data) {
Cryptpad.getRecentPads(function (err, s) { drive[Cryptpad.storageKey] = data;
drive[Cryptpad.storageKey] = s;
onReady(f, rt.proxy, Cryptpad.storageKey, exp); onReady(f, rt.proxy, Cryptpad.storageKey, exp);
}, true); });
return; return;
} }
// Return the existing drive // Drive already exist: return the existing drive, don't load data from legacy store
onReady(f, rt.proxy, Cryptpad.storageKey, exp); onReady(f, rt.proxy, Cryptpad.storageKey, exp);
}) })
.on('disconnect', function (info) { .on('disconnect', function (info) {
// We only manage errors during the loadin screen here. Other websocket errors are handled by the apps // We only manage errors during the loading screen here. Other websocket errors are handled by the apps
if (info.error) { if (info.error) {
if (typeof Cryptpad.storeError === "function") { if (typeof Cryptpad.storeError === "function") {
Cryptpad.storeError(); Cryptpad.storeError();

@ -1,7 +1,7 @@
define([ define([
'/api/config?cb=' + Math.random().toString(16).slice(2), '/api/config?cb=' + Math.random().toString(16).slice(2),
'/customize/messages.js', '/customize/messages.js',
'/customize/store.js', '/customize/fsStore.js',
'/bower_components/chainpad-crypto/crypto.js', '/bower_components/chainpad-crypto/crypto.js',
'/bower_components/alertifyjs/dist/js/alertify.js', '/bower_components/alertifyjs/dist/js/alertify.js',
'/bower_components/spin.js/spin.min.js', '/bower_components/spin.js/spin.min.js',
@ -10,7 +10,7 @@ define([
'/customize/application_config.js', '/customize/application_config.js',
'/bower_components/jquery/dist/jquery.min.js', '/bower_components/jquery/dist/jquery.min.js',
], function (Config, Messages, Store, Crypto, Alertify, Spinner, Clipboard, FS, AppConfig) { ], function (Config, Messages, Store, Crypto, Alertify, Spinner, Clipboard, AppConfig) {
/* This file exposes functionality which is specific to Cryptpad, but not to /* This file exposes functionality which is specific to Cryptpad, but not to
any particular pad type. This includes functions for committing metadata any particular pad type. This includes functions for committing metadata
about pads to your local storage for future use and improved usability. about pads to your local storage for future use and improved usability.
@ -19,18 +19,11 @@ define([
*/ */
var $ = window.jQuery; var $ = window.jQuery;
// When set to true, USE_FS_STORE becomes the default store, but the localStorage store is
// still loaded for migration purpose. When false, the localStorage is used.
var USE_FS_STORE = AppConfig.USE_FS_STORE;
var storeToUse = USE_FS_STORE ? FS : Store;
var common = window.Cryptpad = { var common = window.Cryptpad = {
Messages: Messages, Messages: Messages,
Alertify: Alertify, Alertify: Alertify,
}; };
var store; var store;
var fsStore;
var find = common.find = function (map, path) { var find = common.find = function (map, path) {
return (map && path.reduce(function (p, n) { return (map && path.reduce(function (p, n) {
@ -38,15 +31,14 @@ define([
}, map)); }, map));
}; };
var getStore = common.getStore = function (legacy) { var getStore = common.getStore = function () {
if ((!USE_FS_STORE || legacy) && store) { return store; } if (store) { return store; }
if (USE_FS_STORE && !legacy && fsStore) { return fsStore; }
throw new Error("Store is not ready!"); throw new Error("Store is not ready!");
}; };
var getNetwork = common.getNetwork = function () { var getNetwork = common.getNetwork = function () {
if (USE_FS_STORE && fsStore) { if (store) {
if (fsStore.getProxy() && fsStore.getProxy().info) { if (store.getProxy() && store.getProxy().info) {
return fsStore.getProxy().info.network; return store.getProxy().info.network;
} }
} }
return; return;
@ -131,7 +123,6 @@ define([
}; };
var isLoggedIn = common.isLoggedIn = function () { var isLoggedIn = common.isLoggedIn = function () {
//return typeof getStore().getLoginName() === "string";
return typeof getUserHash() === "string"; return typeof getUserHash() === "string";
}; };
@ -373,6 +364,23 @@ define([
}); });
}; };
// Get the pads from localStorage to migrate them to the object store
var getLegacyPads = common.getLegacyPads = function (cb) {
require(['/customize/store.js'], function(Legacy) {
Legacy.ready(function (err, legacy) {
if (err) { cb(err, null); return; }
Legacy.get(storageKey, function (err2, recentPads) {
if (err2) { cb(err2, null); return; }
if (isArray(recentPads)) {
cb(void 0, migrateRecentPads(recentPads));
return;
}
cb(void 0, []);
});
});
});
};
var getHash = common.getHash = function () { var getHash = common.getHash = function () {
return window.location.hash.slice(1); return window.location.hash.slice(1);
}; };
@ -446,13 +454,13 @@ define([
}; };
// STORAGE // STORAGE
var setPadAttribute = common.setPadAttribute = function (attr, value, cb, legacy) { var setPadAttribute = common.setPadAttribute = function (attr, value, cb) {
getStore(legacy).setDrive([getHash(), attr].join('.'), value, function (err, data) { getStore().setDrive([getHash(), attr].join('.'), value, function (err, data) {
cb(err, data); cb(err, data);
}); });
}; };
var setAttribute = common.setAttribute = function (attr, value, cb, legacy) { var setAttribute = common.setAttribute = function (attr, value, cb) {
getStore(legacy).set(["cryptpad", attr].join('.'), value, function (err, data) { getStore().set(["cryptpad", attr].join('.'), value, function (err, data) {
cb(err, data); cb(err, data);
}); });
}; };
@ -461,13 +469,13 @@ define([
}; };
// STORAGE // STORAGE
var getPadAttribute = common.getPadAttribute = function (attr, cb, legacy) { var getPadAttribute = common.getPadAttribute = function (attr, cb) {
getStore(legacy).getDrive([getHash(), attr].join('.'), function (err, data) { getStore().getDrive([getHash(), attr].join('.'), function (err, data) {
cb(err, data); cb(err, data);
}); });
}; };
var getAttribute = common.getAttribute = function (attr, cb, legacy) { var getAttribute = common.getAttribute = function (attr, cb) {
getStore(legacy).get(["cryptpad", attr].join('.'), function (err, data) { getStore().get(["cryptpad", attr].join('.'), function (err, data) {
cb(err, data); cb(err, data);
}); });
}; };
@ -493,12 +501,8 @@ define([
// STORAGE // STORAGE
/* fetch and migrate your pad history from localStorage */ /* fetch and migrate your pad history from localStorage */
var getRecentPads = common.getRecentPads = function (cb, legacy) { var getRecentPads = common.getRecentPads = function (cb) {
var sstore = getStore(legacy); getStore().getDrive(storageKey, function (err, recentPads) {
if (legacy) {
sstore.getDrive = sstore.get;
}
sstore.getDrive(storageKey, function (err, recentPads) {
if (isArray(recentPads)) { if (isArray(recentPads)) {
cb(void 0, migrateRecentPads(recentPads)); cb(void 0, migrateRecentPads(recentPads));
return; return;
@ -509,21 +513,14 @@ define([
// STORAGE // STORAGE
/* commit a list of pads to localStorage */ /* commit a list of pads to localStorage */
var setRecentPads = common.setRecentPads = function (pads, cb, legacy) { var setRecentPads = common.setRecentPads = function (pads, cb) {
var sstore = getStore(legacy); getStore().setDrive(storageKey, pads, function (err, data) {
if (legacy) {
sstore.setDrive = sstore.set;
}
sstore.setDrive(storageKey, pads, function (err, data) {
cb(err, data); cb(err, data);
}); });
}; };
// STORAGE // STORAGE
var forgetFSPad = function (href, cb) { var forgetPad = common.forgetPad = function (href, cb) {
getStore().forgetPad(href, cb);
};
var forgetPad = common.forgetPad = function (href, cb, legacy) {
var parsed = parsePadUrl(href); var parsed = parsePadUrl(href);
var callback = function (err, data) { var callback = function (err, data) {
@ -532,7 +529,7 @@ define([
return; return;
} }
getStore(legacy).keys(function (err, keys) { getStore().keys(function (err, keys) {
if (err) { if (err) {
cb(err); cb(err);
return; return;
@ -545,35 +542,14 @@ define([
cb(); cb();
return; return;
} }
getStore(legacy).removeBatch(toRemove, function (err, data) { getStore().removeBatch(toRemove, function (err, data) {
cb(err, data); cb(err, data);
}); });
}); });
}; };
if (USE_FS_STORE && !legacy) { if (typeof(getStore().forgetPad) === "function") {
// TODO implement forgetPad in store.js getStore().forgetPad(href, callback);
forgetFSPad(href, callback);
return;
}
getRecentPads(function (err, recentPads) {
setRecentPads(recentPads.filter(function (pad) {
var p = parsePadUrl(pad.href);
// find duplicates
if (parsed.hash === p.hash && parsed.type === p.type) {
console.log("Found a duplicate");
return;
}
return true;
}), callback, legacy);
}, legacy);
if (typeof(getStore(legacy).forgetPad) === "function") {
// TODO implement forgetPad in store.js
getStore(legacy).forgetPad(href, callback);
} }
}; };
@ -626,7 +602,7 @@ define([
if (!contains) { if (!contains) {
var data = makePad(href, name); var data = makePad(href, name);
renamed.push(data); renamed.push(data);
if (USE_FS_STORE && typeof(getStore().addPad) === "function") { if (typeof(getStore().addPad) === "function") {
getStore().addPad(href, common.initialPath, common.initialName || name); getStore().addPad(href, common.initialPath, common.initialName || name);
} }
} }
@ -701,58 +677,47 @@ define([
f(void 0, env); f(void 0, env);
}; };
var todo = function () { Store.ready(function (err, storeObj) {
storeToUse.ready(function (err, store) { store = common.store = env.store = storeObj;
common.store = env.store = store;
if (USE_FS_STORE) { $(function() {
fsStore = store; // Race condition : if document.body is undefined when alertify.js is loaded, Alertify
} // won't work. We have to reset it now to make sure it uses a correct "body"
Alertify.reset();
// Load the new pad when the hash has changed
var oldHash = document.location.hash.slice(1);
window.onhashchange = function () {
var newHash = document.location.hash.slice(1);
var parsedOld = parseHash(oldHash);
var parsedNew = parseHash(newHash);
if (parsedOld && parsedNew && (
parsedOld.channel !== parsedNew.channel
|| parsedOld.mode !== parsedNew.mode
|| parsedOld.key !== parsedNew.key)) {
document.location.reload();
return;
}
if (parsedNew) {
oldHash = newHash;
}
};
$(function() { // Everything's ready, continue...
// Race condition : if document.body is undefined when alertify.js is loaded, Alertify if($('#pad-iframe').length) {
// won't work. We have to reset it now to make sure it uses a correct "body" var $iframe = $('#pad-iframe');
Alertify.reset(); var iframe = $iframe[0];
var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
// Load the new pad when the hash has changed if (iframeDoc.readyState === 'complete') {
var oldHash = document.location.hash.slice(1); cb();
window.onhashchange = function () {
var newHash = document.location.hash.slice(1);
var parsedOld = parseHash(oldHash);
var parsedNew = parseHash(newHash);
if (parsedOld && parsedNew && (
parsedOld.channel !== parsedNew.channel
|| parsedOld.mode !== parsedNew.mode
|| parsedOld.key !== parsedNew.key)) {
document.location.reload();
return;
}
if (parsedNew) {
oldHash = newHash;
}
};
// Everything's ready, continue...
if($('#pad-iframe').length) {
var $iframe = $('#pad-iframe');
var iframe = $iframe[0];
var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
if (iframeDoc.readyState === 'complete') {
cb();
return;
}
$iframe.load(cb);
return; return;
} }
cb(); $iframe.load(cb);
}); return;
}, common); }
}; cb();
// If we use the fs store, make sure the localStorage or iframe store is ready });
if (USE_FS_STORE) { }, common);
Store.ready(todo);
return;
}
todo();
}; };
var errorHandlers = []; var errorHandlers = [];
@ -1263,15 +1228,6 @@ define([
}; };
}; };
// All code which is called implicitly is found below
Store.ready(function (err, Store) {
if (err) {
console.error(err);
return;
}
store = Store;
});
$(function () { $(function () {
Messages._applyTranslation(); Messages._applyTranslation();
}); });

Loading…
Cancel
Save